-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
ViewConverters.fs
1059 lines (923 loc) · 56.4 KB
/
ViewConverters.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 Fabulous contributors. See LICENSE.md for license.
namespace Fabulous.DynamicViews
open System
open System.Collections.Generic
open System.ComponentModel
open System.IO
open System.Windows.Input
open Xamarin.Forms
open Xamarin.Forms.StyleSheets
module ValueOption =
let inline map f x = match x with ValueNone -> ValueNone | ValueSome v -> ValueSome (f v)
/// Defines if the action should be animated or not
type AnimationKind =
| Animated
| NotAnimated
/// A custom data element for the ListView view element
[<AllowNullLiteral>]
type IListElement =
inherit INotifyPropertyChanged
abstract Key : ViewElement
[<AllowNullLiteral>]
type IItemListElement =
inherit INotifyPropertyChanged
abstract Key : ViewElement
/// A custom data element for the ListView view element
[<AllowNullLiteral>]
type ListElementData(key) =
let ev = new Event<_,_>()
let mutable data = key
interface IListElement with
member x.Key = data
[<CLIEvent>] member x.PropertyChanged = ev.Publish
member x.Key
with get() = data
and set(value) =
data <- value
ev.Trigger(x, PropertyChangedEventArgs "Key")
[<AllowNullLiteral>]
type ItemListElementData(key) =
let ev = new Event<_,_>()
let mutable data = key
interface IItemListElement with
member x.Key = data
[<CLIEvent>] member x.PropertyChanged = ev.Publish
member x.Key
with get() = data
and set(value) =
data <- value
ev.Trigger(x, PropertyChangedEventArgs "Key")
/// A custom data element for the GroupedListView view element
[<AllowNullLiteral>]
type ListGroupData(shortName: string, key, coll: ViewElement[]) =
inherit System.Collections.ObjectModel.ObservableCollection<ListElementData>(Seq.map ListElementData coll)
let ev = new Event<_,_>()
let mutable shortNameData = shortName
let mutable keyData = key
interface IListElement with
member x.Key = keyData
[<CLIEvent>] member x.PropertyChanged = ev.Publish
member x.Key
with get() = keyData
and set(value) =
keyData <- value
ev.Trigger(x, PropertyChangedEventArgs "Key")
member x.ShortName
with get() = shortName
and set(value) =
shortNameData <- value
ev.Trigger(x, PropertyChangedEventArgs "ShortName")
member __.Items = coll
/// A custom control for cells in the ListView view element
type ViewElementCell() =
inherit ViewCell()
let mutable listElementOpt : IListElement option = None
let mutable modelOpt : ViewElement option = None
let createView (newModel: ViewElement) =
match newModel.Create () with
| :? View as v -> v
| x -> failwithf "The cells of a ListView must each be some kind of 'View' and not a '%A'" (x.GetType())
member x.OnDataPropertyChanged = PropertyChangedEventHandler(fun _ args ->
match args.PropertyName, listElementOpt, modelOpt with
| "Key", Some curr, Some prevModel ->
curr.Key.UpdateIncremental (prevModel, x.View)
modelOpt <- Some curr.Key
| _ -> ()
)
override x.OnBindingContextChanged () =
base.OnBindingContextChanged ()
match x.BindingContext with
| :? IListElement as curr ->
let newModel = curr.Key
match listElementOpt with
| Some prev ->
prev.PropertyChanged.RemoveHandler x.OnDataPropertyChanged
curr.PropertyChanged.AddHandler x.OnDataPropertyChanged
newModel.UpdateIncremental (prev.Key, x.View)
| None ->
curr.PropertyChanged.AddHandler x.OnDataPropertyChanged
x.View <- createView newModel
listElementOpt <- Some curr
modelOpt <- Some curr.Key
| _ ->
match listElementOpt with
| Some prev ->
prev.PropertyChanged.RemoveHandler x.OnDataPropertyChanged
listElementOpt <- None
modelOpt <- None
| None -> ()
type ContentViewElement() =
inherit ContentView()
let mutable listElementOpt : IItemListElement option = None
let mutable modelOpt : ViewElement option = None
let createView (newModel: ViewElement) =
match newModel.Create () with
| :? View as v -> v
| x -> failwithf "The cells of a CollectionView must each be some kind of 'View' and not a '%A'" (x.GetType())
member x.OnDataPropertyChanged = PropertyChangedEventHandler(fun _ args ->
match args.PropertyName, listElementOpt, modelOpt with
| "Key", Some curr, Some prevModel ->
curr.Key.UpdateIncremental (prevModel, x.Content)
modelOpt <- Some curr.Key
| _ -> ()
)
override x.OnBindingContextChanged () =
base.OnBindingContextChanged ()
match x.BindingContext with
| :? IItemListElement as curr ->
let newModel = curr.Key
match listElementOpt with
| Some prev ->
prev.PropertyChanged.RemoveHandler x.OnDataPropertyChanged
curr.PropertyChanged.AddHandler x.OnDataPropertyChanged
newModel.UpdateIncremental (prev.Key, x.Content)
| None ->
curr.PropertyChanged.AddHandler x.OnDataPropertyChanged
x.Content <- createView newModel
listElementOpt <- Some curr
modelOpt <- Some curr.Key
| _ ->
match listElementOpt with
| Some prev ->
prev.PropertyChanged.RemoveHandler x.OnDataPropertyChanged
listElementOpt <- None
modelOpt <- None
| None -> ()
/// A custom control for the ListView view element
type CustomListView() =
inherit ListView(ItemTemplate=DataTemplate(typeof<ViewElementCell>))
type CustomCollectionListView() =
inherit CollectionView(ItemTemplate=DataTemplate(typeof<ContentViewElement>))
type CustomCarouselView() =
inherit CarouselView(ItemTemplate=DataTemplate(typeof<ContentViewElement>))
/// A custom control for the ListViewGrouped view element
type CustomGroupListView() =
inherit ListView(ItemTemplate=DataTemplate(typeof<ViewElementCell>), GroupHeaderTemplate=DataTemplate(typeof<ViewElementCell>), IsGroupingEnabled=true)
/// The underlying page type for the ContentPage view element
type CustomContentPage() as self =
inherit ContentPage()
do Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page.SetUseSafeArea(self, true)
let sizeAllocated: Event<double * double> = Event<_>()
member __.SizeAllocated = sizeAllocated.Publish
override __.OnSizeAllocated(width, height) =
base.OnSizeAllocated(width, height)
sizeAllocated.Trigger(width, height)
/// DataTemplate that can inflate a View from a ViewElement instead of a Type
type ViewElementDataTemplate(viewElement: ViewElement) as self =
inherit DataTemplate()
do (self :> Xamarin.Forms.Internals.IDataTemplate).LoadTemplate <- Func<obj>(viewElement.Create)
/// A custom SearchHandler which exposes the overridable methods OnQueryChanged, OnQueryConfirmed and OnItemSelected as events
type CustomSearchHandler() =
inherit SearchHandler(ItemTemplate=DataTemplate(typeof<ContentViewElement>))
member val QueryChanged = ignore with get, set
member val QueryConfirmed = ignore with get, set
member val ItemSelected: obj -> unit = ignore with get, set
override this.OnQueryChanged(oldValue, newValue) = this.QueryChanged (oldValue, newValue)
override this.OnQueryConfirmed() = this.QueryConfirmed ()
override this.OnItemSelected(item) = this.ItemSelected item
[<AutoOpen>]
module Converters =
open System.Collections.ObjectModel
/// Converts an F# function to a Xamarin.Forms ICommand
let makeCommand f =
let ev = Event<_,_>()
{ new ICommand with
member __.add_CanExecuteChanged h = ev.Publish.AddHandler h
member __.remove_CanExecuteChanged h = ev.Publish.RemoveHandler h
member __.CanExecute _ = true
member __.Execute _ = f() }
/// Converts an F# function to a Xamarin.Forms ICommand, with a CanExecute value
let makeCommandCanExecute f canExecute =
let ev = Event<_,_>()
{ new ICommand with
member __.add_CanExecuteChanged h = ev.Publish.AddHandler h
member __.remove_CanExecuteChanged h = ev.Publish.RemoveHandler h
member __.CanExecute _ = canExecute
member __.Execute _ = f() }
/// Converts a string, byte array or ImageSource to a Xamarin.Forms ImageSource
let makeImageSource (v: obj) =
match v with
| :? string as path -> ImageSource.op_Implicit path
| :? (byte[]) as bytes -> ImageSource.FromStream(fun () -> new MemoryStream(bytes) :> Stream)
| :? ImageSource as imageSource -> imageSource
| _ -> failwithf "makeImageSource: invalid argument %O" v
/// Converts a string to a Xamarin.Forms Accelerator
let makeAccelerator (accelerator: string) = Accelerator.op_Implicit accelerator
/// Converts a string to a Xamarin.Forms FileImageSource
let makeFileImageSource (image: string) = FileImageSource.op_Implicit image
/// Converts a double or Thickness to a Xamarin.Forms Thickness
let makeThickness (v: obj) =
match v with
| :? double as f -> Thickness.op_Implicit f
| :? Thickness as v -> v
| _ -> failwithf "makeThickness: invalid argument %O" v
/// Converts a string or collection of strings to a Xamarin.Forms StyleClass specification
let makeStyleClass (v:obj) =
match v with
| :? string as s -> [| s |]
| :? (string list) as s -> s |> Array.ofList
| :? (string[]) as s -> s
| :? (seq<string>) as s -> s |> Array.ofSeq
| _ -> failwithf "makeStyleClass: invalid argument %O" v
/// Converts a string, double or GridLength to a Xamarin.Forms GridLength
let makeGridLength (v: obj) =
match v with
| :? string as s when s = "*" -> GridLength.Star
| :? string as s when s.EndsWith "*" && fst (Double.TryParse(s.[0..s.Length-2])) ->
let sz = snd (Double.TryParse(s.[0..s.Length-2]))
GridLength(sz, GridUnitType.Star)
| :? string as s when s = "auto" -> GridLength.Auto
| :? double as f -> GridLength.op_Implicit f
| :? GridLength as v -> v
| _ -> failwithf "makeGridLength: invalid argument %O" v
/// Converts a string, int or double to a Xamarin.Forms font size
let makeFontSize (v: obj) =
match box v with
| :? string as s -> (FontSizeConverter().ConvertFromInvariantString(s) :?> double)
| :? int as i -> double i
| :? double as v -> v
| _ -> System.Convert.ToDouble(v)
/// Converts an F# function to an event handler for a page change
let makeCurrentPageChanged<'a when 'a :> Xamarin.Forms.Page and 'a : null> f =
System.EventHandler(fun sender _args ->
let control = sender :?> Xamarin.Forms.MultiPage<'a>
let index =
match control.CurrentPage with
| null -> None
| page -> Some (Xamarin.Forms.MultiPage<'a>.GetIndex(page))
f index
)
/// Converts a datatemplate to a Xamarin.Forms TemplatedPage
let makeTemplate (v: obj) =
match v with
| :? TemplatedPage as p -> ShellContent.op_Implicit p
| _ -> failwithf "makeTemplate: invalid argument %O" v
/// Converts a ViewElement to a View, or other types to string
let makeViewOrString (v: obj) : obj =
match v with
| null -> null
| :? View as view -> view :> obj
| :? ViewElement as viewElement -> viewElement.Create()
| :? string as str -> str :> obj
| _ -> v.ToString() :> obj
/// Checks whether two objects are reference-equal
let identical (x: 'T) (y:'T) = System.Object.ReferenceEquals(x, y)
/// Checks whether an underlying control can be reused given the previous and new view elements
let rec canReuseChild (prevChild:ViewElement) (newChild:ViewElement) =
if prevChild.TargetType = newChild.TargetType && canReuseAutomationId prevChild newChild then
if newChild.TargetType.IsAssignableFrom(typeof<NavigationPage>) then
canReuseNavigationPage prevChild newChild
else
true
else
false
/// Checks whether an underlying NavigationPage control can be reused given the previous and new view elements
//
// NavigationPage can be reused only if the pages don't change their type (added/removed pages don't prevent reuse)
// E.g. If the first page switch from ContentPage to TabbedPage, the NavigationPage can't be reused.
and internal canReuseNavigationPage (prevChild:ViewElement) (newChild:ViewElement) =
let prevPages = prevChild.TryGetAttribute<ViewElement[]>("Pages")
let newPages = newChild.TryGetAttribute<ViewElement[]>("Pages")
match prevPages, newPages with
| ValueSome prevPages, ValueSome newPages -> (prevPages, newPages) ||> Seq.forall2 canReuseChild
| _, _ -> true
/// Checks whether the control can be reused given the previous and the new AutomationId.
/// Xamarin.Forms can't change an already setted AutomationId
and internal canReuseAutomationId (prevChild: ViewElement) (newChild: ViewElement) =
let prevAutomationId = prevChild.TryGetAttribute<string>("AutomationId")
let newAutomationId = newChild.TryGetAttribute<string>("AutomationId")
match prevAutomationId with
| ValueSome _ when prevAutomationId <> newAutomationId -> false
| _ -> true
/// Update a control given the previous and new view elements
let inline updateChild (prevChild:ViewElement) (newChild:ViewElement) targetChild =
newChild.UpdateIncremental(prevChild, targetChild)
/// Convert a sequence to an array, maintaining the object identity of arrays
let seqToArray (itemsSource:seq<'T>) =
match itemsSource with
| :? ('T []) as arr -> arr
| es -> Array.ofSeq es
/// Convert a sequence to an IList, maintaining the object identity of any IList
let seqToIListUntyped (itemsSource:seq<'T>) =
match itemsSource with
| :? System.Collections.IList as arr -> arr
| es -> (Array.ofSeq es :> System.Collections.IList)
/// Incremental list maintenance: given a collection, and a previous version of that collection, perform
/// a reduced number of clear/add/remove/insert operations
let updateCollectionGeneric
(prevCollOpt: 'T[] voption)
(collOpt: 'T[] voption)
(targetColl: IList<'TargetT>)
(create: 'T -> 'TargetT)
(attach: 'T voption -> 'T -> 'TargetT -> unit) // adjust attached properties
(canReuse : 'T -> 'T -> bool) // Used to check if reuse is possible
(update: 'T -> 'T -> 'TargetT -> unit) // Incremental element-wise update, only if element reuse is allowed
=
match prevCollOpt, collOpt with
| ValueSome prevColl, ValueSome newColl when identical prevColl newColl -> ()
| _, ValueNone -> targetColl.Clear()
| _, ValueSome coll ->
if (coll = null || coll.Length = 0) then
targetColl.Clear()
else
// Remove the excess targetColl
while (targetColl.Count > coll.Length) do
targetColl.RemoveAt (targetColl.Count - 1)
// Count the existing targetColl
// Unused variable n' introduced as a temporary workaround for https://github.com/fsprojects/Fabulous/issues/343
let n' = targetColl.Count
let n = targetColl.Count
// Adjust the existing targetColl and create the new targetColl
for i in 0 .. coll.Length-1 do
let newChild = coll.[i]
let prevChildOpt = match prevCollOpt with ValueNone -> ValueNone | ValueSome coll when i < n -> ValueSome coll.[i] | _ -> ValueNone
let prevChildOpt, targetChild =
if (match prevChildOpt with ValueNone -> true | ValueSome prevChild -> not (identical prevChild newChild)) then
let mustCreate = (i >= n || match prevChildOpt with ValueNone -> true | ValueSome prevChild -> not (canReuse prevChild newChild))
if mustCreate then
let targetChild = create newChild
if i >= n then
targetColl.Insert(i, targetChild)
else
targetColl.[i] <- targetChild
ValueNone, targetChild
else
let targetChild = targetColl.[i]
update prevChildOpt.Value newChild targetChild
prevChildOpt, targetChild
else
prevChildOpt, targetColl.[i]
attach prevChildOpt newChild targetChild
// The public API for extensions to define their incremental update logic
type ViewElement with
/// Update an event handler on a target control, given a previous and current view element description
member inline source.UpdateEvent(prevOpt: ViewElement voption, attribKey: AttributeKey<'T>, targetEvent: IEvent<'T,'Args>) =
let prevValueOpt = match prevOpt with ValueNone -> ValueNone | ValueSome prev -> prev.TryGetAttributeKeyed<'T>(attribKey)
let valueOpt = source.TryGetAttributeKeyed<'T>(attribKey)
match prevValueOpt, valueOpt with
| ValueSome prevValue, ValueSome currValue when identical prevValue currValue -> ()
| ValueSome prevValue, ValueSome currValue -> targetEvent.RemoveHandler(prevValue); targetEvent.AddHandler(currValue)
| ValueNone, ValueSome currValue -> targetEvent.AddHandler(currValue)
| ValueSome prevValue, ValueNone -> targetEvent.RemoveHandler(prevValue)
| ValueNone, ValueNone -> ()
/// Update a primitive value on a target control, given a previous and current view element description
member inline source.UpdatePrimitive(prevOpt: ViewElement voption, target: 'Target, attribKey: AttributeKey<'T>, setter: 'Target -> 'T -> unit, ?defaultValue: 'T) =
let prevValueOpt = match prevOpt with ValueNone -> ValueNone | ValueSome prev -> prev.TryGetAttributeKeyed<'T>(attribKey)
let valueOpt = source.TryGetAttributeKeyed<'T>(attribKey)
match prevValueOpt, valueOpt with
| ValueSome prevValue, ValueSome newValue when prevValue = newValue -> ()
| _, ValueSome newValue -> setter target newValue
| ValueSome _, ValueNone -> setter target (defaultArg defaultValue Unchecked.defaultof<_>)
| ValueNone, ValueNone -> ()
/// Recursively update a nested view element on a target control, given a previous and current view element description
member inline source.UpdateElement(prevOpt: ViewElement voption, target: 'Target, attribKey: AttributeKey<ViewElement>, getter: 'Target -> 'T, setter: 'Target -> 'T -> unit) =
let prevValueOpt = match prevOpt with ValueNone -> ValueNone | ValueSome prev -> prev.TryGetAttributeKeyed<ViewElement>(attribKey)
let valueOpt = source.TryGetAttributeKeyed<ViewElement>(attribKey)
match prevValueOpt, valueOpt with
| ValueSome prevChild, ValueSome newChild when identical prevChild newChild -> ()
| ValueSome prevChild, ValueSome newChild when canReuseChild prevChild newChild ->
newChild.UpdateIncremental(prevChild, getter target)
| _, ValueSome newChild -> setter target (newChild.Create() :?> 'T)
| ValueSome _, ValueNone -> setter target null
| ValueNone, ValueNone -> ()
/// Recursively update a collection of nested view element on a target control, given a previous and current view element description
member inline source.UpdateElementCollection(prevOpt: ViewElement voption, attribKey: AttributeKey<seq<ViewElement>>, targetCollection: IList<'T>) =
let prevCollOpt = match prevOpt with ValueNone -> ValueNone | ValueSome prev -> prev.TryGetAttributeKeyed<_>(attribKey)
let collOpt = source.TryGetAttributeKeyed<_>(attribKey)
updateCollectionGeneric (ValueOption.map seqToArray prevCollOpt) (ValueOption.map seqToArray collOpt) targetCollection (fun x -> x.Create() :?> 'T) (fun _ _ _ -> ()) canReuseChild updateChild
/// Update the items in a ListView control, given previous and current view elements
let internal updateListViewItems (prevCollOpt: seq<'T> voption) (collOpt: seq<'T> voption) (target: Xamarin.Forms.ListView) =
let targetColl =
match target.ItemsSource with
| :? ObservableCollection<ListElementData> as oc -> oc
| _ ->
let oc = ObservableCollection<ListElementData>()
target.ItemsSource <- oc
oc
updateCollectionGeneric (ValueOption.map seqToArray prevCollOpt) (ValueOption.map seqToArray collOpt) targetColl ListElementData (fun _ _ _ -> ()) canReuseChild (fun _ curr target -> target.Key <- curr)
/// Update the items in a SearchHandler control, given previous and current view elements
let updateSearchHandlerItems (prevCollOpt: seq<'T> voption) (collOpt: seq<'T> voption) (target: Xamarin.Forms.SearchHandler) =
let targetColl = List<ItemListElementData>()
updateCollectionGeneric (ValueOption.map seqToArray prevCollOpt) (ValueOption.map seqToArray collOpt) targetColl ItemListElementData (fun _ _ _ -> ()) canReuseChild (fun _ curr target -> target.Key <- curr)
target.ItemsSource <- targetColl
/// Update the items in a CollectionView control, given previous and current view elements
let internal updateCollectionViewItems (prevCollOpt: seq<'T> voption) (collOpt: seq<'T> voption) (target: Xamarin.Forms.CollectionView) =
let targetColl =
match target.ItemsSource with
| :? ObservableCollection<ItemListElementData> as oc -> oc
| _ ->
let oc = ObservableCollection<ItemListElementData>()
target.ItemsSource <- oc
oc
updateCollectionGeneric (ValueOption.map seqToArray prevCollOpt) (ValueOption.map seqToArray collOpt) targetColl ItemListElementData (fun _ _ _ -> ()) canReuseChild (fun _ curr target -> target.Key <- curr)
/// Update the items in a CarouselView control, given previous and current view elements
let internal updateCarouselViewItems (prevCollOpt: seq<'T> voption) (collOpt: seq<'T> voption) (target: Xamarin.Forms.CarouselView) =
let targetColl =
match target.ItemsSource with
| :? ObservableCollection<ItemListElementData> as oc -> oc
| _ ->
let oc = ObservableCollection<ItemListElementData>()
target.ItemsSource <- oc
oc
updateCollectionGeneric (ValueOption.map seqToArray prevCollOpt) (ValueOption.map seqToArray collOpt) targetColl ItemListElementData (fun _ _ _ -> ()) canReuseChild (fun _ curr target -> target.Key <- curr)
let private updateListGroupData (_prevShortName: string, _prevKey, prevColl: ViewElement[]) (currShortName: string, currKey, currColl: ViewElement[]) (target: ListGroupData) =
target.ShortName <- currShortName
target.Key <- currKey
updateCollectionGeneric (ValueSome prevColl) (ValueSome currColl) target ListElementData (fun _ _ _ -> ()) canReuseChild (fun _ curr target -> target.Key <- curr)
/// Update the items in a GroupedListView control, given previous and current view elements
let internal updateListViewGroupedItems (prevCollOpt: (string * ViewElement * ViewElement[])[] voption) (collOpt: (string * ViewElement * ViewElement[])[] voption) (target: Xamarin.Forms.ListView) =
let targetColl =
match target.ItemsSource with
| :? ObservableCollection<ListGroupData> as oc -> oc
| _ ->
let oc = ObservableCollection<ListGroupData>()
target.ItemsSource <- oc
oc
updateCollectionGeneric prevCollOpt collOpt targetColl ListGroupData (fun _ _ _ -> ()) (fun (_, prevKey, _) (_, currKey, _) -> canReuseChild prevKey currKey) updateListGroupData
/// Update the ShowJumpList property of a GroupedListView control, given previous and current view elements
let internal updateListViewGroupedShowJumpList (prevOpt: bool voption) (currOpt: bool voption) (target: Xamarin.Forms.ListView) =
let updateTarget enableJumpList = target.GroupShortNameBinding <- (if enableJumpList then new Binding("ShortName") else null)
match (prevOpt, currOpt) with
| ValueNone, ValueSome curr -> updateTarget curr
| ValueSome prev, ValueSome curr when prev <> curr -> updateTarget curr
| ValueSome _, ValueNone -> target.GroupShortNameBinding <- null
| _, _ -> ()
/// Update the items of a TableView control, given previous and current view elements
let internal updateTableViewItems (prevCollOpt: (string * 'T[])[] voption) (collOpt: (string * 'T[])[] voption) (target: Xamarin.Forms.TableView) =
let create (desc: ViewElement) = (desc.Create() :?> Cell)
match prevCollOpt with
| ValueNone -> target.Root <- TableRoot()
| ValueSome _ -> ()
updateCollectionGeneric prevCollOpt collOpt target.Root
(fun (s, es) -> let section = TableSection(s) in section.Add(Seq.map create es); section)
(fun _ _ _ -> ()) // attach
(fun _ _ -> true) // canReuse
(fun (_prevTitle,prevChild) (newTitle, newChild) target ->
target.Title <- newTitle
updateCollectionGeneric (ValueSome prevChild) (ValueSome newChild) target create (fun _ _ _ -> ()) canReuseChild updateChild)
/// Update the resources of a control, given previous and current view elements describing the resources
let internal updateResources (prevCollOpt: (string * obj) list voption) (collOpt: (string * obj) list voption) (target: Xamarin.Forms.VisualElement) =
match prevCollOpt, collOpt with
| ValueNone, ValueNone -> ()
| ValueSome prevColl, ValueSome newColl when identical prevColl newColl -> ()
| _, ValueNone -> target.Resources.Clear()
| _, ValueSome coll ->
let targetColl = target.Resources
let coll = Array.ofSeq coll
if (coll = null || coll.Length = 0) then
targetColl.Clear()
else
for (key, newChild) in coll do
if targetColl.ContainsKey(key) then
let prevChildOpt =
match prevCollOpt with
| ValueNone -> ValueNone
| ValueSome prevColl ->
match prevColl |> List.tryFind(fun (prevKey, _) -> key = prevKey) with
| Some (_, prevChild) -> ValueSome prevChild
| None -> ValueNone
if (match prevChildOpt with ValueNone -> true | ValueSome prevChild -> not (identical prevChild newChild)) then
targetColl.Add(key, newChild)
else
targetColl.[key] <- newChild
else
targetColl.Remove(key) |> ignore
for (KeyValue(key, _newChild)) in targetColl do
if not (coll |> Array.exists(fun (key2, _v2) -> key = key2)) then
targetColl.Remove(key) |> ignore
/// Update the style sheets of a control, given previous and current view elements describing them
// Note, style sheets can't be removed
// Note, style sheets are compared by object identity
let internal updateStyleSheets (prevCollOpt: list<StyleSheet> voption) (collOpt: list<StyleSheet> voption) (target: Xamarin.Forms.VisualElement) =
match prevCollOpt, collOpt with
| ValueNone, ValueNone -> ()
| ValueSome prevColl, ValueSome newColl when identical prevColl newColl -> ()
| _, ValueNone -> target.Resources.Clear()
| _, ValueSome coll ->
let targetColl = target.Resources
let coll = Array.ofSeq coll
if (coll = null || coll.Length = 0) then
targetColl.Clear()
else
for styleSheet in coll do
let prevChildOpt =
match prevCollOpt with
| ValueNone -> None
| ValueSome prevColl -> prevColl |> List.tryFind(fun prevStyleSheet -> identical styleSheet prevStyleSheet)
match prevChildOpt with
| None -> targetColl.Add(styleSheet)
| Some _ -> ()
match prevCollOpt with
| ValueNone -> ()
| ValueSome prevColl ->
for prevStyleSheet in prevColl do
let childOpt =
match prevCollOpt with
| ValueNone -> None
| ValueSome prevColl -> prevColl |> List.tryFind(fun styleSheet -> identical styleSheet prevStyleSheet)
match childOpt with
| None ->
eprintfn "**** WARNING: style sheets may not be removed, and are compared by object identity, so should be created independently of your update or view functions ****"
| Some _ -> ()
/// Update the styles of a control, given previous and current view elements describing them
// Note, styles can't be removed
// Note, styles are compared by object identity
let internal updateStyles (prevCollOpt: Style list voption) (collOpt: Style list voption) (target: Xamarin.Forms.VisualElement) =
match prevCollOpt, collOpt with
| ValueNone, ValueNone -> ()
| ValueSome prevColl, ValueSome newColl when identical prevColl newColl -> ()
| _, ValueNone -> target.Resources.Clear()
| _, ValueSome coll ->
let targetColl = target.Resources
let coll = Array.ofSeq coll
if (coll = null || coll.Length = 0) then
targetColl.Clear()
else
for styleSheet in coll do
let prevChildOpt =
match prevCollOpt with
| ValueNone -> None
| ValueSome prevColl -> prevColl |> Seq.tryFind(fun prevStyleSheet -> identical styleSheet prevStyleSheet)
match prevChildOpt with
| None -> targetColl.Add(styleSheet)
| Some _ -> ()
match prevCollOpt with
| ValueNone -> ()
| ValueSome prevColl ->
for prevStyle in prevColl do
let childOpt =
match prevCollOpt with
| ValueNone -> None
| ValueSome prevColl -> prevColl |> Seq.tryFind(fun style-> identical style prevStyle)
match childOpt with
| None ->
eprintfn "**** WARNING: styles may not be removed, and are compared by object identity. They should be created independently of your update or view functions ****"
| Some _ -> ()
/// Update the style class of a control, given previous and current view elements
let internal updateStyleClass (prevCollOpt: IList<string> voption) (collOpt: IList<string> voption) (target: Xamarin.Forms.NavigableElement) =
match prevCollOpt, collOpt with
| ValueNone, ValueNone -> ()
| ValueSome prevColl, ValueSome newColl when prevColl = newColl -> ()
| _, ValueNone -> target.StyleClass <- null
| _, ValueSome coll -> target.StyleClass <- coll
/// Incremental NavigationPage maintenance: push/pop the right pages
let internal updateNavigationPages (prevCollOpt: ViewElement[] voption) (collOpt: ViewElement[] voption) (target: NavigationPage) attach =
match prevCollOpt, collOpt with
| ValueSome prevColl, ValueSome newColl when identical prevColl newColl -> ()
| _, ValueNone -> failwith "Error while updating NavigationPage pages: the pages collection should never be empty for a NavigationPage"
| _, ValueSome coll ->
let create (desc: ViewElement) = (desc.Create() :?> Page)
if (coll = null || coll.Length = 0) then
failwith "Error while updating NavigationPage pages: the pages collection should never be empty for a NavigationPage"
else
// Count the existing pages
let prevCount = target.Pages |> Seq.length
let newCount = coll.Length
printfn "Updating NavigationPage, prevCount = %d, newCount = %d" prevCount newCount
// Remove the excess pages
if newCount = 1 && prevCount > 1 then
printfn "Updating NavigationPage --> PopToRootAsync"
target.PopToRootAsync() |> ignore
elif prevCount > newCount then
for i in prevCount - 1 .. -1 .. newCount do
printfn "PopAsync, page number %d" i
target.PopAsync () |> ignore
let n = min prevCount newCount
// Push and/or adjust pages
for i in 0 .. newCount-1 do
let newChild = coll.[i]
let prevChildOpt = match prevCollOpt with ValueNone -> ValueNone | ValueSome coll when i < coll.Length && i < n -> ValueSome coll.[i] | _ -> ValueNone
let prevChildOpt, targetChild =
if (match prevChildOpt with ValueNone -> true | ValueSome prevChild -> not (identical prevChild newChild)) then
let mustCreate = (i >= n || match prevChildOpt with ValueNone -> true | ValueSome prevChild -> not (canReuseChild prevChild newChild))
if mustCreate then
//printfn "Creating child %d, prevChildOpt = %A, newChild = %A" i prevChildOpt newChild
let targetChild = create newChild
if i >= n then
printfn "PushAsync, page number %d" i
target.PushAsync(targetChild) |> ignore
else
failwith "Error while updating NavigationPage pages: can't change type of one of the pages in the navigation chain during navigation"
ValueNone, targetChild
else
printfn "Adjust page number %d" i
let targetChild = target.Pages |> Seq.item i
updateChild prevChildOpt.Value newChild targetChild
prevChildOpt, targetChild
else
//printfn "Skipping child %d" i
let targetChild = target.Pages |> Seq.item i
prevChildOpt, targetChild
attach prevChildOpt newChild targetChild
/// Update the OnSizeAllocated callback of a control, given previous and current values
let internal updateOnSizeAllocated prevValueOpt valueOpt (target: obj) =
let target = (target :?> CustomContentPage)
match prevValueOpt with ValueNone -> () | ValueSome f -> target.SizeAllocated.RemoveHandler(f)
match valueOpt with ValueNone -> () | ValueSome f -> target.SizeAllocated.AddHandler(f)
/// Update the Command and CanExecute properties of a control, given previous and current values
let inline internal updateCommand prevCommandValueOpt commandValueOpt argTransform setter prevCanExecuteValueOpt canExecuteValueOpt target =
match prevCommandValueOpt, prevCanExecuteValueOpt, commandValueOpt, canExecuteValueOpt with
| ValueNone, ValueNone, ValueNone, ValueNone -> ()
| ValueSome prevf, ValueNone, ValueSome f, ValueNone when identical prevf f -> ()
| ValueSome prevf, ValueSome prevx, ValueSome f, ValueSome x when identical prevf f && prevx = x -> ()
| _, _, ValueNone, _ -> setter target null
| _, _, ValueSome f, ValueNone -> setter target (makeCommand (fun () -> f (argTransform target)))
| _, _, ValueSome f, ValueSome k -> setter target (makeCommandCanExecute (fun () -> f (argTransform target)) k)
/// Update the CurrentPage of a control, given previous and current values
let internal updateCurrentPage<'a when 'a :> Xamarin.Forms.Page and 'a : null> prevValueOpt valueOpt (target: obj) =
let control = target :?> Xamarin.Forms.MultiPage<'a>
match prevValueOpt, valueOpt with
| ValueNone, ValueNone -> ()
| ValueSome prev, ValueSome curr when prev = curr -> ()
| ValueSome _, ValueNone -> control.CurrentPage <- null
| _, ValueSome curr -> control.CurrentPage <- control.Children.[curr]
/// Update the Minium and Maximum values of a slider, given previous and current values
let internal updateSliderMinimumMaximum prevValueOpt valueOpt (target: obj) =
let control = target :?> Xamarin.Forms.Slider
let defaultValue = (0.0, 1.0)
let updateFunc (prevMinimum, prevMaximum) (newMinimum, newMaximum) =
if newMinimum > prevMaximum then
control.Maximum <- newMaximum
control.Minimum <- newMinimum
else
control.Minimum <- newMinimum
control.Maximum <- newMaximum
match prevValueOpt, valueOpt with
| ValueNone, ValueNone -> ()
| ValueSome prev, ValueSome curr when prev = curr -> ()
| ValueSome prev, ValueSome curr -> updateFunc prev curr
| ValueSome prev, ValueNone -> updateFunc prev defaultValue
| ValueNone, ValueSome curr -> updateFunc defaultValue curr
/// Update the Minium and Maximum values of a stepper, given previous and current values
let internal updateStepperMinimumMaximum prevValueOpt valueOpt (target: obj) =
let control = target :?> Xamarin.Forms.Stepper
let defaultValue = (0.0, 1.0)
let updateFunc (prevMinimum, prevMaximum) (newMinimum, newMaximum) =
if newMinimum > prevMaximum then
control.Maximum <- newMaximum
control.Minimum <- newMinimum
else
control.Minimum <- newMinimum
control.Maximum <- newMaximum
match prevValueOpt, valueOpt with
| ValueNone, ValueNone -> ()
| ValueSome prev, ValueSome curr when prev = curr -> ()
| ValueSome prev, ValueSome curr -> updateFunc prev curr
| ValueSome prev, ValueNone -> updateFunc prev defaultValue
| ValueNone, ValueSome curr -> updateFunc defaultValue curr
/// Update the attached NavigationPage.TitleView property of a Page, given previous and current values
let internal updatePageTitleView (prevOpt: ViewElement voption) (currOpt: ViewElement voption) (target: Page) =
match prevOpt, currOpt with
| ValueSome prev, ValueSome curr when identical prev curr -> ()
| ValueSome prev, ValueSome curr when canReuseChild prev curr ->
updateChild prev curr (NavigationPage.GetTitleView(target))
| _, ValueSome curr ->
NavigationPage.SetTitleView(target, (curr.Create() :?> Xamarin.Forms.View))
| ValueSome _, ValueNone ->
NavigationPage.SetTitleView(target, null)
| _, _ -> ()
/// Update the AcceleratorProperty of a MenuItem, given previous and current Accelerator
let internal updateAccelerator prevValue currValue (target: Xamarin.Forms.MenuItem) =
match prevValue, currValue with
| ValueNone, ValueNone -> ()
| ValueSome prevVal, ValueSome newVal when prevVal = newVal -> ()
| _, ValueNone -> Xamarin.Forms.MenuItem.SetAccelerator(target, null)
| _, ValueSome newVal -> Xamarin.Forms.MenuItem.SetAccelerator(target, makeAccelerator newVal)
/// Update the items of a Shell, given previous and current view elements
let internal updateShellItems (prevCollOpt: ViewElement array voption) (collOpt: ViewElement array voption) (target: Xamarin.Forms.Shell) =
let create (desc: ViewElement) =
match desc.Create() with
| :? ShellContent as shellContent -> ShellItem.op_Implicit shellContent
| :? TemplatedPage as templatedPage -> ShellItem.op_Implicit templatedPage
| :? ShellSection as shellSection -> ShellItem.op_Implicit shellSection
| :? MenuItem as menuItem -> ShellItem.op_Implicit menuItem
| :? ShellItem as shellItem -> shellItem
| child -> failwithf "%s is not compatible with the type ShellItem" (child.GetType().Name)
let update prevViewElement (currViewElement: ViewElement) (target: ShellItem) =
let realTarget =
match currViewElement.TargetType with
| t when t = typeof<ShellContent> -> target.Items.[0].Items.[0] :> Element
| t when t = typeof<TemplatedPage> -> target.Items.[0].Items.[0] :> Element
| t when t = typeof<ShellSection> -> target.Items.[0] :> Element
| t when t = typeof<MenuItem> -> target.GetType().GetProperty("MenuItem").GetValue(target) :?> Element // MenuShellItem is marked as internal
| _ -> target :> Element
updateChild prevViewElement currViewElement realTarget
updateCollectionGeneric prevCollOpt collOpt target.Items create (fun _ _ _ -> ()) (fun _ _ -> true) update
/// Update the menu items of a ShellContent, given previous and current view elements
let internal updateMenuItemsShellContent (prevCollOpt: ViewElement array voption) (collOpt: ViewElement array voption) (target: Xamarin.Forms.ShellContent) =
let create (desc: ViewElement) =
desc.Create() :?> Xamarin.Forms.MenuItem
updateCollectionGeneric prevCollOpt collOpt target.MenuItems create (fun _ _ _ -> ()) (fun _ _ -> true) updateChild
/// Update the items of a ShellItem, given previous and current view elements
let internal updateShellItemItems (prevCollOpt: ViewElement array voption) (collOpt: ViewElement array voption) (target: Xamarin.Forms.ShellItem) =
let create (desc: ViewElement) =
match desc.Create() with
| :? ShellContent as shellContent -> ShellSection.op_Implicit shellContent
| :? TemplatedPage as templatedPage -> ShellSection.op_Implicit templatedPage
| :? ShellSection as shellSection -> shellSection
| child -> failwithf "%s is not compatible with the type ShellSection" (child.GetType().Name)
let update prevViewElement (currViewElement: ViewElement) (target: ShellSection) =
let realTarget =
match currViewElement.TargetType with
| t when t = typeof<ShellContent> -> target.Items.[0] :> BaseShellItem
| t when t = typeof<TemplatedPage> -> target.Items.[0] :> BaseShellItem
| _ -> target :> BaseShellItem
updateChild prevViewElement currViewElement realTarget
updateCollectionGeneric prevCollOpt collOpt target.Items create (fun _ _ _ -> ()) (fun _ _ -> true) update
/// Update the items of a ShellSection, given previous and current view elements
let internal updateShellSectionItems (prevCollOpt: ViewElement array voption) (collOpt: ViewElement array voption) (target: Xamarin.Forms.ShellSection) =
let create (desc: ViewElement) =
desc.Create() :?> Xamarin.Forms.ShellContent
updateCollectionGeneric prevCollOpt collOpt target.Items create (fun _ _ _ -> ()) (fun _ _ -> true) updateChild
/// Update the IsFocusedProperty of a SearchHandler, given previous and current IsFocused
let internal updateIsFocused prevValue currValue (target: Xamarin.Forms.SearchHandler) =
match prevValue, currValue with
| ValueNone, ValueNone -> ()
| ValueSome prevVal, ValueSome newVal when prevVal = newVal -> ()
| _, ValueNone -> target.SetIsFocused(false)
| _, ValueSome newVal -> target.SetIsFocused(newVal)
/// Update the SelectedItemProperty of a SearchHandler, given previous and current SelectedItem
let internal updateSelectedItem prevValue currValue (target: Xamarin.Forms.SearchHandler) =
match prevValue, currValue with
| ValueNone, ValueNone -> ()
| ValueSome prevVal, ValueSome newVal when prevVal = newVal -> ()
| _, ValueNone -> target.ClearValue(Xamarin.Forms.SearchHandler.SelectedItemProperty)
| _, ValueSome newVal -> target.SetValue(Xamarin.Forms.SearchHandler.SelectedItemProperty, newVal)
/// Update the IsCheckedProperty of a BaseShellItem, given previous and current IsChecked
let internal updateIsChecked prevValue currValue (target: Xamarin.Forms.BaseShellItem) =
match prevValue, currValue with
| ValueNone, ValueNone -> ()
| ValueSome prevVal, ValueSome newVal when prevVal = newVal -> ()
| _, ValueNone -> target.SetValue(Xamarin.Forms.BaseShellItem.IsCheckedProperty, null)
| _, ValueSome newVal -> target.SetValue(Xamarin.Forms.BaseShellItem.IsCheckedProperty, newVal)
/// Update the selectedItems of a SeletableItemsView, given previous and current view elements
let internal updateSelectedItems (prevCollOpt: seq<'T> voption) (collOpt: seq<'T> voption) (target: Xamarin.Forms.SelectableItemsView) =
let create (desc: ViewElement) = desc.Create()
let prevArray = ValueOption.map seqToArray prevCollOpt
let currArray = ValueOption.map seqToArray collOpt
updateCollectionGeneric prevArray currArray target.SelectedItems create (fun _ _ _ -> ()) (fun _ _ -> true) updateChild
/// Trigger ScrollView.ScrollToAsync if needed, given the current values
let internal triggerScrollToAsync (currValue: (float * float * AnimationKind) voption) (target: Xamarin.Forms.ScrollView) =
match currValue with
| ValueSome (x, y, animationKind) when x <> target.ScrollX || y <> target.ScrollY ->
let animated =
match animationKind with
| Animated -> true
| NotAnimated -> false
target.ScrollToAsync(x, y, animated) |> ignore
| _ -> ()
/// Trigger ItemsView.ScrollTo if needed, given the current values
let internal triggerScrollTo (currValue: (obj * obj * ScrollToPosition * AnimationKind) voption) (target: Xamarin.Forms.ItemsView) =
match currValue with
| ValueSome (x, y, scrollToPosition, animationKind) ->
let animated =
match animationKind with
| Animated -> true
| NotAnimated -> false
target.ScrollTo(x,y, scrollToPosition, animated)
| _ -> ()
/// Trigger Shell.GoToAsync if needed, given the current values
let internal triggerGoToAsync (currValue: (ShellNavigationState * AnimationKind) voption) (target: Xamarin.Forms.Shell) =
match currValue with
| ValueSome (navigationState, animationKind) ->
let animated =
match animationKind with
| Animated -> true
| NotAnimated -> false
target.GoToAsync(navigationState, animated) |> ignore
| _ -> ()
/// Check if two LayoutOptions are equal
let internal equalLayoutOptions (x:Xamarin.Forms.LayoutOptions) (y:Xamarin.Forms.LayoutOptions) =
x.Alignment = y.Alignment && x.Expands = y.Expands
/// Check if two Thickness values are equal
let internal equalThickness (x:Xamarin.Forms.Thickness) (y:Xamarin.Forms.Thickness) =
x.Bottom = y.Bottom && x.Top = y.Top && x.Left = y.Left && x.Right = y.Right
/// Try and find a specific ListView item
let tryFindListViewItem (sender: obj) (item: obj) =
match item with
| null -> None
| :? ListElementData as item ->
let items = (sender :?> Xamarin.Forms.ListView).ItemsSource :?> System.Collections.Generic.IList<ListElementData>
// POSSIBLE IMPROVEMENT: don't use a linear search
items |> Seq.tryFindIndex (fun item2 -> identical item.Key item2.Key)
| _ -> None
let private tryFindGroupedListViewItemIndex (items: System.Collections.Generic.IList<ListGroupData>) (item: ListElementData) =
// POSSIBLE IMPROVEMENT: don't use a linear search
items
|> Seq.indexed
|> Seq.tryPick (fun (i,items2) ->
// POSSIBLE IMPROVEMENT: don't use a linear search
items2
|> Seq.indexed
|> Seq.tryPick (fun (j,item2) -> if identical item.Key item2.Key then Some (i,j) else None))
/// Try and find a specific item in a GroupedListView
let tryFindGroupedListViewItemOrGroupItem (sender: obj) (item: obj) =
match item with
| null -> None
| :? ListGroupData as item ->
let items = (sender :?> Xamarin.Forms.ListView).ItemsSource :?> System.Collections.Generic.IList<ListGroupData>
// POSSIBLE IMPROVEMENT: don't use a linear search
items
|> Seq.indexed
|> Seq.tryPick (fun (i, item2) -> if identical item.Key item2.Key then Some (i, None) else None)
| :? ListElementData as item ->
let items = (sender :?> Xamarin.Forms.ListView).ItemsSource :?> System.Collections.Generic.IList<ListGroupData>
tryFindGroupedListViewItemIndex items item
|> (function
| None -> None
| Some (i, j) -> Some (i, Some j))
| _ -> None
/// Try and find a specific GroupedListView item
let tryFindGroupedListViewItem (sender: obj) (item: obj) =
match item with
| null -> None
| :? ListElementData as item ->
let items = (sender :?> Xamarin.Forms.ListView).ItemsSource :?> System.Collections.Generic.IList<ListGroupData>
tryFindGroupedListViewItemIndex items item
| _ -> None
let internal updateShellSearchHandler prevValueOpt (currValueOpt: ViewElement voption) target =
match prevValueOpt, currValueOpt with
| ValueNone, ValueNone -> ()
| ValueSome prevValue, ValueSome currValue when prevValue = currValue -> ()
| ValueSome prevValue, ValueSome currValue ->
let searchHandler = Shell.GetSearchHandler(target)
currValue.UpdateIncremental(prevValue, searchHandler)
| ValueNone, ValueSome currValue -> Shell.SetSearchHandler(target, currValue.Create() :?> Xamarin.Forms.SearchHandler)
| ValueSome _, ValueNone -> Shell.SetSearchHandler(target, null)
let internal updateShellBackgroundColor prevValueOpt currValueOpt target =
match prevValueOpt, currValueOpt with
| ValueSome prevValue, ValueSome currValue when prevValue = currValue -> ()
| ValueNone, ValueNone -> ()
| _, ValueSome currValue -> Shell.SetBackgroundColor(target, currValue)
| ValueSome _, ValueNone -> Shell.SetBackgroundColor(target, Color.Default)
let internal updateShellForegroundColor prevValueOpt currValueOpt target =
match prevValueOpt, currValueOpt with
| ValueSome prevValue, ValueSome currValue when prevValue = currValue -> ()
| ValueNone, ValueNone -> ()
| _, ValueSome currValue -> Shell.SetForegroundColor(target, currValue)
| ValueSome _, ValueNone -> Shell.SetForegroundColor(target, Color.Default)
let internal updateShellTitleColor prevValueOpt currValueOpt target =
match prevValueOpt, currValueOpt with
| ValueSome prevValue, ValueSome currValue when prevValue = currValue -> ()
| ValueNone, ValueNone -> ()
| _, ValueSome currValue -> Shell.SetTitleColor(target, currValue)
| ValueSome _, ValueNone -> Shell.SetTitleColor(target, Color.Default)
let internal updateShellDisabledColor prevValueOpt currValueOpt target =
match prevValueOpt, currValueOpt with
| ValueSome prevValue, ValueSome currValue when prevValue = currValue -> ()
| ValueNone, ValueNone -> ()
| _, ValueSome currValue -> Shell.SetDisabledColor(target, currValue)
| ValueSome _, ValueNone -> Shell.SetDisabledColor(target, Color.Default)
let internal updateShellUnselectedColor prevValueOpt currValueOpt target =