Skip to content

Commit a8bc181

Browse files
parhamsaremiwebwarrior-ws
authored andcommitted
Frontend.Maui: use XF XAML files for building proj
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.
1 parent 3641520 commit a8bc181

File tree

11 files changed

+125
-104
lines changed

11 files changed

+125
-104
lines changed

.github/workflows/CI.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup .NET SDK 6.0.x
2929
uses: actions/[email protected]
3030
with:
31-
dotnet-version: '6.0.113'
31+
dotnet-version: '6.0.425'
3232
- name: Install specific Xamarin.iOS and Xamarin.Android versions
3333
run: |
3434
wget https://download.visualstudio.microsoft.com/download/pr/81c41aaa-a3d7-4875-8416-d04b472379b7/21d9f6c5ad3a6bc2479b2ec4b0685b6c/xamarin.ios-16.0.0.72.pkg
@@ -40,6 +40,8 @@ jobs:
4040
mono --version
4141
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mtouch --version
4242
ls -l /Library/Frameworks/Xamarin.Android.framework/Versions/
43+
- name: install maui workload
44+
run: dotnet workload install maui
4345
- name: configure
4446
run: ./configure.sh
4547
- name: build in DEBUG mode
@@ -140,15 +142,6 @@ jobs:
140142
uses: actions/[email protected]
141143
with:
142144
dotnet-version: '6.0.x'
143-
- name: Install JDK 21
144-
# needed for MAUI Android build
145-
run: |
146-
Invoke-WebRequest -Uri https://aka.ms/download-jdk/microsoft-jdk-11-windows-x64.msi -OutFile jdk-11.msi -UseBasicParsing
147-
Start-Process msiexec.exe -Wait -ArgumentList '/I jdk-11.msi /quiet'
148-
- name: Build Maui Frontend
149-
run: |
150-
dotnet workload install maui
151-
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
152145
- name: configure
153146
run: .\configure.bat
154147
- name: build in DEBUG mode

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ fabric.properties
9797
.fake
9898
.ionide
9999

100+
# Maui generated code
101+
src/GWallet.Frontend.Maui/WelcomePage.xaml

scripts/make.fsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ let PASCALCASE_NAME = "GWallet"
2727

2828
let XF_FRONTEND_LIB = sprintf "%s.Frontend.XF" PASCALCASE_NAME
2929
let GTK_FRONTEND_APP = sprintf "%s.Frontend.XF.Gtk" PASCALCASE_NAME
30+
let MAUI_FRONTEND_APP = sprintf "%s.Frontend.Maui" PASCALCASE_NAME
3031
let CONSOLE_FRONTEND_APP = sprintf "%s.Frontend.ConsoleApp" PASCALCASE_NAME
3132
let BACKEND_LIB = sprintf "%s.Backend" PASCALCASE_NAME
3233

3334
type FrontendProject =
3435
| XF
3536
| Gtk
37+
| Maui
3638
member self.GetProjectFile(): FileInfo =
3739
let projName =
3840
match self with
3941
| Gtk -> GTK_FRONTEND_APP
4042
| XF -> XF_FRONTEND_LIB
43+
| Maui -> MAUI_FRONTEND_APP
4144

4245
let prjFile =
4346
let projFileName = sprintf "%s.fsproj" projName
@@ -201,6 +204,11 @@ let BuildSolutionOrProject
201204
Seq.append ["LEGACY_FRAMEWORK"] defineConstantsFromBuildConfig
202205
else
203206
defineConstantsFromBuildConfig
207+
let defineConstantsSoFar =
208+
if not(file.FullName.EndsWith "maui.sln") then
209+
Seq.append ["XAMARIN"] defineConstantsSoFar
210+
else
211+
defineConstantsSoFar
204212
let allDefineConstants =
205213
match maybeConstant with
206214
| Some constant -> Seq.append [constant] defineConstantsSoFar
@@ -249,6 +257,55 @@ let BuildSolutionOrProject
249257
Environment.Exit 1
250258
| _ -> ()
251259

260+
// TODO: we have to change this function to be the other way around (i.e. copy from Maui to XF) once we
261+
// have a finished version of Maui and we consider XF as legacy.
262+
let CopyXamlFiles() =
263+
let files = [| "WelcomePage.xaml" |]
264+
for file in files do
265+
let sourcePath = Path.Combine("src", "GWallet.Frontend.XF", file)
266+
let destPath = Path.Combine("src", "GWallet.Frontend.Maui", file)
267+
268+
File.Copy(sourcePath, destPath, true)
269+
let fileText = File.ReadAllText(destPath)
270+
File.WriteAllText(
271+
destPath,
272+
fileText
273+
.Replace("http://xamarin.com/schemas/2014/forms","http://schemas.microsoft.com/dotnet/2021/maui")
274+
.Replace("GWallet.Frontend.XF", "GWallet.Frontend.Maui")
275+
)
276+
277+
278+
279+
let DotNetBuild
280+
(solutionProjectFileName: string)
281+
(binaryConfig: BinaryConfig)
282+
(args: string)
283+
(ignoreError: bool)
284+
=
285+
let configOption = sprintf "-c %s" (binaryConfig.ToString())
286+
let buildArgs = (sprintf "build %s %s %s" configOption solutionProjectFileName args)
287+
let buildProcess = Process.Execute ({ Command = "dotnet"; Arguments = buildArgs }, Echo.All)
288+
match buildProcess.Result with
289+
| Error _ ->
290+
if not ignoreError then
291+
Console.WriteLine()
292+
Console.Error.WriteLine "dotnet build failed"
293+
#if LEGACY_FRAMEWORK
294+
PrintNugetVersion() |> ignore
295+
#endif
296+
Environment.Exit 1
297+
else
298+
()
299+
| _ -> ()
300+
301+
// We have to build Maui project for android twice because the first time we get
302+
// an error about Resource file not found. The second time it works.
303+
// https://github.com/fabulous-dev/FSharp.Mobile.Templates/tree/55a1f3a0fd5cc397e48677ef4ff9241b360b0e84
304+
let BuildMauiProject binaryConfig =
305+
let mauiProjectFilePath = FrontendProject.Maui.GetProjectFile().FullName
306+
DotNetBuild mauiProjectFilePath binaryConfig "--framework net6.0-android" true
307+
DotNetBuild mauiProjectFilePath binaryConfig "--framework net6.0-android" false
308+
252309
let JustBuild binaryConfig maybeConstant: FrontendApp*FileInfo =
253310
let maybeBuildTool = Map.tryFind "BuildTool" buildConfigContents
254311
let maybeLegacyBuildTool = Map.tryFind "LegacyBuildTool" buildConfigContents
@@ -312,6 +369,7 @@ let JustBuild binaryConfig maybeConstant: FrontendApp*FileInfo =
312369
// somehow, msbuild doesn't restore the frontend dependencies (e.g. Xamarin.Forms) when targetting
313370
// the {LINUX|MAC}_SOLUTION_FILE below, so we need this workaround. TODO: just finish migrating to MAUI(dotnet restore)
314371
NugetRestore solution
372+
CopyXamlFiles()
315373
MSBuildRestoreAndBuild solution
316374

317375
FrontendApp.Console

src/GWallet.Frontend.Maui/AppShell.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ShellContent
1010
Title="Home"
11-
ContentTemplate="{DataTemplate local:MainPage}"
12-
Route="MainPage" />
11+
ContentTemplate="{DataTemplate local:WelcomePage}"
12+
Route="WelcomePage" />
1313

1414
</Shell>

src/GWallet.Frontend.Maui/GWallet.Frontend.Maui.fsproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
</PropertyGroup>
4141

4242
<ItemGroup>
43-
<EmbeddedResource Include="MainPage.xaml" />
44-
<Compile Include="MainPage.xaml.fs">
45-
<DependentUpon>MainPage.xaml</DependentUpon>
43+
<EmbeddedResource Include="WelcomePage.xaml" />
44+
<Compile Include="..\GWallet.Frontend.XF\FrontendHelpers.fs" />
45+
<Compile Include="..\GWallet.Frontend.XF\GlobalState.fs" />
46+
<Compile Include="..\GWallet.Frontend.XF\WelcomePage.xaml.fs">
47+
<DependentUpon>WelcomePage.xaml</DependentUpon>
4648
</Compile>
4749
<EmbeddedResource Include="AppShell.xaml" />
4850
<Compile Include="AppShell.xaml.fs">

src/GWallet.Frontend.Maui/MainPage.xaml

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/GWallet.Frontend.Maui/MainPage.xaml.fs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/GWallet.Frontend.XF/FrontendHelpers.fs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
namespace GWallet.Frontend.XF
2-
1+
#if !XAMARIN
2+
namespace GWallet.Frontend.Maui
3+
#else
4+
namespace GWallet.Frontend.XF
5+
#endif
36
open System
47
open System.Linq
58
open System.Threading.Tasks
69

10+
#if !XAMARIN
11+
open Microsoft.Maui.Controls
12+
open Microsoft.Maui.ApplicationModel
13+
#else
714
open Xamarin.Forms
815
open Xamarin.Essentials
916
open ZXing
1017
open ZXing.Mobile
18+
#endif
1119
open Fsdk
12-
1320
open GWallet.Backend
1421
open GWallet.Backend.FSharpUtil.UwpHacks
1522

@@ -46,7 +53,7 @@ module FrontendHelpers =
4653

4754
type IAugmentablePayPage =
4855
abstract member AddTransactionScanner: unit -> unit
49-
56+
#if XAMARIN
5057
let IsDesktop() =
5158
match Device.RuntimePlatform with
5259
| Device.Android | Device.iOS ->
@@ -252,13 +259,17 @@ module FrontendHelpers =
252259
let allCancelSources =
253260
Seq.map fst sourcesAndJobs
254261
allCancelSources,parallelJobs
255-
262+
#endif
256263
let private MaybeCrash (canBeCanceled: bool) (ex: Exception) =
257264
let LastResortBail() =
258265
// this is just in case the raise(throw) doesn't really tear down the program:
259266
Infrastructure.LogError ("FATAL PROBLEM: " + ex.ToString())
260267
Infrastructure.LogError "MANUAL FORCED SHUTDOWN NOW"
268+
#if XAMARIN
261269
Device.PlatformServices.QuitApplication()
270+
#else
271+
Application.Current.Quit()
272+
#endif
262273

263274
if isNull ex then
264275
()
@@ -332,7 +343,7 @@ module FrontendHelpers =
332343
|> Async.AwaitTask
333344
return ()
334345
}
335-
346+
#if XAMARIN
336347
let ChangeTextAndChangeBack (button: Button) (newText: string) =
337348
let initialText = button.Text
338349
button.IsEnabled <- false
@@ -452,4 +463,4 @@ module FrontendHelpers =
452463
let imageSource = CreateCurrencyImageSource currency readOnly size
453464
let currencyLogoImg = Image(Source = imageSource, IsVisible = true)
454465
currencyLogoImg
455-
466+
#endif

src/GWallet.Frontend.XF/GWallet.Frontend.XF.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<DefineConstants>XAMARIN</DefineConstants>
4+
</PropertyGroup>
25
<PropertyGroup>
36
<TargetFramework>netstandard2.0</TargetFramework>
47
<EnableDefaultEmbeddedResourceItems>true</EnableDefaultEmbeddedResourceItems>

src/GWallet.Frontend.XF/GlobalState.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
namespace GWallet.Frontend.XF
1+
#if !XAMARIN
2+
namespace GWallet.Frontend.Maui
3+
#else
4+
namespace GWallet.Frontend.XF
5+
#endif
26

7+
#if !XAMARIN
8+
open Microsoft.Maui.Controls
9+
#else
310
open Xamarin.Forms
11+
#endif
412

513
type GlobalState() =
614

0 commit comments

Comments
 (0)