From 22041df25681a486e66727970a2fe09868e3277e Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 8 Jun 2021 20:51:59 +0900 Subject: [PATCH] Convert DMLastNotifiedDao, DMLastNotifiedDataSource and DMLastNotifiedRepository to kotlin --- .../instagrabber/db/dao/DMLastNotifiedDao.kt | 31 ++-- .../datasources/DMLastNotifiedDataSource.kt | 103 +++++------- .../repositories/DMLastNotifiedRepository.kt | 147 ++++-------------- .../instagrabber/services/DMSyncService.java | 53 +++---- 4 files changed, 115 insertions(+), 219 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/db/dao/DMLastNotifiedDao.kt b/app/src/main/java/awais/instagrabber/db/dao/DMLastNotifiedDao.kt index ddd6968c..50f6f371 100644 --- a/app/src/main/java/awais/instagrabber/db/dao/DMLastNotifiedDao.kt +++ b/app/src/main/java/awais/instagrabber/db/dao/DMLastNotifiedDao.kt @@ -1,34 +1,25 @@ -package awais.instagrabber.db.dao; +package awais.instagrabber.db.dao -import androidx.room.Dao; -import androidx.room.Delete; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; -import androidx.room.Update; - -import java.util.List; - -import awais.instagrabber.db.entities.DMLastNotified; +import androidx.room.* +import awais.instagrabber.db.entities.DMLastNotified @Dao -public interface DMLastNotifiedDao { - +interface DMLastNotifiedDao { @Query("SELECT * FROM dm_last_notified") - List getAllDMDmLastNotified(); + suspend fun getAllDMDmLastNotified(): List @Query("SELECT * FROM dm_last_notified WHERE thread_id = :threadId") - DMLastNotified findDMLastNotifiedByThreadId(String threadId); + suspend fun findDMLastNotifiedByThreadId(threadId: String): DMLastNotified? @Insert(onConflict = OnConflictStrategy.REPLACE) - List insertDMLastNotified(DMLastNotified... dmLastNotified); + suspend fun insertDMLastNotified(vararg dmLastNotified: DMLastNotified) @Update - void updateDMLastNotified(DMLastNotified... dmLastNotified); + suspend fun updateDMLastNotified(vararg dmLastNotified: DMLastNotified) @Delete - void deleteDMLastNotified(DMLastNotified... dmLastNotified); + suspend fun deleteDMLastNotified(vararg dmLastNotified: DMLastNotified) @Query("DELETE from dm_last_notified") - void deleteAllDMLastNotified(); -} + suspend fun deleteAllDMLastNotified() +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt b/app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt index 70a9a171..70c24afb 100644 --- a/app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt +++ b/app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt @@ -1,70 +1,53 @@ -package awais.instagrabber.db.datasources; +package awais.instagrabber.db.datasources -import android.content.Context; +import android.content.Context +import awais.instagrabber.db.AppDatabase +import awais.instagrabber.db.dao.DMLastNotifiedDao +import awais.instagrabber.db.entities.DMLastNotified +import java.time.LocalDateTime -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; +class DMLastNotifiedDataSource private constructor(private val dmLastNotifiedDao: DMLastNotifiedDao) { + suspend fun getDMLastNotified(threadId: String): DMLastNotified? = dmLastNotifiedDao.findDMLastNotifiedByThreadId(threadId) -import java.time.LocalDateTime; -import java.util.List; + suspend fun getAllDMDmLastNotified(): List = dmLastNotifiedDao.getAllDMDmLastNotified() -import awais.instagrabber.db.AppDatabase; -import awais.instagrabber.db.dao.DMLastNotifiedDao; -import awais.instagrabber.db.entities.DMLastNotified; - -public class DMLastNotifiedDataSource { - private static final String TAG = DMLastNotifiedDataSource.class.getSimpleName(); - - private static DMLastNotifiedDataSource INSTANCE; - - private final DMLastNotifiedDao dmLastNotifiedDao; - - private DMLastNotifiedDataSource(final DMLastNotifiedDao dmLastNotifiedDao) { - this.dmLastNotifiedDao = dmLastNotifiedDao; + suspend fun insertOrUpdateDMLastNotified( + threadId: String, + lastNotifiedMsgTs: LocalDateTime, + lastNotifiedAt: LocalDateTime, + ) { + val dmLastNotified = getDMLastNotified(threadId) + val toUpdate = DMLastNotified( + dmLastNotified?.id ?: 0, + threadId, + lastNotifiedMsgTs, + lastNotifiedAt + ) + if (dmLastNotified != null) { + dmLastNotifiedDao.updateDMLastNotified(toUpdate) + return + } + dmLastNotifiedDao.insertDMLastNotified(toUpdate) } - public static DMLastNotifiedDataSource getInstance(@NonNull Context context) { - if (INSTANCE == null) { - synchronized (DMLastNotifiedDataSource.class) { - if (INSTANCE == null) { - final AppDatabase database = AppDatabase.getDatabase(context); - INSTANCE = new DMLastNotifiedDataSource(database.dmLastNotifiedDao()); + suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified) + + suspend fun deleteAllDMLastNotified() = dmLastNotifiedDao.deleteAllDMLastNotified() + + companion object { + private lateinit var INSTANCE: DMLastNotifiedDataSource + + @JvmStatic + fun getInstance(context: Context): DMLastNotifiedDataSource { + if (!this::INSTANCE.isInitialized) { + synchronized(DMLastNotifiedDataSource::class.java) { + if (!this::INSTANCE.isInitialized) { + val database = AppDatabase.getDatabase(context) + INSTANCE = DMLastNotifiedDataSource(database.dmLastNotifiedDao()) + } } } + return INSTANCE } - return INSTANCE; } - - @Nullable - public final DMLastNotified getDMLastNotified(final String threadId) { - return dmLastNotifiedDao.findDMLastNotifiedByThreadId(threadId); - } - - @NonNull - public final List getAllDMDmLastNotified() { - return dmLastNotifiedDao.getAllDMDmLastNotified(); - } - - public final void insertOrUpdateDMLastNotified(final String threadId, - final LocalDateTime lastNotifiedMsgTs, - final LocalDateTime lastNotifiedAt) { - final DMLastNotified dmLastNotified = getDMLastNotified(threadId); - final DMLastNotified toUpdate = new DMLastNotified(dmLastNotified == null ? 0 : dmLastNotified.getId(), - threadId, - lastNotifiedMsgTs, - lastNotifiedAt); - if (dmLastNotified != null) { - dmLastNotifiedDao.updateDMLastNotified(toUpdate); - return; - } - dmLastNotifiedDao.insertDMLastNotified(toUpdate); - } - - public final void deleteDMLastNotified(@NonNull final DMLastNotified dmLastNotified) { - dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified); - } - - public final void deleteAllDMLastNotified() { - dmLastNotifiedDao.deleteAllDMLastNotified(); - } -} +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/db/repositories/DMLastNotifiedRepository.kt b/app/src/main/java/awais/instagrabber/db/repositories/DMLastNotifiedRepository.kt index 07334503..d419ebfd 100644 --- a/app/src/main/java/awais/instagrabber/db/repositories/DMLastNotifiedRepository.kt +++ b/app/src/main/java/awais/instagrabber/db/repositories/DMLastNotifiedRepository.kt @@ -1,126 +1,47 @@ -package awais.instagrabber.db.repositories; +package awais.instagrabber.db.repositories -import java.time.LocalDateTime; -import java.util.List; +import awais.instagrabber.db.datasources.DMLastNotifiedDataSource +import awais.instagrabber.db.entities.DMLastNotified +import java.time.LocalDateTime -import awais.instagrabber.db.datasources.DMLastNotifiedDataSource; -import awais.instagrabber.db.entities.DMLastNotified; -import awais.instagrabber.utils.AppExecutors; +class DMLastNotifiedRepository private constructor(private val dmLastNotifiedDataSource: DMLastNotifiedDataSource) { -public class DMLastNotifiedRepository { - private static final String TAG = DMLastNotifiedRepository.class.getSimpleName(); + suspend fun getDMLastNotified(threadId: String): DMLastNotified? = dmLastNotifiedDataSource.getDMLastNotified(threadId) - private static DMLastNotifiedRepository instance; + suspend fun getAllDMDmLastNotified(): List = dmLastNotifiedDataSource.getAllDMDmLastNotified() - private final AppExecutors appExecutors; - private final DMLastNotifiedDataSource dmLastNotifiedDataSource; - - private DMLastNotifiedRepository(final AppExecutors appExecutors, final DMLastNotifiedDataSource dmLastNotifiedDataSource) { - this.appExecutors = appExecutors; - this.dmLastNotifiedDataSource = dmLastNotifiedDataSource; - } - - public static DMLastNotifiedRepository getInstance(final DMLastNotifiedDataSource dmLastNotifiedDataSource) { - if (instance == null) { - instance = new DMLastNotifiedRepository(AppExecutors.INSTANCE, dmLastNotifiedDataSource); + suspend fun insertOrUpdateDMLastNotified(dmLastNotifiedList: List) { + for (dmLastNotified in dmLastNotifiedList) { + dmLastNotifiedDataSource.insertOrUpdateDMLastNotified( + dmLastNotified.threadId, + dmLastNotified.lastNotifiedMsgTs, + dmLastNotified.lastNotifiedAt + ) } - return instance; } - public void getDMLastNotified(final String threadId, - final RepositoryCallback callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - final DMLastNotified dmLastNotified = dmLastNotifiedDataSource.getDMLastNotified(threadId); - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - if (dmLastNotified == null) { - callback.onDataNotAvailable(); - return; - } - callback.onSuccess(dmLastNotified); - }); - }); + suspend fun insertOrUpdateDMLastNotified( + threadId: String, + lastNotifiedMsgTs: LocalDateTime, + lastNotifiedAt: LocalDateTime, + ): DMLastNotified? { + dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt) + return dmLastNotifiedDataSource.getDMLastNotified(threadId) } - public void getAllDMDmLastNotified(final RepositoryCallback> callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - final List allDMDmLastNotified = dmLastNotifiedDataSource.getAllDMDmLastNotified(); - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - if (allDMDmLastNotified == null) { - callback.onDataNotAvailable(); - return; - } - // cachedAccounts = accounts; - callback.onSuccess(allDMDmLastNotified); - }); - }); - } + suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified) - public void insertOrUpdateDMLastNotified(final List dmLastNotifiedList, - final RepositoryCallback callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - for (final DMLastNotified dmLastNotified : dmLastNotifiedList) { - dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(dmLastNotified.getThreadId(), - dmLastNotified.getLastNotifiedMsgTs(), - dmLastNotified.getLastNotifiedAt()); + suspend fun deleteAllDMLastNotified() = dmLastNotifiedDataSource.deleteAllDMLastNotified() + + companion object { + private lateinit var instance: DMLastNotifiedRepository + + @JvmStatic + fun getInstance(dmLastNotifiedDataSource: DMLastNotifiedDataSource): DMLastNotifiedRepository { + if (!this::instance.isInitialized) { + instance = DMLastNotifiedRepository(dmLastNotifiedDataSource) } - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - callback.onSuccess(null); - }); - }); + return instance + } } - - public void insertOrUpdateDMLastNotified(final String threadId, - final LocalDateTime lastNotifiedMsgTs, - final LocalDateTime lastNotifiedAt, - final RepositoryCallback callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt); - final DMLastNotified updated = dmLastNotifiedDataSource.getDMLastNotified(threadId); - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - if (updated == null) { - callback.onDataNotAvailable(); - return; - } - callback.onSuccess(updated); - }); - }); - } - - public void deleteDMLastNotified(final DMLastNotified dmLastNotified, - final RepositoryCallback callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified); - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - callback.onSuccess(null); - }); - }); - } - - public void deleteAllDMLastNotified(final RepositoryCallback callback) { - // request on the I/O thread - appExecutors.getDiskIO().execute(() -> { - dmLastNotifiedDataSource.deleteAllDMLastNotified(); - // notify on the main thread - appExecutors.getMainThread().execute(() -> { - if (callback == null) return; - callback.onSuccess(null); - }); - }); - } - -} +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/services/DMSyncService.java b/app/src/main/java/awais/instagrabber/services/DMSyncService.java index 6a299945..e970b6c2 100644 --- a/app/src/main/java/awais/instagrabber/services/DMSyncService.java +++ b/app/src/main/java/awais/instagrabber/services/DMSyncService.java @@ -30,7 +30,6 @@ import awais.instagrabber.activities.MainActivity; import awais.instagrabber.db.datasources.DMLastNotifiedDataSource; import awais.instagrabber.db.entities.DMLastNotified; import awais.instagrabber.db.repositories.DMLastNotifiedRepository; -import awais.instagrabber.db.repositories.RepositoryCallback; import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.managers.DirectMessagesManager; import awais.instagrabber.managers.InboxManager; @@ -39,11 +38,14 @@ import awais.instagrabber.repositories.responses.directmessages.DirectInbox; import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThreadLastSeenAt; +import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; +import awais.instagrabber.utils.CoroutineUtilsKt; import awais.instagrabber.utils.DMUtils; import awais.instagrabber.utils.DateUtils; import awais.instagrabber.utils.TextUtils; +import kotlinx.coroutines.Dispatchers; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -61,25 +63,26 @@ public class DMSyncService extends LifecycleService { Log.d(TAG, "onCreate: Service created"); final DirectMessagesManager directMessagesManager = DirectMessagesManager.INSTANCE; inboxManager = directMessagesManager.getInboxManager(); - dmLastNotifiedRepository = DMLastNotifiedRepository.getInstance(DMLastNotifiedDataSource.getInstance(getApplicationContext())); + final Context context = getApplicationContext(); + if (context == null) return; + dmLastNotifiedRepository = DMLastNotifiedRepository.getInstance(DMLastNotifiedDataSource.getInstance(context)); } private void parseUnread(@NonNull final DirectInbox directInbox) { - dmLastNotifiedRepository.getAllDMDmLastNotified(new RepositoryCallback>() { - @Override - public void onSuccess(final List result) { - dmLastNotifiedMap = result != null - ? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity())) - : Collections.emptyMap(); - parseUnreadActual(directInbox); - } - - @Override - public void onDataNotAvailable() { - dmLastNotifiedMap = Collections.emptyMap(); - parseUnreadActual(directInbox); - } - }); + dmLastNotifiedRepository.getAllDMDmLastNotified( + CoroutineUtilsKt.getContinuation((result, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { + if (throwable != null) { + Log.e(TAG, "parseUnread: ", throwable); + dmLastNotifiedMap = Collections.emptyMap(); + parseUnreadActual(directInbox); + return; + } + dmLastNotifiedMap = result != null + ? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity())) + : Collections.emptyMap(); + parseUnreadActual(directInbox); + }), Dispatchers.getIO()) + ); // Log.d(TAG, "inbox observer: " + directInbox); } @@ -117,17 +120,15 @@ public class DMSyncService extends LifecycleService { } dmLastNotifiedRepository.insertOrUpdateDMLastNotified( lastNotifiedListBuilder.build(), - new RepositoryCallback() { - @Override - public void onSuccess(final Void result) { + CoroutineUtilsKt.getContinuation((unit, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { + try { + if (throwable != null) { + Log.e(TAG, "parseUnreadActual: ", throwable); + } + } finally { stopSelf(); } - - @Override - public void onDataNotAvailable() { - stopSelf(); - } - } + }), Dispatchers.getIO()) ); }