Apply MVC(ModelViewController) design 90/282490/2 accepted/tizen/unified/20221005.090825
authorWootak Jung <wootak.jung@samsung.com>
Tue, 4 Oct 2022 08:49:42 +0000 (17:49 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Tue, 4 Oct 2022 08:54:29 +0000 (17:54 +0900)
Change-Id: I73635f056fe8c6fec6fb9403351d0cbed88dc074
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
15 files changed:
SettingBluetooth/SettingBluetooth/Controller/AdapterController.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Controller/ViewController.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Device.cs [deleted file]
SettingBluetooth/SettingBluetooth/Model/BtModel.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Model/BtModelDevice.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Model/BtModelEnumerations.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Model/BtModelEventArgs.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/SettingBluetooth.cs
SettingBluetooth/SettingBluetooth/View/BtDetailView.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/View/BtMainView.cs [new file with mode: 0644]
SettingBluetooth/SettingBluetooth/WidgetSettingBluetooth.cs [deleted file]
packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk

diff --git a/SettingBluetooth/SettingBluetooth/Controller/AdapterController.cs b/SettingBluetooth/SettingBluetooth/Controller/AdapterController.cs
new file mode 100644 (file)
index 0000000..9e7dea7
--- /dev/null
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using SettingBluetooth.Model;
+using SettingBluetooth;
+using Tizen;
+using Tizen.NUI.Components;
+using SettingBluetooth.res.locale;
+
+namespace SettingBluetooth.Controller
+{
+    internal static class AdapterController
+    {
+        static bool deviceViewInit = false;
+        static DeviceSource deviceSource;
+
+        internal static void BtModelDiscoveryStateChanged(object obj, BtModelDiscoveryStateChangedEventArgs ev)
+        {
+            if (ev.DiscoveryState == BtModelDiscoveryState.Started)
+            {
+                Log.Debug(Program.LogTag, "Device discovery started");
+            }
+            else if (ev.DiscoveryState == BtModelDiscoveryState.Found)
+            {
+                if (deviceViewInit == false)
+                {
+                    deviceSource = new DeviceSource();
+                    BtMainView.AddDeviceView(deviceSource);
+                    BtMainView.AddScanButton();
+                    deviceViewInit = true;
+                }
+
+                Log.Debug(Program.LogTag, "Device found. Address: " + ev.DiscoveredDevice.RemoteDeviceAddress);
+                deviceSource.AddDevice(ev.DiscoveredDevice.RemoteDeviceAddress);
+            }
+            else
+            {
+                Log.Debug(Program.LogTag, "Device discovery finished");
+                deviceSource.UpdateTitle(Resources.IDS_BT_BODY_AVAILABLE_DEVICES);
+                BtMainView.UpdateScanButton(Resources.IDS_BT_SK_SCAN);
+            }
+        }
+
+        internal static void BtModelStateChanged(object obj, BtModelStateChangedEventArgs ev)
+        {
+            if (ev.IsEnabled)
+            {
+                Log.Debug(Program.LogTag, "BT enabled. Start discovery");
+                BtModel.DiscoveryStateChanged += BtModelDiscoveryStateChanged;
+                BtModel.StartDiscovery();
+            }
+            else
+            {
+                Log.Debug(Program.LogTag, "BT disabled");
+                BtMainView.RemoveDeviceView();
+                BtMainView.RemoveScanButton();
+            }
+        }
+
+        internal static void OnOffSwitchSelectedChanged(object obj, SelectedChangedEventArgs ev)
+        {
+            Log.Debug(Program.LogTag, "btOnOffSwitch clicked. IsSelected: " + ev.IsSelected + ", isEnabled: " + BtModel.IsEnabled);
+            if (ev.IsSelected == true && BtModel.IsEnabled == false)
+            {
+                Log.Info(Program.LogTag, "Enable BT");
+                BtModel.StateChanged += BtModelStateChanged;
+                BtModel.Enable();
+            }
+            else if (ev.IsSelected == false && BtModel.IsEnabled == true)
+            {
+                Log.Info(Program.LogTag, "Disable BT");
+                BtModel.Disable();
+            }
+            else
+            {
+                Log.Error(Program.LogTag, "Invalid state");
+            }
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs b/SettingBluetooth/SettingBluetooth/Controller/DeviceController.cs
new file mode 100644 (file)
index 0000000..28ef44a
--- /dev/null
@@ -0,0 +1,10 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Controller
+{
+    class DeviceController
+    {
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Controller/ViewController.cs b/SettingBluetooth/SettingBluetooth/Controller/ViewController.cs
new file mode 100644 (file)
index 0000000..c1adb17
--- /dev/null
@@ -0,0 +1,10 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Controller
+{
+    class ViewController
+    {
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Device.cs b/SettingBluetooth/SettingBluetooth/Device.cs
deleted file mode 100644 (file)
index 13c77f8..0000000
+++ /dev/null
@@ -1,186 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using Tizen.NUI.BaseComponents;
-using Tizen.NUI.Components;
-using Tizen.NUI.Binding;
-using Tizen;
-using Tizen.Network.Bluetooth;
-using SettingBluetooth.res.locale;
-
-namespace SettingBluetooth
-{
-    public class Device : INotifyPropertyChanged
-    {
-        string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";
-        private string name;
-        private bool connected;
-        private bool registered;
-        private BluetoothAppearanceType appearance;
-        public event PropertyChangedEventHandler PropertyChanged;
-
-        private void OnPropertyChanged(string propertyName)
-        {
-
-            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
-        }
-
-        public Device(string deviceName, bool con, bool reg)
-        {
-            name = deviceName;
-            connected = con;
-            registered = reg;
-        }
-
-        public Device(string deviceName, BluetoothAppearanceType deviceAppearance)
-        {
-            Log.Debug(Program.LogTag, "Device added. deviceName: " + deviceName + ", deviceAppearance: " + deviceAppearance);
-            name = deviceName;
-            appearance = deviceAppearance;
-        }
-
-        public string Name
-        {
-            get
-            {
-                return name;
-            }
-            set
-            {
-                name = value;
-                OnPropertyChanged("Name");
-            }
-        }
-
-        public string ImageUrl
-        {
-            get
-            {
-                return iconDir;
-            }
-        }
-
-        public bool Connected
-        {
-            get
-            {
-                return connected;
-            }
-            set
-            {
-                connected = value;
-                OnPropertyChanged("Connected");
-            }
-        }
-        public bool Registered
-        {
-            get
-            {
-                return registered;
-            }
-            set
-            {
-                registered = value;
-                OnPropertyChanged("Registered");
-            }
-        }
-        public BluetoothAppearanceType Appearance
-        {
-            get
-            {
-                return appearance;
-            }
-            set
-            {
-                appearance = value;
-                OnPropertyChanged("Appearance");
-            }
-        }
-    };
-
-    public class DeviceCollection : ObservableCollection<Device>
-    {
-        string[] devicePool = {
-           "Galaxy Buds2 Pro",
-           "Galaxy Fold 4",
-        };
-
-        private string title;
-
-        public DeviceCollection(string groupTitle)
-        {
-            title = groupTitle;
-            //UpdateDevices();
-        }
-
-        public string Title
-        {
-            get
-            {
-                return title;
-            }
-            set
-            {
-                title = value;
-                OnPropertyChanged(new PropertyChangedEventArgs("Title"));
-            }
-        }
-
-        public void UpdateDevices()
-        {
-            // Clear method have some issue about asynchronous actions,
-            // so call Remove for all item is recommanded.
-            Log.Info(Program.LogTag, "count: " + this.Count + ", pool count: " + devicePool.Length);
-            while (this.Count > 0)
-            {
-                this.RemoveAt(this.Count - 1);
-            }
-
-            int count = devicePool.Length;
-
-            for (int i = 0; i < count; i++)
-            {
-                this.Add(new Device(devicePool[i], false, false));
-            }
-        }
-
-        public void AddDevice(string deviceName, BluetoothAppearanceType deviceAppearance)
-        {   
-            this.Add(new Device(deviceName, deviceAppearance));
-        }
-    }
-
-    public class DeviceSource : ObservableCollection<DeviceCollection>
-    {
-        private DeviceCollection collection;
-
-        public DeviceSource()
-        {
-            Log.Info(Program.LogTag, "DeviceSource created");
-            collection = new DeviceCollection(Resources.IDS_BT_BODY_SCANNING_FOR_DEVICES_ING);
-            this.Add(collection);
-        }
-
-        public void UpdateTitle(string title)
-        {
-            collection.Title = title;
-        }
-
-        public void AddDevice(string deviceName, BluetoothAppearanceType deviceAppearance)
-        {
-            collection.AddDevice(deviceName, deviceAppearance);
-        }
-
-        public int GetCount()
-        {
-            Log.Debug(Program.LogTag, "count: " + collection.Count);
-            return collection.Count;
-        }
-
-        public void UpdateDevices()
-        {
-            collection.UpdateDevices();
-        }
-    }
-}
\ No newline at end of file
diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModel.cs b/SettingBluetooth/SettingBluetooth/Model/BtModel.cs
new file mode 100644 (file)
index 0000000..5a98d40
--- /dev/null
@@ -0,0 +1,59 @@
+using SettingBluetooth.Controller;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen;
+using Tizen.Network.Bluetooth;
+
+namespace SettingBluetooth.Model
+{
+    internal static class BtModel
+    {
+        internal static bool IsEnabled
+        {
+            get
+            {
+                return BtModelImpl.Instance.IsEnabled;
+            }
+        }
+
+        internal static void Enable()
+        {
+            BtModelImpl.Instance.Enable();
+        }
+
+        internal static void Disable()
+        {
+            BtModelImpl.Instance.Disable();
+        }
+
+        internal static void StartDiscovery()
+        {
+            BtModelImpl.Instance.StartDiscovery();
+        }
+
+        internal static event EventHandler<BtModelStateChangedEventArgs> StateChanged
+        {
+            add
+            {
+                BtModelImpl.Instance.StateChanged += value;
+            }
+            remove
+            {
+                BtModelImpl.Instance.StateChanged -= value;
+            }
+        }
+
+        internal static event EventHandler<BtModelDiscoveryStateChangedEventArgs> DiscoveryStateChanged
+        {
+            add
+            {
+                BtModelImpl.Instance.DiscoveryStateChanged += value;
+            }
+            remove
+            {
+                BtModelImpl.Instance.DiscoveryStateChanged -= value;
+            }
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelDevice.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelDevice.cs
new file mode 100644 (file)
index 0000000..5b9cf1f
--- /dev/null
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Model
+{
+    public class BtModelDevice
+    {
+        internal string RemoteDeviceAddress;
+        // TODO
+
+        internal BtModelDevice(string address) // TODO
+        {
+            RemoteDeviceAddress = address;
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelEnumerations.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelEnumerations.cs
new file mode 100644 (file)
index 0000000..2a64e2a
--- /dev/null
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth.Model
+{
+    public enum BtModelDiscoveryState
+    {
+        Started,
+        Finished,
+        Found
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelEventArgs.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelEventArgs.cs
new file mode 100644 (file)
index 0000000..2570e20
--- /dev/null
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen.Network.Bluetooth;
+using Tizen;
+
+namespace SettingBluetooth.Model
+{
+    internal class BtModelStateChangedEventArgs : EventArgs
+    {
+        internal bool IsEnabled;
+
+        internal BtModelStateChangedEventArgs(StateChangedEventArgs args)
+        {
+            IsEnabled = args.BTState == BluetoothState.Enabled;
+        }
+    }
+
+    internal class BtModelDiscoveryStateChangedEventArgs : EventArgs
+    {
+        internal BtModelDiscoveryState DiscoveryState;
+        internal BtModelDevice DiscoveredDevice;
+
+        internal BtModelDiscoveryStateChangedEventArgs(BluetoothDeviceDiscoveryState state)
+        {
+            DiscoveryState = (BtModelDiscoveryState)state;
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs b/SettingBluetooth/SettingBluetooth/Model/BtModelImpl.cs
new file mode 100644 (file)
index 0000000..190d708
--- /dev/null
@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen;
+using Tizen.Network.Bluetooth;
+
+namespace SettingBluetooth.Model
+{
+    internal class BtModelImpl
+    {
+        static readonly BtModelImpl instance = new BtModelImpl();
+        static event EventHandler<BtModelStateChangedEventArgs> stateChanged = null;
+        static event EventHandler<BtModelDiscoveryStateChangedEventArgs> discoveryStateChanged = null;
+
+        internal bool IsEnabled
+        {
+            get
+            {
+                return BluetoothAdapter.IsBluetoothEnabled;
+            }
+        }
+
+        internal void Enable()
+        {
+            BluetoothAdapter.Enable();
+        }
+
+        internal void Disable()
+        {
+            BluetoothAdapter.Disable();
+        }
+
+        internal void StartDiscovery()
+        {
+            BluetoothAdapter.StartDiscovery();
+        }
+
+        private static void AdapterStateChanged(object obj, StateChangedEventArgs ev)
+        {
+            stateChanged?.Invoke(null, new BtModelStateChangedEventArgs(ev));
+        }
+
+        internal event EventHandler<BtModelStateChangedEventArgs> StateChanged
+        {
+            add
+            {
+                stateChanged += value;
+            }
+            remove
+            {
+                stateChanged -= value;
+            }
+        }
+
+        private static void AdapterDiscoveryChanged(object obj, DiscoveryStateChangedEventArgs ev)
+        {
+            BtModelDiscoveryStateChangedEventArgs args = new BtModelDiscoveryStateChangedEventArgs(ev.DiscoveryState);
+
+            if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Started)
+            {
+                discoveryStateChanged?.Invoke(null, args);
+            }
+            else if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Finished)
+            {
+                discoveryStateChanged?.Invoke(null, args);
+            }
+            else
+            {
+                BtModelDevice device = new BtModelDevice(ev.DeviceFound.Address);
+                args.DiscoveredDevice = device;
+            }
+
+            discoveryStateChanged?.Invoke(null, args);
+        }
+
+        internal event EventHandler<BtModelDiscoveryStateChangedEventArgs> DiscoveryStateChanged
+        {
+            add
+            {
+                discoveryStateChanged += value;
+            }
+            remove
+            {
+                discoveryStateChanged -= value;
+            }
+        }
+
+        private void Initialize()
+        {
+            BluetoothAdapter.StateChanged += AdapterStateChanged;
+            BluetoothAdapter.DiscoveryStateChanged += AdapterDiscoveryChanged;
+        }
+
+        internal static BtModelImpl Instance
+        {
+            get
+            {
+                return instance;
+            }
+        }
+
+        private BtModelImpl()
+        {
+            Initialize();
+        }
+    }
+}
index aa14589a467ed19ba9160fcaa8998fc1b8dce755..ab763413fe081c0a082a86d9ddaef96db5491d94 100644 (file)
@@ -28,7 +28,7 @@ namespace SettingBluetooth
         static void Main(string[] args)
         {
             Dictionary<System.Type, string> widgetSet = new Dictionary<Type, string>();
-            widgetSet.Add(typeof(WidgetSettingBluetooth), "bluetooth@org.tizen.cssetting-bluetooth");
+            widgetSet.Add(typeof(BtMainView), "bluetooth@org.tizen.cssetting-bluetooth");
             var app = new Program(widgetSet);
             app.Run(args);
         }
diff --git a/SettingBluetooth/SettingBluetooth/View/BtDetailView.cs b/SettingBluetooth/SettingBluetooth/View/BtDetailView.cs
new file mode 100644 (file)
index 0000000..cd0a487
--- /dev/null
@@ -0,0 +1,10 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SettingBluetooth
+{
+    class BtDetailView
+    {
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs b/SettingBluetooth/SettingBluetooth/View/BtDeviceView.cs
new file mode 100644 (file)
index 0000000..905a23d
--- /dev/null
@@ -0,0 +1,137 @@
+using System;
+using System.ComponentModel;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+using Tizen.NUI.Binding;
+using Tizen;
+using Tizen.Network.Bluetooth;
+using SettingBluetooth.res.locale;
+
+namespace SettingBluetooth
+{
+    public class Device : INotifyPropertyChanged
+    {
+        string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";
+        private string name;
+        private bool connected;
+        private bool registered;
+        private BluetoothAppearanceType appearance;
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        private void OnPropertyChanged(string propertyName)
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+        }
+
+        public Device(string deviceName, bool con, bool reg)
+        {
+            name = deviceName;
+            connected = con;
+            registered = reg;
+        }
+
+        public Device(string deviceName)
+        {
+            Log.Debug(Program.LogTag, "Device added. deviceName: " + deviceName);
+            name = deviceName;
+        }
+
+        public string Name
+        {
+            get
+            {
+                return name;
+            }
+            set
+            {
+                name = value;
+                OnPropertyChanged("Name");
+            }
+        }
+
+        public string ImageUrl
+        {
+            get
+            {
+                return iconDir;
+            }
+        }
+
+        public bool Connected
+        {
+            get
+            {
+                return connected;
+            }
+            set
+            {
+                connected = value;
+                OnPropertyChanged("Connected");
+            }
+        }
+        public bool Registered
+        {
+            get
+            {
+                return registered;
+            }
+            set
+            {
+                registered = value;
+                OnPropertyChanged("Registered");
+            }
+        }
+    }
+
+    public class DeviceCollection : ObservableCollection<Device>
+    {
+        private string title;
+
+        public DeviceCollection(string groupTitle)
+        {
+            title = groupTitle;
+        }
+
+        public string Title
+        {
+            get
+            {
+                return title;
+            }
+            set
+            {
+                title = value;
+                OnPropertyChanged(new PropertyChangedEventArgs("Title"));
+            }
+        }
+
+        public void AddDevice(string deviceName)
+        {
+            this.Add(new Device(deviceName));
+        }
+    }
+
+    public class DeviceSource : ObservableCollection<DeviceCollection>
+    {
+        private DeviceCollection collection;
+
+        public DeviceSource()
+        {
+            Log.Info(Program.LogTag, "DeviceSource created");
+            collection = new DeviceCollection(Resources.IDS_BT_BODY_SCANNING_FOR_DEVICES_ING);
+            this.Add(collection);
+        }
+
+        public void UpdateTitle(string title)
+        {
+            collection.Title = title;
+        }
+
+        public void AddDevice(string deviceName)
+        {
+            collection.AddDevice(deviceName);
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/View/BtMainView.cs b/SettingBluetooth/SettingBluetooth/View/BtMainView.cs
new file mode 100644 (file)
index 0000000..65a24d1
--- /dev/null
@@ -0,0 +1,187 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Tizen.Applications;
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+using Tizen.Network.Bluetooth;
+using Tizen;
+using SettingBluetooth.res.locale;
+using Tizen.NUI.Binding;
+using SettingBluetooth.Model;
+using SettingBluetooth.Controller;
+
+namespace SettingBluetooth
+{
+    class BtMainView : Widget
+    {
+        static ContentPage mainPage;
+        static View mainView;
+        static CollectionView deviceView = null;
+        static Button scanButton = null;
+
+        public BtMainView() : base()
+        {
+        }
+
+        public static void DeviceSelectionChanged(object obj, SelectionChangedEventArgs ev)
+        {
+            Log.Debug(Program.LogTag, "DeviceSelectionChanged called");
+
+            //SingleSelection Only have 1 or nil object in the list.
+            foreach (object item in ev.PreviousSelection)
+            {
+                if (item == null) break;
+                if (item is Device device)
+                {
+                    device.Connected = false;
+                }
+            }
+            foreach (object item in ev.CurrentSelection)
+            {
+                if (item == null) break;
+                if (item is Device device)
+                {
+                    device.Connected = true;
+                    device.Registered = true;
+                }
+            }
+        }
+
+        internal static void AddDeviceView(DeviceSource deviceSource)
+        {
+            deviceView = new CollectionView()
+            {
+                ItemsSource = deviceSource,
+                ItemsLayouter = new LinearLayouter(),
+                ItemTemplate = new DataTemplate(() =>
+                {
+                    DefaultLinearItem item = new DefaultLinearItem()
+                    {
+                        WidthSpecification = LayoutParamPolicies.MatchParent,
+                    };
+                    item.Label.SetBinding(TextLabel.TextProperty, "Name");
+                    item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+                    item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
+                    item.Icon.WidthSpecification = 40;
+                    item.Icon.HeightSpecification = 40;
+                    return item;
+                }),
+
+                GroupHeaderTemplate = new DataTemplate(() =>
+                {
+                    DefaultTitleItem group = new DefaultTitleItem()
+                    {
+                        WidthSpecification = LayoutParamPolicies.MatchParent,
+                    };
+                    group.Label.SetBinding(TextLabel.TextProperty, "Title");
+                    group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+
+                    return group;
+                }),
+
+                IsGrouped = true,
+                ScrollingDirection = ScrollableBase.Direction.Vertical,
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+                SelectionMode = ItemSelectionMode.Single,
+            };
+            deviceView.SelectionChanged += DeviceSelectionChanged;
+
+            mainView.Add(deviceView);
+        }
+
+        internal static void AddScanButton()
+        {
+            scanButton = new Button
+            {
+                Text = Resources.IDS_BT_SK_STOP,
+                //WidthSpecification = 300,
+                //HeightSpecification = 80,
+            };
+            scanButton.Clicked += (object obj, ClickedEventArgs ev) =>
+            {
+                //Log.Debug(Program.LogTag, "scanButton clicked. isScanning: " + isScanning);
+                // check isScanning
+                // scan stop
+                //if (isScanning == true)
+                //{
+                //    BluetoothAdapter.StopDiscovery();
+                //}
+                //else
+                //{
+                //    BluetoothAdapter.StartDiscovery();
+                //}
+            };
+
+            mainView.Add(scanButton);
+        }
+
+        internal static void UpdateScanButton(string text)
+        {
+            scanButton.Text = text;
+        }
+
+        internal static void RemoveDeviceView()
+        {
+            if (deviceView)
+            {
+                mainView.Remove(deviceView);
+            }
+        }
+
+        internal static void RemoveScanButton()
+        {
+            if (scanButton)
+            {
+                mainView.Remove(scanButton);
+            }
+        }
+
+        protected override void OnCreate(string contentInfo, Window window)
+        {
+            //window = NUIApplication.GetDefaultWindow();
+            //window.BackgroundColor = Color.White;
+
+            var appBar = new AppBar()
+            {
+                Title = Resources.IDS_BT_BODY_BLUETOOTH,
+            };
+
+            mainView = new View()
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                },
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            var onOffItem = new DefaultLinearItem
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                Text = Resources.IDS_BT_BODY_BLUETOOTH,
+            };
+            onOffItem.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+
+            var onOffSwitch = new Switch()
+            {
+                IsSelected = BtModel.IsEnabled,
+            };
+            onOffSwitch.SelectedChanged += AdapterController.OnOffSwitchSelectedChanged;
+            onOffItem.Extra = onOffSwitch;
+            mainView.Add(onOffItem);
+
+            mainPage = new ContentPage()
+            {
+                AppBar = appBar,
+                Content = mainView,
+            };
+
+            window.GetDefaultNavigator().Push(mainPage);
+        }
+    }
+}
diff --git a/SettingBluetooth/SettingBluetooth/WidgetSettingBluetooth.cs b/SettingBluetooth/SettingBluetooth/WidgetSettingBluetooth.cs
deleted file mode 100644 (file)
index e044252..0000000
+++ /dev/null
@@ -1,263 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Tizen.Applications;
-using Tizen.NUI;
-using Tizen.NUI.BaseComponents;
-using Tizen.NUI.Components;
-using Tizen.Network.Bluetooth;
-using Tizen;
-using SettingBluetooth.res.locale;
-using Tizen.NUI.Binding;
-
-namespace SettingBluetooth
-{
-    internal class WidgetSettingBluetooth : Widget
-    {
-        ContentPage mainPage;
-        View mainView;
-        CollectionView deviceView = null;
-        DeviceSource deviceSource;
-        static Button scanButton = null;
-        bool isEnabled = false;
-        bool isScanning = false;
-        bool deviceSourceInit = false;
-
-        public WidgetSettingBluetooth() : base()
-        {
-        }
-
-        public void DeviceSelectionChanged(object obj, SelectionChangedEventArgs ev)
-        {
-            Log.Debug(Program.LogTag, "DeviceSelectionChanged called");
-
-            //SingleSelection Only have 1 or nil object in the list.
-            foreach (object item in ev.PreviousSelection)
-            {
-                if (item == null) break;
-                if (item is Device device)
-                {
-                    device.Connected = false;
-                }
-            }
-            foreach (object item in ev.CurrentSelection)
-            {
-                if (item == null) break;
-                if (item is Device device)
-                {
-                    device.Connected = true;
-                    device.Registered = true;
-                }
-            }
-        }
-
-        private void AddDeviceView()
-        {
-            deviceSource = new DeviceSource();
-            deviceView = new CollectionView()
-            {
-                ItemsSource = deviceSource,
-                ItemsLayouter = new LinearLayouter(),
-                ItemTemplate = new DataTemplate(() =>
-                {
-                    DefaultLinearItem item = new DefaultLinearItem()
-                    {
-                        WidthSpecification = LayoutParamPolicies.MatchParent,
-                    };
-                    item.Label.SetBinding(TextLabel.TextProperty, "Name");
-                    item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
-                    item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
-                    item.Icon.WidthSpecification = 40;
-                    item.Icon.HeightSpecification = 40;
-                    return item;
-                }),
-
-                GroupHeaderTemplate = new DataTemplate(() =>
-                {
-                    DefaultTitleItem group = new DefaultTitleItem()
-                    {
-                        WidthSpecification = LayoutParamPolicies.MatchParent,
-                    };
-                    group.Label.SetBinding(TextLabel.TextProperty, "Title");
-                    group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
-
-                    return group;
-                }),
-
-                IsGrouped = true,
-                ScrollingDirection = ScrollableBase.Direction.Vertical,
-                WidthSpecification = LayoutParamPolicies.MatchParent,
-                HeightSpecification = LayoutParamPolicies.MatchParent,
-                SelectionMode = ItemSelectionMode.Single,
-            };
-            deviceView.SelectionChanged += DeviceSelectionChanged;
-
-            mainView.Add(deviceView);
-        }
-
-        private void AddScanButton()
-        {
-            scanButton = new Button
-            {
-                Text = Resources.IDS_BT_SK_STOP,
-                //WidthSpecification = 300,
-                //HeightSpecification = 80,
-            };
-            scanButton.Clicked += (object obj, ClickedEventArgs ev) =>
-            {
-                Log.Debug(Program.LogTag, "scanButton clicked. isScanning: " + isScanning);
-                // check isScanning
-                // scan stop
-                if (isScanning == true)
-                {
-                    BluetoothAdapter.StopDiscovery();
-                }
-                else
-                {
-                    BluetoothAdapter.StartDiscovery();
-                }
-            };
-
-            mainView.Add(scanButton);
-        }
-
-        internal static void UpdateScanButton(string text)
-        {
-            scanButton.Text = text;
-        }
-
-        private void RemoveDeviceView()
-        {
-            if (deviceView)
-            {
-                mainView.Remove(deviceView);
-            }
-        }
-
-        private void RemoveScanButton()
-        {
-            if (scanButton)
-            {
-                mainView.Remove(scanButton);
-            }
-        }
-
-        private void EventHandlerDiscoveryChanged(object obj, DiscoveryStateChangedEventArgs ev)
-        {
-            if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Started)
-            {
-                Log.Debug(Program.LogTag, "Device discovery started");
-            }
-            else if (ev.DiscoveryState == BluetoothDeviceDiscoveryState.Found)
-            {
-                if (deviceSourceInit == false)
-                {
-                    AddDeviceView();
-                    AddScanButton();
-                    deviceSourceInit = true;
-                }
-
-                Log.Debug(Program.LogTag, "Device found. Address: " + ev.DeviceFound.Address + ", Appearance: " + ev.DeviceFound.AppearanceType);
-                deviceSource.AddDevice(ev.DeviceFound.Name, ev.DeviceFound.AppearanceType);
-            }
-            else
-            {
-                Log.Debug(Program.LogTag, "Device discovery finished");
-                isScanning = false;
-                deviceSource.UpdateTitle(Resources.IDS_BT_BODY_AVAILABLE_DEVICES);
-                UpdateScanButton(Resources.IDS_BT_SK_SCAN);
-            }
-        }
-
-        private void EventHandlerStateChanged(object obj, StateChangedEventArgs ev)
-        {
-            if (ev.BTState == BluetoothState.Enabled)
-            {
-                Log.Info(Program.LogTag, "BT enabled. Start discovery");
-                isEnabled = true;
-                BluetoothAdapter.StartDiscovery();
-                isScanning = true;
-            }
-            else
-            {
-                Log.Info(Program.LogTag, "BT disabled");
-                isEnabled = false;
-                RemoveDeviceView();
-                RemoveScanButton();
-                deviceSourceInit = false;
-            }
-        }
-
-        protected override void OnCreate(string contentInfo, Window window)
-        {
-            window = NUIApplication.GetDefaultWindow();
-            window.BackgroundColor = Color.White;
-
-            var appBar = new AppBar()
-            {
-                Title = Resources.IDS_BT_BODY_BLUETOOTH,
-            };
-
-            mainView = new View()
-            {
-                Layout = new LinearLayout()
-                {
-                    LinearOrientation = LinearLayout.Orientation.Vertical,
-                    HorizontalAlignment = HorizontalAlignment.Center,
-                },
-                WidthSpecification = LayoutParamPolicies.MatchParent,
-                HeightSpecification = LayoutParamPolicies.MatchParent,
-            };
-
-            isEnabled = BluetoothAdapter.IsBluetoothEnabled;
-            BluetoothAdapter.StateChanged += EventHandlerStateChanged;
-            BluetoothAdapter.DiscoveryStateChanged += EventHandlerDiscoveryChanged;
-
-            var onOffItem = new DefaultLinearItem
-            {
-                WidthSpecification = LayoutParamPolicies.MatchParent,
-                Text = Resources.IDS_BT_BODY_BLUETOOTH,
-            };
-            onOffItem.Label.HorizontalAlignment = HorizontalAlignment.Begin;
-
-            var onOffSwitch = new Switch()
-            {
-                IsSelected = isEnabled,
-            };
-            onOffSwitch.SelectedChanged += (object obj, SelectedChangedEventArgs ev) =>
-            {
-                Log.Debug(Program.LogTag, "btOnOffSwitch clicked. IsSelected: " + ev.IsSelected + ", isEnabled: " + isEnabled);
-                if (ev.IsSelected == true && isEnabled == false)
-                {
-                    Log.Info(Program.LogTag, "Enable BT adapter");
-                    BluetoothAdapter.Enable();
-                }
-                else if (ev.IsSelected == false && isEnabled == true)
-                {
-                    Log.Info(Program.LogTag, "Disable BT adapter");
-                    BluetoothAdapter.Disable();
-                }
-                else
-                {
-                    Log.Error(Program.LogTag, "Invalid state");
-                }
-            };
-            onOffItem.Extra = onOffSwitch;
-            mainView.Add(onOffItem);
-
-            if (isEnabled)
-            {
-                AddDeviceView();
-                AddScanButton();
-            }
-
-            mainPage = new ContentPage()
-            {
-                AppBar = appBar,
-                Content = mainView,
-            };
-
-            window.GetDefaultNavigator().Push(mainPage);
-        }
-    }
-}
index 83c797a00d783ff40408d8878e77355151d03e2b..f24c62472069bd8e6751d903fefcb46779055f63 100644 (file)
Binary files a/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk and b/packaging/org.tizen.cssetting-bluetooth-1.0.0.tpk differ