1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +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.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<DMLastNotified> getAllDMDmLastNotified();
suspend fun getAllDMDmLastNotified(): List<DMLastNotified>
@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<Long> 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()
}

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;
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<DMLastNotified> = 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;
}
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(),
suspend fun insertOrUpdateDMLastNotified(
threadId: String,
lastNotifiedMsgTs: LocalDateTime,
lastNotifiedAt: LocalDateTime,
) {
val dmLastNotified = getDMLastNotified(threadId)
val toUpdate = DMLastNotified(
dmLastNotified?.id ?: 0,
threadId,
lastNotifiedMsgTs,
lastNotifiedAt);
lastNotifiedAt
)
if (dmLastNotified != null) {
dmLastNotifiedDao.updateDMLastNotified(toUpdate);
return;
dmLastNotifiedDao.updateDMLastNotified(toUpdate)
return
}
dmLastNotifiedDao.insertDMLastNotified(toUpdate);
dmLastNotifiedDao.insertDMLastNotified(toUpdate)
}
public final void deleteDMLastNotified(@NonNull final DMLastNotified dmLastNotified) {
dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified);
}
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified)
public final void deleteAllDMLastNotified() {
dmLastNotifiedDao.deleteAllDMLastNotified();
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
}
}
}

View File

@ -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<DMLastNotified> = dmLastNotifiedDataSource.getAllDMDmLastNotified()
private final AppExecutors appExecutors;
private final DMLastNotifiedDataSource dmLastNotifiedDataSource;
private DMLastNotifiedRepository(final AppExecutors appExecutors, final DMLastNotifiedDataSource dmLastNotifiedDataSource) {
this.appExecutors = appExecutors;
this.dmLastNotifiedDataSource = dmLastNotifiedDataSource;
suspend fun insertOrUpdateDMLastNotified(dmLastNotifiedList: List<DMLastNotified>) {
for (dmLastNotified in dmLastNotifiedList) {
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(
dmLastNotified.threadId,
dmLastNotified.lastNotifiedMsgTs,
dmLastNotified.lastNotifiedAt
)
}
}
public static DMLastNotifiedRepository getInstance(final DMLastNotifiedDataSource dmLastNotifiedDataSource) {
if (instance == null) {
instance = new DMLastNotifiedRepository(AppExecutors.INSTANCE, dmLastNotifiedDataSource);
}
return instance;
suspend fun insertOrUpdateDMLastNotified(
threadId: String,
lastNotifiedMsgTs: LocalDateTime,
lastNotifiedAt: LocalDateTime,
): DMLastNotified? {
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt)
return dmLastNotifiedDataSource.getDMLastNotified(threadId)
}
public void getDMLastNotified(final String threadId,
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);
});
});
}
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified)
public void getAllDMDmLastNotified(final RepositoryCallback<List<DMLastNotified>> callback) {
// 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);
});
});
}
suspend fun deleteAllDMLastNotified() = dmLastNotifiedDataSource.deleteAllDMLastNotified()
public void insertOrUpdateDMLastNotified(final List<DMLastNotified> dmLastNotifiedList,
final RepositoryCallback<Void> callback) {
// 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);
});
});
}
companion object {
private lateinit var instance: DMLastNotifiedRepository
public void insertOrUpdateDMLastNotified(final String threadId,
final LocalDateTime lastNotifiedMsgTs,
final LocalDateTime lastNotifiedAt,
final RepositoryCallback<DMLastNotified> 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;
@JvmStatic
fun getInstance(dmLastNotifiedDataSource: DMLastNotifiedDataSource): DMLastNotifiedRepository {
if (!this::instance.isInitialized) {
instance = DMLastNotifiedRepository(dmLastNotifiedDataSource)
}
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.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<List<DMLastNotified>>() {
@Override
public void onSuccess(final List<DMLastNotified> result) {
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);
}
@Override
public void onDataNotAvailable() {
dmLastNotifiedMap = 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<Void>() {
@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())
);
}