Skip to content

Instantly share code, notes, and snippets.

@jakkaj
Created January 27, 2015 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakkaj/8453b826901f56c4633b to your computer and use it in GitHub Desktop.
Save jakkaj/8453b826901f56c4633b to your computer and use it in GitHub Desktop.
Example of how to track in progress workflows for updates in to the app UI
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ModernHttpClient;
using UIKit;
using ProjectTripod.Portable.Model.Messages;
using ProjectTripod.Portable.Model.Workflow;
using XamlingCore.Portable.Messages.Network;
using XamlingCore.Portable.Messages.XamlingMessenger;
using XamlingCore.Portable.View.ViewModel;
using XamlingCore.Portable.Workflow.Flow;
namespace ProjectTripod.Client.iOS.CommonViewModels.UIHelpers
{
public class UIWorkflowWatchingViewModel : XViewModel
{
private readonly XWorkflowHub _hub;
private XFlow _entryPublishFlow;
private XFlow _tripodPublishFlow;
List<XFlowState> _inProgressStates = new List<XFlowState>();
AsyncLock _stateLock = new AsyncLock();
public UIWorkflowWatchingViewModel(XWorkflowHub hub)
{
_hub = hub;
init();
}
async void _updateFlows()
{
var inProgress = await _entryPublishFlow.GetInProgressItems();
inProgress.AddRange(await _tripodPublishFlow.GetInProgressItems());
using (var l = await _stateLock.LockAsync())
{
foreach (var item in inProgress)
{
if (!_inProgressStates.Contains(item) && item.State != XFlowStates.Fail && item.State != XFlowStates.Success)
{
_inProgressStates.Add(item);
item.PropertyChanged += item_PropertyChanged;
}
}
}
}
void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
_processNewChanges(sender as XFlowState);
}
async void _processNewChanges(XFlowState state)
{
if (state == null)
{
return;
}
using (var l = await _stateLock.LockAsync())
{
if (state.State == XFlowStates.Fail || state.State == XFlowStates.Success)
{
state.PropertyChanged -= item_PropertyChanged;
_inProgressStates.Remove(state);
}
if (!string.IsNullOrWhiteSpace(state.Text))
{
//Show the message on the UI or something... in this casue we're firing off to somewhere else,
//but you can do stuff here.
new BannerMessage(GetResource(state.ParentFlow.FriendlyName), GetResource(state.Text)).Send();
}
}
}
async void init()
{
_entryPublishFlow = _hub.GetFlow(Workflows.SaveNewEntryToServer);
_tripodPublishFlow = _hub.GetFlow(Workflows.NewTripod);
while (_entryPublishFlow == null || _tripodPublishFlow == null)
{
await Task.Delay(1000);
_entryPublishFlow = _hub.GetFlow(Workflows.SaveNewEntryToServer);
_tripodPublishFlow = _hub.GetFlow(Workflows.NewTripod);
}
_entryPublishFlow.FlowsUpdated += _entryPublishFlow_FlowsUpdated;
_tripodPublishFlow.FlowsUpdated += _tripodPublishFlow_FlowsUpdated;
_updateFlows();
}
void _tripodPublishFlow_FlowsUpdated(object sender, EventArgs e)
{
_updateFlows();
}
void _entryPublishFlow_FlowsUpdated(object sender, EventArgs e)
{
_updateFlows();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment