Skip to content

Instantly share code, notes, and snippets.

@BitStab
Created May 21, 2012 06:59
Show Gist options
  • Save BitStab/2760889 to your computer and use it in GitHub Desktop.
Save BitStab/2760889 to your computer and use it in GitHub Desktop.
Fragment in MVVMCross
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Cirrious.MvvmCross.Android.Views;
using Cirrious.MvvmCross.Binding.Android.Views;
using MobSales.Logic.ViewModels;
namespace MobSales.DroidUI
{
[Activity(Label = "MobSales Dashboard", WindowSoftInputMode = SoftInput.AdjustPan/*, ScreenOrientation=Android.Content.PM.ScreenOrientation.Landscape*/)]
public class DashboardView : MvxBindingFragmentActivityView<DashboardViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.test_frag);
}
protected override void OnViewModelSet()
{
//SetContentView(Resource.Layout.test_frag);
//BindingInflate(Resource.Layout.test_frag, (ViewGroup)this);
}
}
}
using System;
using System.Collections.Generic;
using Android.OS;
using Android.Views;
using Android.Content;
using Android.Util;
using Android.Widget;
using Cirrious.MvvmCross.Android.Views;
using Cirrious.MvvmCross.Binding.Android.Binders;
using Cirrious.MvvmCross.Binding.Android.Interfaces.Views;
using Cirrious.MvvmCross.Interfaces.ViewModels;
namespace Cirrious.MvvmCross.Binding.Android.Views
{
public abstract class MvxBindingFragment<TViewModel>
: MvxFragmentView<TViewModel>
, IMvxBindingActivity
where TViewModel : class, IMvxViewModel
{
public MvxBindingFragment()
: base()
{
}
#region Code shared across all binding activities - I hate this cut and paste
private readonly List<View> _boundViews = new List<View>();
public override void OnCreate(Bundle bundle)
{
ClearBoundViews();
base.OnCreate(bundle);
}
public override void OnActivityCreated(Bundle bundle)
{
ClearBoundViews();
base.OnActivityCreated(bundle);
}
public override void OnDestroy()
{
ClearBoundViews();
base.OnDestroy();
}
protected override void Dispose(bool disposing)
{
if (disposing)
ClearBoundViews();
base.Dispose(disposing);
}
public void ClearBindings(View view)
{
if (view == null)
return;
var cleaner = new MvxBindingLayoutCleaner();
cleaner.Clean(view);
for (var i = 0; i < _boundViews.Count; i++)
{
if (_boundViews[i] == view)
{
_boundViews.RemoveAt(i);
break;
}
}
}
private void ClearBoundViews()
{
var cleaner = new MvxBindingLayoutCleaner();
_boundViews.ForEach(cleaner.Clean);
_boundViews.Clear();
}
//public override LayoutInflater LayoutInflater
//{
// get
// {
// throw new InvalidOperationException("LayoutInflater must not be accessed directly in MvxBindingFragmentView - use IMvxBindingActivity.BindingInflate or IMvxBindingActivity.NonBindingInflate instead");
// }
//}
public virtual object DefaultBindingSource
{
get { return ViewModel; }
}
public View BindingInflate(int resourceId, ViewGroup viewGroup)
{
var view = BindingInflate(DefaultBindingSource, resourceId, viewGroup);
if (view != null)
_boundViews.Add(view);
return view;
}
public View BindingInflate(object source, int resourceId, ViewGroup viewGroup)
{
return CommonInflate(
resourceId,
viewGroup,
(layoutInflator) => new MvxBindingLayoutInflatorFactory(source, layoutInflator));
}
public View NonBindingInflate(int resourceId, ViewGroup viewGroup)
{
return CommonInflate(
resourceId,
viewGroup,
(layoutInflator) => null);
}
//public override void SetContentView(int layoutResId)
//{
// var view = BindingInflate(layoutResId, null);
// SetContentView(view);
//}
private View CommonInflate(int resourceId, ViewGroup viewGroup, Func<LayoutInflater, MvxBindingLayoutInflatorFactory> factoryProvider)
{
var layoutInflator = base.Activity.LayoutInflater;
using (var clone = layoutInflator.CloneInContext(viewGroup.Context))
{
using (var factory = factoryProvider(clone))
{
if (factory != null)
clone.Factory = factory;
var toReturn = clone.Inflate(resourceId, viewGroup);
if (factory != null)
{
factory.StoreBindings(toReturn);
}
return toReturn;
}
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using Android.OS;
using Android.Views;
using Android.Support.V4.App;
using Cirrious.MvvmCross.Android.Views;
using Cirrious.MvvmCross.Binding.Android.Binders;
using Cirrious.MvvmCross.Binding.Android.Interfaces.Views;
using Cirrious.MvvmCross.Interfaces.ViewModels;
namespace Cirrious.MvvmCross.Binding.Android.Views
{
public abstract class MvxBindingFragmentActivityView<TViewModel>
: MvxFragmentActivityView<TViewModel>
, IMvxBindingActivity
where TViewModel : class, IMvxViewModel
{
#region Code shared across all binding activities - I hate this cut and paste
private readonly List<View> _boundViews = new List<View>();
protected override void OnCreate(Bundle bundle)
{
ClearBoundViews();
base.OnCreate(bundle);
}
protected override void OnDestroy()
{
ClearBoundViews();
base.OnDestroy();
}
protected override void Dispose(bool disposing)
{
if (disposing)
ClearBoundViews();
base.Dispose(disposing);
}
public void ClearBindings(View view)
{
if (view == null)
return;
var cleaner = new MvxBindingLayoutCleaner();
cleaner.Clean(view);
for (var i = 0; i < _boundViews.Count; i++)
{
if (_boundViews[i] == view)
{
_boundViews.RemoveAt(i);
break;
}
}
}
private void ClearBoundViews()
{
var cleaner = new MvxBindingLayoutCleaner();
_boundViews.ForEach(cleaner.Clean);
_boundViews.Clear();
}
public override LayoutInflater LayoutInflater
{
get
{
return base.LayoutInflater;
}
}
//public override LayoutInflater LayoutInflater
//{
// get
// {
// throw new InvalidOperationException("LayoutInflater must not be accessed directly in MvxBindingFragmentView - use IMvxBindingFragment.BindingInflate or IMvxBindingFragment.NonBindingInflate instead");
// }
//}
public virtual object DefaultBindingSource
{
get { return ViewModel; }
}
public View BindingInflate(int resourceId, ViewGroup viewGroup)
{
var view = BindingInflate(DefaultBindingSource, resourceId, viewGroup);
if (view != null)
_boundViews.Add(view);
return view;
}
public View BindingInflate(object source, int resourceId, ViewGroup viewGroup)
{
return CommonInflate(
resourceId,
viewGroup,
(layoutInflator) => new MvxBindingLayoutInflatorFactory(source, layoutInflator));
}
public View NonBindingInflate(int resourceId, ViewGroup viewGroup)
{
return CommonInflate(
resourceId,
viewGroup,
(layoutInflator) => null);
}
public override void SetContentView(int layoutResId)
{
var view = BindingInflate(layoutResId, null);
base.SetContentView(view);
}
private View CommonInflate(int resourceId, ViewGroup viewGroup, Func<LayoutInflater, MvxBindingLayoutInflatorFactory> factoryProvider)
{
var layoutInflator = this.LayoutInflater;
using (var clone = layoutInflator.CloneInContext(this))
{
using (var factory = factoryProvider(clone))
{
if (factory != null)
clone.Factory = factory;
var toReturn = clone.Inflate(resourceId, viewGroup);
if (factory != null)
{
factory.StoreBindings(toReturn);
}
return toReturn;
}
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V4.App;
using Cirrious.MvvmCross.Android.ExtensionMethods;
using Cirrious.MvvmCross.Android.Interfaces;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Cirrious.MvvmCross.Platform.Diagnostics;
using Cirrious.MvvmCross.Views;
namespace Cirrious.MvvmCross.Android.Views
{
public abstract class MvxFragmentActivityView<TViewModel>
: FragmentActivity
, IMvxAndroidView<TViewModel>
, IMvxServiceConsumer<IMvxAndroidSubViewModelCache>
where TViewModel : class, IMvxViewModel
{
private readonly List<int> _ownedSubViewModelIndicies = new List<int>();
protected MvxFragmentActivityView()
{
IsVisible = true;
}
protected Intent CreateIntentFor<TTargetViewModel>(object parameterObject)
where TTargetViewModel : class, IMvxViewModel
{
return CreateIntentFor<TTargetViewModel>(parameterObject.ToSimplePropertyDictionary());
}
protected Intent CreateIntentFor<TTargetViewModel>(IDictionary<string, string> parameterValues = null)
where TTargetViewModel : class, IMvxViewModel
{
parameterValues = parameterValues ?? new Dictionary<string, string>();
var request = new MvxShowViewModelRequest<TTargetViewModel>(parameterValues, false, MvxRequestedBy.UserAction);
return CreateIntentFor(request);
}
protected Intent CreateIntentFor(MvxShowViewModelRequest request)
{
return this.GetService<IMvxAndroidViewModelRequestTranslator>().GetIntentFor(request);
}
protected Intent CreateIntentFor(IMvxViewModel subViewModel)
{
var intentWithKey = this.GetService<IMvxAndroidViewModelRequestTranslator>().GetIntentWithKeyFor(subViewModel);
_ownedSubViewModelIndicies.Add(intentWithKey.Item2);
return intentWithKey.Item1;
}
private void ClearOwnedSubIndicies()
{
var translator = this.GetService<IMvxAndroidViewModelRequestTranslator>();
foreach (var ownedSubViewModelIndex in _ownedSubViewModelIndicies)
{
translator.RemoveSubViewModelWithKey(ownedSubViewModelIndex);
}
_ownedSubViewModelIndicies.Clear();
}
#region Common code across all android views - one case for multiple inheritance?
private TViewModel _viewModel;
public Type ViewModelType
{
get { return typeof(TViewModel); }
}
public bool IsVisible { get; private set; }
public TViewModel ViewModel
{
get { return _viewModel; }
set
{
_viewModel = value;
OnViewModelSet();
}
}
public void MvxInternalStartActivityForResult(Intent intent, int requestCode)
{
base.StartActivityForResult(intent, requestCode);
}
public event EventHandler<MvxIntentResultEventArgs> MvxIntentResultReceived;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.OnViewCreate();
}
protected override void OnDestroy()
{
this.OnViewDestroy();
base.OnDestroy();
ClearOwnedSubIndicies();
}
protected abstract void OnViewModelSet();
protected override void OnResume()
{
base.OnResume();
IsVisible = true;
this.OnViewResume();
}
protected override void OnPause()
{
this.OnViewPause();
IsVisible = false;
base.OnPause();
}
protected override void OnStart()
{
base.OnStart();
this.OnViewStart();
}
protected override void OnRestart()
{
base.OnRestart();
this.OnViewRestart();
}
protected override void OnStop()
{
this.OnViewStop();
base.OnStop();
}
public override void StartActivityForResult(Intent intent, int requestCode)
{
switch (requestCode)
{
case (int)MvxIntentRequestCode.PickFromFile:
MvxTrace.Trace("Warning - activity request code may clash with Mvx code for {0}", (MvxIntentRequestCode)requestCode);
break;
default:
// ok...
break;
}
base.StartActivityForResult(intent, requestCode);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
var handler = MvxIntentResultReceived;
if (handler != null)
handler(this, new MvxIntentResultEventArgs(requestCode, resultCode, data));
base.OnActivityResult(requestCode, resultCode, data);
}
#endregion
}
}
using System;
using Android.Views;
using Android.Widget;
using Android.Content;
using Android.OS;
using Android.Util;
using Android.App;
using Cirrious.MvvmCross.Android.ExtensionMethods;
using Cirrious.MvvmCross.Android.Interfaces;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Cirrious.MvvmCross.Platform.Diagnostics;
namespace Cirrious.MvvmCross.Android.Views
{
public abstract class MvxFragmentView<TViewModel>
: Fragment
, IMvxAndroidView<TViewModel>
where TViewModel : class, IMvxViewModel
{
protected MvxFragmentView()
{
IsVisible = true;
}
public MvxFragmentView(Context context, IAttributeSet attrs)
: base()
{
}
#region Common code across all android views - one case for multiple inheritance?
private TViewModel _viewModel;
public Type ViewModelType
{
get { return typeof(TViewModel); }
}
public bool IsVisible { get; private set; }
public TViewModel ViewModel
{
get { return _viewModel; }
set
{
_viewModel = value;
this.OnViewModelSet();
}
}
public void MvxInternalStartActivityForResult(Intent intent, int requestCode)
{
this.Activity.StartActivityForResult(intent, requestCode);
}
protected abstract void OnViewModelSet();
public event EventHandler<MvxIntentResultEventArgs> MvxIntentResultReceived;
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.OnViewCreate(true);
}
public override void OnDestroy()
{
this.OnViewDestroy();
base.OnDestroy();
}
public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
{
return base.OnCreateView(p0, p1, p2);
}
public override void OnResume()
{
base.OnResume();
IsVisible = true;
this.OnViewResume();
}
public override void OnPause()
{
this.OnViewPause();
IsVisible = false;
base.OnPause();
}
public override void OnStart()
{
base.OnStart();
this.OnViewStart();
}
//protected override void OnRestart()
//{
// base.OnResume
// this.OnViewRestart();
//}
public override void OnStop()
{
this.OnViewStop();
base.OnStop();
}
public override void StartActivityForResult(Intent intent, int requestCode)
{
switch (requestCode)
{
case (int)MvxIntentRequestCode.PickFromFile:
MvxTrace.Trace("Warning - activity request code may clash with Mvx code for {0}", (MvxIntentRequestCode)requestCode);
break;
default:
// ok...
break;
}
base.StartActivityForResult(intent, requestCode);
}
public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
//Result code = (Result)resultCode;
var handler = MvxIntentResultReceived;
if (handler != null)
handler(this, new MvxIntentResultEventArgs(requestCode,resultCode, data));
base.OnActivityResult(requestCode, resultCode, data);
}
#endregion
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<cirrious.mvvmcross.binidng.android.views.MvxBindingFragment
class="mobsales.droidui.TitlesFragment"
android:id="@+id/titles_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Cirrious.MvvmCross.Binding.Android.Views;
using Cirrious.MvvmCross.Android.Views;
using MobSales.Logic.ViewModels;
namespace MobSales.DroidUI
{
public class TitlesFragment : MvxBindingFragment<CustomersViewModel>
{
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
}
public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
{
View fragmentView = p0.Inflate(Resource.Layout.Dashboard, p1, false);
return fragmentView;
}
protected override void OnViewModelSet() { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment