Skip to content

Commit

Permalink
Implemented basics of new MvvmCross->Forms coexiistence support.
Browse files Browse the repository at this point in the history
Implemented scratch of Droid new example.
  • Loading branch information
thefex committed May 1, 2016
1 parent 39180b6 commit 846d8ed
Show file tree
Hide file tree
Showing 115 changed files with 24,029 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace MvvmCross.Forms.Presenter.Core.Attribute
{
public class MvxAssociatedViewTypeAttribute : System.Attribute
{
public MvxViewType ViewType { get; }

public MvxAssociatedViewTypeAttribute(MvxViewType viewType)
{
ViewType = viewType;
}
}
}
8 changes: 8 additions & 0 deletions MvvmCross.Forms.Presenter.Core/Attribute/MvxViewType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MvvmCross.Forms.Presenter.Core.Attribute
{
public enum MvxViewType
{
Forms = 0,
Classic = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Attribute\MvxAssociatedViewTypeAttribute.cs" />
<Compile Include="Attribute\MvxViewType.cs" />
<Compile Include="IMvxFormsPageLoader.cs" />
<Compile Include="MvxFormsPageLoader.cs" />
<Compile Include="MvxFormsPagePresenter.cs" />
<Compile Include="MvxFormsPresenterProxy.cs" />
<Compile Include="MvxPresenterHelpers.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MvxFormsApp.cs" />
Expand Down
52 changes: 52 additions & 0 deletions MvvmCross.Forms.Presenter.Core/MvxFormsPresenterProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Linq;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using MvvmCross.Forms.Presenter.Core.Attribute;
using MvvmCross.Platform;

namespace MvvmCross.Forms.Presenter.Core
{
public class MvxFormsPresenterProxy : MvxViewPresenter
{
public readonly IMvxViewPresenter ClassicMvxViewPresenter;
public readonly MvxFormsPagePresenter FormsViewPresenter;

public MvxFormsPresenterProxy(IMvxViewPresenter classicMvxViewPresenter, MvxFormsPagePresenter formsViewPresenter)
{
ClassicMvxViewPresenter = classicMvxViewPresenter;
FormsViewPresenter = formsViewPresenter;
}

public override void Show(MvxViewModelRequest request)
{
var mvxViewType = GetViewType(request);
switch (mvxViewType)
{
case MvxViewType.Forms:
FormsViewPresenter.Show(request);
break;
case MvxViewType.Classic:
ClassicMvxViewPresenter.Show(request);
break;
}
}


public override void ChangePresentation(MvxPresentationHint hint)
{
// TODO: At this moment only Classic ViewModels support change presentation
ClassicMvxViewPresenter.ChangePresentation(hint);
}

protected virtual MvxViewType GetViewType(MvxViewModelRequest fromViewModelRequest)
{
var mvxAssociatedViewTypeAttribute =
fromViewModelRequest.ViewModelType.GetCustomAttributes(typeof(MvxAssociatedViewTypeAttribute), false)
.Cast<MvxAssociatedViewTypeAttribute>()
.SingleOrDefault();

return mvxAssociatedViewTypeAttribute?.ViewType ?? MvxViewType.Forms;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MvxFormsDroidPagePresenter.cs" />
<Compile Include="MvxFormsAppCompatActivity.cs" />
<Compile Include="MvxDroidFormsPresenterProxy.cs" />
<Compile Include="MvxFormsSplashScreenActivity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// MvxFormsDroidPagePresenter.cs
// MvxDroidFormsPresenterProxy.cs
// 2015 (c) Copyright Cheesebaron. http://ostebaronen.dk
// MvvmCross.Forms.Presenter is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Tomasz Cielecki, @cheesebaron, [email protected]
// Contributor - Marcos Cobeña Morián, @CobenaMarcos, [email protected]

using MvvmCross.Core.Views;
using MvvmCross.Droid.Views;
using MvvmCross.Forms.Presenter.Core;

namespace MvvmCross.Forms.Presenter.Droid
{
public class MvxFormsDroidPagePresenter
: MvxFormsPagePresenter
public class MvxDroidFormsPresenterProxy
: MvxFormsPresenterProxy
, IMvxAndroidViewPresenter
{
public MvxFormsDroidPagePresenter()
{
}

public MvxFormsDroidPagePresenter(MvxFormsApp mvxFormsApp)
: base(mvxFormsApp)
public MvxDroidFormsPresenterProxy(IMvxViewPresenter classicMvxViewPresenter, MvxFormsPagePresenter formsViewPresenter) : base(classicMvxViewPresenter, formsViewPresenter)
{
}
}


}
45 changes: 45 additions & 0 deletions MvvmCross.Forms.Presenter.Droid/MvxFormsAppCompatActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Android.OS;
using MvvmCross.Core.ViewModels;
using MvvmCross.Core.Views;
using MvvmCross.Forms.Presenter.Core;
using MvvmCross.Platform;
using Xamarin.Forms.Platform.Android;

namespace MvvmCross.Forms.Presenter.Droid
{
public abstract class MvxFormsAppCompatActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

Xamarin.Forms.Forms.Init(this, bundle);

var mvxFormsApp = new MvxFormsApp();
LoadApplication(mvxFormsApp);

var presenter = Mvx.Resolve<IMvxViewPresenter>() as MvxFormsPresenterProxy;
if (presenter == null)
{
var errorMsg = $"To use MvvmCross with Forms you have to use {nameof(MvxFormsPresenterProxy)}";
Mvx.Error(errorMsg);
throw new InvalidOperationException(errorMsg);
}

var formsPresenter = presenter.FormsViewPresenter;
if (formsPresenter == null)
{
var errorMsg =
$"To use MvvmCross with Android and Forms your {nameof(MvxFormsPresenterProxy)} have to use ${nameof(MvxFormsPagePresenter)} as Forms presenter.";
Mvx.Error(errorMsg);
throw new InvalidOperationException(errorMsg);
}

formsPresenter.MvxFormsApp = mvxFormsApp;

// TODO: Forms specific MvxAppStart
Mvx.Resolve<IMvxAppStart>().Start();
}
}
}
27 changes: 27 additions & 0 deletions MvvmCross.Forms.Presenter.Droid/MvxFormsSplashScreenActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Android.OS;
using MvvmCross.Droid.Views;

namespace MvvmCross.Forms.Presenter.Droid
{
public abstract class MvxFormsSplashScreenActivity : MvxSplashScreenActivity
{
protected MvxFormsSplashScreenActivity(int resourceId = 0) : base(resourceId)
{
}

protected override void OnCreate(Bundle bundle)
{
Xamarin.Forms.Forms.Init(this, bundle);
// Leverage controls' StyleId attrib. to Xamarin.UITest
Xamarin.Forms.Forms.ViewInitialized += (sender, e) =>
{
if (!string.IsNullOrWhiteSpace(e.View.StyleId))
{
e.NativeView.ContentDescription = e.View.StyleId;
}
};

base.OnCreate(bundle);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with you package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;

namespace IntegrationSample.Droid
{
class FeedbackActivity
{
}
}
Loading

0 comments on commit 846d8ed

Please sign in to comment.