Exemplo n.º 1
0
        private void AddToMyApps(ApplicationViewModel applicationViewModel, bool saveLocalStorage = true)
        {
            var application = applicationViewModel.Application;

            application.PinnedToTaskbar = true;

            lock (ServiceLocator.LocalStorage.Locker)
            {
                if (application.IsLocalApp)
                {
                    ServiceLocator.LocalStorage.InstalledLocalApps.Add(application);
                }
                else
                {
                    ServiceLocator.LocalStorage.InstalledAppDirectApps.Add(application);
                }

                if (saveLocalStorage)
                {
                    ServiceLocator.LocalStorage.SaveAppSettings();
                }
            }

            Helper.PerformInUiThread(() => MyApplications.Add(applicationViewModel));

            if (ApplicationAddedNotifier != null)
            {
                ApplicationAddedNotifier(applicationViewModel, null);
            }
        }
Exemplo n.º 2
0
        private void RemoveFromMyApps(ApplicationViewModel applicationViewModel, bool saveLocalStorage = true)
        {
            lock (ServiceLocator.LocalStorage.Locker)
            {
                if (applicationViewModel.Application.IsLocalApp)
                {
                    ServiceLocator.LocalStorage.InstalledLocalApps.Remove(applicationViewModel.Application);
                }
                else
                {
                    ServiceLocator.LocalStorage.InstalledAppDirectApps.Remove(applicationViewModel.Application);
                }

                if (saveLocalStorage)
                {
                    ServiceLocator.LocalStorage.SaveAppSettings();
                }
            }

            Helper.PerformInUiThread(() => MyApplications.Remove(applicationViewModel));

            if (ApplicationRemovedNotifier != null)
            {
                ApplicationRemovedNotifier(applicationViewModel, null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to sync MyApplications with API. Default behavior ignores exceptions.  Will fail if user is logged in and the API can not be reached.
        /// </summary>
        public void SyncMyApplications(bool throwExceptions = false, bool forceAuthentication = false)
        {
            try
            {
                var apiApps = new List <Application>();

                if (forceAuthentication || !ServiceLocator.CachedAppDirectApi.IsAuthenticated)
                {
                    bool loginSuccessful = false;
                    try
                    {
                        loginSuccessful = Helper.Authenticate();
                    }
                    catch (Exception e)
                    {
                        Logout();
                        _log.ErrorException("Exception thrown by authentication", e);
                    }

                    if (IsLoggedIn && !loginSuccessful)
                    {
                        Logout();
                    }
                }

                if (ServiceLocator.CachedAppDirectApi.IsAuthenticated)
                {
                    apiApps = ServiceLocator.CachedAppDirectApi.MyApps.ToList();
                    ServiceLocator.BrowserWindowsCommunicator.UpdateApplications(apiApps.Cast <IApplication>().ToList());
                }

                var displayedApps = MyApplications.Select(a => a.Application).ToList();

                var newApps     = new List <Application>();
                var expiredApps = displayedApps.Where(a => !a.IsLocalApp && a.Status != DisplayStatus.ApiCallInProgress).Except(apiApps).ToList();

                apiApps = apiApps.OrderByDescending(a => a.SubscriptionId).Distinct().ToList();

                foreach (var application in apiApps)
                {
                    var matchedApp = MyApplications.FirstOrDefault(a => a.Application.Equals(application));
                    if (application.Status >= DisplayStatus.Cancelled)//Remove applications that have been cancelled or are no longer active
                    {
                        if (matchedApp != null)
                        {
                            expiredApps.Add(application);
                        }

                        continue;
                    }

                    if (matchedApp != null)//Update active applications
                    {
                        matchedApp.Application.UrlString = application.UrlString;

                        //Only change the status to active after the URL has been reset by myApps API call
                        matchedApp.Application.SubscriptionId = application.SubscriptionId;
                        matchedApp.ApplicationStatus          = application.Status;
                    }
                    else//Add new applications
                    {
                        newApps.Add(application);
                    }
                }

                foreach (var application in expiredApps)
                {
                    var applicationViewModel = MyApplications.FirstOrDefault(a => a.Application.Equals(application));
                    RemoveFromMyApps(applicationViewModel, false);
                }

                if (IsLoggedIn)
                {
                    foreach (var application in newApps)
                    {
                        application.LocalImagePath = ServiceLocator.LocalStorage.SaveAppIcon(application.ImagePath,
                                                                                             application.Id);
                        AddToMyApps(new ApplicationViewModel(application), false);
                    }
                }

                lock (ServiceLocator.LocalStorage.Locker)
                {
                    ServiceLocator.LocalStorage.SaveAppSettings();
                }
            }
            catch (Exception e)
            {
                if (throwExceptions)
                {
                    throw;
                }
                else
                {
                    _log.ErrorException("Sync error", e);
                }
            }
        }