/// <summary> /// Initialize the settings interface for a particular category. /// </summary> public CategoryConfigViewModel(IMeetingListRef meeting) { // First, we need to determine if this meeting is already in the // database. _meetingInfo = CategoryDB.Find(meeting); InitializeVM(meeting); }
/// <summary> /// Get everything else up and configured. /// </summary> /// <param name="meeting"></param> private void InitializeVM(IMeetingListRef meeting) { _isSubscribed = _meetingInfo != null; if (_meetingInfo == null) { _meetingInfo = new CategoryConfigInfo() { MeetingList = meeting, CategoryTitle = "Meeting List", DisplayOnHomePage = false }; } _title = _meetingInfo.CategoryTitle; // If they want it to be displayed on the main page, then we have to subscribe to it. this.WhenAny(x => x.IsDisplayedOnMainPage, x => x.Value) .Where(isDisplayedValue => isDisplayedValue) .Subscribe(v => IsSubscribed = true); // If they don't want to subscribe, then we can't display it. _isDisplayedOnMainPage = _meetingInfo.DisplayOnHomePage; this.WhenAny(x => x.IsSubscribed, x => x.Value) .Where(isSubscribed => !isSubscribed) .Subscribe(x => IsDisplayedOnMainPage = false); // When things change, we need to reflect the changes back into the main store. this.WhenAny(x => x.IsSubscribed, x => x.GetValue()) .Where(x => x && !string.IsNullOrWhiteSpace(CategoryTitle)) .Subscribe(_ => CategoryDB.UpdateOrInsert(GetMeetingInfo())); this.WhenAny(x => x.IsSubscribed, x => x.GetValue()) .Where(x => !x) .Subscribe(_ => CategoryDB.Remove(GetMeetingInfo())); this.WhenAny(x => x.IsDisplayedOnMainPage, x => x.GetValue()) .Where(_ => IsSubscribed && !string.IsNullOrWhiteSpace(CategoryTitle)) .Subscribe(x => CategoryDB.UpdateOrInsert(GetMeetingInfo())); this.WhenAny(x => x.CategoryTitle, x => x.GetValue()) .Where(x => !string.IsNullOrWhiteSpace(x)) .Where(_ => IsSubscribed && !string.IsNullOrWhiteSpace(CategoryTitle)) .Subscribe(x => CategoryDB.UpdateOrInsert(GetMeetingInfo())); // Setup the logic for subscribing (or not). UpdateToCI = this.WhenAny(x => x.CategoryTitle, x => x.IsDisplayedOnMainPage, (x, y) => default(Unit)) .Where(_ => !string.IsNullOrWhiteSpace(CategoryTitle)) .Select(_ => GetMeetingInfo()); }
/// <summary> /// Init ourselves with a new meeting ref /// </summary> /// <param name="meetings"></param> public CategoryURIViewModel(IMeetingListRef meetings, IBlobCache cache = null) { cache = cache ?? Blobs.LocalStorage; // Get the list of items we are going to show. If there // is an error we should display it. MeetingList = new ReactiveList <IMeetingRefExtended>(); var meetingStream = meetings.FetchAndUpdateRecentMeetings(cache: cache) .Replay(1); meetingStream .OnErrorResumeNext(Observable.Empty <IMeetingRefExtended[]>()) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(m => SetMeetings(m)); var errorStream = new Subject <Exception>(); ErrorsVM = new ErrorUserControlViewModel(errorStream); meetingStream .Subscribe(_ => { }, except => errorStream.OnNext(except)); meetingStream .Select(_ => true) .ToProperty(this, x => x.IsReady, out _isReady, false, RxApp.MainThreadScheduler); meetingStream.Connect(); // When the user wants to view one of the meetings we are showing ViewMeeting = ReactiveCommand.Create(); MeetingToVisit = ViewMeeting .Select(m => m as IMeetingRefExtended) .Where(m => m != null) .Select(m => new MeetingPageViewModel(Locator.Current.GetService <IScreen>(), m.Meeting)); }
/// <summary> /// Initialize a new category page view model /// </summary> /// <param name="parent"></param> public CategoryPageViewModel(IScreen parent, IMeetingListRef meetings, IBlobCache cache = null) { HostScreen = parent; CategoryListing = new CategoryURIViewModel(meetings, cache); CategoryConfig = new CategoryConfigViewModel(meetings); }
/// <summary> /// Get the most recent meetings, and re-fetch as well if they are in the cache. /// </summary> /// <param name="meetings">Meetings reference we should go after</param> /// <param name="updateAlways">If true, we will always update the meeting list. Otherwise we won't do it if we've recently done it</param> /// <returns></returns> public static IObservable <IMeetingRefExtended[]> FetchAndUpdateRecentMeetings(this IMeetingListRef meetings, bool updateAlways = true, IBlobCache cache = null) { Func <DateTimeOffset, bool> refetchFunc = null; cache = cache ?? Blobs.LocalStorage; if (!updateAlways) { refetchFunc = lasttime => (DateTime.Now - lasttime).TotalHours > Settings.MeetingCategoryStaleHours; } return(cache .GetAndFetchLatest(meetings.UniqueString, async() => (await meetings.GetMeetings(Settings.DaysBackToFetchMeetings)).ToArray(), refetchFunc)); }
/// <summary> /// Find a meeting in the db. If it isn't there, return null. /// </summary> /// <param name="meeting"></param> /// <returns></returns> public static CategoryConfigInfo Find(IMeetingListRef meeting) { return(LoadCategories() .Where(m => m.MeetingList.UniqueString == meeting.UniqueString) .FirstOrDefault()); }
/// <summary> /// Look up a category config /// </summary> /// <param name="meetingListRef"></param> /// <returns></returns> private CategoryConfigInfo FindDBConfigInfo(IMeetingListRef meetingListRef) { return(CategoryDB.Find(meetingListRef)); }