Use Executor in repository to submit asynchronous tasks

This commit is contained in:
Stefan Niedermann 2021-04-25 13:14:47 +02:00
parent 8208a3019b
commit 94a9628427
2 changed files with 26 additions and 22 deletions

View file

@ -81,7 +81,7 @@ public class NotesRepository {
private static NotesRepository instance; private static NotesRepository instance;
private final ExecutorService executor = Executors.newSingleThreadExecutor(); private final ExecutorService executor;
private final Context context; private final Context context;
private final NotesDatabase db; private final NotesDatabase db;
private final String defaultNonEmptyTitle; private final String defaultNonEmptyTitle;
@ -112,13 +112,13 @@ public class NotesRepository {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
updateNetworkStatus(); updateNetworkStatus();
if (isSyncPossible() && SSOUtil.isConfigured(context)) { if (isSyncPossible() && SSOUtil.isConfigured(context)) {
new Thread(() -> { executor.submit(() -> {
try { try {
scheduleSync(getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name), false); scheduleSync(getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name), false);
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) { } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
Log.v(TAG, "Can not select current SingleSignOn account after network changed, do not sync."); Log.v(TAG, "Can not select current SingleSignOn account after network changed, do not sync.");
} }
}).start(); });
} }
} }
}; };
@ -134,14 +134,15 @@ public class NotesRepository {
public static synchronized NotesRepository getInstance(@NonNull Context context) { public static synchronized NotesRepository getInstance(@NonNull Context context) {
if (instance == null) { if (instance == null) {
instance = new NotesRepository(context, NotesDatabase.getInstance(context.getApplicationContext())); instance = new NotesRepository(context, NotesDatabase.getInstance(context.getApplicationContext()), Executors.newCachedThreadPool());
} }
return instance; return instance;
} }
private NotesRepository(@NonNull final Context context, @NonNull final NotesDatabase db) { private NotesRepository(@NonNull final Context context, @NonNull final NotesDatabase db, @NonNull final ExecutorService executor) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.db = db; this.db = db;
this.executor = executor;
this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context); this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context);
this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only); this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only);
@ -364,7 +365,7 @@ public class NotesRepository {
public LiveData<Note> addNoteAndSync(Account account, Note note) { public LiveData<Note> addNoteAndSync(Account account, Note note) {
final Note entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0); final Note entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0);
final MutableLiveData<Note> ret = new MutableLiveData<>(); final MutableLiveData<Note> ret = new MutableLiveData<>();
new Thread(() -> ret.postValue(addNote(account.getId(), entity))).start(); executor.submit(() -> ret.postValue(addNote(account.getId(), entity)));
return map(ret, newNote -> { return map(ret, newNote -> {
notifyWidgets(); notifyWidgets();
scheduleSync(account, true); scheduleSync(account, true);
@ -413,10 +414,10 @@ public class NotesRepository {
@AnyThread @AnyThread
public void toggleFavoriteAndSync(Account account, long noteId) { public void toggleFavoriteAndSync(Account account, long noteId) {
new Thread(() -> { executor.submit(() -> {
db.getNoteDao().toggleFavorite(noteId); db.getNoteDao().toggleFavorite(noteId);
scheduleSync(account, true); scheduleSync(account, true);
}).start(); });
} }
/** /**
@ -430,11 +431,11 @@ public class NotesRepository {
*/ */
@AnyThread @AnyThread
public void setCategory(@NonNull Account account, long noteId, @NonNull String category) { public void setCategory(@NonNull Account account, long noteId, @NonNull String category) {
new Thread(() -> { executor.submit(() -> {
db.getNoteDao().updateStatus(noteId, DBStatus.LOCAL_EDITED); db.getNoteDao().updateStatus(noteId, DBStatus.LOCAL_EDITED);
db.getNoteDao().updateCategory(noteId, category); db.getNoteDao().updateCategory(noteId, category);
scheduleSync(account, true); scheduleSync(account, true);
}).start(); });
} }
/** /**
@ -491,7 +492,7 @@ public class NotesRepository {
*/ */
@AnyThread @AnyThread
public void deleteNoteAndSync(Account account, long id) { public void deleteNoteAndSync(Account account, long id) {
new Thread(() -> { executor.submit(() -> {
db.getNoteDao().updateStatus(id, DBStatus.LOCAL_DELETED); db.getNoteDao().updateStatus(id, DBStatus.LOCAL_DELETED);
notifyWidgets(); notifyWidgets();
scheduleSync(account, true); scheduleSync(account, true);
@ -510,23 +511,23 @@ public class NotesRepository {
Log.e(TAG, ShortcutManager.class.getSimpleName() + "is null."); Log.e(TAG, ShortcutManager.class.getSimpleName() + "is null.");
} }
} }
}).start(); });
} }
/** /**
* Notify about changed notes. * Notify about changed notes.
*/ */
@AnyThread @AnyThread
protected void notifyWidgets() { private void notifyWidgets() {
new Thread(() -> { executor.submit(() -> {
updateSingleNoteWidgets(context); updateSingleNoteWidgets(context);
updateNoteListWidgets(context); updateNoteListWidgets(context);
}).start(); });
} }
@AnyThread @AnyThread
private void updateDynamicShortcuts(long accountId) { private void updateDynamicShortcuts(long accountId) {
new Thread(() -> { executor.submit(() -> {
if (SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { if (SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = context.getApplicationContext().getSystemService(ShortcutManager.class); ShortcutManager shortcutManager = context.getApplicationContext().getSystemService(ShortcutManager.class);
if (shortcutManager != null) { if (shortcutManager != null) {
@ -555,7 +556,7 @@ public class NotesRepository {
} }
} }
} }
}).start(); });
} }
/** /**
@ -605,7 +606,7 @@ public class NotesRepository {
*/ */
@AnyThread @AnyThread
public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory selectedCategory, @NonNull CategorySortingMethod sortingMethod) { public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory selectedCategory, @NonNull CategorySortingMethod sortingMethod) {
new Thread(() -> { executor.submit(() -> {
final Context ctx = context.getApplicationContext(); final Context ctx = context.getApplicationContext();
final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
int orderIndex = sortingMethod.getId(); int orderIndex = sortingMethod.getId();
@ -642,7 +643,7 @@ public class NotesRepository {
} }
} }
sp.apply(); sp.apply();
}).start(); });
} }
/** /**
@ -728,7 +729,7 @@ public class NotesRepository {
* *
* @param callback Implementation of ISyncCallback, contains one method that shall be executed. * @param callback Implementation of ISyncCallback, contains one method that shall be executed.
*/ */
public void addCallbackPush(Account account, ISyncCallback callback) { private void addCallbackPush(Account account, ISyncCallback callback) {
if (account == null) { if (account == null) {
Log.i(TAG, "ssoAccount is null. Is this a local account?"); Log.i(TAG, "ssoAccount is null. Is this a local account?");
callback.onScheduled(); callback.onScheduled();

File diff suppressed because one or more lines are too long