Forráskód Böngészése

Added MobileAccordion and various converters
MobileGrid Image Columns can now have image headers

Frank van den Bos 2 éve
szülő
commit
ceae242a72

+ 55 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileAccordion/MobileAccordion.xaml

@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
+             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+             xmlns:mobile="clr-namespace:InABox.Mobile;assembly=InABox.Mobile.Shared"
+             x:Class="InABox.Mobile.MobileAccordion">
+    <ContentView.Resources>
+        <mobile:EmptyConverter x:Key="EmptyConverter" Converting="EmptyConverter_OnConverting"/>
+    </ContentView.Resources>
+    <ContentView.Content>
+        <StackLayout
+            x:Name="_stack"
+            VerticalOptions="Fill"
+            BackgroundColor="Transparent"
+            Spacing="2"
+            >
+
+            <BindableLayout.ItemTemplate>
+                <DataTemplate x:DataType="mobile:MobileAccordionItem">
+                    <StackLayout 
+                        Orientation="Vertical"
+                        Spacing="2"
+                        BackgroundColor="Transparent"
+                        >
+                        
+                        <mobile:MobileButton
+                            MinimumHeightRequest="50"
+                            HeightRequest="50"
+                            Text="{Binding Text}"
+                            Tag="{Binding .}"
+                            Clicked="Header_Clicked"
+                            IsVisible="{Binding ButtonVisible}"/>
+                        
+                        <Frame
+                            CornerRadius="5"
+                            BorderColor="Gray"
+                            BackgroundColor="Transparent"
+                            Padding="0"
+                            Margin="2"
+                            HeightRequest="2000"
+                            VerticalOptions="Fill"
+                            HasShadow="False"
+                            IsVisible="{Binding Visible}"
+                            >
+                            <ScrollView>
+                                <ContentView Content="{Binding Content, Converter={StaticResource EmptyConverter}}" />
+                            </ScrollView>
+                        </Frame>
+                        
+                    </StackLayout>
+                </DataTemplate>
+            </BindableLayout.ItemTemplate>
+        </StackLayout>
+    </ContentView.Content>
+</ContentView>

+ 78 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileAccordion/MobileAccordion.xaml.cs

@@ -0,0 +1,78 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Xamarin.Forms;
+using Xamarin.Forms.Xaml;
+
+namespace InABox.Mobile
+{
+
+    public class MobileAccordionItem : BindableObject
+    {
+        
+        private static readonly BindableProperty TextProperty =
+            BindableProperty.Create(nameof(Text), typeof(String), typeof(MobileAccordionItem), "");
+        public String Text
+        {
+            get => GetValue(TextProperty) as String;
+            set => SetValue(TextProperty, value);
+        }
+        
+        private static readonly BindableProperty ContentProperty =
+            BindableProperty.Create(nameof(Content), typeof(View), typeof(MobileAccordionItem));
+        
+        public View Content
+        {
+            get => GetValue(ContentProperty) as View;
+            set => SetValue(ContentProperty, value);
+        }
+
+        private static readonly BindableProperty VisibleProperty =
+            BindableProperty.Create(nameof(Visible), typeof(bool), typeof(MobileAccordionItem), true);
+        
+        public bool Visible
+        {
+            get => (bool)GetValue(VisibleProperty);
+            set => SetValue(VisibleProperty, value);
+        }
+        
+        private static readonly BindableProperty ButtonVisibleProperty =
+            BindableProperty.Create(nameof(ButtonVisible), typeof(bool), typeof(MobileAccordionItem), true);
+        
+        public bool ButtonVisible
+        {
+            get => (bool)GetValue(ButtonVisibleProperty);
+            set => SetValue(ButtonVisibleProperty, value);
+        }
+        
+    }
+    
+    [XamlCompilation(XamlCompilationOptions.Compile)]
+    public partial class MobileAccordion 
+    {
+
+        public IList<MobileAccordionItem> Items { get; private set; }
+        
+        public MobileAccordion()
+        {
+            Items = new ObservableCollection<MobileAccordionItem>();
+            InitializeComponent();
+            BindableLayout.SetItemsSource(_stack, Items);
+        }
+
+        private void Header_Clicked(object sender, MobileButtonClickEventArgs args)
+        {
+            foreach (var item in Items)
+                item.Visible = (item == args.Tag);
+        }
+
+        private void EmptyConverter_OnConverting(object sender, EmptyConverterConvertingEventArgs args)
+        {
+
+        }
+    }
+}

+ 15 - 12
InABox.Mobile/InABox.Mobile.Shared/Components/MobileGrid/MobileGrid.xaml

@@ -4,7 +4,9 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:xForms="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
              xmlns:ui="clr-namespace:XF.Material.Forms.UI;assembly=XF.Material"
-             x:Class="InABox.Mobile.MobileGrid" Background="LightGray">
+             xmlns:mobile="clr-namespace:InABox.Mobile;assembly=InABox.Mobile.Shared"
+             x:Class="InABox.Mobile.MobileGrid" 
+             BackgroundColor="LightGray">
     <ContentView.Content>
         <Grid RowSpacing="0" Background="LightGray">
             <Grid.RowDefinitions>
@@ -12,15 +14,15 @@
                 <RowDefinition Height="*"/>
             </Grid.RowDefinitions>
 
-            <Grid x:Name="_searchGrid" Grid.Row="0" Background="Transparent" Padding="5">
-                <Frame CornerRadius="5" Background="White" Padding="0,0,0,2">
-                <SearchBar x:Name="Search"
-                           IsVisible="true"
-                           Placeholder="Search"
-                           TextChanged="Search_OnTextChanged" 
-                           FontSize="14" BackgroundColor="White"/>
-                </Frame>
-            </Grid>
+
+            <mobile:MobileSearchBar 
+                x:Name="Search"
+                Grid.Row="0"
+                IsVisible="true"
+                PlaceHolder="Search"
+                TextChanged="Search_OnTextChanged" 
+                BackgroundColor="Transparent"/>
+
             
             <xForms:SfDataGrid 
                 x:Name="Grid" 
@@ -31,7 +33,7 @@
                 SelectionChanged="Grid_OnSelectionChanged"
                 GridTapped="Grid_OnGridTapped"
                 AllowPullToRefresh="True"
-                Background="White">
+                BackgroundColor="White">
                 <xForms:SfDataGrid.GridStyle>
                     <xForms:DefaultStyle 
                         GridCellBorderWidth="0"
@@ -40,7 +42,8 @@
                         AlternatingRowColor="WhiteSmoke"
                         SelectionBackgroundColor="Purple"
                         SelectionForegroundColor="White"
-                        HeaderBackgroundColor="LightGray"/>
+                        HeaderBackgroundColor="LightGray"
+                        HeaderCellBorderWidth="0"/>
                 </xForms:SfDataGrid.GridStyle>
             </xForms:SfDataGrid>
             

+ 7 - 4
InABox.Mobile/InABox.Mobile.Shared/Components/MobileGrid/MobileGrid.xaml.cs

@@ -130,12 +130,13 @@ namespace InABox.Mobile
         {
             InitializeComponent();
             Columns = new MobileGridColumns(Grid);
+            Columns.Changed += (sender, args) => CheckSearchVisibility(); 
             SelectionMode = MobileGridSelectionMode.Single;
             CanSearch = true;
             Grid.PullToRefreshCommand = new PullToRefreshCommand(this);
         }
 
-        private void Search_OnTextChanged(object sender, TextChangedEventArgs e)
+        private void Search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
         {
             Grid.View.RefreshFilter();
         }
@@ -150,11 +151,12 @@ namespace InABox.Mobile
             var searchables = Grid.Columns.OfType<GridTextColumn>().ToArray();
             if (searchables.Any() && CanSearch)
             {
-                _searchGrid.IsVisible = true;
-                Grid.View.Filter = DoSearch;
+                Search.IsVisible = true;
+                if (Grid?.View != null)
+                    Grid.View.Filter = DoSearch;
             }
             else
-                _searchGrid.IsVisible = false;
+                Search.IsVisible = false;
         }
 
         private void Grid_OnSelectionChanged(object sender, GridSelectionChangedEventArgs e)
@@ -169,5 +171,6 @@ namespace InABox.Mobile
             var column = Columns[e.RowColumnIndex.ColumnIndex];
             column.Tapped?.Invoke(column, e.RowData);
         }
+        
     }
 }

+ 4 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileGrid/MobileGridColumns.cs

@@ -1,3 +1,4 @@
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using Syncfusion.SfDataGrid.XForms;
@@ -11,6 +12,8 @@ namespace InABox.Mobile
         private bool _updating;
 
         public IMobileGridColumn this[int index] => _columns[index];
+
+        public event EventHandler Changed;
         
         public MobileGridColumns(SfDataGrid grid)
         {
@@ -43,6 +46,7 @@ namespace InABox.Mobile
             
             _grid.Columns.Resume();
             _grid.RefreshColumns();
+            Changed?.Invoke(this, EventArgs.Empty);
 
         }
 

+ 9 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileGrid/MobileGridImageColumn.cs

@@ -17,9 +17,12 @@ namespace InABox.Mobile
         
         public double Margin { get; set; }
         
+        public ImageSource Header { get; set; }
+        
         public MobileGridImageColumn() : base()
         {
             Width = 40;
+            
         }
         
         public override GridColumn CreateColumn()
@@ -29,6 +32,12 @@ namespace InABox.Mobile
             result.Aspect = Aspect == MobileGridImageAspect.Fill
                 ? Xamarin.Forms.Aspect.AspectFill
                 : Xamarin.Forms.Aspect.AspectFit;
+            if (Header != null)
+                result.HeaderTemplate = new DataTemplate(() =>
+                {
+                    var image = new Image() { Source = Header };
+                    return image;
+                });
             return result;
         }
     }

+ 26 - 0
InABox.Mobile/InABox.Mobile.Shared/Converters/BoolToGridLengthConverter.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Globalization;
+using Xamarin.Forms;
+
+namespace InABox.Mobile
+{
+    public class BoolToGridLengthConverter : IValueConverter
+    {
+        public GridLength TrueValue { get; set; }
+        public GridLength FalseValue { get; set; }
+        
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if ((value is bool bvalue) && bvalue)
+                return TrueValue;
+            return FalseValue;
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if (value is GridLength length)
+                return length.Equals(TrueValue);
+            return false;
+        }
+    }
+}

+ 27 - 0
InABox.Mobile/InABox.Mobile.Shared/Converters/BoolToLayoutOptionsConverter.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Globalization;
+using Xamarin.Forms;
+
+namespace InABox.Mobile
+{
+    public class BoolToLayoutOptionsConverter : IValueConverter
+    {
+        public LayoutOptions TrueValue { get; set; }
+        public LayoutOptions FalseValue { get; set; }
+        
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            return TrueValue;
+            //if ((value is bool bvalue) && bvalue)
+            //    return TrueValue;
+            //return FalseValue;
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if (value is LayoutOptions length)
+                return length.Equals(TrueValue);
+            return false;
+        }
+    }
+}

+ 23 - 0
InABox.Mobile/InABox.Mobile.Shared/Converters/TimeSpanToStringConverter.cs

@@ -0,0 +1,23 @@
+using System;
+using System.Globalization;
+using Xamarin.Forms;
+
+namespace InABox.Mobile
+{
+    public class FormatConverter : IValueConverter
+    {
+        public String Format { get; set; }
+
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            return String.IsNullOrWhiteSpace(Format)
+                ? value
+                : String.Format($"{{0:{Format}}}", value);
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}