Skip to content

Commit

Permalink
Frontend.Maui: add LoadingPage
Browse files Browse the repository at this point in the history
Added LoadingPage.

Abstracted StartTimer function to work on both Xamarin.Forms
and Maui (Device.StartTimer is deprecated in Maui).

Add mappers for BorderColor and CornerRadius for Frame
elements so they can now have borders.

Use Grid instead of StackLayout so that Maui layout is fixed.

Co-authored-by: Parham <[email protected]>
  • Loading branch information
webwarrior-ws and parhamsaremi committed Dec 11, 2024
1 parent 14df1e7 commit 8fbade2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ fabric.properties
# Maui generated code
src/GWallet.Frontend.Maui/WelcomePage.xaml
src/GWallet.Frontend.Maui/WelcomePage2.xaml
src/GWallet.Frontend.Maui/LoadingPage.xaml
2 changes: 1 addition & 1 deletion scripts/make.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ let BuildSolutionOrProject
// TODO: we have to change this function to be the other way around (i.e. copy from Maui to XF) once we
// have a finished version of Maui and we consider XF as legacy.
let CopyXamlFiles() =
let files = [| "WelcomePage.xaml"; "WelcomePage2.xaml" |]
let files = [| "WelcomePage.xaml"; "WelcomePage2.xaml"; "LoadingPage.xaml" |]
for file in files do
let sourcePath = Path.Combine("src", "GWallet.Frontend.XF", file)
let destPath = Path.Combine("src", "GWallet.Frontend.Maui", file)
Expand Down
4 changes: 4 additions & 0 deletions src/GWallet.Frontend.Maui/GWallet.Frontend.Maui.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="LoadingPage.xaml" />
<EmbeddedResource Include="WelcomePage.xaml" />
<EmbeddedResource Include="WelcomePage2.xaml" />
<Compile Include="..\GWallet.Frontend.XF\FrontendHelpers.fs" />
<Compile Include="..\GWallet.Frontend.XF\GlobalState.fs" />
<Compile Include="..\GWallet.Frontend.XF\LoadingPage.xaml.fs">
<DependentUpon>LoadingPage.xaml</DependentUpon>
</Compile>
<Compile Include="..\GWallet.Frontend.XF\WelcomePage2.xaml.fs">
<DependentUpon>WelcomePage2.xaml</DependentUpon>
</Compile>
Expand Down
11 changes: 10 additions & 1 deletion src/GWallet.Frontend.XF/FrontendHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ module FrontendHelpers =
),
UseNativeScanning = true
)

#endif
let GetImageSource name =
let thisAssembly = typeof<BalanceState>.Assembly
let thisAssemblyName = thisAssembly.GetName().Name
Expand Down Expand Up @@ -463,5 +463,14 @@ module FrontendHelpers =
let imageSource = CreateCurrencyImageSource currency readOnly size
let currencyLogoImg = Image(Source = imageSource, IsVisible = true)
currencyLogoImg

let StartTimer(interval: TimeSpan, action: unit -> bool) =
#if XAMARIN
Device.StartTimer(interval, Func<bool> action)
#else
let timer = Application.Current.Dispatcher.CreateTimer(Interval = interval, IsRepeating = true)
timer.Tick.Add(fun _ ->
if not <| action() then timer.Stop() )
timer.Start()
#endif
let DefaultDesktopWindowSize = 500, 1000
6 changes: 0 additions & 6 deletions src/GWallet.Frontend.XF/Initialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ module Initialization =
let internal LandingPage(): NavigationPage =
GlobalInit ()

#if XAMARIN
let accounts = Account.GetAllActiveAccounts()
#endif
let landingPage:Page =
#if XAMARIN
if not (accounts.Any()) then
(WelcomePage GlobalState) :> Page
else
(LoadingPage (GlobalState, true)) :> Page
#else
(WelcomePage GlobalState) :> Page
#endif

let navPage = NavigationPage landingPage
NavigationPage.SetHasNavigationBar(landingPage, false)
Expand Down
39 changes: 34 additions & 5 deletions src/GWallet.Frontend.XF/LoadingPage.xaml.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
namespace GWallet.Frontend.XF
#if XAMARIN
namespace GWallet.Frontend.XF
#else
namespace GWallet.Frontend.Maui
#endif

open System
open System.Linq

#if !XAMARIN
open Microsoft.Maui
open Microsoft.Maui.Controls
open Microsoft.Maui.Controls.Xaml
open Microsoft.Maui.ApplicationModel
open Microsoft.Maui.Devices
#else
open Xamarin.Forms
open Xamarin.Forms.Xaml
open Xamarin.Essentials
#endif
open Fsdk

open GWallet.Backend
Expand All @@ -14,7 +26,11 @@ open GWallet.Backend
/// true if just the logo should be shown first, and title text and loading text after some seconds,
/// false if title text and loading text should be shown immediatly.
/// </param>
#if XAMARIN
type LoadingPage(state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as self =
#else
type LoadingPage(_state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as self =
#endif
inherit ContentPage()

let _ = base.LoadFromXaml(typeof<LoadingPage>)
Expand All @@ -26,6 +42,7 @@ type LoadingPage(state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as
let dotsMaxCount = 3
let loadingTextNoDots = loadingLabel.Text

#if XAMARIN
let allAccounts = Account.GetAllActiveAccounts()
let normalAccounts = allAccounts.OfType<NormalAccount>() |> List.ofSeq
|> List.map (fun account -> account :> IAccount)
Expand All @@ -50,8 +67,10 @@ type LoadingPage(state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as
}
let PreLoadCurrencyImages(): Map<Currency*bool,Image> =
GetAllImages() |> Map.ofSeq

let logoImageSource = FrontendHelpers.GetSizedImageSource "logo" 512
#endif

let logoImageSizeInPixels= 512
let logoImageSource = FrontendHelpers.GetSizedImageSource "logo" logoImageSizeInPixels
let logoImg = Image(Source = logoImageSource, IsVisible = true)

let mutable keepAnimationTimerActive = true
Expand Down Expand Up @@ -80,14 +99,15 @@ type LoadingPage(state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as
)

let dotsAnimationLength = TimeSpan.FromMilliseconds 500.
Device.StartTimer(dotsAnimationLength, Func<bool> UpdateDotsLabel)
FrontendHelpers.StartTimer(dotsAnimationLength, UpdateDotsLabel)
do
self.Init()

[<Obsolete(DummyPageConstructorHelper.Warning)>]
new() = LoadingPage(DummyPageConstructorHelper.GlobalFuncToRaiseExceptionIfUsedAtRuntime(),false)

member self.Transition(): unit =
#if XAMARIN
let currencyImages = PreLoadCurrencyImages()

let normalAccountsBalances = FrontendHelpers.CreateWidgetsForAccounts normalAccounts currencyImages false
Expand Down Expand Up @@ -116,19 +136,28 @@ type LoadingPage(state: FrontendHelpers.IGlobalAppState, showLogoFirst: bool) as
:> Page
FrontendHelpers.SwitchToNewPageDiscardingCurrentOne self balancesPage
}
#else
async {
()
}
#endif
|> FrontendHelpers.DoubleCheckCompletionAsync false

()

member self.Init (): unit =
if showLogoFirst then
MainThread.BeginInvokeOnMainThread(fun _ ->
#if !XAMARIN && GTK
logoImg.WidthRequest <- float(logoImageSizeInPixels) / 2.0
logoImg.HeightRequest <- float(logoImageSizeInPixels) / 2.0
#endif
mainLayout.Children.Add logoImg
)

self.Transition()

Device.StartTimer(TimeSpan.FromSeconds 5.0, fun _ ->
FrontendHelpers.StartTimer(TimeSpan.FromSeconds 5.0, fun _ ->
ShowLoadingText()

false // do not run timer again
Expand Down
9 changes: 0 additions & 9 deletions src/GWallet.Frontend.XF/WelcomePage2.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ open Xamarin.Essentials

open GWallet.Backend

#if !XAMARIN
// state and masterPrivateKeyGenerationTask are unused for now in MAUI until LoadingPage is ported
type WelcomePage2(_state: FrontendHelpers.IGlobalAppState, _masterPrivateKeyGenerationTask: Task<array<byte>>) =
#else
type WelcomePage2(state: FrontendHelpers.IGlobalAppState, masterPrivateKeyGenerationTask: Task<array<byte>>) =
#endif
inherit ContentPage()

let _ = base.LoadFromXaml(typeof<WelcomePage2>)
Expand Down Expand Up @@ -68,16 +63,12 @@ type WelcomePage2(state: FrontendHelpers.IGlobalAppState, masterPrivateKeyGenera
ToggleInputWidgetsEnabledOrDisabled false

async {
#if XAMARIN
let! privateKeyBytes = Async.AwaitTask masterPrivateKeyGenerationTask
do! Account.CreateAllAccounts privateKeyBytes password.Text
let loadingPage () =
LoadingPage (state, false)
:> Page
FrontendHelpers.SwitchToNewPageDiscardingCurrentOne self loadingPage
#else
()
#endif
} |> FrontendHelpers.DoubleCheckCompletionAsync false

member __.OnPasswordTextChanged(_sender: Object, _args: EventArgs) =
Expand Down

0 comments on commit 8fbade2

Please sign in to comment.