1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Convert DMLastNotifiedDao, DMLastNotifiedDataSource and DMLastNotifiedRepository to kotlin

This commit is contained in:
Ammar Githam 2021-06-08 20:51:59 +09:00
parent 3d0b18e422
commit 22041df256
4 changed files with 115 additions and 219 deletions

View File

@ -1,34 +1,25 @@
package awais.instagrabber.db.dao; package awais.instagrabber.db.dao
import androidx.room.Dao; import androidx.room.*
import androidx.room.Delete; import awais.instagrabber.db.entities.DMLastNotified
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;
@Dao @Dao
public interface DMLastNotifiedDao { interface DMLastNotifiedDao {
@Query("SELECT * FROM dm_last_notified") @Query("SELECT * FROM dm_last_notified")
List<DMLastNotified> getAllDMDmLastNotified(); suspend fun getAllDMDmLastNotified(): List<DMLastNotified>
@Query("SELECT * FROM dm_last_notified WHERE thread_id = :threadId") @Query("SELECT * FROM dm_last_notified WHERE thread_id = :threadId")
DMLastNotified findDMLastNotifiedByThreadId(String threadId); suspend fun findDMLastNotifiedByThreadId(threadId: String): DMLastNotified?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
List<Long> insertDMLastNotified(DMLastNotified... dmLastNotified); suspend fun insertDMLastNotified(vararg dmLastNotified: DMLastNotified)
@Update @Update
void updateDMLastNotified(DMLastNotified... dmLastNotified); suspend fun updateDMLastNotified(vararg dmLastNotified: DMLastNotified)
@Delete @Delete
void deleteDMLastNotified(DMLastNotified... dmLastNotified); suspend fun deleteDMLastNotified(vararg dmLastNotified: DMLastNotified)
@Query("DELETE from dm_last_notified") @Query("DELETE from dm_last_notified")
void deleteAllDMLastNotified(); suspend fun deleteAllDMLastNotified()
} }

View File

@ -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; class DMLastNotifiedDataSource private constructor(private val dmLastNotifiedDao: DMLastNotifiedDao) {
import androidx.annotation.Nullable; suspend fun getDMLastNotified(threadId: String): DMLastNotified? = dmLastNotifiedDao.findDMLastNotifiedByThreadId(threadId)
import java.time.LocalDateTime; suspend fun getAllDMDmLastNotified(): List<DMLastNotified> = dmLastNotifiedDao.getAllDMDmLastNotified()
import java.util.List;
import awais.instagrabber.db.AppDatabase; suspend fun insertOrUpdateDMLastNotified(
import awais.instagrabber.db.dao.DMLastNotifiedDao; threadId: String,
import awais.instagrabber.db.entities.DMLastNotified; lastNotifiedMsgTs: LocalDateTime,
lastNotifiedAt: LocalDateTime,
public class DMLastNotifiedDataSource { ) {
private static final String TAG = DMLastNotifiedDataSource.class.getSimpleName(); val dmLastNotified = getDMLastNotified(threadId)
val toUpdate = DMLastNotified(
private static DMLastNotifiedDataSource INSTANCE; dmLastNotified?.id ?: 0,
private final DMLastNotifiedDao dmLastNotifiedDao;
private DMLastNotifiedDataSource(final DMLastNotifiedDao dmLastNotifiedDao) {
this.dmLastNotifiedDao = dmLastNotifiedDao;
}
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());
}
}
}
return INSTANCE;
}
@Nullable
public final DMLastNotified getDMLastNotified(final String threadId) {
return dmLastNotifiedDao.findDMLastNotifiedByThreadId(threadId);
}
@NonNull
public final List<DMLastNotified> 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, threadId,
lastNotifiedMsgTs, lastNotifiedMsgTs,
lastNotifiedAt); lastNotifiedAt
)
if (dmLastNotified != null) { if (dmLastNotified != null) {
dmLastNotifiedDao.updateDMLastNotified(toUpdate); dmLastNotifiedDao.updateDMLastNotified(toUpdate)
return; return
} }
dmLastNotifiedDao.insertDMLastNotified(toUpdate); dmLastNotifiedDao.insertDMLastNotified(toUpdate)
} }
public final void deleteDMLastNotified(@NonNull final DMLastNotified dmLastNotified) { suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified)
dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified);
}
public final void deleteAllDMLastNotified() { suspend fun deleteAllDMLastNotified() = dmLastNotifiedDao.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
}
} }
} }

View File

@ -1,126 +1,47 @@
package awais.instagrabber.db.repositories; package awais.instagrabber.db.repositories
import java.time.LocalDateTime; import awais.instagrabber.db.datasources.DMLastNotifiedDataSource
import java.util.List; import awais.instagrabber.db.entities.DMLastNotified
import java.time.LocalDateTime
import awais.instagrabber.db.datasources.DMLastNotifiedDataSource; class DMLastNotifiedRepository private constructor(private val dmLastNotifiedDataSource: DMLastNotifiedDataSource) {
import awais.instagrabber.db.entities.DMLastNotified;
import awais.instagrabber.utils.AppExecutors;
public class DMLastNotifiedRepository { suspend fun getDMLastNotified(threadId: String): DMLastNotified? = dmLastNotifiedDataSource.getDMLastNotified(threadId)
private static final String TAG = DMLastNotifiedRepository.class.getSimpleName();
private static DMLastNotifiedRepository instance; suspend fun getAllDMDmLastNotified(): List<DMLastNotified> = dmLastNotifiedDataSource.getAllDMDmLastNotified()
private final AppExecutors appExecutors; suspend fun insertOrUpdateDMLastNotified(dmLastNotifiedList: List<DMLastNotified>) {
private final DMLastNotifiedDataSource dmLastNotifiedDataSource; for (dmLastNotified in dmLastNotifiedList) {
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(
private DMLastNotifiedRepository(final AppExecutors appExecutors, final DMLastNotifiedDataSource dmLastNotifiedDataSource) { dmLastNotified.threadId,
this.appExecutors = appExecutors; dmLastNotified.lastNotifiedMsgTs,
this.dmLastNotifiedDataSource = dmLastNotifiedDataSource; dmLastNotified.lastNotifiedAt
)
}
} }
public static DMLastNotifiedRepository getInstance(final DMLastNotifiedDataSource dmLastNotifiedDataSource) { suspend fun insertOrUpdateDMLastNotified(
if (instance == null) { threadId: String,
instance = new DMLastNotifiedRepository(AppExecutors.INSTANCE, dmLastNotifiedDataSource); lastNotifiedMsgTs: LocalDateTime,
} lastNotifiedAt: LocalDateTime,
return instance; ): DMLastNotified? {
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt)
return dmLastNotifiedDataSource.getDMLastNotified(threadId)
} }
public void getDMLastNotified(final String threadId, suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified)
final RepositoryCallback<DMLastNotified> 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);
});
});
}
public void getAllDMDmLastNotified(final RepositoryCallback<List<DMLastNotified>> callback) { suspend fun deleteAllDMLastNotified() = dmLastNotifiedDataSource.deleteAllDMLastNotified()
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
final List<DMLastNotified> 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);
});
});
}
public void insertOrUpdateDMLastNotified(final List<DMLastNotified> dmLastNotifiedList, companion object {
final RepositoryCallback<Void> callback) { private lateinit var instance: DMLastNotifiedRepository
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
for (final DMLastNotified dmLastNotified : dmLastNotifiedList) {
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(dmLastNotified.getThreadId(),
dmLastNotified.getLastNotifiedMsgTs(),
dmLastNotified.getLastNotifiedAt());
}
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
callback.onSuccess(null);
});
});
}
public void insertOrUpdateDMLastNotified(final String threadId, @JvmStatic
final LocalDateTime lastNotifiedMsgTs, fun getInstance(dmLastNotifiedDataSource: DMLastNotifiedDataSource): DMLastNotifiedRepository {
final LocalDateTime lastNotifiedAt, if (!this::instance.isInitialized) {
final RepositoryCallback<DMLastNotified> callback) { instance = DMLastNotifiedRepository(dmLastNotifiedDataSource)
// 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); return instance
});
});
} }
public void deleteDMLastNotified(final DMLastNotified dmLastNotified,
final RepositoryCallback<Void> 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<Void> 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);
});
});
}
} }

View File

@ -30,7 +30,6 @@ import awais.instagrabber.activities.MainActivity;
import awais.instagrabber.db.datasources.DMLastNotifiedDataSource; import awais.instagrabber.db.datasources.DMLastNotifiedDataSource;
import awais.instagrabber.db.entities.DMLastNotified; import awais.instagrabber.db.entities.DMLastNotified;
import awais.instagrabber.db.repositories.DMLastNotifiedRepository; import awais.instagrabber.db.repositories.DMLastNotifiedRepository;
import awais.instagrabber.db.repositories.RepositoryCallback;
import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.fragments.settings.PreferenceKeys;
import awais.instagrabber.managers.DirectMessagesManager; import awais.instagrabber.managers.DirectMessagesManager;
import awais.instagrabber.managers.InboxManager; 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.DirectItem;
import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.repositories.responses.directmessages.DirectThreadLastSeenAt; import awais.instagrabber.repositories.responses.directmessages.DirectThreadLastSeenAt;
import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CookieUtils;
import awais.instagrabber.utils.CoroutineUtilsKt;
import awais.instagrabber.utils.DMUtils; import awais.instagrabber.utils.DMUtils;
import awais.instagrabber.utils.DateUtils; import awais.instagrabber.utils.DateUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import kotlinx.coroutines.Dispatchers;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
@ -61,25 +63,26 @@ public class DMSyncService extends LifecycleService {
Log.d(TAG, "onCreate: Service created"); Log.d(TAG, "onCreate: Service created");
final DirectMessagesManager directMessagesManager = DirectMessagesManager.INSTANCE; final DirectMessagesManager directMessagesManager = DirectMessagesManager.INSTANCE;
inboxManager = directMessagesManager.getInboxManager(); 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) { private void parseUnread(@NonNull final DirectInbox directInbox) {
dmLastNotifiedRepository.getAllDMDmLastNotified(new RepositoryCallback<List<DMLastNotified>>() { dmLastNotifiedRepository.getAllDMDmLastNotified(
@Override CoroutineUtilsKt.getContinuation((result, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
public void onSuccess(final List<DMLastNotified> result) { if (throwable != null) {
Log.e(TAG, "parseUnread: ", throwable);
dmLastNotifiedMap = Collections.emptyMap();
parseUnreadActual(directInbox);
return;
}
dmLastNotifiedMap = result != null dmLastNotifiedMap = result != null
? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity())) ? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity()))
: Collections.emptyMap(); : Collections.emptyMap();
parseUnreadActual(directInbox); parseUnreadActual(directInbox);
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {
dmLastNotifiedMap = Collections.emptyMap();
parseUnreadActual(directInbox);
}
});
// Log.d(TAG, "inbox observer: " + directInbox); // Log.d(TAG, "inbox observer: " + directInbox);
} }
@ -117,17 +120,15 @@ public class DMSyncService extends LifecycleService {
} }
dmLastNotifiedRepository.insertOrUpdateDMLastNotified( dmLastNotifiedRepository.insertOrUpdateDMLastNotified(
lastNotifiedListBuilder.build(), lastNotifiedListBuilder.build(),
new RepositoryCallback<Void>() { CoroutineUtilsKt.getContinuation((unit, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
@Override try {
public void onSuccess(final Void result) { if (throwable != null) {
Log.e(TAG, "parseUnreadActual: ", throwable);
}
} finally {
stopSelf(); stopSelf();
} }
}), Dispatchers.getIO())
@Override
public void onDataNotAvailable() {
stopSelf();
}
}
); );
} }