PlatformAlarmService and PlatformAlarm implementations 57/171557/2
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Mon, 5 Mar 2018 15:49:14 +0000 (16:49 +0100)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Tue, 6 Mar 2018 10:12:56 +0000 (11:12 +0100)
Change-Id: I7996c8b378fb82d28429b2166399626fa22c51ea
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
13 files changed:
Clock/Clock.Tizen.Mobile/Clock.Tizen.Mobile.cs
Clock/Clock.Tizen.Mobile/Clock.cs
Clock/Clock.Tizen.Mobile/Controls/Lists/MultiSelectList.xaml
Clock/Clock.Tizen.Mobile/Services/PlatformAlarm.cs [new file with mode: 0644]
Clock/Clock.Tizen.Mobile/Services/TizenPlatformAlarmService.cs
Clock/Clock.Tizen.Mobile/Tools/WeekFlagsExtensions.cs [new file with mode: 0644]
Clock/Clock.Tizen.Mobile/ViewModelFactory.cs
Clock/Clock.Tizen.Mobile/Views/MainView.xaml
Clock/Clock.Tizen.Mobile/Views/MainView.xaml.cs
Clock/Clock.Tizen.Mobile/Views/TimerView.xaml
Clock/Clock/Abstractions/IPlatformAlarm.cs
Clock/Clock/Model/DaysOfWeek.cs
clock/Clock/Tools/Converters/CollectionToBooleanConverter.cs

index 26b9a1e..09ced48 100644 (file)
@@ -1,6 +1,8 @@
 using System;
+using Clock.Abstractions;
 using Tizen;
 using Tizen.Applications;
+using Xamarin.Forms;
 
 namespace Clock.Tizen.Mobile
 {
@@ -11,17 +13,17 @@ namespace Clock.Tizen.Mobile
         protected override void OnCreate()
         {
             base.OnCreate();
-            LoadApplication(new App());
+            LoadApplication(new App(ApplicationInfo.ApplicationId));
         }
 
         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
         {
             base.OnAppControlReceived(e);
 
-            if (int.TryParse(e.ReceivedAppControl.ExtraData.Get(APP_CONTROL_DATA_ALARM_ID).ToString(), out var id))
-            {
-                //DependencyService.Get<IPlatformAlarmsService>().OnPlatformAlarmTriggered(id);
-            }
+            //if (int.TryParse(e.ReceivedAppControl.ExtraData.Get(APP_CONTROL_DATA_ALARM_ID).ToString(), out var id))
+            //{
+            //    DependencyService.Get<IPlatformAlarmsService>().OnPlatformAlarmTriggered(id);
+            //}
         }
 
         static void Main(string[] args)
index 1d2a8c9..b458cb2 100644 (file)
@@ -10,12 +10,16 @@ namespace Clock
     {
         public const string Tag = "d";
 
-        public App()
+        public string ApplicationId { get;}
+
+        public App(string applicationId)
         {
+            ApplicationId = applicationId;
+
             try
             {
                 var view = new MainView();
-                view.BindingContext = ViewModelFactory.CreateMain(view.Navigation);
+                //view.BindingContext = ViewModelFactory.CreateMain(view.Navigation);
                 MainPage = new NavigationPage(view);
             }
             catch (Exception e)
@@ -24,4 +28,4 @@ namespace Clock
             }
         }
     }
-}
+}
\ No newline at end of file
index 59d9aff..be63667 100644 (file)
@@ -24,7 +24,7 @@
         </DataTemplate>
     </ListView.HeaderTemplate>
 
-    <ListView.WrapperTemplate>
+    <!--<ListView.WrapperTemplate>
         <DataTemplate>
             <StackLayout BindingContext="{Binding Item}" x:Name="stackLayout" Orientation="Horizontal"
                          HorizontalOptions="FillAndExpand">
@@ -32,5 +32,5 @@
                         HorizontalOptions="End" VerticalOptions="Center" Margin="8,8,16,8" />
             </StackLayout>
         </DataTemplate>
-    </ListView.WrapperTemplate>
+    </ListView.WrapperTemplate>-->
 </ListView>
\ No newline at end of file
diff --git a/Clock/Clock.Tizen.Mobile/Services/PlatformAlarm.cs b/Clock/Clock.Tizen.Mobile/Services/PlatformAlarm.cs
new file mode 100644 (file)
index 0000000..fed7e5a
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using Clock.Abstractions;
+using Clock.Model;
+using Clock.Tizen.Mobile.Tools;
+using Alarm = Tizen.Applications.Alarm;
+
+namespace Clock.Tizen.Mobile.Services
+{
+    internal class PlatformAlarm : IPlatformAlarm
+    {
+        private readonly Alarm _alarm;
+
+        public int Id => _alarm.AlarmId;
+
+        public PlatformAlarm(Alarm alarm)
+        {
+            _alarm = alarm;
+        }
+
+        public void Cancel()
+        {
+            _alarm.Cancel();
+        }
+
+        public DateTime GetScheduledDateTime()
+        {
+            return _alarm.ScheduledDate;
+        }
+
+        public DaysOfWeek GetDaysOfWeek()
+        {
+            return _alarm.WeekFlag.ConvertToApiFlag();
+        }
+    }
+}
\ No newline at end of file
index 5349d25..11cf342 100644 (file)
@@ -1,7 +1,28 @@
-using System;
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
 using System.Collections.Generic;
+using System.Linq;
 using Clock.Abstractions;
 using Clock.Model;
+using Clock.Tizen.Mobile.Tools;
+using Tizen.Applications;
+using AlarmManager = Tizen.Applications.AlarmManager;
+using Application = Xamarin.Forms.Application;
 
 namespace Clock.Tizen.Mobile.Services
 {
@@ -11,22 +32,29 @@ namespace Clock.Tizen.Mobile.Services
 
         public IPlatformAlarm CreateNewPlatformAlarm(DateTime time, DaysOfWeek days)
         {
-            throw new NotImplementedException();
+            var appControl = new AppControl {ApplicationId = ((App)Application.Current).ApplicationId};
+
+            var alarm = days == DaysOfWeek.Never
+                ? AlarmManager.CreateAlarm(time, appControl)
+                : AlarmManager.CreateAlarm(time, days.ConvertToPlatformFlag(), appControl);
+
+            return new PlatformAlarm(alarm);
         }
 
         public IEnumerable<IPlatformAlarm> GetPlatformAlarms()
         {
-            throw new NotImplementedException();
+            return AlarmManager.GetAllScheduledAlarms().Select(alarm => new PlatformAlarm(alarm));
         }
 
         public IPlatformAlarm GetPlatformAlarm(int id)
         {
-            throw new NotImplementedException();
+            var alarm = AlarmManager.GetAllScheduledAlarms().FirstOrDefault(a => a.AlarmId == id);
+            return alarm != null ? new PlatformAlarm(alarm) : null;
         }
 
         public DateTime GetCurrentPlatformTime()
         {
-            throw new NotImplementedException();
+            return DateTime.Now;
         }
 
         public void OnPlatformAlarmTriggered(int id)
diff --git a/Clock/Clock.Tizen.Mobile/Tools/WeekFlagsExtensions.cs b/Clock/Clock.Tizen.Mobile/Tools/WeekFlagsExtensions.cs
new file mode 100644 (file)
index 0000000..9a5f7a9
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using Clock.Model;
+using Tizen.Applications;
+
+namespace Clock.Tizen.Mobile.Tools
+{
+    public static class WeekFlagsExtensions
+    {
+        public static AlarmWeekFlag ConvertToPlatformFlag(this DaysOfWeek daysOfWeek)
+        {
+            AlarmWeekFlag weekFlag = 0;
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Sunday))
+            {
+                weekFlag |= AlarmWeekFlag.Sunday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Monday))
+            {
+                weekFlag |= AlarmWeekFlag.Monday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Tuesday))
+            {
+                weekFlag |= AlarmWeekFlag.Tuesday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Wednesday))
+            {
+                weekFlag |= AlarmWeekFlag.Wednesday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Thursday))
+            {
+                weekFlag |= AlarmWeekFlag.Thursday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Friday))
+            {
+                weekFlag |= AlarmWeekFlag.Friday;
+            }
+
+            if (daysOfWeek.HasFlag(DaysOfWeek.Saturday))
+            {
+                weekFlag |= AlarmWeekFlag.Saturday;
+            }
+
+            return weekFlag;
+        }
+
+        public static DaysOfWeek ConvertToApiFlag(this AlarmWeekFlag alarmWeekFlag)
+        {
+            DaysOfWeek daysOfWeek = 0;
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Sunday))
+            {
+                daysOfWeek |= DaysOfWeek.Sunday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Monday))
+            {
+                daysOfWeek |= DaysOfWeek.Monday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Tuesday))
+            {
+                daysOfWeek |= DaysOfWeek.Tuesday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Wednesday))
+            {
+                daysOfWeek |= DaysOfWeek.Wednesday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Thursday))
+            {
+                daysOfWeek |= DaysOfWeek.Thursday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Friday))
+            {
+                daysOfWeek |= DaysOfWeek.Friday;
+            }
+
+            if (alarmWeekFlag.HasFlag(AlarmWeekFlag.Saturday))
+            {
+                daysOfWeek |= DaysOfWeek.Saturday;
+            }
+
+            return daysOfWeek;
+        }
+    }
+}
\ No newline at end of file
index 334a985..9c515e5 100644 (file)
@@ -41,8 +41,8 @@ namespace Clock.Tizen.Mobile
                 return await navigation.NavigateAndPop(deletingPage, taskSource);
             }
 
-            return new WorldClockViewModel(DS.Get<IAppControl>(), NavigateToDeletingPlaces, NavigateToReorderingPlaces
-                DS.Get<ILogger>());
+            return new WorldClockViewModel(DS.Get<IAppControl>(), NavigateToDeletingPlaces, NavigateToReorderingPlaces,
+                DS.Get<ILogger>());
         }
 
         public static ReorderingPlacesViewModel CreateReorderingPlaces(IEnumerable<ClockLocation> locations,
index 3a3b89a..c5b1362 100644 (file)
@@ -3,15 +3,22 @@
 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pages="clr-namespace:Clock.Tizen.Mobile.Views;assembly=Clock.Tizen.Mobile"
-            x:Class="Clock.Tizen.Mobile.Views.MainView">
+            x:Class="Clock.Tizen.Mobile.Views.MainView"
+            Title="Test">
 
     <TabbedPage.Children>
 
-        <pages:AlarmView Title="Alarm" Icon="tabs/clock_tabs_ic_alarm.png" />
+        <ContentPage Title="Test">
+            <StackLayout>
+                <Label Text="label1"/>
+                <Label Text="label2"/>
+            </StackLayout>
+        </ContentPage>
+        <!--<pages:AlarmView Title="Alarm" Icon="tabs/clock_tabs_ic_alarm.png" />
         <pages:WorldClockView Title="World clock" Icon="tabs/clock_tabs_ic_worldclock.png"
                               BindingContext="{Binding WorldClockViewModel}" />
         <pages:StopWatchView Title="Stopwatch" Icon="tabs/clock_tabs_ic_stopwatch.png" />
-        <pages:TimerView Title="Timer" Icon="tabs/clock_tabs_ic_timer.png" />
+        <pages:TimerView Title="Timer" Icon="tabs/clock_tabs_ic_timer.png" />-->
 
     </TabbedPage.Children>
 
index 5ed9607..561599d 100644 (file)
@@ -1,4 +1,7 @@
 using Xamarin.Forms;
+using Xamarin.Forms.Xaml;
+
+[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
 
 namespace Clock.Tizen.Mobile.Views
 {
@@ -7,7 +10,6 @@ namespace Clock.Tizen.Mobile.Views
         public MainView()
         {
             InitializeComponent();
-
             NavigationPage.SetHasNavigationBar(this, false);
         }
     }
index ca4805a..c100637 100644 (file)
@@ -3,7 +3,7 @@
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:controls="clr-namespace:Clock.Tizen.Mobile.Controls;assembly=Clock.Tizen.Mobile"
-             xmlns:viewmodels="clr-namespace:Clock.ViewModel;assembly=Clock"
+             xmlns:viewmodels="clr-namespace:Clock.ViewModels;assembly=Clock"
              xmlns:converters="clr-namespace:Clock.Tools.Converters;assembly=Clock"
              x:Class="Clock.Tizen.Mobile.Views.TimerView">
 
index a4c076c..790c294 100644 (file)
@@ -27,7 +27,7 @@ namespace Clock.Abstractions
         /// <summary>
         /// Unique id of the alarm.
         /// </summary>
-        int Id { get; set; }
+        int Id { get; }
 
         /// <summary>
         /// Cancels this alarm.
index 8c91fa7..60c242d 100644 (file)
@@ -26,11 +26,11 @@ namespace Clock.Model
     {
         Never = 0,
         Sunday = 1,
-        Monday = 2,
-        Tuesday = 4,
-        Wednesday = 8,
-        Thursday = 16,
-        Friday = 32,
-        Saturday = 64
+        Monday = 1 << 2,
+        Tuesday = 1 << 3,
+        Wednesday = 1 << 4,
+        Thursday = 1 << 5,
+        Friday = 1 << 6,
+        Saturday = 1 << 7
     }
 }
\ No newline at end of file
index b2d8048..7905051 100644 (file)
@@ -8,7 +8,7 @@ using Xamarin.Forms;
 
 namespace Clock.Tools.Converters
 {
-    class CollectionToBooleanConverter : IValueConverter
+    public class CollectionToBooleanConverter : IValueConverter
     {
         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {