2 * Copyright (c) 2017 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
21 using LibTVRefCommonPortable.DataModels;
22 using LibTVRefCommonPortable.Utils;
25 namespace TVHome.ViewModels
28 /// A HomeStatus type enumeration
30 public enum HomeStatus
40 /// A HomeMenuItem type enumeration
42 public enum HomeMenuItem
51 /// A class for ViewModel of TV Home
53 public class MainPageViewModel : INotifyPropertyChanged, IHomeViewModel
56 /// Gets or set MainList for MainPanel
58 public IEnumerable<ShortcutInfo> MainList { get; set; }
60 /// Gets or set RecentList for Recent SubPanel
62 public IEnumerable<ShortcutInfo> RecentList { get; set; }
64 /// Gets or set AppList for Apps SubPanel
66 public IEnumerable<ShortcutInfo> AppList { get; set; }
68 /// Gets or set SettingsList for Settings SubPanel
70 public IEnumerable<ShortcutInfo> SettingsList { get; set; }
73 /// A command for executing when MainPanel is focused.
75 public Command MainPanelFocusedCommand { get; set; }
77 /// A command for executing when SubPanel is focused.
79 public Command SubPanelFocusedCommand { get; set; }
81 /// A command for changing the status to Move status
83 public Command SetMoveStatusCommand { get; set; }
85 /// A command for changing the status to Unpin status
87 public Command SetUnpinStatusCommand { get; set; }
89 public Command OnMoveCommand { get; set; }
90 public Command OnUnpinCommand { get; set; }
91 public Command OnClearCommand { get; set; }
92 public Command OnClearAllCommand { get; set; }
95 /// Gets or set CurrentStatus of HomeStatus
97 public HomeStatus CurrentStatus { get; private set; }
99 /// Gets or set SelectedMenuName of HomeMenuItem
101 public HomeMenuItem SelectedMenuName { get; private set; }
104 /// A flag indicates whether "No recent info" in Recent SubPanel to be displayed or not
106 public bool IsShowNoRecentContents { get; set; }
109 /// A event that is occurred when property of MainPageViewModel is changed
111 public event PropertyChangedEventHandler PropertyChanged;
114 /// Main panel icon's width
116 public int MainPanelIconWidth
120 return SizeUtils.GetWidthSize(236);
125 /// Sub panel's padding size
127 public Thickness SubPanelPadding
131 int lr = SizeUtils.GetWidthSize(74);
132 return new Thickness(lr, 0, lr, SizeUtils.GetWidthSize(26));
137 /// Sub panel's spacing size
139 public int SubPanelSpacing
143 // TODO : set proper size
144 return SizeUtils.GetWidthSize(34);
149 /// Information text font size.
151 public int NoContentInformationFontSize
155 return SizeUtils.GetFontSize(28);
160 /// Sub thumbnail panel's padding size
162 public Thickness SubThumbNailPanelPadding
166 return new Thickness(SizeUtils.GetHeightSize(223), SizeUtils.GetWidthSize(32));
172 /// Initialize MainPanel, SubPanel, Command and EventListeners
174 public MainPageViewModel()
177 // MainPage ( MainPageViewModel -> MainPanel -> SubPanel )
182 MakeSettingsButtons();
184 UpdateAppList(null, null);
186 TVHomeImpl.GetInstance.AppShortcutControllerInstance.AddFileSystemChangedListener(UpdateAppList);
188 App.SetAppPinnedNotificationListener((s, e) =>
190 // TODO : Make this for Move a pinned app/Show pinned apps(scroll to last)
191 ChangeCurrentStatus(HomeStatus.SubPanelFocused);
192 ChangeSelectedPanelName(HomeMenuItem.Apps);
193 if (e.arg.Length > 0)
195 DebuggingUtils.Dbg("Move, AppID : " + e.arg);
199 DebuggingUtils.Dbg("Show, Pinned Apps");
203 MessagingCenter.Subscribe<App, TVHomeStatus>(this, App.AppStatus, (sender, arg) =>
207 case TVHomeStatus.OnResume:
210 case TVHomeStatus.OnSleep:
211 ChangeCurrentStatus(HomeStatus.Default, true);
218 /// A method for initializing HomeStatus to Default
220 private void InitStatus()
222 ChangeCurrentStatus(HomeStatus.Default, true);
226 /// A method for initializing commands
228 private void InitCommands()
230 MainPanelFocusedCommand = new Command<HomeMenuItem>((key) =>
232 DebuggingUtils.Dbg(key.ToString());
233 ChangeCurrentStatus(HomeStatus.MainPanelFocused);
234 ChangeSelectedPanelName(key);
237 SubPanelFocusedCommand = new Command(() =>
239 ChangeCurrentStatus(HomeStatus.SubPanelFocused);
242 SetMoveStatusCommand = new Command(() =>
244 ChangeCurrentStatus(HomeStatus.Move);
247 SetUnpinStatusCommand = new Command(() =>
249 ChangeCurrentStatus(HomeStatus.UnPin);
252 OnMoveCommand = new Command<List<View>>((moveList) =>
254 MoveAppShortcutInfo(moveList);
257 OnUnpinCommand = new Command<string>((appId) =>
259 UnpinAppShortcutInfo(appId);
262 OnClearCommand = new Command<string>((appId) =>
264 RemoveRecentApplication(appId);
267 OnClearAllCommand = new Command(() =>
269 ClearAllRecentApplications();
274 /// A method for makeing MainMenu items and display in MainPanel
276 private void MakeMainMenuItems()
278 string[] AppName = { "Recent", "Apps", "Settings" };
279 // TODO : Revert this before release
280 //string[] AppControlID = { "org.tizen.settings", "org.tizen.apps", "org.tizen.settings" };
281 string[] AppControlID = { "org.tizen.xaapps", "org.tizen.xaapps", "org.tizen.settings" };
282 string[] AppIconPath =
284 "ic_tizen_home_menu_{0}_normal.png",
285 "ic_tizen_home_menu_{0}_focused.png",
286 "ic_tizen_home_menu_{0}_selected.png",
287 "ic_tizen_home_menu_{0}_unselect.png"
290 List<ShortcutInfo> TempList = new List<ShortcutInfo>();
291 for (int i = 0; i < AppName.Length; i++)
293 AppControlAction appControlAction = new AppControlAction()
295 AppID = AppControlID[i]
298 ShortcutInfo shortcutInfo = new HomeMenuAppShortcutInfo()
307 IconPath = String.Format(AppIconPath[0], AppName[i].ToLower()),
308 Action = appControlAction,
316 IconPath = String.Format(AppIconPath[1], AppName[i].ToLower()),
317 Action = appControlAction,
325 IconPath = String.Format(AppIconPath[2], AppName[i].ToLower()),
326 Action = appControlAction,
334 IconPath = String.Format(AppIconPath[3], AppName[i].ToLower()),
335 Action = appControlAction,
340 shortcutInfo.UpdateState();
341 TempList.Add(shortcutInfo);
345 OnPropertyChanged("MainList");
349 /// Gets default AppList for displaying items and updates the list to Apps SubPanel
351 private void SetDefaultAppList()
353 AppList = TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetDefaultShortcuts();
354 OnPropertyChanged("AppList");
358 /// Gets the AppList for displaying items and updates the list to Apps SubPanel
360 /// <param name="sender">The source of event</param>
361 /// <param name="e">The event that is occurred pinned app file is changed</param>
362 private async void UpdateAppList(object sender, EventArgs e)
364 AppList = await TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetPinnedAppsWithDefaultShortcuts();
365 OnPropertyChanged("AppList");
369 /// Gets the SettingsList for displaying items and updates the list to Settings SubPanel
371 private void MakeSettingsButtons()
373 string[] ShortCutLabel = { "Brightness", "Contrast", "Color", "Tint" };
375 List<ShortcutInfo> TempList = new List<ShortcutInfo>();
377 ShortcutInfo Settings = new SettingShortcutInfo()
385 Label = "All Settings",
386 IconPath = "ic_home_settings_all_138.png",
387 Action = new AppControlAction()
389 AppID = "org.tizen.settings"
397 Label = "All Settings",
398 IconPath = "ic_home_settings_all_182.png",
399 Action = new AppControlAction()
401 AppID = "org.tizen.settings"
407 Settings.UpdateState();
408 TempList.Add(Settings);
410 for (int i = 0; i < ShortCutLabel.Length; i++)
412 AppControlAction appControlAction = new AppControlAction()
414 AppID = "org.tizen.settings",
416 appControlAction.ExtraData.Add("subview", ShortCutLabel[i].ToLower());
418 ShortcutInfo shortcutInfo = new SettingShortcutInfo()
426 Label = ShortCutLabel[i],
427 IconPath = "ic_home_settings_" + ShortCutLabel[i].ToLower() + "_138.png",
428 Action = appControlAction,
435 Label = ShortCutLabel[i],
436 IconPath = "ic_home_settings_" + ShortCutLabel[i].ToLower() + "_182.png",
437 Action = appControlAction,
442 shortcutInfo.UpdateState();
443 TempList.Add(shortcutInfo);
446 SettingsList = TempList;
447 OnPropertyChanged("SettingsList");
451 /// Gets the RecentList for displaying items and updates the list to Recent SubPanel
453 private void MakeRecentButtons()
455 RecentList = TVHomeImpl.GetInstance.RecentShortcutControllerInstance.GetList();
456 if (RecentList.Count<ShortcutInfo>() > 0)
458 IsShowNoRecentContents = false;
459 OnPropertyChanged("RecentList");
463 IsShowNoRecentContents = true;
466 OnPropertyChanged("IsShowNoRecentContents");
470 /// Clears all recent applications and updates the list to Recent SubPanel
472 private void ClearAllRecentApplications()
474 TVHomeImpl.GetInstance.RecentShortcutControllerInstance.RemoveAll();
479 /// Removes specified recent application and updates the list to Recent SubPanel
481 /// <param name="appId">A application ID</param>
482 private void RemoveRecentApplication(string appId)
484 TVHomeImpl.GetInstance.RecentShortcutControllerInstance.Remove(appId);
489 /// A method for invoking PropertyChanged event
491 /// <param name="name">The name of property</param>
492 public void OnPropertyChanged(string name)
494 PropertyChangedEventHandler handler = PropertyChanged;
497 handler(this, new PropertyChangedEventArgs(name));
502 /// A method for changing CurrentStatus according to parameter
504 /// <param name="newStatus">The next status name</param>
505 /// <param name="isForceUpdate">A flag indicates whether CurrentStatus to be changed or not</param>
506 public void ChangeCurrentStatus(HomeStatus newStatus, bool isForceUpdate = false)
509 CurrentStatus.CompareTo(newStatus) != 0)
511 CurrentStatus = newStatus;
512 if (CurrentStatus.CompareTo(HomeStatus.Default) == 0)
514 foreach (HomeMenuAppShortcutInfo item in MainList)
516 if (item.CurrentStateDescription.Label.CompareTo("Apps") == 0)
518 DebuggingUtils.Dbg(item.CurrentStateDescription.Label + " To Focused");
519 item.ChangeStatus("focused");
523 DebuggingUtils.Dbg(item.CurrentStateDescription.Label + " To Default");
524 item.ChangeStatus("default");
528 ChangeSelectedPanelName(HomeMenuItem.Apps, isForceUpdate);
530 else if (CurrentStatus.CompareTo(HomeStatus.MainPanelFocused) == 0)
532 HomeMenuItem index = HomeMenuItem.Recent;
533 foreach (HomeMenuAppShortcutInfo item in MainList)
535 if (index == SelectedMenuName)
537 DebuggingUtils.Dbg(index.ToString() + " To Focused");
538 item.ChangeStatus("focused");
542 DebuggingUtils.Dbg(index.ToString() + " To Default");
543 item.ChangeStatus("default");
549 else if (CurrentStatus.CompareTo(HomeStatus.SubPanelFocused) == 0)
551 HomeMenuItem index = HomeMenuItem.Recent;
552 foreach (HomeMenuAppShortcutInfo item in MainList)
554 if (index == SelectedMenuName)
556 DebuggingUtils.Dbg(index.ToString() + " To be selected");
557 item.ChangeStatus("selected");
561 DebuggingUtils.Dbg(index.ToString() + " To be unselect");
562 item.ChangeStatus("unselect");
569 OnPropertyChanged("CurrentStatus");
574 /// A method for changing selected HomeMenuItem according to parameter
576 /// <param name="panelName">Selected menu name in MainPanel</param>
577 /// <param name="isForceUpdate">A flag indicates whether selected menu name to be changed or not</param>
578 public void ChangeSelectedPanelName(HomeMenuItem panelName, bool isForceUpdate = false)
581 SelectedMenuName.CompareTo(panelName) != 0)
583 SelectedMenuName = panelName;
584 HomeMenuItem index = HomeMenuItem.Recent;
585 foreach (HomeMenuAppShortcutInfo item in MainList)
587 if (index == panelName)
589 DebuggingUtils.Dbg(index.ToString() + " To Focused");
590 item.ChangeStatus("focused");
594 DebuggingUtils.Dbg(index.ToString() + " To Default");
595 item.ChangeStatus("default");
601 OnPropertyChanged("SelectedMenuName");
605 private void UnpinAppShortcutInfo(string appId)
607 RemovePinnedApp(appId);
610 private void UpdatePinnedApps(Dictionary<string, string> pinnedApps)
612 List<AppShortcutInfo> pinnedAppList = new List<AppShortcutInfo>();
613 foreach (var item in pinnedApps)
615 pinnedAppList.Add(new AppShortcutInfo()
621 TVHomeImpl.GetInstance.AppShortcutControllerInstance.UpdatePinnedApps(pinnedAppList);
624 private async void RemovePinnedApp(string appID)
626 Dictionary<string, string> PinnedApps = await TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetPinnedAppsAppIDs();
627 if (PinnedApps.ContainsKey(appID))
629 PinnedApps.Remove(appID);
630 UpdatePinnedApps(PinnedApps);
635 private void MoveAppShortcutInfo(List<View> moveList)
637 List<AppShortcutInfo> pinnedAppList = new List<AppShortcutInfo>();
638 AppShortcutInfo appShortcut;
640 foreach (var item in moveList)
642 appShortcut = (AppShortcutInfo)item.BindingContext;
643 if (appShortcut.AppID != null && appShortcut.AppID.Length > 0)
645 pinnedAppList.Add(new AppShortcutInfo()
647 AppID = appShortcut.AppID,
652 TVHomeImpl.GetInstance.AppShortcutControllerInstance.UpdatePinnedApps(pinnedAppList);