Skip to content

Commit

Permalink
Frontend.Maui: use XF XAML files for building proj
Browse files Browse the repository at this point in the history
This way we don't have to implement everything from the
beginning, and we can reuse existing code.

Remove "Install JDK 21" step as it's no longer needed.
  • Loading branch information
parhamsaremi authored and webwarrior-ws committed Jul 1, 2024
1 parent a37df96 commit 8f9c5be
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 103 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
uses: actions/setup-dotnet@v1.7.2
with:
dotnet-version: '6.0.113'
- name: install maui workload
run: dotnet workload install maui
- name: configure
run: ./configure.sh
- name: build in DEBUG mode
Expand Down Expand Up @@ -105,15 +107,6 @@ jobs:
uses: actions/setup-dotnet@v1.7.2
with:
dotnet-version: '6.0.x'
- name: Install JDK 21
# needed for MAUI Android build
run: |
Invoke-WebRequest -Uri https://aka.ms/download-jdk/microsoft-jdk-11-windows-x64.msi -OutFile jdk-11.msi -UseBasicParsing
Start-Process msiexec.exe -Wait -ArgumentList '/I jdk-11.msi /quiet'
- name: Build Maui Frontend
run: |
dotnet workload install maui
dotnet build src\GWallet.Frontend.Maui\GWallet.Frontend.Maui.fsproj --framework net6.0-android || dotnet build src\GWallet.Frontend.Maui\GWallet.Frontend.Maui.fsproj --framework net6.0-android
- name: configure
run: .\configure.bat
- name: build in DEBUG mode
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ fabric.properties
.fake
.ionide

# Maui generated code
src/GWallet.Frontend.Maui/WelcomePage.xaml
58 changes: 58 additions & 0 deletions scripts/make.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ let PASCALCASE_NAME = "GWallet"

let XF_FRONTEND_LIB = sprintf "%s.Frontend.XF" PASCALCASE_NAME
let GTK_FRONTEND_APP = sprintf "%s.Frontend.XF.Gtk" PASCALCASE_NAME
let MAUI_FRONTEND_APP = sprintf "%s.Frontend.Maui" PASCALCASE_NAME
let CONSOLE_FRONTEND_APP = sprintf "%s.Frontend.ConsoleApp" PASCALCASE_NAME
let BACKEND_LIB = sprintf "%s.Backend" PASCALCASE_NAME

type FrontendProject =
| XF
| Gtk
| Maui
member self.GetProjectFile(): FileInfo =
let projName =
match self with
| Gtk -> GTK_FRONTEND_APP
| XF -> XF_FRONTEND_LIB
| Maui -> MAUI_FRONTEND_APP

let prjFile =
let projFileName = sprintf "%s.fsproj" projName
Expand Down Expand Up @@ -201,6 +204,11 @@ let BuildSolutionOrProject
Seq.append ["LEGACY_FRAMEWORK"] defineConstantsFromBuildConfig
else
defineConstantsFromBuildConfig
let defineConstantsSoFar =
if not(file.FullName.EndsWith "maui.sln") then
Seq.append ["XAMARIN"] defineConstantsSoFar
else
defineConstantsSoFar
let allDefineConstants =
match maybeConstant with
| Some constant -> Seq.append [constant] defineConstantsSoFar
Expand Down Expand Up @@ -249,6 +257,55 @@ let BuildSolutionOrProject
Environment.Exit 1
| _ -> ()

// 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" |]
for file in files do
let sourcePath = Path.Combine("src", "GWallet.Frontend.XF", file)
let destPath = Path.Combine("src", "GWallet.Frontend.Maui", file)

File.Copy(sourcePath, destPath, true)
let fileText = File.ReadAllText(destPath)
File.WriteAllText(
destPath,
fileText
.Replace("http://xamarin.com/schemas/2014/forms","http://schemas.microsoft.com/dotnet/2021/maui")
.Replace("GWallet.Frontend.XF", "GWallet.Frontend.Maui")
)



let DotNetBuild
(solutionProjectFileName: string)
(binaryConfig: BinaryConfig)
(args: string)
(ignoreError: bool)
=
let configOption = sprintf "-c %s" (binaryConfig.ToString())
let buildArgs = (sprintf "build %s %s %s" configOption solutionProjectFileName args)
let buildProcess = Process.Execute ({ Command = "dotnet"; Arguments = buildArgs }, Echo.All)
match buildProcess.Result with
| Error _ ->
if not ignoreError then
Console.WriteLine()
Console.Error.WriteLine "dotnet build failed"
#if LEGACY_FRAMEWORK
PrintNugetVersion() |> ignore
#endif
Environment.Exit 1
else
()
| _ -> ()

// We have to build Maui project for android twice because the first time we get
// an error about Resource file not found. The second time it works.
// https://github.com/fabulous-dev/FSharp.Mobile.Templates/tree/55a1f3a0fd5cc397e48677ef4ff9241b360b0e84
let BuildMauiProject binaryConfig =
let mauiProjectFilePath = FrontendProject.Maui.GetProjectFile().FullName
DotNetBuild mauiProjectFilePath binaryConfig "--framework net6.0-android" true
DotNetBuild mauiProjectFilePath binaryConfig "--framework net6.0-android" false

let JustBuild binaryConfig maybeConstant: FrontendApp*FileInfo =
let maybeBuildTool = Map.tryFind "BuildTool" buildConfigContents
let maybeLegacyBuildTool = Map.tryFind "LegacyBuildTool" buildConfigContents
Expand Down Expand Up @@ -312,6 +369,7 @@ let JustBuild binaryConfig maybeConstant: FrontendApp*FileInfo =
// somehow, msbuild doesn't restore the frontend dependencies (e.g. Xamarin.Forms) when targetting
// the {LINUX|MAC}_SOLUTION_FILE below, so we need this workaround. TODO: just finish migrating to MAUI(dotnet restore)
NugetRestore solution
CopyXamlFiles()
MSBuildRestoreAndBuild solution

FrontendApp.Console
Expand Down
4 changes: 2 additions & 2 deletions src/GWallet.Frontend.Maui/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
ContentTemplate="{DataTemplate local:WelcomePage}"
Route="WelcomePage" />

</Shell>
8 changes: 5 additions & 3 deletions src/GWallet.Frontend.Maui/GWallet.Frontend.Maui.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="MainPage.xaml" />
<Compile Include="MainPage.xaml.fs">
<DependentUpon>MainPage.xaml</DependentUpon>
<EmbeddedResource Include="WelcomePage.xaml" />
<Compile Include="..\GWallet.Frontend.XF\FrontendHelpers.fs" />
<Compile Include="..\GWallet.Frontend.XF\GlobalState.fs" />
<Compile Include="..\GWallet.Frontend.XF\WelcomePage.xaml.fs">
<DependentUpon>WelcomePage.xaml</DependentUpon>
</Compile>
<EmbeddedResource Include="AppShell.xaml" />
<Compile Include="AppShell.xaml.fs">
Expand Down
67 changes: 0 additions & 67 deletions src/GWallet.Frontend.Maui/MainPage.xaml

This file was deleted.

11 changes: 0 additions & 11 deletions src/GWallet.Frontend.Maui/MainPage.xaml.fs

This file was deleted.

25 changes: 18 additions & 7 deletions src/GWallet.Frontend.XF/FrontendHelpers.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
namespace GWallet.Frontend.XF

#if !XAMARIN
namespace GWallet.Frontend.Maui
#else
namespace GWallet.Frontend.XF
#endif
open System
open System.Linq
open System.Threading.Tasks

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

open GWallet.Backend
open GWallet.Backend.FSharpUtil.UwpHacks

Expand Down Expand Up @@ -46,7 +53,7 @@ module FrontendHelpers =

type IAugmentablePayPage =
abstract member AddTransactionScanner: unit -> unit

#if XAMARIN
let IsDesktop() =
match Device.RuntimePlatform with
| Device.Android | Device.iOS ->
Expand Down Expand Up @@ -252,13 +259,17 @@ module FrontendHelpers =
let allCancelSources =
Seq.map fst sourcesAndJobs
allCancelSources,parallelJobs

#endif
let private MaybeCrash (canBeCanceled: bool) (ex: Exception) =
let LastResortBail() =
// this is just in case the raise(throw) doesn't really tear down the program:
Infrastructure.LogError ("FATAL PROBLEM: " + ex.ToString())
Infrastructure.LogError "MANUAL FORCED SHUTDOWN NOW"
#if XAMARIN
Device.PlatformServices.QuitApplication()
#else
Application.Current.Quit()
#endif

if isNull ex then
()
Expand Down Expand Up @@ -332,7 +343,7 @@ module FrontendHelpers =
|> Async.AwaitTask
return ()
}

#if XAMARIN
let ChangeTextAndChangeBack (button: Button) (newText: string) =
let initialText = button.Text
button.IsEnabled <- false
Expand Down Expand Up @@ -452,4 +463,4 @@ module FrontendHelpers =
let imageSource = CreateCurrencyImageSource currency readOnly size
let currencyLogoImg = Image(Source = imageSource, IsVisible = true)
currencyLogoImg

#endif
3 changes: 3 additions & 0 deletions src/GWallet.Frontend.XF/GWallet.Frontend.XF.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DefineConstants>XAMARIN</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EnableDefaultEmbeddedResourceItems>true</EnableDefaultEmbeddedResourceItems>
Expand Down
10 changes: 9 additions & 1 deletion src/GWallet.Frontend.XF/GlobalState.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
namespace GWallet.Frontend.XF
#if !XAMARIN
namespace GWallet.Frontend.Maui
#else
namespace GWallet.Frontend.XF
#endif

#if !XAMARIN
open Microsoft.Maui.Controls
#else
open Xamarin.Forms
#endif

type GlobalState() =

Expand Down
28 changes: 25 additions & 3 deletions src/GWallet.Frontend.XF/WelcomePage.xaml.fs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
namespace GWallet.Frontend.XF
#if !XAMARIN
namespace GWallet.Frontend.Maui
#else
namespace GWallet.Frontend.XF
#endif

open System
open System.Linq

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


open GWallet.Backend
open GWallet.Backend.FSharpUtil.UwpHacks

// state is unused in Maui for now because it's only used to pass it to
// WelcomePage2() ctor, which is not created in Maui app yet
#if !XAMARIN
type WelcomePage(_state: FrontendHelpers.IGlobalAppState) =
#else
type WelcomePage(state: FrontendHelpers.IGlobalAppState) =
#endif
inherit ContentPage()

let _ = base.LoadFromXaml(typeof<WelcomePage>)
Expand Down Expand Up @@ -81,6 +97,9 @@ type WelcomePage(state: FrontendHelpers.IGlobalAppState) =
else
None

// ToggleInputWidgetsEnabledOrDisabled is not used since its usage is in another
// #if block.
#if XAMARIN
let ToggleInputWidgetsEnabledOrDisabled (enabled: bool) =
let newCreateButtonCaption =
if enabled then
Expand All @@ -96,6 +115,7 @@ type WelcomePage(state: FrontendHelpers.IGlobalAppState) =
nextButton.IsEnabled <- enabled
nextButton.Text <- newCreateButtonCaption
)
#endif

do
welcomeLabel.Text <- SPrintF1 "Welcome to %s" Config.AppName
Expand Down Expand Up @@ -125,6 +145,7 @@ type WelcomePage(state: FrontendHelpers.IGlobalAppState) =
let! mainThreadSynchContext =
Async.AwaitTask <| MainThread.GetMainThreadSynchronizationContextAsync()
do! Async.SwitchToContext mainThreadSynchContext
#if XAMARIN
let dateTime = dobDatePicker.Date
ToggleInputWidgetsEnabledOrDisabled false
do! Async.SwitchToThreadPool()
Expand All @@ -136,6 +157,7 @@ type WelcomePage(state: FrontendHelpers.IGlobalAppState) =
WelcomePage2 (state, masterPrivKeyTask)
:> Page
do! FrontendHelpers.SwitchToNewPageDiscardingCurrentOneAsync self welcomePage
#endif
}

if dobDatePicker.Date.Date = middleDateEighteenYearsAgo.Date then
Expand Down

0 comments on commit 8f9c5be

Please sign in to comment.