Ejemplo n.º 1
0
 void _smartOfflineService_OffliningOpportunity(OffliningOpportunityPriority arg1, NetworkConnectivityStatus arg2, System.Threading.CancellationToken arg3)
 {
     if ((DateTime.Now - _lastCheckedMessages).TotalMinutes > 15)
     {
         _lastCheckedMessages = DateTime.Now;
         GetMessages();
     }
 }
        async void _smartOfflineService_OffliningOpportunity(OffliningOpportunityPriority priority, NetworkConnectivityStatus networkStatus, System.Threading.CancellationToken token)
        {
            if (_inflightOfflining)
                return;

            if (!_settingsService.AllowPredictiveOfflining)
                return;

            _inflightOfflining = true;
            try
            {
                if (!_smartOfflineService.IsActivityIdle && priority == OffliningOpportunityPriority.Image)
                {
                    //we're probably looking at images right now, possibly in a gallery so grab as much as possible
                    //metered or not doesnt matter here since they were going to download the image anyway
                    foreach (var offlinableImage in _smartOfflineService.OfflineableImagesFromContext)
                    {
                        if (token.IsCancellationRequested)
                            break;

                        await ImageBytesFromUrl(offlinableImage);
                    }
                    return;
                }


                //if we're on unmetered we can be pretty agressive about our downloading
                //otherwise just download the thumbnails and make the api calls since the platform default behavior would have been to download them anyway
                if (networkStatus == NetworkConnectivityStatus.Unmetered)
                {
                    if (token.IsCancellationRequested)
                        return;

                    string targetImageToOffline = null;
                    lock (_waitingOfflineImages)
                    {
                        if (_waitingOfflineImages.Count == 0)
                            foreach (var item in _smartOfflineService.OfflineableImagesFromContext.Reverse())
                                _waitingOfflineImages.Push(item);

                        if(_waitingOfflineImages.Count > 0)
                            targetImageToOffline = _waitingOfflineImages.Pop();
                    }

                    if(targetImageToOffline != null)
                        await ImageBytesFromUrl(targetImageToOffline);
                }

                if (token.IsCancellationRequested)
                    return;

                string targetAPIToOffline = null;
                do
                {
                    lock (_waitingOfflineAPI)
                    {
                        if (_waitingOfflineAPI.Count == 0 && targetAPIToOffline == null)
                            foreach (var item in _smartOfflineService.OfflineableImageAPIsFromContext.Reverse())
                                _waitingOfflineAPI.Push(item);

                        if (_waitingOfflineAPI.Count > 0)
                            targetAPIToOffline = _waitingOfflineAPI.Pop();
                        else
                            targetAPIToOffline = null;
                    }

                    if (targetAPIToOffline != null)
                    {
                        if (_urlsOfflined.Contains(targetAPIToOffline))
                            continue;
                        else
                            _urlsOfflined.Add(targetAPIToOffline);

                        await GetImagesFromUrl("", targetAPIToOffline);
                    }

                    if (token.IsCancellationRequested)
                        return;

                } while (targetAPIToOffline != null);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _inflightOfflining = false;
            }
        }
        async void _smartOfflineService_OffliningOpportunity(OffliningOpportunityPriority priority, NetworkConnectivityStatus networkStatus, CancellationToken token)
        {
            if (!_settingsService.AllowPredictiveOfflining || (DateTime.Now - _nextOffliningTime).TotalMinutes < 10)
                return;

            try
            {
                //good chance to try and process our delayed actions
                //we dont want to do to much if we've got actions to run and we're just in an idle state
                if (_smartOfflineService.IsActivityIdle)
                {
                    if (await RunPeriodic() || token.IsCancellationRequested)
                        return;
                }

                var user = await _userService.GetUser();
                if (user != null && (DateTime.Now - _lastMeCheck) >= TimeSpan.FromMinutes(10))
                {
                    _lastMeCheck = DateTime.Now;
                    user.Me = await GetMe(user);
                }

                if ((user != null && user.Me != null && user.Me.HasMail)
                    || !(await _offlineService.UserHasOfflineMessages(user)))
                {
                    await GetMessages(null);
                }

                if (_linkThingsAwaitingOfflining.Count > 0)
                {
                    while (!token.IsCancellationRequested && _linkThingsAwaitingOfflining.Count > 0)
                    {
                        var linkThingToOffline = _linkThingsAwaitingOfflining.Pop();
                        if (_recentlyLoadedComments.Contains(linkThingToOffline.Permalink))
                        {
                            _recentlyLoadedComments.Add(linkThingToOffline.Permalink);
                            OfflineComments(linkThingToOffline.SubredditId, linkThingToOffline.Permalink);
                        }
                    }
                }
                else
                {
                    HashSet<string> targetDomains = new HashSet<string>();
                    var newLinks = _smartOfflineService.OfflineableLinkThingsFromContext.Select(thing => thing.TypedData).ToList();

                    var domainAggs = await _offlineService.GetDomainAggregates(TopSubsetMaximum, 25);
                    var subredditAggs = await _offlineService.GetSubredditAggregates(TopSubsetMaximum, 25);

                    var linkTotal = subredditAggs.Sum(p => p.LinkClicks);
                    var commentTotal = subredditAggs.Sum(p => p.CommentClicks);
                    var hashes = domainAggs.Select(p => p.DomainHash);
                    var subs = subredditAggs.Select(p => p.SubredditId);

                    foreach (var domain in newLinks.Select(p => p.Domain).Distinct())
                    {
                        var hash = (uint)domain.GetHashCode();
                        if (hashes.Contains(hash))
                            targetDomains.Add(domain);
                    }

                    var filteredLinks = newLinks.Where(p => targetDomains.Contains(p.Domain) || subs.Contains(p.SubredditId)).ToList();
                    if (filteredLinks.Count < 5)
                    {
                        filteredLinks = newLinks;
                    }

                    if (filteredLinks.All(link => _recentlyLoadedComments.Contains(link.Permalink)))
                    {
                        _nextOffliningTime = DateTime.Now.AddMinutes(10);
                    }

                    _linkThingsAwaitingOfflining = new Stack<Link>(filteredLinks);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _isOfflining = false;
            }
        }
Ejemplo n.º 4
0
        async void BaconProvider_OffliningOpportunity(OffliningOpportunityPriority arg1, NetworkConnectivityStatus arg2, System.Threading.CancellationToken arg3)
        {
            try
            {
                var settingsService = _baconProvider.GetService<ISettingsService>();
                if (((DateTime.Now - settingsService.LastUpdatedImages).TotalHours > 4) &&
                    Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable &&
                    (Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication || ShellTile.ActiveTiles.FirstOrDefault() != null))
                {
                    await Utility.DoActiveLockScreen(_baconProvider.GetService<ISettingsService>(), _baconProvider.GetService<IRedditService>(),
                        _baconProvider.GetService<IUserService>(), _baconProvider.GetService<IImagesService>(), _baconProvider.GetService<INotificationService>(), true);

                    settingsService.LastUpdatedImages = DateTime.Now;
                }

                if (!arg3.IsCancellationRequested && (DateTime.Now - settingsService.LastCleanedCache).TotalDays > settingsService.OfflineCacheDays)
                {
                    await _baconProvider.GetService<IOfflineService>().CleanupAll(new TimeSpan(settingsService.OfflineCacheDays, 0, 0), arg3);
                    settingsService.LastCleanedCache = DateTime.Now;
                }
            }
            catch { }
        }