Skip to content

Commit

Permalink
Fixed Droid Forms Presenter.
Browse files Browse the repository at this point in the history
Navigation from Mvx-Non Forms Activity to Forms Activity is available by ShowViewModel<..> now.

Issue #49
  • Loading branch information
thefex committed May 1, 2016
1 parent 846d8ed commit 2b0dfda
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 56 deletions.
24 changes: 17 additions & 7 deletions MvvmCross.Forms.Presenter.Core/MvxFormsPagePresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

namespace MvvmCross.Forms.Presenter.Core
{
public class MvxFormsPagePresenter
: MvxViewPresenter
public abstract class MvxFormsPagePresenter : MvxViewPresenter
{
private Application _mvxFormsApp;

Expand All @@ -26,18 +25,18 @@ public Application MvxFormsApp
set
{
if (value == null)
{
throw new ArgumentException("MvxFormsApp cannot be null");
}

_mvxFormsApp = value;
}
}

public MvxFormsPagePresenter()
{ }
protected MvxFormsPagePresenter()
{

}

public MvxFormsPagePresenter(Application mvxFormsApp)
protected MvxFormsPagePresenter(Application mvxFormsApp)
{
MvxFormsApp = mvxFormsApp;
}
Expand All @@ -63,12 +62,23 @@ public override async void ChangePresentation(MvxPresentationHint hint)

public override void Show(MvxViewModelRequest request)
{
if (!IsNativeFormPageActive())
{
NavigateToNativeFormPage(request);
return;
}

if (TryShowPage(request))
return;

Mvx.Error("Skipping request for {0}", request.ViewModelType.Name);
}

protected abstract bool IsNativeFormPageActive();

protected abstract void NavigateToNativeFormPage(MvxViewModelRequest withViewModelRequest);


protected virtual void CustomPlatformInitialization(NavigationPage mainPage)
{
}
Expand Down
1 change: 0 additions & 1 deletion MvvmCross.Forms.Presenter.Core/MvxFormsPresenterProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public override void Show(MvxViewModelRequest request)
}
}


public override void ChangePresentation(MvxPresentationHint hint)
{
// TODO: At this moment only Classic ViewModels support change presentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
<HintPath>..\packages\MvvmCross.Platform.4.1.4\lib\MonoAndroid\MvvmCross.Platform.Droid.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -134,6 +138,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MvxDroidFormsPagePresenter.cs" />
<Compile Include="MvxFormsAppCompatActivity.cs" />
<Compile Include="MvxDroidFormsPresenterProxy.cs" />
<Compile Include="MvxFormsSplashScreenActivity.cs" />
Expand Down
54 changes: 54 additions & 0 deletions MvvmCross.Forms.Presenter.Droid/MvxDroidFormsPagePresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Android.Content;
using MvvmCross.Core.ViewModels;
using MvvmCross.Forms.Presenter.Core;
using MvvmCross.Platform;
using MvvmCross.Platform.Droid.Platform;
using Newtonsoft.Json;

namespace MvvmCross.Forms.Presenter.Droid
{
public class MvxDroidFormsPagePresenter : MvxFormsPagePresenter
{
private readonly Type _formsAppCompatActivityType;
public static readonly string FirstNavigationRequestPackageExtraKey = "FirstNavigationRequestPackageKey";


public MvxDroidFormsPagePresenter(Type formsAppCompatActivityType) : base()
{
_formsAppCompatActivityType = formsAppCompatActivityType;

if (!typeof(MvxFormsAppCompatActivity).IsAssignableFrom(_formsAppCompatActivityType))
throw new InvalidOperationException($"Passed type should inherit from {nameof(MvxFormsAppCompatActivity)}");
}

public MvxDroidFormsPagePresenter(Type formsAppCompatActivityType, Xamarin.Forms.Application mvxFormsApp) : base(mvxFormsApp)
{
_formsAppCompatActivityType = formsAppCompatActivityType;

if (!typeof(MvxFormsAppCompatActivity).IsAssignableFrom(_formsAppCompatActivityType))
throw new InvalidOperationException($"Passed type should inherit from {nameof(MvxFormsAppCompatActivity)}");
}

protected override bool IsNativeFormPageActive()
{
Type baseDroidFormPage = typeof(MvxFormsAppCompatActivity);
Type currentActivityType = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity.GetType();

return baseDroidFormPage.IsAssignableFrom(currentActivityType);
}

protected override void NavigateToNativeFormPage(MvxViewModelRequest withViewModelRequest)
{
var currentActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
var requestText = JsonConvert.SerializeObject(withViewModelRequest);
var intent = new Intent(currentActivity, _formsAppCompatActivityType);
intent.PutExtra(FirstNavigationRequestPackageExtraKey, requestText);

currentActivity.StartActivity(intent);
}



}
}
73 changes: 71 additions & 2 deletions MvvmCross.Forms.Presenter.Droid/MvxFormsAppCompatActivity.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using MvvmCross.Droid.Platform;
using MvvmCross.Forms.Presenter.Core;
using MvvmCross.Platform;
using MvvmCross.Platform.Droid.Views;
using Newtonsoft.Json;
using Xamarin.Forms.Platform.Android;

namespace MvvmCross.Forms.Presenter.Droid
{
public abstract class MvxFormsAppCompatActivity : FormsAppCompatActivity
{
// thefex notes in comments

// TODO: LOGIC EXCEPT OnCreate should be refactored into something like Native MvxEventSource..Activity
// MvxForms..Activity should be a bridge between Forms and Classic Xamarin world
protected MvxFormsAppCompatActivity()
{
}

protected override void OnCreate(Bundle bundle)
{
// TODO ENSURE SETUP OF MVVMCROSS DONE
OnLifeTimeEvent((listener, activity) => listener.OnCreate(activity));

base.OnCreate(bundle);

Xamarin.Forms.Forms.Init(this, bundle);
Expand All @@ -38,8 +54,61 @@ protected override void OnCreate(Bundle bundle)

formsPresenter.MvxFormsApp = mvxFormsApp;

// TODO: Forms specific MvxAppStart
Mvx.Resolve<IMvxAppStart>().Start();

if (Intent?.Extras != null && Intent.Extras.ContainsKey(MvxDroidFormsPagePresenter.FirstNavigationRequestPackageExtraKey))
{
var jsonViewModelRequest = Intent.Extras.GetString(MvxDroidFormsPagePresenter.FirstNavigationRequestPackageExtraKey) as string;
var viewModelRequest = JsonConvert.DeserializeObject<MvxViewModelRequest>(jsonViewModelRequest);
formsPresenter.Show(viewModelRequest);
}
}


// TODO: REFACTOR IMVXANDROIDVIEW INTO FEW INTERFACES
// TODO: SO LIFE TIME EVENTS CAN BE USED Like in EventSource activities

protected override void OnDestroy()
{
OnLifeTimeEvent((listener, activity) => listener.OnDestroy(activity));
base.OnDestroy();
}



protected override void OnResume()
{
base.OnResume();
OnLifeTimeEvent((listener, activity) => listener.OnResume(activity));
}

protected override void OnPause()
{
OnLifeTimeEvent((listener, activity) => listener.OnPause(activity));
base.OnPause();
}

protected override void OnStart()
{
base.OnStart();
OnLifeTimeEvent((listener, activity) => listener.OnStart(activity));
}

protected override void OnRestart()
{
base.OnRestart();
OnLifeTimeEvent((listener, activity) => listener.OnRestart(activity));
}

protected override void OnStop()
{
OnLifeTimeEvent((listener, activity) => listener.OnStop(activity));
base.OnStop();
}

private void OnLifeTimeEvent(Action<IMvxAndroidActivityLifetimeListener, Activity> report)
{
var activityTracker = Mvx.Resolve<IMvxAndroidActivityLifetimeListener>();
report(activityTracker, this);
}
}
}
1 change: 1 addition & 0 deletions MvvmCross.Forms.Presenter.Droid/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="MvvmCross.Binding" version="4.1.4" targetFramework="monoandroid60" />
<package id="MvvmCross.Core" version="4.1.4" targetFramework="monoandroid60" />
<package id="MvvmCross.Platform" version="4.1.4" targetFramework="monoandroid60" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.Animated.Vector.Drawable" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.Design" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v4" version="23.3.0" targetFramework="monoandroid60" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using IntegrationSample.ViewModels;
using MvvmCross.Droid.Views;

namespace IntegrationSample.Droid
{
class FeedbackActivity
[Activity(Label = "Feedback", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class FeedbackActivity : MvxActivity<FeedbackViewModel>
{
public FeedbackActivity()
{

}

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.FeedbackLayout);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<HintPath>..\..\packages\MvvmCross.Core.4.1.4\lib\MonoAndroid\MvvmCross.Droid.Shared.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Forms.Presenter.Core">
<HintPath>..\..\..\bin\Debug\Droid\MvvmCross.Forms.Presenter.Core.dll</HintPath>
</Reference>
<Reference Include="MvvmCross.Forms.Presenter.Droid">
<HintPath>..\..\..\bin\Debug\Droid\MvvmCross.Forms.Presenter.Droid.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -145,10 +148,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="FeedbackActivity.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Setup.cs" />
<Compile Include="SplashScreenActivity.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -165,8 +170,8 @@
<None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Tabbar.axml" />
<AndroidResource Include="Resources\layout\Toolbar.axml" />
<AndroidResource Include="Resources\layout\Tabbar.xml" />
<AndroidResource Include="Resources\layout\Toolbar.xml" />
<AndroidResource Include="Resources\values\styles.xml" />
</ItemGroup>
<ItemGroup>
Expand All @@ -175,6 +180,12 @@
<Name>IntegrationSample</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\SplashScreen.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\FeedbackLayout.xml" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets" Condition="Exists('..\..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
using Android.Views;
using Android.Widget;
using Android.OS;
using MvvmCross.Forms.Presenter.Droid;

namespace IntegrationSample.Droid
{
[Activity(Label = "IntegrationSample", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
[Activity(Label = "IntegrationSample", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : MvxFormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

base.OnCreate(bundle);

global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
base.OnCreate(bundle);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
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.Views;
using Android.Widget;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using MvvmCross.Droid.Platform;
using MvvmCross.Droid.Views;
using MvvmCross.Forms.Presenter.Core;
using MvvmCross.Forms.Presenter.Droid;
using MvvmCross.Platform;

namespace IntegrationSample.Droid
{
class Setup : MvxAndroidSetup
public class Setup : MvxAndroidSetup
{
public Setup(Context applicationContext) : base(applicationContext)
{
}

protected override IMvxApplication CreateApp()
=> new IntegrationSampleMvxApp();
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var baseAndroidPresenter = base.CreateViewPresenter();

var presenter = new MvxDroidFormsPresenterProxy(baseAndroidPresenter, new MvxDroidFormsPagePresenter(typeof(MainActivity)));
Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);

return presenter;
}
}
}
Loading

0 comments on commit 2b0dfda

Please sign in to comment.