Skip to content

Commit

Permalink
Update Android Gallery to use Maui (#1558)
Browse files Browse the repository at this point in the history
* Update Android Gallery to use Maui

* - latest fixes

* - fix page resolvers

* - fix registration services
  • Loading branch information
PureWeen authored Feb 15, 2022
1 parent 67bc44d commit b00586c
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/Compatibility/ControlGallery/src/Android/Activity1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.Android
{
public partial class Activity1
{
App _app;

void AddNativeControls(NestedNativeControlGalleryPage page)
{
if (page.NativeControlsAdded)
Expand Down Expand Up @@ -144,13 +142,13 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
[Export("NavigateToTest")]
public bool NavigateToTest(string test)
{
return _app.NavigateToTestPage(test);
return App.NavigateToTestPage(test);
}

[Export("Reset")]
public void Reset()
{
_app.Reset();
App.Reset();
}

void SetUpForceRestartTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.Android
DataScheme = "http", DataHost = App.AppName, DataPathPrefix = "/gallery/"
)
]
public partial class Activity1 : FormsAppCompatActivity
public partial class Activity1 : MauiAppCompatActivity
{
App App => Microsoft.Maui.Controls.Application.Current as App;

protected override void OnCreate(Bundle bundle)
{
Profile.Start();

ToolbarResource = Resource.Layout.Toolbar;
TabLayoutResource = Resource.Layout.Tabbar;

// Uncomment the next line to run this as a full screen app (no status bar)
//Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);

Expand All @@ -52,17 +51,8 @@ protected override void OnCreate(Bundle bundle)
// At some point the Resources class types will go away so
// reflection will stop working
ResourceManager.Init(null);

Forms.Init(new MauiContext(MauiApplication.Current.Services, this));
FormsMaps.Init(this, bundle);

ViewHandler.ViewMapper
.Add(nameof(IView.AutomationId), (h, v) =>
{
if (h.NativeView is global::Android.Views.View nativeView)
nativeView.ContentDescription = v.AutomationId;
});

//FormsMaterial.Init(this, bundle);
AndroidAppLinks.Init(this);
Forms.ViewInitialized += (sender, e) =>
Expand All @@ -75,24 +65,14 @@ protected override void OnCreate(Bundle bundle)
// uncomment to verify turning off title bar works. This is not intended to be dynamic really.
//Forms.SetTitleBarVisibility (AndroidTitleBarVisibility.Never);

if (RestartAppTest.App != null)
{
_app = (App)RestartAppTest.App;
RestartAppTest.Reinit = true;
}
else
{
_app = new App();
}

// When the native control gallery loads up, it'll let us know so we can add the nested native controls
MessagingCenter.Subscribe<NestedNativeControlGalleryPage>(this, NestedNativeControlGalleryPage.ReadyForNativeControlsMessage, AddNativeControls);

// When the native binding gallery loads up, it'll let us know so we can set up the native bindings
MessagingCenter.Subscribe<NativeBindingGalleryPage>(this, NativeBindingGalleryPage.ReadyForNativeBindingsMessage, AddNativeBindings);

// Listen for the message from the status bar color toggle test
MessagingCenter.Subscribe<AndroidStatusBarColor>(this, AndroidStatusBarColor.Message, color => SetStatusBarColor(global::Android.Graphics.Color.Red));
// MessagingCenter.Subscribe<AndroidStatusBarColor>(this, AndroidStatusBarColor.Message, color => SetStatusBarColor(global::Android.Graphics.Color.Red));

SetUpForceRestartTest();

Expand All @@ -109,8 +89,6 @@ protected override void OnCreate(Bundle bundle)

DependencyService.Register<IMultiWindowService, MultiWindowService>();

LoadApplication(_app);

#if LEGACY_RENDERERS
if ((int)Build.VERSION.SdkInt >= 21)
{
Expand All @@ -122,7 +100,7 @@ protected override void OnCreate(Bundle bundle)

public void ReloadApplication()
{
LoadApplication(_app);
App.Windows[0].Page = App.CreateDefaultMainPage();
}

protected override void OnResume()
Expand Down
11 changes: 7 additions & 4 deletions src/Compatibility/ControlGallery/src/Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using Android.OS;
using Android.Runtime;
using Microsoft.Maui.Controls.DualScreen;
using Microsoft.Maui.Hosting;
using Plugin.CurrentActivity;

namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.Android
{
//You can specify additional application information in this attribute
[Application]
public class MainApplication : global::Android.App.Application, global::Android.App.Application.IActivityLifecycleCallbacks
{
[Application]
public class MainApplication : MauiApplication, global::Android.App.Application.IActivityLifecycleCallbacks
{
internal static Context ActivityContext { get; private set; }

public MainApplication(IntPtr handle, JniHandleOwnership transer)
Expand Down Expand Up @@ -69,5 +70,7 @@ public void OnActivityStopped(Activity activity)
{
ActivityContext = activity;
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using AndroidX.Fragment.App;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility.ControlGallery;
using Microsoft.Maui.Controls.Compatibility.ControlGallery.Android;
using Microsoft.Maui.Platform;

[assembly: Dependency(typeof(RegistrarValidationService))]
namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.Android
Expand All @@ -28,8 +30,13 @@ public bool Validate(VisualElement element, out string message)
|| renderer.GetType().Name == "DefaultRenderer"
)
{
var sp = MauiApplication.Current.Services.GetRequiredService<IMauiHandlersFactory>();
renderer = sp.GetHandler(element.GetType());
var activity =
DependencyService.Resolve<global::Android.Content.Context>() as MauiAppCompatActivity;
var mc = activity.GetWindow().Handler.MauiContext;

renderer = (element as IElement).ToHandler(mc);
if (renderer is IElementHandler vh)
vh.DisconnectHandler();
}

if (renderer == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.DayNight.DarkActionBar">
<style name="MyTheme.Base" parent="Maui.MainTheme.NoActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
Expand Down Expand Up @@ -40,11 +40,11 @@
<!--<item name="android:buttonStyle">@style/red_button_style</item>-->
</style>

<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<style name="AppCompatDialogStyle" parent="Theme.MaterialComponents.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>

<style name="DayNightDialogStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert">
<style name="DayNightDialogStyle" parent="Theme.MaterialComponents.DayNight.Dialog.Alert">
<item name="colorAccent">#00FF21</item>
</style>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public static List<Page> GetAllPages()
{
var controls = new List<Page>
{
new FlyoutPage { Flyout = new Page { Title = "Flyout" }, Detail = new Page() },
new NavigationPage(new Page()),
new Page(),
// TODO MAUI: These can come back when we have nested navigation
//new FlyoutPage { Flyout = new ContentPage { Title = "Flyout" }, Detail = new ContentPage() },
//new NavigationPage(new ContentPage()),
// TODO MAUI: These can come back with we get a defaultrenderer
//new FlyoutPage { Flyout = new Page { Title = "Flyout" }, Detail = new Page() },
//new Page(),
//new TemplatedPage(),
new ContentPage(),
new CarouselPage(),
new TabbedPage(),
new TemplatedPage(),
};

return controls;
Expand Down
11 changes: 10 additions & 1 deletion src/Compatibility/Core/src/Android/AppCompat/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ internal static IVisualElementRenderer CreateRenderer(
IViewHandler handler = null;

//TODO: Handle this with AppBuilderHost

if (Forms.MauiContext?.Handlers == null)
{
throw new InvalidOperationException("Forms.MauiContext.Handlers cannot be null here");
}

try
{
var mauiContext = Forms.MauiContext;
Expand All @@ -333,8 +339,11 @@ internal static IVisualElementRenderer CreateRenderer(
handler = mauiContext.Handlers.GetHandler(element.GetType()) as IViewHandler;
handler.SetMauiContext(mauiContext);
}
catch
catch (Exception e)
{
Microsoft.Extensions.Logging.LoggerExtensions
.LogWarning(Forms.MauiContext.CreateLogger<Platform>(), $"{e}");

// TODO define better catch response or define if this is needed?
}

Expand Down
5 changes: 5 additions & 0 deletions src/Compatibility/Core/src/RendererToHandlerShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ void OnElementChanged(object? sender, VisualElementChangedEventArgs e)

if (e.NewElement is IView newView)
{
var currentContext = newView.Handler?.MauiContext;
newView.Handler = this;
if (this.MauiContext == null && currentContext != null)
{
this.SetMauiContext(currentContext);
}

if (VirtualView != newView)
this.SetVirtualView(newView);
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.iOS")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.Windows")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.ControlGallery")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.ControlGallery.Android")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Xaml")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Xaml.UnitTests")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.HotReload.Forms")]
Expand Down

0 comments on commit b00586c

Please sign in to comment.