use of it.niedermann.owncloud.notes.shared.model.ISyncCallback in project nextcloud-notes by stefan-niedermann.
the class NoteEditFragment method autoSave.
/**
* Saves the current changes and show the status in the ActionBar
*/
private void autoSave() {
Log.d(LOG_TAG_AUTOSAVE, "STARTAUTOSAVE");
saveActive = true;
saveNote(new ISyncCallback() {
@Override
public void onFinish() {
onSaved();
}
@Override
public void onScheduled() {
onSaved();
}
private void onSaved() {
// AFTER SYNCHRONIZATION
Log.d(LOG_TAG_AUTOSAVE, "FINISHED AUTOSAVE");
saveActive = false;
// AFTER "DELAY_AFTER_SYNC" SECONDS: allow next auto-save or start it directly
handler.postDelayed(runAutoSave, DELAY_AFTER_SYNC);
}
});
}
use of it.niedermann.owncloud.notes.shared.model.ISyncCallback 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");
}
}
}
}
}
Aggregations