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.
17 using LibTVRefCommonPortable.DataModels;
18 using LibTVRefCommonPortable.Utils;
19 using System.Collections.Generic;
21 using System.Threading.Tasks;
24 namespace TVApps.ViewModels
27 /// A class contains installed, pinned application list.
28 /// The MainPageViewModel changes state of application item by AppsHolder's API
30 internal class AppsHolder
33 /// The interface for interaction with view model
35 IAppsViewModel ViewModel;
38 /// A flag indicates how to sort InstalledApps
40 /// <see cref="SortingOptions"/>
41 public SortingOptions SortingOption;
44 /// A list contains AppShortcutInfo about installed applications
46 /// <see cref="AppShortcutInfo"/>
47 public List<AppShortcutInfo> InstalledApps;
50 /// A dictionary contains ID of pinned applications
52 public Dictionary<string, string> PinnedApps;
54 public string PinnedAppName;
55 public string UnpinnedAppName;
59 /// Initializes installed and pinned app list
61 /// <param name="ViewModel">The instance of MainPageViewModel</param>
62 public AppsHolder(IAppsViewModel ViewModel)
64 this.ViewModel = ViewModel;
66 InstalledApps = new List<AppShortcutInfo>();
67 PinnedApps = new Dictionary<string, string>();
69 App.SetAppInstalledListener((s, e) =>
74 App.SetAppUninstalledListener((s, e) =>
83 /// A method gets installed, pinned application information from AppShortcutController.
84 /// And adds state descriptions about pin, delete, long press state
86 private async void SetApps()
88 var pinnedAppsGettingTask = TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetPinnedAppsAppIDs();
89 var installedAppsGettingTask = TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetInstalledApps();
91 await Task.WhenAll(pinnedAppsGettingTask, installedAppsGettingTask);
93 PinnedApps = pinnedAppsGettingTask.Result;
94 ViewModel.OnPropertyChanged("SumOfCheckedApp");
96 var installedApps = installedAppsGettingTask.Result;
98 foreach (AppShortcutInfo item in installedApps)
100 if (item.CurrentStateDescription == null)
102 DebuggingUtils.Err("Invalid AppshortcutInfo, " + item.AppID);
106 item.IsPinned = PinnedApps.ContainsKey(item.AppID);
108 var pinStateDescription = new StateDescription()
110 Label = item.CurrentStateDescription.Label,
111 IconPath = item.CurrentStateDescription.IconPath,
112 Action = new CommandAction()
114 NextStateDescription = AppsStatus.Pin.ToString().ToLower(),
115 Command = new Command<string>((key) =>
119 CommandParameter = item.AppID
123 item.StateDescriptions.Add(AppsStatus.Pin.ToString().ToLower(), pinStateDescription);
125 var deleteStateDescription = new StateDescription()
127 Label = item.CurrentStateDescription.Label,
128 IconPath = item.CurrentStateDescription.IconPath,
129 Action = new CommandAction()
131 NextStateDescription = AppsStatus.Delete.ToString().ToLower(),
132 Command = new Command<string>((key) =>
136 CommandParameter = item.AppID
140 item.StateDescriptions.Add(AppsStatus.Delete.ToString().ToLower(), deleteStateDescription);
142 var longPressStateDescription = new StateDescription()
144 Label = item.CurrentStateDescription.Label,
145 IconPath = item.CurrentStateDescription.IconPath,
146 Action = new CommandAction()
148 NextStateDescription = AppsStatus.LongPress.ToString().ToLower(),
149 Command = new Command<string>((key) =>
153 CommandParameter = item.AppID
156 item.StateDescriptions.Add(AppsStatus.LongPress.ToString().ToLower(), longPressStateDescription);
159 InstalledApps = installedApps.ToList();
160 SortApps(SortingOption);
161 UpdateStateDescription(ViewModel.CurrentStatus);
165 /// A method changes pin state of AppShortcutInfo
166 /// And adds or removes application ID in PinnedApps
168 /// <param name="appID">The ID of application for changing pin state</param>
169 private void PinToggle(string appID)
171 AppShortcutInfo selectedApp = InstalledApps.FirstOrDefault(a => a.AppID == appID);
172 if (selectedApp == null)
174 DebuggingUtils.Err("Failed to Pin!!!, Nothing selected, AppID = " + appID);
178 if (PinnedApps.ContainsKey(appID))
180 DebuggingUtils.Dbg("UnPin! : " + selectedApp);
181 selectedApp.IsChecked = false;
182 selectedApp.IsPinned = false;
183 PinnedApps.Remove(appID);
184 UnpinnedAppName = selectedApp.CurrentStateDescription.Label;
185 ViewModel.OnPropertyChanged("UnpinnedAppName");
189 DebuggingUtils.Dbg("Pin! : " + selectedApp);
190 selectedApp.IsChecked = true;
191 selectedApp.IsPinned = true;
192 PinnedApps.Add(appID, appID);
193 PinnedAppName = selectedApp.CurrentStateDescription.Label;
194 ViewModel.OnPropertyChanged("PinnedAppName");
198 ViewModel.OnPropertyChanged("SumOfCheckedApp");
202 /// A method changes pin state of AppShortcutInfo by the Option Menu Pin/Unpin button
203 /// And adds or removes application ID in PinnedApps
205 /// <param name="appID">The ID of application for changing pin state</param>
206 public void OptionMenuPinToggle(string appID)
208 AppShortcutInfo SelectedApp = InstalledApps.FirstOrDefault(a => a.AppID.Equals(appID));
209 if (SelectedApp == null)
211 DebuggingUtils.Err("Failed to get selected app : " + appID);
215 if (PinnedApps.ContainsKey(appID))
217 DebuggingUtils.Dbg("Unpin : " + appID);
218 SelectedApp.IsPinned = false;
219 PinnedApps.Remove(appID);
223 DebuggingUtils.Dbg("Pin : " + appID);
224 SelectedApp.IsPinned = true;
225 PinnedApps.Add(appID, appID);
228 ViewModel.OnPropertyChanged("SumOfCheckedApp");
230 this.UpdatePinnedApps();
231 ViewModel.ChangeCurrentStatus(AppsStatus.Default);
235 /// Removes the pinned app by app ID
237 /// <param name="removableAppsList">The apps list to remove at the pinned app list</param>
238 public void RemovePinnedApp(List<string> removableAppsList)
240 foreach (string app in removableAppsList)
242 if (PinnedApps.ContainsKey(app))
244 DebuggingUtils.Dbg("UnPin! : " + app);
246 PinnedApps.Remove(app);
254 /// A method sends application delete request
255 /// If application is pinned, removes in PinnedApps
257 /// <param name="appID">The ID of application for deleting</param>
258 public void CheckDeleteApp(string appID)
260 DebuggingUtils.Dbg("check that the app( " + appID + ") can be deleted");
262 if (!ApplicationManagerUtils.GetAppInfoRemovable(appID))
264 DebuggingUtils.Dbg("This app is not removable : " + appID);
268 string appLabel = null;
269 if (ApplicationManagerUtils.GetInstalledApplication(appID).TryGetValue("Label", out appLabel))
271 ViewModel.ShowDeletePopup(appLabel);
275 public void DeleteApp(string appID)
277 // if the app to be deleted is pinned, remove the app in the pinned apps list
278 string pkgID = PackageManagerUtils.GetPackageIDByAppID(appID);
281 DebuggingUtils.Err("Failed to get the package ID by app ID(" + appID + ")");
286 List<string> appsList = PackageManagerUtils.GetApplicationsByPkgID(pkgID);
287 if (appsList == null || appsList.Count == 0)
289 DebuggingUtils.Err("Failed to get the apps list by package ID(" + pkgID + ")");
294 ViewModel.ChangeCurrentStatus(AppsStatus.Default);
296 if (PackageManagerUtils.UninstallPackageByAppID(appID) == false)
298 DebuggingUtils.Err("App uninstall is failed!!!, " + appID);
302 var removed = from app in InstalledApps
303 where app.AppID == appID
305 InstalledApps.Remove(removed.First());
306 foreach (var item in InstalledApps)
308 DebuggingUtils.Dbg("- " + item.AppID);
311 RemovePinnedApp(appsList);
317 /// A method changes long press state of AppShortcutInfo
319 /// <param name="AppID">The ID of application for showing option menu</param>
320 private void LongPressApp(string AppID)
323 DebuggingUtils.Dbg(" +++++ long press app : " + AppID);
327 /// A method updates state description of applications
329 /// <param name="status">The state name for updating state description</param>
330 public void UpdateStateDescription(AppsStatus status)
332 string tag = status.ToString().ToLower();
334 // TODO : remove this code. this code is temporary.
335 if (status == AppsStatus.LongPress)
337 AppShortcutInfo focuesedApp = InstalledApps.Find(a => a.IsFocused);
338 if (focuesedApp == null)
340 DebuggingUtils.Dbg("FocusedApp is not exist. Change app status to default");
341 ViewModel.ChangeCurrentStatus(AppsStatus.Default);
346 DebuggingUtils.Dbg("AppsListStateUpdate, status = " + status.ToString() + ", tag = " + tag);
347 foreach (AppShortcutInfo item in InstalledApps)
349 item.CurrentStateDescription = item.StateDescriptions[tag];
352 case AppsStatus.Default:
353 item.IsChecked = false;
355 item.IsShowOptions = false;
358 item.IsChecked = item.IsPinned;
360 case AppsStatus.LongPress:
363 item.IsShowOptions = true;
371 case AppsStatus.Delete:
372 if (!item.IsRemovable)
377 item.IsChecked = false;
381 item.IsPinned = PinnedApps.ContainsKey(item.AppID);
384 ViewModel.OnPropertyChanged("InstalledAppList");
388 /// A method refresh InstalledApps
390 private void RefreshApps()
392 // Only updating elements of InstalledApps is not update the GUI.
393 InstalledApps = new List<AppShortcutInfo>(InstalledApps);
394 ViewModel.OnPropertyChanged("InstalledAppList");
398 /// A method sorts InstalledApps according to parameter
400 /// <param name="sortOption">The option for sorting InstalledApps</param>
401 /// <see cref="SortingOptions"></see>
402 public void SortApps(SortingOptions sortOption)
404 AppsListSorter.GetSortedAppsList(sortOption, ref InstalledApps);
409 /// A method reloads PinnedApps from AppShortcutController
411 public async void ResetPinnedApps()
413 PinnedApps = await TVHomeImpl.GetInstance.AppShortcutControllerInstance.GetPinnedAppsAppIDs();
417 /// A method updates list about pinned application to AppShortcutController
419 public void UpdatePinnedApps()
421 List<AppShortcutInfo> pinnedAppList = new List<AppShortcutInfo>();
422 foreach (var item in PinnedApps)
424 pinnedAppList.Add(new AppShortcutInfo()
430 TVHomeImpl.GetInstance.AppShortcutControllerInstance.UpdatePinnedApps(pinnedAppList);
434 /// A method changes visible state of option menu to true
436 /// <param name="appId">The ID of application for showing option menu</param>
437 public void ShowLongPressOption(string appId)
439 AppShortcutInfo longPressedApp = InstalledApps.Find(app => app.AppID.Equals(appId));
440 if (longPressedApp != null)
442 longPressedApp.IsDim = false;
443 longPressedApp.IsShowOptions = true;
448 /// A method changes visible state of option menu to false
450 /// <param name="appId">The ID of application for hiding option menu</param>
451 public void HideLongPressOption(string appId)
453 AppShortcutInfo longPressedApp = InstalledApps.Find(app => app.AppID.Equals(appId));
454 if (longPressedApp != null)
456 longPressedApp.IsShowOptions = false;