From f497cf1fb56cd9b4a2595bc1db8057499eb067e5 Mon Sep 17 00:00:00 2001 From: Rinse Cramer Date: Sat, 8 Jun 2024 19:21:06 +0200 Subject: [PATCH] Added property to view model --- .../MainPage.xaml | 8 +++- .../ViewModels/MainPage.cs | 18 +++++++- .../Plugin.Maui.Rotatable.csproj | 7 --- .../Rotatable.android.cs | 14 ++++++ src/Plugin.Maui.Rotatable/Rotatable.macios.cs | 14 ++++++ .../Rotatable.samsung.cs | 43 ------------------- .../Rotatable.windows.cs | 14 ++++++ 7 files changed, 65 insertions(+), 53 deletions(-) delete mode 100644 src/Plugin.Maui.Rotatable/Rotatable.samsung.cs diff --git a/samples/Plugin.Maui.Rotatable.Sample/MainPage.xaml b/samples/Plugin.Maui.Rotatable.Sample/MainPage.xaml index e275ba8..83e00d8 100644 --- a/samples/Plugin.Maui.Rotatable.Sample/MainPage.xaml +++ b/samples/Plugin.Maui.Rotatable.Sample/MainPage.xaml @@ -5,12 +5,16 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:c="clr-namespace:Plugin.Maui.Rotatable.Sample.Converters" xmlns:vm="clr-namespace:Plugin.Maui.Rotatable.Sample.ViewModels" - Title="Rotatable Plugin"> + Title="Rotatable Plugin" + x:DataType="{x:Type vm:MainPage}"> - + diff --git a/samples/Plugin.Maui.Rotatable.Sample/ViewModels/MainPage.cs b/samples/Plugin.Maui.Rotatable.Sample/ViewModels/MainPage.cs index d0ce7ce..3586dae 100644 --- a/samples/Plugin.Maui.Rotatable.Sample/ViewModels/MainPage.cs +++ b/samples/Plugin.Maui.Rotatable.Sample/ViewModels/MainPage.cs @@ -2,5 +2,21 @@ public class MainPage : RotatableImplementation { - + private bool _isAvailable = false; + public bool IsAvailable + { + get + { + return _isAvailable; + } + private set + { + SetProperty(ref _isAvailable, value); + } + } + + public MainPage() + { + IsAvailable = true; + } } \ No newline at end of file diff --git a/src/Plugin.Maui.Rotatable/Plugin.Maui.Rotatable.csproj b/src/Plugin.Maui.Rotatable/Plugin.Maui.Rotatable.csproj index a607516..e4bed17 100644 --- a/src/Plugin.Maui.Rotatable/Plugin.Maui.Rotatable.csproj +++ b/src/Plugin.Maui.Rotatable/Plugin.Maui.Rotatable.csproj @@ -67,13 +67,6 @@ - - - - - - - diff --git a/src/Plugin.Maui.Rotatable/Rotatable.android.cs b/src/Plugin.Maui.Rotatable/Rotatable.android.cs index 937803c..3aabd49 100644 --- a/src/Plugin.Maui.Rotatable/Rotatable.android.cs +++ b/src/Plugin.Maui.Rotatable/Rotatable.android.cs @@ -1,4 +1,6 @@ using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace Plugin.Maui.Rotatable; @@ -40,4 +42,16 @@ private void OnWindowInfoChanged(object? sender, DisplayInfoChangedEventArgs e) } public event PropertyChangedEventHandler? PropertyChanged; + + protected bool SetProperty([NotNullIfNotNull(nameof(newValue))] ref T field, T newValue, [CallerMemberName] string? propertyName = null) + { + if (EqualityComparer.Default.Equals(field, newValue)) + { + return false; + } + + field = newValue; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + return true; + } } \ No newline at end of file diff --git a/src/Plugin.Maui.Rotatable/Rotatable.macios.cs b/src/Plugin.Maui.Rotatable/Rotatable.macios.cs index 937803c..3aabd49 100644 --- a/src/Plugin.Maui.Rotatable/Rotatable.macios.cs +++ b/src/Plugin.Maui.Rotatable/Rotatable.macios.cs @@ -1,4 +1,6 @@ using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace Plugin.Maui.Rotatable; @@ -40,4 +42,16 @@ private void OnWindowInfoChanged(object? sender, DisplayInfoChangedEventArgs e) } public event PropertyChangedEventHandler? PropertyChanged; + + protected bool SetProperty([NotNullIfNotNull(nameof(newValue))] ref T field, T newValue, [CallerMemberName] string? propertyName = null) + { + if (EqualityComparer.Default.Equals(field, newValue)) + { + return false; + } + + field = newValue; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + return true; + } } \ No newline at end of file diff --git a/src/Plugin.Maui.Rotatable/Rotatable.samsung.cs b/src/Plugin.Maui.Rotatable/Rotatable.samsung.cs deleted file mode 100644 index 937803c..0000000 --- a/src/Plugin.Maui.Rotatable/Rotatable.samsung.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.ComponentModel; - -namespace Plugin.Maui.Rotatable; - -public partial class RotatableImplementation : IRotatable, INotifyPropertyChanged -{ - private bool _isPortrait = false; - /// - public bool IsPortrait - { - get - { - return _isPortrait; - } - private set - { - if (_isPortrait != value) - { - _isPortrait = value; - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsPortrait))); - } - } - } - - public RotatableImplementation() - { - if (DeviceInfo.Idiom == DeviceIdiom.Desktop) return; // TODO: support desktop form factor - DeviceDisplay.MainDisplayInfoChanged += OnWindowInfoChanged; - IsPortrait = DeviceDisplay.MainDisplayInfo.Orientation == DisplayOrientation.Portrait; - } - - ~RotatableImplementation() - { - DeviceDisplay.MainDisplayInfoChanged -= OnWindowInfoChanged; - } - - private void OnWindowInfoChanged(object? sender, DisplayInfoChangedEventArgs e) - { - IsPortrait = e.DisplayInfo.Orientation == DisplayOrientation.Portrait; - } - - public event PropertyChangedEventHandler? PropertyChanged; -} \ No newline at end of file diff --git a/src/Plugin.Maui.Rotatable/Rotatable.windows.cs b/src/Plugin.Maui.Rotatable/Rotatable.windows.cs index a17ad1d..aa058d3 100644 --- a/src/Plugin.Maui.Rotatable/Rotatable.windows.cs +++ b/src/Plugin.Maui.Rotatable/Rotatable.windows.cs @@ -1,4 +1,6 @@ using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace Plugin.Maui.Rotatable; @@ -66,4 +68,16 @@ private void OnWindowInfoChanged(object? sender, DisplayInfoChangedEventArgs e) } public event PropertyChangedEventHandler? PropertyChanged; + + protected bool SetProperty([NotNullIfNotNull(nameof(newValue))] ref T field, T newValue, [CallerMemberName] string? propertyName = null) + { + if (EqualityComparer.Default.Equals(field, newValue)) + { + return false; + } + + field = newValue; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + return true; + } } \ No newline at end of file