From baecdd934805c486d0536804b143f8a12b1d0456 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Wed, 29 Aug 2018 23:16:41 -0400 Subject: [PATCH] Show number of messages per backup --- .../QKSMS/repository/BackupRepositoryImpl.kt | 29 +++++++++++++++---- .../com/moez/QKSMS/util/QkFileObserver.kt | 2 +- .../QKSMS/feature/backup/BackupController.kt | 5 +--- .../QKSMS/feature/backup/BackupPresenter.kt | 13 ++++++++- .../moez/QKSMS/feature/backup/BackupState.kt | 2 +- presentation/src/main/res/values/strings.xml | 1 + 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt index ec222ec1..a55608d5 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt @@ -52,9 +52,18 @@ class BackupRepositoryImpl @Inject constructor( } data class Backup( + val messageCount: Int = 0, val messages: List = listOf() ) + /** + * Simpler version of [Backup] which allows us to read certain fields from the backup without + * needing to parse the entire file + */ + data class BackupMetadata( + val messageCount: Int = 0 + ) + data class BackupMessage( val type: Int, val address: String, @@ -76,11 +85,13 @@ class BackupRepositoryImpl @Inject constructor( // If a backup or restore is already running, don't do anything if (isBackupOrRestoreRunning()) return + var messageCount = 0 + // Map all the messages into our object we'll use for the Json mapping val backupMessages = Realm.getDefaultInstance().use { realm -> // Get the messages from realm val messages = realm.where(Message::class.java).sort("date").findAll().createSnapshot() - val messageCount = messages.size + messageCount = messages.size // Map the messages to the new format val startTime = System.currentTimeMillis() @@ -103,7 +114,7 @@ class BackupRepositoryImpl @Inject constructor( // Convert the data to json val adapter = moshi.adapter(Backup::class.java).indent("\t") - val json = adapter.toJson(Backup(backupMessages)).toByteArray() + val json = adapter.toJson(Backup(messageCount, backupMessages)).toByteArray() try { // Create the directory and file @@ -129,7 +140,7 @@ class BackupRepositoryImpl @Inject constructor( protocol = 0, serviceCenter = null, locked = message.locked, - subId = message.subId) + subId = message.subId) override fun getBackupProgress(): Observable = backupProgress @@ -139,9 +150,14 @@ class BackupRepositoryImpl @Inject constructor( .observeOn(Schedulers.computation()) .map { files -> files.map { file -> + val backup = Okio.buffer(Okio.source(file)).use { source -> + moshi.adapter(BackupMetadata::class.java).fromJson(source) + } + + val path = file.path val date = file.lastModified() - val messages = 0 + val messages = backup?.messageCount ?: 0 val size = file.length() BackupFile(path, date, messages, size) } @@ -155,9 +171,10 @@ class BackupRepositoryImpl @Inject constructor( restoreProgress.onNext(BackupRepository.Progress.Running(0, "Parsing backup")) val file = File(filePath) - val source = Okio.buffer(Okio.source(file)) + val backup = Okio.buffer(Okio.source(file)).use { source -> + moshi.adapter(Backup::class.java).fromJson(source) + } - val backup = moshi.adapter(Backup::class.java).fromJson(source) val messageCount = backup?.messages?.size ?: 0 backup?.messages?.forEachIndexed { index, message -> diff --git a/data/src/main/java/com/moez/QKSMS/util/QkFileObserver.kt b/data/src/main/java/com/moez/QKSMS/util/QkFileObserver.kt index 27e79e94..f1f72809 100644 --- a/data/src/main/java/com/moez/QKSMS/util/QkFileObserver.kt +++ b/data/src/main/java/com/moez/QKSMS/util/QkFileObserver.kt @@ -23,7 +23,7 @@ import io.reactivex.Observable import io.reactivex.subjects.BehaviorSubject import java.io.File -class QkFileObserver(path: String) : FileObserver(path) { +class QkFileObserver(path: String) : FileObserver(path, CREATE or DELETE or MODIFY) { private val subject = BehaviorSubject.createDefault(path).toSerialized() diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt index e8fa6138..d43ff362 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt @@ -113,10 +113,7 @@ class BackupController : QkController( } } - backup.summary = when (state.lastBackup) { - null -> activity?.getString(R.string.backup_never) - else -> dateFormatter.getDetailedTimestamp(state.lastBackup) - } + backup.summary = state.lastBackup adapter.data = state.backups diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt index 836c71d4..bd70e1b5 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt @@ -24,6 +24,7 @@ import com.moez.QKSMS.R import com.moez.QKSMS.common.Navigator import com.moez.QKSMS.common.base.QkPresenter import com.moez.QKSMS.common.util.BillingManager +import com.moez.QKSMS.common.util.DateFormatter import com.moez.QKSMS.interactor.PerformBackup import com.moez.QKSMS.repository.BackupRepository import com.moez.QKSMS.service.RestoreBackupService @@ -37,6 +38,7 @@ class BackupPresenter @Inject constructor( private val backupRepo: BackupRepository, private val billingManager: BillingManager, private val context: Context, + private val dateFormatter: DateFormatter, private val navigator: Navigator, private val performBackup: PerformBackup ) : QkPresenter(BackupState()) { @@ -53,7 +55,16 @@ class BackupPresenter @Inject constructor( .subscribe { progress -> newState { copy(restoreProgress = progress) } } disposables += backupRepo.getBackups() - .subscribe { backups -> newState { copy(lastBackup = backups.map { it.date }.max(), backups = backups) } } + .doOnNext { backups -> newState { copy(backups = backups) } } + .map { backups -> backups.map { it.date }.max() ?: 0L } + .map { lastBackup -> + when (lastBackup) { + 0L -> context.getString(R.string.backup_never) + else -> dateFormatter.getDetailedTimestamp(lastBackup) + } + } + .startWith(context.getString(R.string.backup_loading)) + .subscribe { lastBackup -> newState { copy(lastBackup = lastBackup) } } disposables += billingManager.upgradeStatus .subscribe { upgraded -> newState { copy(upgraded = upgraded) } } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupState.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupState.kt index cc0b7e02..8547dee4 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupState.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupState.kt @@ -24,7 +24,7 @@ import com.moez.QKSMS.repository.BackupRepository data class BackupState( val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(), val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(), - val lastBackup: Long? = null, + val lastBackup: String = "", val backups: List = listOf(), val upgraded: Boolean = false ) \ No newline at end of file diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index d5bd2fa8..2f1dc6e8 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -149,6 +149,7 @@ Backing up messages Restoring from backup Last backup + Loading… Never Restore Select a backup