[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / RecyclerView / ItemSource / ItemsSourceFactory.cs
1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16
17 using System.Collections;
18 using System.Collections.Generic;
19 using System.Collections.Specialized;
20 //using AndroidX.RecyclerView.Widget; ??? need to find whot it needs? adapter?
21
22 namespace Tizen.NUI.Components
23 {
24     internal static class ItemsSourceFactory
25     {
26         public static IItemSource Create(IEnumerable itemsSource, ICollectionChangedNotifier notifier)
27         {
28             if (itemsSource == null)
29             {
30                 return new EmptySource();
31             }
32
33             switch (itemsSource)
34             {
35                 case IList list when itemsSource is INotifyCollectionChanged:
36                     return new ObservableItemSource(new MarshalingObservableCollection(list), notifier);
37                 case IEnumerable _ when itemsSource is INotifyCollectionChanged:
38                     return new ObservableItemSource(itemsSource, notifier);
39                 case IEnumerable<object> generic:
40                     return new ListSource(generic);
41             }
42
43             return new ListSource(itemsSource);
44         }
45
46         public static IItemSource Create(RecyclerView recyclerView)
47         {
48             return Create(recyclerView.ItemsSource, recyclerView);
49         }
50
51         public static IGroupableItemSource Create(CollectionView colView)
52         {
53             var source = colView.ItemsSource;
54
55             if (colView.IsGrouped && source != null)
56                 return new ObservableGroupedSource(colView, colView);
57
58             else
59                 return new UngroupedItemSource(Create(colView.ItemsSource, colView));
60         }
61     }
62 }