Search in sources :

Example 1 with SyncResultStatus

use of it.niedermann.owncloud.notes.shared.model.SyncResultStatus in project nextcloud-notes by stefan-niedermann.

the class NotesServerSyncTaskTest method setup.

@Before
public void setup() throws NextcloudFilesAppAccountNotFoundException, IOException {
    when(apiProvider.getNotesAPI(any(), any(), any())).thenReturn(notesAPI);
    NotesTestingUtil.mockSingleSignOn(new SingleSignOnAccount(account.getAccountName(), account.getUserName(), "", account.getUrl(), ""));
    this.task = new NotesServerSyncTask(mock(Context.class), repo, account, false, apiProvider) {

        @Override
        void onPreExecute() {
        }

        @Override
        void onPostExecute(SyncResultStatus status) {
        }
    };
}
Also used : SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) SyncResultStatus(it.niedermann.owncloud.notes.shared.model.SyncResultStatus) Before(org.junit.Before)

Example 2 with SyncResultStatus

use of it.niedermann.owncloud.notes.shared.model.SyncResultStatus in project nextcloud-notes by stefan-niedermann.

the class NotesRepository method scheduleSync.

/**
 * Schedules a synchronization and start it directly, if the network is connected and no
 * synchronization is currently running.
 *
 * @param onlyLocalChanges Whether to only push local changes to the server or to also load the whole list of notes from the server.
 */
public synchronized void scheduleSync(@Nullable Account account, boolean onlyLocalChanges) {
    if (account == null) {
        Log.i(TAG, SingleSignOnAccount.class.getSimpleName() + " is null. Is this a local account?");
    } else {
        if (syncActive.get(account.getId()) == null) {
            syncActive.put(account.getId(), false);
        }
        Log.d(TAG, "Sync requested (" + (onlyLocalChanges ? "onlyLocalChanges" : "full") + "; " + (Boolean.TRUE.equals(syncActive.get(account.getId())) ? "sync active" : "sync NOT active") + ") ...");
        if (isSyncPossible() && (!Boolean.TRUE.equals(syncActive.get(account.getId())) || onlyLocalChanges)) {
            syncActive.put(account.getId(), true);
            try {
                Log.d(TAG, "... starting now");
                final NotesServerSyncTask syncTask = new NotesServerSyncTask(context, this, account, onlyLocalChanges, apiProvider) {

                    @Override
                    void onPreExecute() {
                        syncStatus.postValue(true);
                        if (!syncScheduled.containsKey(localAccount.getId()) || syncScheduled.get(localAccount.getId()) == null) {
                            syncScheduled.put(localAccount.getId(), false);
                        }
                        if (!onlyLocalChanges && Boolean.TRUE.equals(syncScheduled.get(localAccount.getId()))) {
                            syncScheduled.put(localAccount.getId(), false);
                        }
                    }

                    @Override
                    void onPostExecute(SyncResultStatus status) {
                        for (Throwable e : exceptions) {
                            Log.e(TAG, e.getMessage(), e);
                        }
                        if (!status.pullSuccessful || !status.pushSuccessful) {
                            syncErrors.postValue(exceptions);
                        }
                        syncActive.put(localAccount.getId(), false);
                        // notify callbacks
                        if (callbacks.containsKey(localAccount.getId()) && callbacks.get(localAccount.getId()) != null) {
                            for (ISyncCallback callback : Objects.requireNonNull(callbacks.get(localAccount.getId()))) {
                                callback.onFinish();
                            }
                        }
                        notifyWidgets();
                        updateDynamicShortcuts(localAccount.getId());
                        // start next sync if scheduled meanwhile
                        if (syncScheduled.containsKey(localAccount.getId()) && syncScheduled.get(localAccount.getId()) != null && Boolean.TRUE.equals(syncScheduled.get(localAccount.getId()))) {
                            scheduleSync(localAccount, false);
                        }
                        syncStatus.postValue(false);
                    }
                };
                syncTask.addCallbacks(account, callbacksPush.get(account.getId()));
                callbacksPush.put(account.getId(), new ArrayList<>());
                if (!onlyLocalChanges) {
                    syncTask.addCallbacks(account, callbacksPull.get(account.getId()));
                    callbacksPull.put(account.getId(), new ArrayList<>());
                }
                syncExecutor.submit(syncTask);
            } catch (NextcloudFilesAppAccountNotFoundException e) {
                Log.e(TAG, "... Could not find " + SingleSignOnAccount.class.getSimpleName() + " for account name " + account.getAccountName());
                e.printStackTrace();
            }
        } else if (!onlyLocalChanges) {
            Log.d(TAG, "... scheduled");
            syncScheduled.put(account.getId(), true);
            if (callbacksPush.containsKey(account.getId()) && callbacksPush.get(account.getId()) != null) {
                final var callbacks = callbacksPush.get(account.getId());
                if (callbacks != null) {
                    for (final var callback : callbacks) {
                        callback.onScheduled();
                    }
                } else {
                    Log.w(TAG, "List of push-callbacks was set for account \"" + account.getAccountName() + "\" but it was null");
                }
            }
        } else {
            Log.d(TAG, "... do nothing");
            if (callbacksPush.containsKey(account.getId()) && callbacksPush.get(account.getId()) != null) {
                final var callbacks = callbacksPush.get(account.getId());
                if (callbacks != null) {
                    for (final var callback : callbacks) {
                        callback.onScheduled();
                    }
                } else {
                    Log.w(TAG, "List of push-callbacks was set for account \"" + account.getAccountName() + "\" but it was null");
                }
            }
        }
    }
}
Also used : SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) ISyncCallback(it.niedermann.owncloud.notes.shared.model.ISyncCallback) SyncResultStatus(it.niedermann.owncloud.notes.shared.model.SyncResultStatus) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Aggregations

SingleSignOnAccount (com.nextcloud.android.sso.model.SingleSignOnAccount)2 SyncResultStatus (it.niedermann.owncloud.notes.shared.model.SyncResultStatus)2 NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)1 ISyncCallback (it.niedermann.owncloud.notes.shared.model.ISyncCallback)1 Before (org.junit.Before)1