Add Forms.Init and static Ctors to Forms.Preload (#119)
author이비오/Common Platform Lab(SR)/Staff Engineer/삼성전자 <pius.lee@samsung.com>
Fri, 20 Dec 2019 06:50:21 +0000 (15:50 +0900)
committer부정균/Common Platform Lab(SR)/Staff Engineer/삼성전자 <jk.pu@samsung.com>
Fri, 20 Dec 2019 06:50:21 +0000 (15:50 +0900)
create new FormsApplication subclass for linked to lazy lifecycle.
now user can use Interface for lifecycle to implement application
lifecycle.

Xamarin.Forms.Platform.Tizen/AdaptedFormsApplication.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/ApplicationLifecycle.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/Forms.cs

diff --git a/Xamarin.Forms.Platform.Tizen/AdaptedFormsApplication.cs b/Xamarin.Forms.Platform.Tizen/AdaptedFormsApplication.cs
new file mode 100644 (file)
index 0000000..8df56ca
--- /dev/null
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using ElmSharp;
+using Tizen.Applications;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       class AdaptedFormsApplication : FormsApplication
+       {
+               public ApplicationLifecycle Lifecycle { get; set; }
+
+               protected override void OnPause()
+               {
+                       base.OnPause();
+                       Lifecycle?.SendPause();
+               }
+               protected override void OnPreCreate()
+               {
+                       if (MainWindow == null)
+                       {
+                               base.OnPreCreate();
+                       }
+                       Lifecycle?.SendPreCreate();
+               }
+               protected override void OnResume()
+               {
+                       base.OnResume();
+                       Lifecycle?.SendResume();
+               }
+               protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
+               {
+                       base.OnAppControlReceived(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnCreate()
+               {
+                       base.OnCreate();
+                       Lifecycle?.SendCreate();
+               }
+               protected override void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
+               {
+                       base.OnDeviceOrientationChanged(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnLocaleChanged(LocaleChangedEventArgs e)
+               {
+                       base.OnLocaleChanged(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnLowBattery(LowBatteryEventArgs e)
+               {
+                       base.OnLowBattery(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnLowMemory(LowMemoryEventArgs e)
+               {
+                       base.OnLowMemory(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
+               {
+                       base.OnRegionFormatChanged(e);
+                       Lifecycle?.Send(e);
+               }
+               protected override void OnTerminate()
+               {
+                       base.OnTerminate();
+                       Lifecycle?.SendTerminate();
+               }
+       }
+}
diff --git a/Xamarin.Forms.Platform.Tizen/ApplicationLifecycle.cs b/Xamarin.Forms.Platform.Tizen/ApplicationLifecycle.cs
new file mode 100644 (file)
index 0000000..eaf3143
--- /dev/null
@@ -0,0 +1,55 @@
+using Tizen.Applications;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       public abstract class ApplicationLifecycle
+       {
+               public FormsApplication FormsApplication { get; internal set; }
+
+               internal void SendPause() => OnPause();
+               internal void SendPreCreate() => OnPreCreate();
+               internal void SendResume() => OnResume();
+               internal void SendCreate() => OnCreate();
+               internal void SendTerminate() => OnTerminate();
+               internal void Send(AppControlReceivedEventArgs e) => OnAppControlReceived(e);
+               internal void Send(DeviceOrientationEventArgs e) => OnDeviceOrientationChanged(e);
+               internal void Send(LocaleChangedEventArgs e) => OnLocaleChanged(e);
+               internal void Send(LowBatteryEventArgs e) => OnLowBattery(e);
+               internal void Send(LowMemoryEventArgs e) => OnLowMemory(e);
+               internal void Send(RegionFormatChangedEventArgs e) => OnRegionFormatChanged(e);
+
+               protected virtual void OnPause()
+               {
+               }
+               protected virtual void OnPreCreate()
+               {
+               }
+               protected virtual void OnResume()
+               {
+               }
+               protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
+               {
+               }
+               protected virtual void OnCreate()
+               {
+               }
+               protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
+               {
+               }
+               protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
+               {
+               }
+               protected virtual void OnLowBattery(LowBatteryEventArgs e)
+               {
+               }
+               protected virtual void OnLowMemory(LowMemoryEventArgs e)
+               {
+               }
+               protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
+               {
+               }
+               protected virtual void OnTerminate()
+               {
+               }
+       }
+}
index 917f99d..0bf69e2 100644 (file)
@@ -142,13 +142,35 @@ namespace Xamarin.Forms
                                }
                        }
 
-                       public TizenDeviceInfo()
+                       public TizenDeviceInfo() : this(getScreenWidth(), getScreenHeight())
                        {
-                               int width = 0;
-                               int height = 0;
+                       }
 
-                               TSystemInfo.TryGetValue("http://tizen.org/feature/screen.width", out width);
-                               TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out height);
+                       static int getScreenWidth()
+                       {
+                               if (TSystemInfo.TryGetValue("http://tizen.org/feature/screen.width", out int width))
+                               {
+                                       return width;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException("Can not get screen.Width");
+                               }
+                       }
+                       static int getScreenHeight()
+                       {
+                               if (TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out int height))
+                               {
+                                       return height;
+                               }
+                               else
+                               {
+                                       throw new InvalidOperationException("Can not get screen.Height");
+                               }
+                       }
+                       TizenDeviceInfo(int width, int height)
+                       {
+                               pixelScreenSize = new Size(width, height);
 
                                scalingFactor = 1.0;  // scaling is disabled, we're using pixels as Xamarin's geometry units
                                if (s_useDeviceIndependentPixel)
@@ -156,10 +178,14 @@ namespace Xamarin.Forms
                                        scalingFactor = s_dpi.Value / 160.0;
                                }
 
-                               pixelScreenSize = new Size(width, height);
                                scaledScreenSize = new Size(width / scalingFactor, height / scalingFactor);
                                profile = s_profile.Value;
                        }
+
+                       public TizenDeviceInfo RefreshByDIP()
+                       {
+                               return new TizenDeviceInfo((int)pixelScreenSize.Width, (int)pixelScreenSize.Height);
+                       }
                }
 
                static bool s_useDeviceIndependentPixel = false;
@@ -316,6 +342,37 @@ namespace Xamarin.Forms
                        }
                }
 
+               public static void Init(ApplicationLifecycle lifecycle, bool useDeviceIndependentPixel, Dictionary<Type, Func<IRegisterable>> customHandlers = null)
+               {
+                       if (!IsInitialized)
+                       {
+                               Init();
+                       }
+
+                       s_useDeviceIndependentPixel = useDeviceIndependentPixel;
+
+                       if (Device.info != null)
+                       {
+                               var info = ((TizenDeviceInfo)Device.info).RefreshByDIP();
+                               ((TizenDeviceInfo)Device.info).Dispose();
+                               Device.info = info;
+                       }
+
+                       var appAdapter = Context as AdaptedFormsApplication;
+                       appAdapter.Lifecycle = lifecycle;
+                       lifecycle.FormsApplication = appAdapter;
+
+                       if (customHandlers != null)
+                               StaticRegistrar.RegisterHandlers(customHandlers);
+               }
+
+               static void Init()
+               {
+                       TizenPlatformServices.AppDomain.CurrentDomain.AddAssembly(typeof(View).Assembly);
+                       var appAdapter = new AdaptedFormsApplication();
+                       Init(appAdapter, false);
+               }
+
                public static void Init(CoreApplication application)
                {
                        Init(application, false);
@@ -583,6 +640,55 @@ namespace Xamarin.Forms
                        var window = new PreloadedWindow();
                        TSystemInfo.TryGetValue("http://tizen.org/feature/screen.width", out int width);
                        TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out int height);
+
+                       Forms.Init();
+
+                       var types = new[]
+                       {
+                               typeof(Application),
+                               typeof(ContentPage),
+                               typeof(NavigationPage),
+                               typeof(Shell),
+                               typeof(ContentPresenter),
+                               typeof(ContentView),
+                               typeof(ScrollView),
+                               typeof(Frame),
+                               typeof(TemplatedView),
+                               typeof(StackLayout),
+                               typeof(AbsoluteLayout),
+                               typeof(RelativeLayout),
+                               typeof(Grid),
+                               typeof(FlexLayout),
+                               typeof(Image),
+                               typeof(Label),
+                               typeof(BoxView),
+                               typeof(Button),
+                               typeof(ImageButton),
+                               typeof(CheckBox),
+                               typeof(Slider),
+                               typeof(Stepper),
+                               typeof(Switch),
+                               typeof(DatePicker),
+                               typeof(TimePicker),
+                               typeof(Entry),
+                               typeof(Editor),
+                               typeof(ActivityIndicator),
+                               typeof(ProgressBar),
+                               typeof(CarouselView),
+                               typeof(CollectionView),
+                               typeof(ListView),
+                               typeof(Picker),
+                               typeof(TableView),
+                               typeof(TextCell),
+                               typeof(ImageCell),
+                               typeof(SwitchCell),
+                               typeof(EntryCell)
+                       };
+
+                       foreach (var type in types)
+                       {
+                               System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);
+                       }
                }
        }