Browse Source
Convert DMLastNotifiedDao, DMLastNotifiedDataSource and DMLastNotifiedRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Convert DMLastNotifiedDao, DMLastNotifiedDataSource and DMLastNotifiedRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
4 changed files with 116 additions and 220 deletions
-
29app/src/main/java/awais/instagrabber/db/dao/DMLastNotifiedDao.kt
-
105app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt
-
143app/src/main/java/awais/instagrabber/db/repositories/DMLastNotifiedRepository.kt
-
53app/src/main/java/awais/instagrabber/services/DMSyncService.java
@ -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() |
|||
} |
@ -1,70 +1,53 @@ |
|||
package awais.instagrabber.db.datasources; |
|||
|
|||
import android.content.Context; |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import androidx.annotation.Nullable; |
|||
|
|||
import java.time.LocalDateTime; |
|||
import java.util.List; |
|||
|
|||
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(); |
|||
package awais.instagrabber.db.datasources |
|||
|
|||
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 |
|||
|
|||
class DMLastNotifiedDataSource private constructor(private val dmLastNotifiedDao: DMLastNotifiedDao) { |
|||
suspend fun getDMLastNotified(threadId: String): DMLastNotified? = dmLastNotifiedDao.findDMLastNotifiedByThreadId(threadId) |
|||
|
|||
suspend fun getAllDMDmLastNotified(): List<DMLastNotified> = dmLastNotifiedDao.getAllDMDmLastNotified() |
|||
|
|||
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) |
|||
} |
|||
|
|||
private static DMLastNotifiedDataSource INSTANCE; |
|||
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDao.deleteDMLastNotified(dmLastNotified) |
|||
|
|||
private final DMLastNotifiedDao dmLastNotifiedDao; |
|||
suspend fun deleteAllDMLastNotified() = dmLastNotifiedDao.deleteAllDMLastNotified() |
|||
|
|||
private DMLastNotifiedDataSource(final DMLastNotifiedDao dmLastNotifiedDao) { |
|||
this.dmLastNotifiedDao = dmLastNotifiedDao; |
|||
} |
|||
companion object { |
|||
private lateinit var INSTANCE: DMLastNotifiedDataSource |
|||
|
|||
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()); |
|||
@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 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; |
|||
} |
|||
|
|||
public static DMLastNotifiedRepository getInstance(final DMLastNotifiedDataSource dmLastNotifiedDataSource) { |
|||
if (instance == null) { |
|||
instance = new DMLastNotifiedRepository(AppExecutors.INSTANCE, dmLastNotifiedDataSource); |
|||
suspend fun insertOrUpdateDMLastNotified(dmLastNotifiedList: List<DMLastNotified>) { |
|||
for (dmLastNotified in dmLastNotifiedList) { |
|||
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified( |
|||
dmLastNotified.threadId, |
|||
dmLastNotified.lastNotifiedMsgTs, |
|||
dmLastNotified.lastNotifiedAt |
|||
) |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
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 insertOrUpdateDMLastNotified( |
|||
threadId: String, |
|||
lastNotifiedMsgTs: LocalDateTime, |
|||
lastNotifiedAt: LocalDateTime, |
|||
): DMLastNotified? { |
|||
dmLastNotifiedDataSource.insertOrUpdateDMLastNotified(threadId, lastNotifiedMsgTs, lastNotifiedAt) |
|||
return dmLastNotifiedDataSource.getDMLastNotified(threadId) |
|||
} |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
suspend fun deleteDMLastNotified(dmLastNotified: DMLastNotified) = dmLastNotifiedDataSource.deleteDMLastNotified(dmLastNotified) |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
suspend fun deleteAllDMLastNotified() = dmLastNotifiedDataSource.deleteAllDMLastNotified() |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
companion object { |
|||
private lateinit var instance: DMLastNotifiedRepository |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
@JvmStatic |
|||
fun getInstance(dmLastNotifiedDataSource: DMLastNotifiedDataSource): DMLastNotifiedRepository { |
|||
if (!this::instance.isInitialized) { |
|||
instance = DMLastNotifiedRepository(dmLastNotifiedDataSource) |
|||
} |
|||
return instance |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue