c7ebab7b15ce15dc05a106e24edbe08f516deb9c
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / RefreshViewTest1 / RefreshViewTest1.xaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using Tizen.NUI;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components;
21
22 namespace NUITizenGallery
23 {
24     public partial class RefreshViewTest1Page : ContentPage
25     {
26         Random rand;
27         bool clicked = false;
28         TextLabel lbl1;
29
30         public RefreshViewTest1Page()
31         {
32             InitializeComponent();
33
34             lbl1 = new TextLabel
35             {
36                 Text = "It is Empty!"
37             };
38             Scroller.Add(lbl1);
39
40             btn1.Clicked += (s, e) =>
41             {
42                 if (!clicked)
43                 {
44                     rand = new Random();
45                     int i = 0;
46                     Scroller.Remove(lbl1);
47                     for (i = 1; i <= 50; i++)
48                     {
49                         var label = new TextLabel(DateTime.UtcNow.AddMinutes(i).ToString("F"));
50                         var r = new decimal(rand.NextDouble());
51                         var g = new decimal(rand.NextDouble());
52                         var b = new decimal(rand.NextDouble());
53                         var boxview = new View
54                         {
55                             Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, 100),
56                             BackgroundColor = new Tizen.NUI.Color((float)r, (float)g, (float)b, 1.0f)
57                         };
58                         Scroller.Add(label);
59                         Scroller.Add(boxview);
60                     }
61                 }
62                 else
63                 {
64                     View[] a = Scroller.Children.ToArray();
65                     int i = 0;
66                     for (i = 0; i < a.Length; i++)
67                     {
68                         Scroller.Remove(a[i]);
69                     }
70                     lbl1 = new TextLabel
71                     {
72                         Text = "It is Empty!"
73                     };
74                     Scroller.Add(lbl1);
75                 }
76                 clicked = !clicked;
77             };
78         }
79
80         protected override void Dispose(DisposeTypes type)
81         {
82             if (Disposed)
83             {
84                 return;
85             }
86
87             if (type == DisposeTypes.Explicit)
88             {
89                 RemoveAllChildren(true);
90             }
91
92             base.Dispose(type);
93         }
94
95         private void RemoveAllChildren(bool dispose = false)
96         {
97             RecursiveRemoveChildren(this, dispose);
98         }
99
100         private void RecursiveRemoveChildren(View parent, bool dispose)
101         {
102             if (parent == null)
103             {
104                 return;
105             }
106
107             int maxChild = (int)parent.ChildCount;
108             for (int i = maxChild - 1; i >= 0; --i)
109             {
110                 View child = parent.GetChildAt((uint)i);
111                 if (child == null)
112                 {
113                     continue;
114                 }
115
116                 RecursiveRemoveChildren(child, dispose);
117                 parent.Remove(child);
118                 if (dispose)
119                 {
120                     child.Dispose();
121                 }
122             }
123         }
124     }
125 }
126