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:
parent
3d0b18e422
commit
22041df256
@ -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()
|
||||||
}
|
}
|
@ -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,
|
||||||
|
threadId,
|
||||||
private final DMLastNotifiedDao dmLastNotifiedDao;
|
lastNotifiedMsgTs,
|
||||||
|
lastNotifiedAt
|
||||||
private DMLastNotifiedDataSource(final DMLastNotifiedDao dmLastNotifiedDao) {
|
)
|
||||||
this.dmLastNotifiedDao = dmLastNotifiedDao;
|
if (dmLastNotified != null) {
|
||||||
|
dmLastNotifiedDao.updateDMLastNotified(toUpdate)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dmLastNotifiedDao.insertDMLastNotified(toUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DMLastNotifiedDataSource getInstance(@NonNull Context context) {
|
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified)
|
||||||
if (INSTANCE == null) {
|
|
||||||
synchronized (DMLastNotifiedDataSource.class) {
|
suspend fun deleteAllDMLastNotified() = dmLastNotifiedDao.deleteAllDMLastNotified()
|
||||||
if (INSTANCE == null) {
|
|
||||||
final AppDatabase database = AppDatabase.getDatabase(context);
|
companion object {
|
||||||
INSTANCE = new DMLastNotifiedDataSource(database.dmLastNotifiedDao());
|
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<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,
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -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) {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new DMLastNotifiedRepository(AppExecutors.INSTANCE, dmLastNotifiedDataSource);
|
|
||||||
}
|
}
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getDMLastNotified(final String threadId,
|
suspend fun insertOrUpdateDMLastNotified(
|
||||||
final RepositoryCallback<DMLastNotified> callback) {
|
threadId: String,
|
||||||
// request on the I/O thread
|
lastNotifiedMsgTs: LocalDateTime,
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
lastNotifiedAt: LocalDateTime,
|
||||||
final DMLastNotified dmLastNotified = dmLastNotifiedDataSource.getDMLastNotified(threadId);
|
): DMLastNotified? {
|
||||||
// notify on the main thread
|
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt)
|
||||||
appExecutors.getMainThread().execute(() -> {
|
return dmLastNotifiedDataSource.getDMLastNotified(threadId)
|
||||||
if (callback == null) return;
|
|
||||||
if (dmLastNotified == null) {
|
|
||||||
callback.onDataNotAvailable();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
callback.onSuccess(dmLastNotified);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getAllDMDmLastNotified(final RepositoryCallback<List<DMLastNotified>> callback) {
|
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified)
|
||||||
// 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,
|
suspend fun deleteAllDMLastNotified() = dmLastNotifiedDataSource.deleteAllDMLastNotified()
|
||||||
final RepositoryCallback<Void> callback) {
|
|
||||||
// request on the I/O thread
|
companion object {
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
private lateinit var instance: DMLastNotifiedRepository
|
||||||
for (final DMLastNotified dmLastNotified : dmLastNotifiedList) {
|
|
||||||
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(dmLastNotified.getThreadId(),
|
@JvmStatic
|
||||||
dmLastNotified.getLastNotifiedMsgTs(),
|
fun getInstance(dmLastNotifiedDataSource: DMLastNotifiedDataSource): DMLastNotifiedRepository {
|
||||||
dmLastNotified.getLastNotifiedAt());
|
if (!this::instance.isInitialized) {
|
||||||
|
instance = DMLastNotifiedRepository(dmLastNotifiedDataSource)
|
||||||
}
|
}
|
||||||
// notify on the main thread
|
return instance
|
||||||
appExecutors.getMainThread().execute(() -> {
|
}
|
||||||
if (callback == null) return;
|
|
||||||
callback.onSuccess(null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
callback.onSuccess(updated);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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) {
|
||||||
dmLastNotifiedMap = result != null
|
Log.e(TAG, "parseUnread: ", throwable);
|
||||||
? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity()))
|
dmLastNotifiedMap = Collections.emptyMap();
|
||||||
: Collections.emptyMap();
|
parseUnreadActual(directInbox);
|
||||||
parseUnreadActual(directInbox);
|
return;
|
||||||
}
|
}
|
||||||
|
dmLastNotifiedMap = result != null
|
||||||
@Override
|
? result.stream().collect(Collectors.toMap(DMLastNotified::getThreadId, Function.identity()))
|
||||||
public void onDataNotAvailable() {
|
: Collections.emptyMap();
|
||||||
dmLastNotifiedMap = Collections.emptyMap();
|
parseUnreadActual(directInbox);
|
||||||
parseUnreadActual(directInbox);
|
}), Dispatchers.getIO())
|
||||||
}
|
);
|
||||||
});
|
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user