Xamarin.Android Swipe to Refresh in MVVMCross loader loading infinitely - xamarin.android

My code is
.AXML file
<MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
local:MvxBind="Refreshing IsBusy;RefreshCommand RefreshCommand">
my view model
private MvxCommand refreshCommand;
public ICommand RefreshCommand
{
get
{
return refreshCommand ?? (refreshCommand = new MvxCommand(ExecuteRefreshCommand));
}
}
private bool m_IsBusy;
public new bool IsBusy
{
get { return m_IsBusy; }
set
{
m_IsBusy = value; RaisePropertyChanged(() => IsBusy);
BaseMessage = m_IsBusy ? "Refreshing..." : string.Empty;
}
}
async private void ExecuteRefreshCommand()
{
await Task.Run(() => { m_IsBusy = false; });
}
I can able to see the loader,loader is loading indefinitely , but couldn't able to stop the Swipe to refresh loader.

Looking at the code of MvxSwipeRefreshLayout you can see it inherits from SwipeRefreshLayout. SwipeRefreshLayout contains a boolean Refreshing. This indicates wether it's loading and thus a loading icon should be shown. Setting this to false will hide the animation.
You can do this by binding to it. Create an extra property for example isRefreshing in your ViewModel like:
private bool _isRefreshing;
public bool IsRefreshing
{
get { return _isRefreshing; }
set
{
_isRefreshing = value;
RaisePropertyChanged(() => IsRefreshing);
}
}
Now in your view bind actually bind the two like:
<MvxSwipeRefreshLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
local:MvxBind="Refreshing IsRefreshing; RefreshCommand RefreshCommand">
Now you have to manually set the boolean to false when your 'refresh work' is done. For example:
public IMvxCommand RefreshCommand
{
get
{
return new MvxCommand(async () => {
// This simulates some refresh work which takes 3 seconds
await Task.Delay(3000);
IsRefreshing = false;
});
}
}

Related

xamarin ios Tableview is not getting Reloaded when i use MvxSimpleTableViewSource

My table view does not update whenever its source property is changed. The code is as follows:
public override void ViewDidLoad()
{
base.ViewDidLoad();
viewmodel = this.ViewModel as ListViewModel;
viewmodel.PropertyChanged += HandlePropertyChangedEventHandler;;
var source = new MvxSimpleTableViewSource( TableView, LaborCell.Key, LaborCell.Key);
TableView.Source = source;
var set = this.CreateBindingSet<ListView, ListViewModel>();
set.Bind(source).To(vm => vm.LaborTransactions);
set.Apply();
TableView.ReloadData();
}
ViewModel:
public class ListViewModel :MaxRawBaseViewModel
{
public ListViewModel():base()
{
LoadLaborTransactions();
}
private Collection<LaborTransaction> _laborTransactions;
public Collection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
}
public void LoadLaborTransactions()
{
_laborTransactions = DataService.GetLaborTransactions(somenumber);
RaisePropertyChanged(() => LaborTransactions);
}
}
When ever the change in Transactions am calling the tablview.reolad() on propertychanged method. but it is not reloading my tableview
void HandlePropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e){
if (e.PropertyName.Equals("LaborTransactions"))
{
TableView.ReloadData();
}
}
Collection<T> does not implement INotifyPropertyChanged. You can verify that in the docs here. You need to change your LaborTransactions property to a collection type that implements INotifyPropertyChanged like ObservableCollection<T> and MvxObservableCollection<T>. You can see that ObservableCollection<T> implements INotifyPropertyChanged here
Change your LaborTransactions as such:
private ObservableCollection<LaborTransaction> _laborTransactions;
public ObservableCollection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
set {
return _laborTransactions;
RaisePropertyChanged(() => LaborTransactions);
}
}

MVVMCROSS Ios Binding ShowViewModel

I've got problem with data-binding in mvvmcross after doing the navigation in the model by calling the showviewmodel-method. On the android side it works.
So the Problem is, that the navigation itself is working but I don't get any data from the model.
Navigation in the model:
ShowViewModel<TeamEventDetailsViewModel>(new { eventID = item.ID });
ViewModel which containts the Data:
public class TeamEventDetailsViewModel
: EventDetailsViewModel
{
public TeamEventModel CurrentEvent
{
get { return MyCurrentEvent as TeamEventModel; }
set
{
MyCurrentEvent = value;
RaisePropertyChanged(() => CurrentEvent);
TickerModel.Comments = value.Comments;
RaisePropertyChanged(() => TickerModel);
LineupModel.Team1Players = value.Team1Players;
LineupModel.Team2Players = value.Team2Players;
RaisePropertyChanged(() => LineupModel);
}
}
private EventDetailsLineupViewModel _lineupModel = new EventDetailsLineupViewModel();
public EventDetailsLineupViewModel LineupModel
{
get { return _lineupModel; }
set { _lineupModel = value; RaisePropertyChanged(() => LineupModel); }
}
public TeamEventDetailsViewModel()
{
EventToken = MvxMessenger.Subscribe<EventUpdateMessage>(OnEventUpdateMessage);
}
private void OnEventUpdateMessage(EventUpdateMessage eventUpdate)
{
if (MyCurrentEvent != null && eventUpdate.Event.ID == MyCurrentEvent.ID)
{
var updatedEvent = (TeamEventModel)eventUpdate.Event;
var myEvent = CurrentEvent;
if(updatedEvent.Score!=null)
myEvent.Score = updatedEvent.Score;
if (updatedEvent.Team1Players != null)
myEvent.Team1Players = updatedEvent.Team1Players;
if (updatedEvent.Team2Players != null)
myEvent.Team2Players = updatedEvent.Team2Players;
CurrentEvent = myEvent;
}
}
protected override void Update(EventModel eventdetails)
{
CurrentEvent = (TeamEventModel) eventdetails;
}
private string _teststring = "success";
public string Teststring
{
get { return _teststring; }
set
{
_teststring = value;
RaisePropertyChanged(()=>_teststring);
}
}
}
As you can see at the bottom I implemented a teststring to prove functionality.
Binding in the View:
public class TeamEventDetailsView : MvxViewController
{
public UILabel TestLabel = new UILabel();
public TeamEventDetailsViewModel TeamEventDetailsViewModel
{
get { return (TeamEventDetailsViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
public override void ViewDidLoad()
{
View.AddSubview(TestLabel);
this.CreateBinding(TestLabel).To<TeamEventDetailsViewModel>(vm => vm.Teststring).Apply();
TestLabel.BackgroundColor = UIColor.Orange;
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
TestLabel.Frame=new RectangleF(0,20,View.Frame.Width,80);
}
}
So I repeat, the navigation itself works but the data from model doesn't get shown on the view.
If I create the ViewModel manually in the View then the binding works also, but in my Situation I can't do that because the Data is pulled depending on the generated Data from the ViewModel which calls the navigation-proceed.
Manual ViewModel:
TeamEventDetailsViewModel = new TeamEventDetailsViewModel();
TeamEventDetailsViewModel.Init(9816);
As I can tell I did exactly the same as Stuard does in his Tutorial:
https://www.youtube.com/watch?v=cbdPDZmuHk8
Does anyone has an advice for me?
Thanks.
MvvmCross does the ViewModel create in base.ViewDidLoad() - if you add that call to your ViewDidLoad override then everything should work ok

MVVMCROSS Binding TimeSyncProblem

I've got a special problem with binding data to an itemsource of an mvxtablieviewsource.
I'm trying to generate a list of favorites, which are generated in the core by clicking on a different tablview.
Normally I get the databinding working like this: (Just basic structure)
controller:
var source = new MySource(TableView);
this.AddBindings(new Dictionary<object, string>
{
{source, "ItemsSource Favs"}
});
Source:
private List<FavModel> _favs;
public override IEnumerable ItemsSource
{
get { return _favs; }
set
{
_favs = (List<FavModel>)value;
ReloadTableData();
}
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
var cell = new AdFavCell();
cell.TextLabel.Text = ((FavModel)item).Display;
return cell;
}
Normally it works really great but no in this case where i generate the data by reacting on users touch, I've got this strange failure;
When I set a breakpoint in the setter of the ItemsSource, and wait for a while then it works correctly.
When I run without a breakpoint the tableview keeps empty.
I also figured out that if I insert a manually pause in the setter then it works too:
Setter with pause:
public override IEnumerable ItemsSource
{
get { return _mydata; }
set
{
_favs = (List<FavModel>)value;
ReloadTableData();
Task.Delay(1000).Wait();
}
}
I also tried to do a delaybinding, but it didn't work.
Have anyone an idea where the problem is?
Edit:
Here some additional Information:
How the Data is generated:
I've got a tableview with content and depending on a longclick on a cell, I create a popumenu where you can add your favorites.
Detecting the longclick:
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
MvxTableViewCell cell = null;
if (item is SoccerEventListModel)
{
cell = tableView.DequeueReusableCell(this.CellIdentifier) as SoccerEvent;
if (cell == null)
{
cell = new SoccerEvent((SoccerEventListModel)item);
cell.AddGestureRecognizer(new UILongPressGestureRecognizer((e) =>
{
if (e.State == UIGestureRecognizerState.Began)
{
var command = ItemLongClickCommand;
if (command != null)
command.Execute(item);
}
}));
return cell;
}
}
}
Binding the Longclick to the core:
EventListViewModel.EventFavViewCallbackEvent += EventListViewModel_EventFavViewCallbackEvent;
void EventListViewModel_EventFavViewCallbackEvent(EventModel e)
{
var StoreFav = new EventFavoritesView { ViewModel = new EventFavoritesViewModel { ID = e.ID } };
View.Add(StoreFav.View);
}
Depending on the ID of the cell, it creates the list of the favorites by sending a request to our server.
Update:
private long _id;
public long ID
{
get { return _id; }
set { _id = value; RaisePropertyChanged(() => ID); Update(); }
}
When the data is received a RaisePropertyChanged() should make the view to reload its content.
private List<FavModel> _favs;
public List<FavModel> Favs
{
get { return _favs; }
set { _favs = value; RaisePropertyChanged(() => Favs); }
}
ViewModel:
public class EventFavoritesViewModel : MvxViewModel
{
private readonly EventFavoritesService _eventFavoriteService;
private readonly UserFavoritesService _userFavoriteService;
private long _id;
public long ID
{
get { return _id; }
set { _id = value; RaisePropertyChanged(() => ID); Update(); }
}
private string _title;
public string Title
{
get { return _title; }
set { _title = value; RaisePropertyChanged(() => Title); }
}
private List<FavModel> _favs;
public List<FavModel> Favs
{
get { return _favs; }
set { _favs = value; RaisePropertyChanged(() => Favs); }
}
private MvxCommand<FavModel> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<FavModel>(ToggleFav);
return _itemSelectedCommand;
}
}
public void Init(long eventID)
{
MvxTrace.Trace("We get the details", Logger.Errorlevel.Debug);
ID = eventID;
}
public EventFavoritesViewModel()
{
_eventFavoriteService = new EventFavoritesService(UpdateEventFav);
_userFavoriteService = new UserFavoritesService(UpdateUserFav);
}
private void UpdateUserFav(Fav[] favlist)
{
MvxMessenger.Publish(new UserFavUpdateMessage(this, favlist));
}
private void Update()
{
Favs = _eventFavoriteService.GetFavforEvent(ID).MapToFavs();
}
private void UpdateEventFav(Fav[] favlist)
{
Favs = favlist.MapToFavs();
}
private void ToggleFav(FavModel item)
{
MvxTrace.Trace("Got Item: " + item.Display);
item.NewSubscription = !item.NewSubscription;
}
private IMvxMessenger MvxMessenger
{
get
{
return Mvx.Resolve<IMvxMessenger>();
}
}
public void SaveFavs()
{
foreach (var fav in Favs)
{
if (fav.AlreadySubscribed != fav.NewSubscription)
{
if (fav.NewSubscription)
_userFavoriteService.PutToUserFavorites(fav.MapToFav());
else
_userFavoriteService.DeleteFromUserFavorites(fav.MapToFav());
}
}
}
}
I hope this is enough information, otherwise just tell me.:-)
Thanks for any help.

How to make WebView editable on winrt?

I need to create simple HTML editor. I know desktop application I can get access to DOM and set DesignMode=true. How can I do it for WebView in winrt application?
So seems I've found solution how to set DesignMode for WebView in WinRT applications.
I just needed invoke javascript method that could change document.designMode property to "on"
In my case I implemented extension for WebView where added DependencyProperty.
public static class WebViewEx
{
public static readonly DependencyProperty DesignModeProperty = DependencyProperty.RegisterAttached(
"DesignMode", typeof(bool),
typeof(WebViewEx),
new PropertyMetadata(null, OnDesignModePropertyChanged));
private async static void OnDesignModePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (DesignMode.DesignModeEnabled)
return;
WebView view = dependencyObject as WebView;
if (view == null)
return;
if (e.NewValue == e.OldValue)
return;
await view.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
if ((bool)e.NewValue)
{
await view.InvokeScriptAsync("eval", new string[] { "document.designMode = \"on\";" });
}
else
{
await view.InvokeScriptAsync("eval", new string[] { "document.designMode = \"off\";" });
}
});
}
public static void SetDesignMode(DependencyObject element, bool value)
{
element.SetValue(DesignModeProperty, value);
}
public static bool GetDesignMode(DependencyObject element)
{
return (bool)element.GetValue(DesignModeProperty);
}
}
That allows me to turn on\off DesignMode from XAML
<WebView x:Name="webViewBody" Source="about:blank" controls:WebViewEx.DesignMode="true"/>
Mandatory requirement to Invoke javascript methods is webview should be initialized. In my case I set source property to "about:blank"

MvvmCross iOS UITableView doesn't update on Property Changed

My table view does not update whenever its source property is changed. The code is as follows:
ViewController:
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.White;
var table = new UITableView(new RectangleF(0, 80, Device.Width, Device.Height - 80));
Add(table);
var source = new MvxStandardTableViewSource(table, "TitleText SessionInfo");
table.Source = source;
var set = this.CreateBindingSet<JoinSessionViewController, JoinSessionViewModel>();
set.Bind(source).To(vm => vm.AvailableServers);
set.Apply();
table.ReloadData();
}
ViewModel:
private readonly INetworkSessionClient _client;
public JoinSessionViewModel(INetworkSessionClient client)
{
_client = client;
_client.ServerFound += ClientOnServerFound;
_client.BeginSearchingForServers();
}
private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;
AvailableServers.Add(serverFoundEventArgs.ServerInfo);
RaisePropertyChanged(() => AvailableServers);
}
private List<ServerInfo> _availableServers;
public List<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}
This was a tricky one. The culprit is this line in MvxTableViewSource:
https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxTableViewSource.cs#L56
public virtual IEnumerable ItemsSource
{
get { return _itemsSource; }
set
{
if (_itemsSource == value) // **** This one ****
return;
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}
_itemsSource = value;
var collectionChanged = _itemsSource as INotifyCollectionChanged;
if (collectionChanged != null)
{
_subscription = collectionChanged.WeakSubscribe(CollectionChangedOnCollectionChanged);
}
ReloadTableData();
}
}
Since I was only adding to the list and not setting it to a new list, the setter was early returning and not firing ReloadTableData(). The fix was to use an ObservableCollection (or anything else that implements INotifyCollectionChanged) instead of a List.
private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;
// CollectionChanged event is not automatically martialed to UI thread
InvokeOnMainThread(() => AvailableServers.Add(serverFoundEventArgs.ServerInfo));
}
private ObservableCollection<ServerInfo> _availableServers;
public ObservableCollection<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}
Note that because of my particular case, the ServerFound event is being invoked on a separate thread. That means the INotifyCollectionChanged.CollectionChanged event will also be invoked on a separate thread, so the action of adding the server to the list has to be martialed onto the main thread with InvokeOnMainThread() so that the UI will properly update.
It would be nice if CollectionChanged events would automatically be handled on the UI thread, similar to how RaisePropertyChanged() works.

Resources