Inheritance: GalaSoft.MvvmLight.Messaging.MessageBase
Ejemplo n.º 1
0
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (pageState != null && pageState.ContainsKey("ScrollOffset"))
            {
                _scrollOffset = pageState["ScrollOffset"] as Nullable<double> ?? 0.0;

                linksView.Loaded += linksView_Loaded;
            }

            if (pageState != null && pageState.ContainsKey("SelectedSubredditMessage"))
            {
                _selectedSubredditMessage = pageState["SelectedSubredditMessage"] as SelectSubredditMessage;
                Messenger.Default.Send<SelectSubredditMessage>(_selectedSubredditMessage);
            }
            else if (navigationParameter != null)
            {
                if (navigationParameter is SelectSubredditMessage)
                {
                    _selectedSubredditMessage = navigationParameter as SelectSubredditMessage;
                    Messenger.Default.Send<SelectSubredditMessage>(_selectedSubredditMessage);
                }
                else if (navigationParameter is string)
                {
                    var navString = navigationParameter as string;
                    var thing = JsonConvert.DeserializeObject<Thing>(navString);
                    if (thing != null)
                    {
                        var link = thing.Data as Link;
                        var subreddit = thing.Data as Subreddit;

                        if (link != null)
                        {
                            var linkMessage = new NavigateToUrlMessage();
                            linkMessage.TargetUrl = link.Url;
                            linkMessage.Title = link.Title;
                            Messenger.Default.Send<NavigateToUrlMessage>(linkMessage);
                        }
                        else if (subreddit != null)
                        {
                            var selectSubreddit = new SelectSubredditMessage();
                            var typedSubreddit = new TypedThing<Subreddit>(new Thing { Kind = "t5", Data = subreddit });
                            selectSubreddit.Subreddit = new TypedThing<Subreddit>(typedSubreddit);
                            Messenger.Default.Send<SelectSubredditMessage>(selectSubreddit);
                        }
                    }
                }
            }
            else
            {
                Messenger.Default.Send<SelectSubredditMessage>(null);
            }
        }
Ejemplo n.º 2
0
		public void AssignSubreddit(SelectSubredditMessage message)
		{
			OnSubredditChanged(message);
		}
Ejemplo n.º 3
0
        private async void OnSubredditChanged(SelectSubredditMessage message)
        {
            if (_selectedSubreddit == null && message == null)
                return;
            if (message != null)
            {
                if (message.Subreddit == _selectedSubreddit)
                {
                    return;
                }

                _selectedSubreddit = message.Subreddit;
                SelectedLink = null;
				if (!message.DontRefresh)
				{
					_links = null;
					RefreshLinks();
				}

				Heading = _selectedSubreddit.Data.DisplayName;

                RaisePropertyChanged("DisplayingSubreddit");
                var currentUser = await _userService.GetUser();
                if (currentUser != null && currentUser.Me != null)
                {
                    _subscribed = (await _redditService.GetSubscribedSubreddits()).Contains(_selectedSubreddit.Data.Name);
                    RaisePropertyChanged("NotSubscribed");
                    RaisePropertyChanged("Subscribed");
                }
            }
            else
            {
                //set us back to default state
                _selectedSubreddit = null;
                SelectedLink = null;
                _links = null;
                Heading = null;
                RaisePropertyChanged("DisplayingSubreddit");
                _subscribed = false;
                RaisePropertyChanged("NotSubscribed");
                RaisePropertyChanged("Subscribed");

            }
        }
Ejemplo n.º 4
0
		public async Task LoadSubreddits()
		{
            try
            {
                var subreddits = await _offlineService.RetrieveOrderedThings("pivotsubreddits", TimeSpan.FromDays(1024));

                //PivotItems.Add(new SubredditSelectorViewModel(_baconProvider));

                if (subreddits == null || subreddits.Count() == 0)
                    subreddits = new List<TypedThing<Subreddit>> { new TypedThing<Subreddit>(ThingUtility.GetFrontPageThing()) };

                foreach (var sub in subreddits)
                {
                    if (sub.Data is Subreddit && (((Subreddit)sub.Data).Id != null || ((Subreddit)sub.Data).Url.Contains("/m/")))
                    {
                        var message = new SelectSubredditMessage();
                        message.Subreddit = new TypedThing<Subreddit>(sub);
                        message.DontRefresh = true;
                        ChangeSubreddit(message, false);
                    }
                }


                _subreddits.CollectionChanged += _subreddits_CollectionChanged;

                Messenger.Default.Send<SelectIndexMessage>(
                    new SelectIndexMessage
                    {
                        TypeContext = typeof(MainPageViewModel),
                        Index = 0
                    }
                );
            }
            catch 
            {
                _notificationService.CreateNotification("Failed loading subreddits list, file corruption may be present");
            }
		}
Ejemplo n.º 5
0
        private void ChangeSubreddit(SelectSubredditMessage message, bool fireSubredditsChanged = true)
        {
            int indexToPosition;
            bool foundExisting = FindSubredditMessageIndex(message, out indexToPosition);

            if (!foundExisting)
            {
                var newReddit = new RedditViewModel(_baconProvider);
                newReddit.DetachSubredditMessage();
                newReddit.AssignSubreddit(message);
                
                if (PivotItems.Count > 0)
                    PivotItems.Insert(PivotItems.Count, newReddit);
                else
                    PivotItems.Add(newReddit);
                _subreddits.Add(message.Subreddit);
                indexToPosition = PivotItems.Count - 1;
            }

            if (fireSubredditsChanged)
            {
                Messenger.Default.Send<SelectIndexMessage>(
                    new SelectIndexMessage
                    {
                        TypeContext = typeof(MainPageViewModel),
                        Index = indexToPosition
                    }
                );
            }
        }
Ejemplo n.º 6
0
		private void OnSubredditChanged(SelectSubredditMessage message)
		{
			ChangeSubreddit(message, !message.AddOnly);
		}
Ejemplo n.º 7
0
        public bool FindSubredditMessageIndex(SelectSubredditMessage message, out int indexToPosition)
        {
            indexToPosition = 0;
            foreach (var vm in PivotItems)
            {
                if (vm is RedditViewModel)
                {
                    if (((RedditViewModel)vm).Url == message.Subreddit.Data.Url)
                    {
                        return true;
                    }

                }
                indexToPosition++;
            }
            return false;
        }