예제 #1
0
        public async Task StartSync()
        {
            try
            {
                bool isSuccess = false;
                await PreparingFilesAsync();

                var remoteCollectionZipFile = await TryGetItemInSyncFolderAsync(Constant.COLLECTION_NAME_ZIP);

                if (remoteUserPref == null ||
                    MainPage.UserPrefs.LastSyncTime >= remoteUserPref.LastSyncTime)
                {
                    isSuccess = await UploadToserverIfNeeded(isSuccess, remoteCollectionZipFile);
                }
                else
                {
                    bool?isDownload = true;
                    if (GetLastModifiedTimeInSecond() > MainPage.UserPrefs.LastSyncTime)
                    {
                        isDownload = await AskForceSyncInOneDirection();

                        if (isDownload == null)
                        {
                            return;
                        }
                        else if (isDownload == true)
                        {
                            syncStateDialog.Label = "Backing up your database...";
                            await MainPage.BackupDatabase();
                        }
                    }

                    if (isDownload == true)
                    {
                        isSuccess = await DownloadFromServer();
                    }
                    else
                    {
                        isSuccess = await UploadToServer();
                    }
                }
                if (isSuccess)
                {
                    await SyncMediaIfNeeded();

                    syncStateDialog.Label = "Finished.";
                    await Task.Delay(500);
                }
                else
                {
                    syncStateDialog.Label = "Failed.";
                    await UIHelper.ShowMessageDialog("Failed to sync your data. Please check your internet connection.\n");
                }
            }
            catch (Exception ex)
            {
                await syncInstance.ExceptionHandler(ex);
            }
            finally
            {
                syncInstance.Close();
                if (tempSyncFolder != null)
                {
                    await tempSyncFolder.DeleteAsync();
                }
                syncStateDialog.Close();
            }
        }
예제 #2
0
        public async Task StartSync()
        {
            try
            {
                SetSyncLabel("Sync collection to AnkiWeb...");
                syncStateDialog.Show();
                GetHostKeyFromVault();

                server = new RemoteServer(hostKey);
                server.OnHttpProgressEvent += OnServerHttpProgressEvent;
                client = new AnkiCore.Sync.Syncer(mainPage.Collection, server);
                var results = await client.Sync();
                await WaitForCloseSyncStateDialog();

                if (results == null)
                {
                    await UIHelper.ShowMessageDialog("No respone from the server! Either your connection or the server did not work properly.");

                    return;
                }
                else if (results[0] == "badAuth")
                {
                    //Include for completeness purpose.
                    //We should not run into this, as the app only logins when user changed sync service
                    await UIHelper.ShowMessageDialog("AnkiWeb ID or password was incorrect. Please try to log in again.");

                    return;
                }
                else if (results[0] == "clockOff")
                {
                    await UIHelper.ShowMessageDialog("Syncing requires the clock on your computer to be set correctly. Please fix the clock and try again.");

                    return;
                }
                else if (results[0] == "basicCheckFailed" || results[0] == "sanityCheckFailed" || results[0] == "sanityCheckError")
                {
                    await UIHelper.ShowMessageDialog("Your collection is in an inconsistent state.\n" +
                                                     "Please run \"Check Collection\" or \"Force Full Sync\", then try again.");

                    return;
                }
                else if (results[0] == "fullSync")
                {
                    await ConfirmAndStartFullSync();

                    MainPage.UserPrefs.IsFullSyncRequire = false;
                    await ShowAnkiWebReturnMessageIfHas(results);

                    return;
                }
                else if (results[0] == "noChanges" || results[0] == "success")
                {
                    SetSyncLabel("Finished.");
                    syncStateDialog.Show();
                    if (results[0] == "success")
                    {
                        await NavigateToDeckSelectPage();
                    }
                    else
                    {
                        await Task.Delay(250);
                    }

                    MainPage.UserPrefs.IsFullSyncRequire = false;
                    await WaitForCloseSyncStateDialog();
                    await ShowAnkiWebReturnMessageIfHas(results);

                    return;
                }
                else if (results[0] == "serverAbort")
                {
                    await UIHelper.ShowMessageDialog("Server aborted.");

                    return;
                }
                else
                {
                    await UIHelper.ShowMessageDialog("Unknown sync return code.");

                    return;
                }
            }
            catch (HttpSyncerException ex)
            {
                await WaitForCloseSyncStateDialog();

                await UIHelper.ShowMessageDialog("AnkiWeb Sync: " + ex.Message);
            }
            catch (PasswordVaulException ex)
            {
                await WaitForCloseSyncStateDialog();

                await UIHelper.ShowMessageDialog("AnkiWeb Sync: " + ex.Message);
            }
            catch (FileLoadException ex)
            {
                await WaitForCloseSyncStateDialog();

                await UIHelper.ShowMessageDialog("AnkiWeb Sync: " + ex.Message);
            }
            catch (Exception ex)
            {
                await WaitForCloseSyncStateDialog();

                await UIHelper.ShowMessageDialog("AnkiWeb Sync: " + ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                syncStateDialog.Close();
            }
        }