Ver código fonte

Created Platform-based Popup Manager, and implemented into MobilePage, MobileView and MobileTimeButton
Tweaked MobileButtonStrip and MobileGrid handling

Frank van den Bos 1 ano atrás
pai
commit
aabebdcfa3

+ 2 - 1
InABox.Client.RPC/Transports/Socket/RPCClientSocketTransport.cs

@@ -125,7 +125,8 @@ namespace InABox.Rpc
 
                 client.Opened += onOpen;
                 client.Closed += onClose;
-
+                client.EnableAutoSendPing = true;
+                client.AutoSendPingInterval = 10;
                 client.Error += Client_Error;
                 client.MessageReceived += Client_MessageReceived;
                 client.DataReceived += Client_DataReceived;

+ 1 - 0
InABox.Mobile/InABox.Mobile.Android/InABox.Mobile.Android.projitems

@@ -13,6 +13,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)ImageToolsDroid.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)MobileEditorRenderer.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)MobileEntryRenderer.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)PopupManager.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Version_Android.cs" />
   </ItemGroup>
 </Project>

+ 32 - 0
InABox.Mobile/InABox.Mobile.Android/PopupManager.cs

@@ -0,0 +1,32 @@
+using System;
+using InABox.Mobile.Shared;
+using Microsoft.Extensions.Logging;
+
+[assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.Android.PopupManager))]
+namespace InABox.Mobile.Android
+{
+    public class PopupManager : IPopupManager
+    {
+        
+        private static bool _initialised = false;
+        
+        public bool Initialise()
+        {
+            if (!_initialised)
+            {
+                try
+                {
+                    Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init();
+                    _initialised = true;
+                }
+                catch (Exception e)
+                {
+                    MobileLogging.Log(e);
+                    _initialised = false;
+                }
+            }
+            return _initialised;
+        }
+        
+    }
+}

+ 6 - 10
InABox.Mobile/InABox.Mobile.Shared/Components/MobileButtonStrip/MobileButtonStrip.xaml

@@ -8,21 +8,17 @@
     
     <ContentView.Content>
         
-        <CollectionView  
+        <StackLayout Orientation="Horizontal"  
             x:Name="_list"
             BackgroundColor="{Binding Source={RelativeSource Self}, Path=BackgroundColor}"
-            ItemSizingStrategy="MeasureAllItems"
-            SelectionMode="None"
             HeightRequest="40">
-            <CollectionView.ItemsLayout>
-                <LinearItemsLayout x:Name="_layout" ItemSpacing="5" Orientation="Horizontal" />
-            </CollectionView.ItemsLayout>
-            <CollectionView.ItemTemplate>
+            <BindableLayout.ItemTemplate>
                 <DataTemplate x:DataType="mobile:MobileButtonStripItem">
                     
-                    <mobile:MobileCard
+                    <mobile:MobileCard 
                         BorderColor="{Binding Source={RelativeSource AncestorType={x:Type mobile:MobileButtonStrip}}, Path=BorderColor}"
                         BackgroundColor="{Binding Source={RelativeSource AncestorType={x:Type mobile:MobileButtonStrip}}, Path=UnselectedBackgroundColor}">
+
                         <mobile:MobileCard.Triggers>
                             <DataTrigger TargetType="mobile:MobileCard" Binding="{Binding Selected}" Value="True">
                                 <Setter Property="BackgroundColor" Value="{Binding Source={RelativeSource AncestorType={x:Type mobile:MobileButtonStrip}}, Path=SelectedBackgroundColor}" />
@@ -50,8 +46,8 @@
                         </Label>
                     </mobile:MobileCard>
                 </DataTemplate>
-            </CollectionView.ItemTemplate>
-        </CollectionView>
+            </BindableLayout.ItemTemplate>
+        </StackLayout>
 
 
         

+ 5 - 3
InABox.Mobile/InABox.Mobile.Shared/Components/MobileButtonStrip/MobileButtonStrip.xaml.cs

@@ -96,8 +96,8 @@ namespace InABox.Mobile
 
         public double ItemSpacing
         {
-            get => _layout.ItemSpacing; 
-            set => _layout.ItemSpacing = value; 
+            get => _list.Spacing; 
+            set => _list.Spacing = value; 
         }
         
         private readonly BindableProperty FontSizeProperty = BindableProperty.Create(
@@ -148,7 +148,9 @@ namespace InABox.Mobile
             Items = items;
             
             InitializeComponent();
-            _list.ItemsSource = Items;
+            _list.BindingContext = Items;
+            _list.SetValue(BindableLayout.ItemsSourceProperty, Items);
+            //_list.ItemsSource = Items;
 
         }
 

+ 2 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileButtonStrip/MobileButtonStripItems.cs

@@ -1,5 +1,6 @@
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Collections.Specialized;
 
 namespace InABox.Mobile
 {
@@ -9,6 +10,7 @@ namespace InABox.Mobile
         {
             foreach (var item in items)
                 Add(new MobileButtonStripItem() { Text = item } );
+            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
         }
     }
 }

+ 2 - 2
InABox.Mobile/InABox.Mobile.Shared/Components/MobileGrid/MobileGrid.xaml.cs

@@ -139,8 +139,8 @@ namespace InABox.Mobile
             _lastupdate.IsVisible = !LastUpdated.IsEmpty();
             _lastupdate.Text = $"{DateTimeToAgeConverter.FormatTime(LastUpdated)}";
             _pulltorefresh.IsVisible = PullToRefresh;
-            _numrecords.Text = $"{(ItemsSource as IList)?.Count ?? 0} records";
-            _numrecords.IsVisible = ShowRecordCount && (ItemsSource is IList);
+            _numrecords.Text = $"{(ItemsSource as IEnumerable)?.ToList<object>().Count() ?? 0} records";
+            _numrecords.IsVisible = ShowRecordCount && (ItemsSource is IEnumerable);
             _refreshgrid.IsVisible = _lastupdate.IsVisible || _pulltorefresh.IsVisible || _numrecords.IsVisible;
         }
         

+ 6 - 4
InABox.Mobile/InABox.Mobile.Shared/Components/MobileListView/MobileListView.xaml.cs

@@ -163,10 +163,12 @@ namespace InABox.Mobile
         
         private void DoRefresh(object sender)
         {
-            _list.ItemsSource = null;
-            _list.IsRefreshing = true;
-            RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
-            _list.IsRefreshing = false;
+            if (PullToRefresh)
+            {
+                _list.IsRefreshing = true;
+                RefreshRequested?.Invoke(sender, new MobileListRefreshEventArgs());
+                _list.IsRefreshing = false;
+            }
         }
         
     }

+ 9 - 1
InABox.Mobile/InABox.Mobile.Shared/Components/MobilePage/MobilePage.xaml

@@ -8,7 +8,7 @@
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:xForms="clr-namespace:Syncfusion.SfBusyIndicator.XForms;assembly=Syncfusion.SfBusyIndicator.XForms"
     xmlns:mobile="clr-namespace:InABox.Mobile;assembly=InABox.Mobile.Shared"
-    x:Class="InABox.Mobile.MobilePage" >
+    x:Class="InABox.Mobile.MobilePage">
     
     <!-- NavigationPage.HasBackButton="false"     -->
     
@@ -18,6 +18,14 @@
        ColumnSpacing="0" 
        VerticalOptions="FillAndExpand" 
        HorizontalOptions="FillAndExpand">
+        <Grid.Margin>
+            <OnPlatform x:TypeArguments="Thickness">
+                <OnPlatform.Platforms>
+                    <On Platform="iOS" Value="0,-5,0,0" />
+                    <On Platform="Android" Value="0,0,0,0" />
+                </OnPlatform.Platforms>
+            </OnPlatform>
+        </Grid.Margin>
 
         <Grid.RowDefinitions>
             <RowDefinition>

+ 10 - 24
InABox.Mobile/InABox.Mobile.Shared/Components/MobilePage/MobilePage.xaml.cs

@@ -1,8 +1,10 @@
 using System;
 using System.Collections.Generic;
 using System.Threading.Tasks;
+using InABox.Mobile.Shared;
 using Syncfusion.XForms.Core;
 using Syncfusion.XForms.PopupLayout;
+using Xamarin.CommunityToolkit.UI.Views;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;
 using XF.Material.Forms.UI.Dialogs;
@@ -35,8 +37,8 @@ namespace InABox.Mobile
         }
 
         //public event BackButtonClickedEvent BackButtonClicked;
-        
-        private SfPopupLayout _popup = new SfPopupLayout();
+
+        private SfPopupLayout _popup = null;
         
         public MobilePage()
         {
@@ -136,28 +138,12 @@ namespace InABox.Mobile
             }
             return true;
         }
-
-
-
-        protected void ShowPopup(Func<View> view, int height = 500, int width = 300, int padding = 10)
-        {
-            _popup.PopupView.HeightRequest = height;
-            _popup.PopupView.WidthRequest = width;
-            _popup.PopupView.ShowHeader = false;
-            _popup.PopupView.ShowFooter = false;
-            _popup.PopupView.ContentTemplate = new DataTemplate(() =>
-            {
-                Grid grid = new Grid() { Margin = padding, Padding = padding};
-                grid.Children.Add(view());
-                return grid;
-            });
-            _popup.Show();
-        }
-
-        protected void DismissPopup()
-        {
-            _popup.Dismiss();
-        }
+        
+        protected void ShowPopup(Func<View> view, PopupManagerConfiguration config = null) 
+            => PopupManager.ShowPopup(this, view, config);
+        
+        protected void DismissPopup() 
+            => PopupManager.DismissPopup();
 
         public bool ProgressVisible
         {

+ 20 - 23
InABox.Mobile/InABox.Mobile.Shared/Components/MobileTimeSelector/MobileTimeButton.xaml.cs

@@ -58,8 +58,6 @@ namespace InABox.Mobile
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class MobileTimeButton
     {
-        private SfPopupLayout popup;
-        
         public event TimeButtonChanged Changed;
         
         public static readonly BindableProperty PromptProperty = BindableProperty.Create(
@@ -184,50 +182,49 @@ namespace InABox.Mobile
         public MobileTimeButton()
         {
             InitializeComponent();
-            popup = new SfPopupLayout();
         }
         
         private void _frame_OnClicked(object sender, EventArgs e)
         {
             
-            popup.PopupView.WidthRequest = 300;
-            popup.PopupView.HeightRequest = 500;
-            popup.PopupView.ShowHeader = false;
-            popup.PopupView.ShowFooter = false;
-            //popup.PopupView.PopupStyle.HasShadow = false;
-            popup.PopupView.PopupStyle.CornerRadius = 5;
-            //popup.PopupView.Background = new SolidColorBrush(Color.White);
             MobileTimeSelector popupContent = new MobileTimeSelector();
             popupContent.Time = Time;
             popupContent.Changed += (o, args) =>
             {
                 Time = args.Time;
                 DoChanged();
-                popup.Closing -= RestrictClose;
-                popup.Dismiss();
+                PopupManager.DismissPopup();
             };
             popupContent.Cancelled += (o, args) =>
             {
-                popup.Closing -= RestrictClose;
-                popup.Dismiss();
+                PopupManager.DismissPopup();
             };
-            popup.Closing += RestrictClose;
-            popupContent.Margin = new Thickness(10);
             popupContent.HorizontalOptions = LayoutOptions.Fill;
             popupContent.VerticalOptions = LayoutOptions.Fill;
-            popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
             
-            popup.Show();
+            PopupManager.ShowPopup(
+                this, 
+                () => popupContent, 
+                new PopupManagerConfiguration()
+                {
+                    Modal = true,
+                    RequestedHeight = 500,
+                    RequestedWidth = 350
+                }
+            );            
+            // popup.PopupView.WidthRequest = 300;
+            // popup.PopupView.HeightRequest = 500;
+            // popup.PopupView.ShowHeader = false;
+            // popup.PopupView.ShowFooter = false;
+            // popup.PopupView.PopupStyle.CornerRadius = 5;
+            // popup.Closing += RestrictClose;
+            // popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
+            // popup.Show();
         }
 
         protected virtual void DoChanged()
         {
             Changed?.Invoke(this,new TimeButtonChangedArgs(Time));
         }
-
-        private void RestrictClose(object sender, CancelEventArgs e)
-        {
-            e.Cancel = true;
-        }
     }
 }

+ 7 - 21
InABox.Mobile/InABox.Mobile.Shared/Components/MobileView/MobileView.cs

@@ -1,5 +1,6 @@
 using System;
 using Syncfusion.XForms.PopupLayout;
+using Xamarin.CommunityToolkit.Extensions;
 using Xamarin.Forms;
 
 namespace InABox.Mobile
@@ -60,27 +61,12 @@ namespace InABox.Mobile
             if (_loaded)
                 Changed?.Invoke(this, new MobileViewChangedEventArgs(property));
         }
-
-        private readonly SfPopupLayout _popup = new SfPopupLayout();
         
-        protected void ShowPopup(Func<View> view, int height = -1, int width = -1, int padding = 10)
-        {
-            _popup.PopupView.HeightRequest = height == -1 ? Application.Current.MainPage.Height * 0.8 : height;
-            _popup.PopupView.WidthRequest = width == -1 ? Application.Current.MainPage.Width * 0.8 : width;
-            _popup.PopupView.ShowHeader = false;
-            _popup.PopupView.ShowFooter = false;
-            _popup.PopupView.ContentTemplate = new DataTemplate(() =>
-            {
-                Grid grid = new Grid() { Margin = padding, Padding = padding};
-                grid.Children.Add(view());
-                return grid;
-            });
-            _popup.Show();
-        }
-
-        protected void DismissPopup()
-        {
-            _popup.Dismiss();
-        }
+        protected void ShowPopup(Func<View> view, PopupManagerConfiguration config = null) 
+            => PopupManager.ShowPopup(this, view, config);
+        
+        protected void DismissPopup() 
+            => PopupManager.DismissPopup();
+        
     }
 }

+ 89 - 0
InABox.Mobile/InABox.Mobile.Shared/PopupManager.cs

@@ -0,0 +1,89 @@
+using System;
+using Syncfusion.XForms.PopupLayout;
+using Xamarin.Forms;
+
+namespace InABox.Mobile
+{
+    public interface IPopupManager
+    {
+        bool Initialise();
+        
+    }
+
+    public class PopupManagerConfiguration
+    {
+        public int RequestedHeight { get; set; } = 700;
+        public int RequestedWidth { get; set; } = 350;
+        public bool ShowHeader { get; set; } = false;
+        public bool ShowFooter { get; set; } = false;
+        public int Padding { get; set; } = 10;
+
+        public int CornerRadius { get; set; } = 5;
+        public bool Modal { get; set; } = false;
+    }
+    
+    public static class PopupManager
+    {
+        
+        private static SfPopupLayout _popup = null;
+
+        
+        private static ContentPage FindParentPage(Element view)
+        {
+            if (view is ContentPage page)
+                return page;
+            return FindParentPage(view.Parent);
+        }
+        
+        public static void DisplayError(Element view, string message)
+        {
+            MobileLogging.Log("Unable to create Popup Layout!");
+            var page = FindParentPage(view);
+            Device.BeginInvokeOnMainThread(() =>
+            { 
+                page.DisplayAlert("Error", message, "OK");
+            }); 
+        }
+
+        public static void ShowPopup(Element parent, Func<View> view, PopupManagerConfiguration config = null)
+        {
+
+            if (_popup == null)
+            {
+                var service = DependencyService.Get<IPopupManager>();
+                if (service.Initialise())
+                    _popup = new SfPopupLayout();
+            }
+
+            if (_popup == null)
+            {
+                DisplayError(parent, "Unable to create Popup!\nPlease try again or restart the application.");
+                return;
+            }
+
+            var cfg = config ?? new PopupManagerConfiguration();
+            
+            _popup.PopupView.HeightRequest = cfg.RequestedHeight;
+            _popup.PopupView.WidthRequest = cfg.RequestedWidth;
+            _popup.PopupView.ShowHeader = cfg.ShowHeader;
+            _popup.PopupView.ShowFooter = cfg.ShowFooter;
+            _popup.PopupView.PopupStyle.CornerRadius = cfg.CornerRadius;
+            _popup.StaysOpen = cfg.Modal;
+            
+            _popup.PopupView.ContentTemplate = new DataTemplate(() =>
+            {
+                Grid grid = new Grid() { Padding = cfg.Padding };
+                grid.Children.Add(view());
+                return grid;
+            });
+            
+            _popup.Show();
+        }
+
+        public static void DismissPopup()
+        {
+            if (_popup != null)
+                _popup.Dismiss();
+        }
+    }
+}

+ 1 - 3
InABox.Mobile/InABox.Mobile.iOS/InABox.Mobile.iOS.projitems

@@ -15,9 +15,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)ImageToolsiOS.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)MobileEditorRenderer.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)MobileEntryRenderer.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)PopupManager.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Version_iOS.cs" />
   </ItemGroup>
-  <ItemGroup>
-    <Content Include="$(MSBuildThisFileDirectory)InABox.Mobile.iOS.Wrong.csproj" />
-  </ItemGroup>
 </Project>

+ 32 - 0
InABox.Mobile/InABox.Mobile.iOS/PopupManager.cs

@@ -0,0 +1,32 @@
+using System;
+using InABox.Mobile.Shared;
+using Microsoft.Extensions.Logging;
+
+[assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.iOS.PopupManager))]
+namespace InABox.Mobile.iOS
+{
+    public class PopupManager : IPopupManager
+    {
+
+        private static bool _initialised = false;
+        
+        public bool Initialise()
+        {
+            if (!_initialised)
+            {
+                try
+                {
+                    Syncfusion.XForms.iOS.PopupLayout.SfPopupLayoutRenderer.Init();
+                    _initialised = true;
+                }
+                catch (Exception e)
+                {
+                    MobileLogging.Log(e);
+                    _initialised = false;
+                }
+            }
+            return _initialised;
+        }
+        
+    }
+}