Skip to content

Commit

Permalink
Update Android Gallery to use Maui
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Jul 30, 2021
1 parent 9e5d97e commit 595a20f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 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 @@ -29,8 +29,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 @@ -139,13 +137,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,8 +51,6 @@ protected override void OnCreate(Bundle bundle)
// At some point the Resources class types will go away so
// reflection will stop working
ResourceManager.Init(null);

Microsoft.Maui.Controls.Compatibility.Forms.Init(this, bundle);
FormsMaps.Init(this, bundle);

#if ENABLE_TEST_CLOUD
Expand All @@ -75,24 +72,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 @@ -107,8 +94,6 @@ protected override void OnCreate(Bundle bundle)
});

DependencyService.Register<IMultiWindowService, MultiWindowService>();

LoadApplication(_app);

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

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

protected override void OnResume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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
public class MainApplication : MauiApplication, global::Android.App.Application.IActivityLifecycleCallbacks
{
internal static Context ActivityContext { get; private set; }

Expand Down Expand Up @@ -69,5 +69,8 @@ public void OnActivityStopped(Activity activity)
{
ActivityContext = activity;
}
}

protected override IStartup OnCreateStartup() =>
new Startup();
}
}
9 changes: 8 additions & 1 deletion src/Compatibility/Core/src/Android/AppCompat/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,20 @@ internal static IVisualElementRenderer CreateRenderer(VisualElement element, Con
IViewHandler handler = null;

//TODO: Handle this with AppBuilderHost

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

try
{
handler = Forms.MauiContext.Handlers.GetHandler(element.GetType()) as IViewHandler;
handler.SetMauiContext(Forms.MauiContext);
}
catch
catch (Exception e)
{
Internals.Log.Warning(nameof(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
7 changes: 2 additions & 5 deletions src/Core/src/Handlers/View/ViewHandlerOfT.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ public override void NativeArrange(Rectangle frame)
if (nativeView == null)
return;

if (frame.Width < 0 || frame.Height < 0)
if (MauiContext == null || frame.Width < 0 || frame.Height < 0)
{
// This is a legacy layout value from Controls, nothing is actually laying out yet so we just ignore it
// This is just legacy layout stuff from Controls jumping the gun; we aren't ready to actually lay things out yet
return;
}

if (Context == null)
return;

var left = Context.ToPixels(frame.Left);
var top = Context.ToPixels(frame.Top);
var bottom = Context.ToPixels(frame.Bottom);
Expand Down

0 comments on commit 595a20f

Please sign in to comment.