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 Dec 12, 2024
1 parent 3641520 commit 6cff6cd
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 104 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ jobs:
- name: Setup .NET SDK 6.0.x
uses: actions/[email protected]
with:
dotnet-version: '6.0.113'
dotnet-version: '6.0.425'
- name: Install specific Xamarin.iOS and Xamarin.Android versions
run: |
wget https://download.visualstudio.microsoft.com/download/pr/81c41aaa-a3d7-4875-8416-d04b472379b7/21d9f6c5ad3a6bc2479b2ec4b0685b6c/xamarin.ios-16.0.0.72.pkg
sudo installer -pkg xamarin.ios-16.0.0.72.pkg -target /
wget https://aka.ms/xamarin-android-commercial-d17-3-macos --output-document=xamarin.android-13.0.0.0.pkg
sudo installer -pkg xamarin.android-13.0.0.0.pkg -target /
- name: Install Android 31 SDK
run: |
brew install --cask android-commandlinetools
echo y | sdkmanager 'build-tools;31.0.0' --sdk_root=/Users/runner/Library/Android/sdk
echo y | sdkmanager "platform-tools" "platforms;android-31" --sdk_root=/Users/runner/Library/Android/sdk
- name: Check versions
run: |
mono --version
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mtouch --version
ls -l /Library/Frameworks/Xamarin.Android.framework/Versions/
- name: install maui workload
run: dotnet workload install maui
- name: configure
run: ./configure.sh
- name: build in DEBUG mode
Expand Down Expand Up @@ -140,15 +147,6 @@ jobs:
uses: actions/[email protected]
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
Loading

0 comments on commit 6cff6cd

Please sign in to comment.