public async Task LoadMorePosts( PostType type, int count, int offset) { ShowLoadingMoreGrid(); try { var result = await PostApi.Get( SettingsManager.PersistentSettings.CurrentUser.Token, type, count, offset) .ConfigureAwait(true); if (result == null) { return; } result.Data ??= new List <PostSchema>(); await AddMorePosts(result.Data) .ConfigureAwait(true); } finally { HideLoadingMoreGrid(); } }
private async Task <bool> GetApiToDatabase() { if (CrossConnectivity.Current.IsConnected) { var posts = await postApi.Get("albumId", albumModel.Id.ToString()); foreach (var post in posts) { var postDb = postDatabase.Get(i => i.Id == post.Id); if (postDb.Any()) { continue; } var postToInsert = new PostEntity() { Id = post.Id, Title = post.Title, Body = post.Body, AlbumId = post.AlbumId }; postDatabase.Insert(postToInsert); } return(await Task.FromResult(true)); } return(await Task.FromResult(false)); }
private async Task <int> GetAllPostsCount(PostType type, int countPerTime, int offset) { if (!(type == PostType.My || type == PostType.Favorite)) { return(0); } return(await Task.Run(async() => { int countNew = 0; while (true) { var result = await PostApi.Get( SettingsManager.PersistentSettings.CurrentUser.Token, type, countPerTime, offset) .ConfigureAwait(false); if (result?.IsError != false) { continue; } if (result.Data == null) { result.Data = new List <PostSchema>(); } if (result.Data.Count == 0) { break; } countNew += result.Data.Count; offset += countPerTime; } return countNew; }).ConfigureAwait(true)); }
public override async void Start() { try { using (_userDialogs.Loading("Loading")) { var result = await _api.Get(Convert.ToInt32(Id)); var resultSimilar = await _api.GetSimilar(Convert.ToInt32(Id)); SinglePost = result.Data; SimilarPosts = resultSimilar.Data; } } catch (Exception) { await _userDialogs.AlertAsync(Mvx.IoCProvider.Resolve <ILocalizeService>().Translate("Error"), Mvx.IoCProvider.Resolve <ILocalizeService>().Translate("Error"), Mvx.IoCProvider.Resolve <ILocalizeService>().Translate("Ok")); throw; } }
public async Task <int> GetNewPostsCount( PostType type, int countPerTime, int offset) { var postsCount = 0; Dispatcher.Invoke(() => { postsCount = PostsWrapPanel.Children.Count; }); if (postsCount == 0) { if (type == PostType.My || type == PostType.Favorite) { return(await GetAllPostsCount( type, countPerTime, offset) .ConfigureAwait(true)); } return(0); } var headOldId = -1; Dispatcher.Invoke(() => { headOldId = ViewModel.LastNewHeadPostId; }); if (headOldId == -1) { return(0); } return(await Task.Run(async() => { var countNew = 0; var headNewId = -1; var headOldIsFound = false; var isFirstRequest = true; while (!headOldIsFound) { var result = await PostApi.Get( SettingsManager.PersistentSettings.CurrentUser.Token, type, countPerTime, offset) .ConfigureAwait(false); if (result?.IsError != false) { continue; } result.Data ??= new List <PostSchema>(); if (result.Data.Count == 0) { await Dispatcher.Invoke(() => { return UpdatePosts(); }).ConfigureAwait(true); return 0; } if (isFirstRequest) { headNewId = result.Data[0].Id; isFirstRequest = false; } foreach (var post in result.Data) { if (post.Id == headOldId) { headOldIsFound = true; break; } ++countNew; } if (countNew >= 500) { await Dispatcher.Invoke(() => { return UpdatePosts(); }).ConfigureAwait(true); return 0; } offset += countPerTime; } Dispatcher.Invoke(() => { ViewModel.LastNewHeadPostId = headNewId; }); return countNew; }).ConfigureAwait(true)); }
#pragma warning disable SS001 // Async methods should return a Task to make them awaitable protected override async void OnStartup(StartupEventArgs e) { await Memenim.MainWindow.Instance.ShowLoadingGrid(true) .ConfigureAwait(true); MainWindow = Memenim.MainWindow.Instance; await Task.Delay(TimeSpan.FromMilliseconds(200)) .ConfigureAwait(true); Memenim.MainWindow.Instance.Show(); base.OnStartup(e); await Task.Delay(TimeSpan.FromMilliseconds(500)) .ConfigureAwait(true); LocalizationUtils.ReloadLocalizations <XamlLocalizationProvider>(); LocalizationUtils.SwitchLocalization( SettingsManager.AppSettings.Language); LocalizationUtils.SetDefaultCulture("en-US"); if (LocalizationUtils.Localizations.Count == 0) { return; } await StorageManager.Initialize() .ConfigureAwait(true); ProtocolManager.RegisterAll(); await Task.Run(async() => { LogManager.DeleteLogs(SettingsManager.AppSettings .LogRetentionDaysPeriod); try { if (string.IsNullOrEmpty( SettingsManager.PersistentSettings.GetCurrentUserLogin())) { Dispatcher.Invoke(() => { NavigationController.Instance.RequestPage <LoginPage>(); }); return; } if (!SettingsManager.PersistentSettings.SetCurrentUser( SettingsManager.PersistentSettings.GetCurrentUserLogin())) { SettingsManager.PersistentSettings.RemoveUser( SettingsManager.PersistentSettings.GetCurrentUserLogin()); Dispatcher.Invoke(() => { NavigationController.Instance.RequestPage <LoginPage>(); }); return; } var result = await PostApi.Get( SettingsManager.PersistentSettings.CurrentUser.Token, PostType.Popular, 1) .ConfigureAwait(false); if (result.IsError && (result.Code == 400 || result.Code == 401)) { SettingsManager.PersistentSettings.RemoveUser( SettingsManager.PersistentSettings.CurrentUser.Login); Dispatcher.Invoke(() => { NavigationController.Instance.RequestPage <LoginPage>(); }); return; } Dispatcher.Invoke(() => { NavigationController.Instance.RequestPage <FeedPage>(); }); if (!string.IsNullOrEmpty(AppStartupUri)) { ProtocolManager.ParseUri(AppStartupUri); } } catch (Exception) { Dispatcher.Invoke(() => { NavigationController.Instance.RequestPage <LoginPage>(); }); } }).ConfigureAwait(true); await Task.Delay(TimeSpan.FromSeconds(1.5)) .ConfigureAwait(true); await Memenim.MainWindow.Instance.ShowLoadingGrid(false) .ConfigureAwait(true); }