mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-23 06:08:52 +01:00
Show number of messages per backup
This commit is contained in:
@@ -52,9 +52,18 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
data class Backup(
|
data class Backup(
|
||||||
|
val messageCount: Int = 0,
|
||||||
val messages: List<BackupMessage> = listOf()
|
val messages: List<BackupMessage> = 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(
|
data class BackupMessage(
|
||||||
val type: Int,
|
val type: Int,
|
||||||
val address: String,
|
val address: String,
|
||||||
@@ -76,11 +85,13 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
// If a backup or restore is already running, don't do anything
|
// If a backup or restore is already running, don't do anything
|
||||||
if (isBackupOrRestoreRunning()) return
|
if (isBackupOrRestoreRunning()) return
|
||||||
|
|
||||||
|
var messageCount = 0
|
||||||
|
|
||||||
// Map all the messages into our object we'll use for the Json mapping
|
// Map all the messages into our object we'll use for the Json mapping
|
||||||
val backupMessages = Realm.getDefaultInstance().use { realm ->
|
val backupMessages = Realm.getDefaultInstance().use { realm ->
|
||||||
// Get the messages from realm
|
// Get the messages from realm
|
||||||
val messages = realm.where(Message::class.java).sort("date").findAll().createSnapshot()
|
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
|
// Map the messages to the new format
|
||||||
val startTime = System.currentTimeMillis()
|
val startTime = System.currentTimeMillis()
|
||||||
@@ -103,7 +114,7 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
// Convert the data to json
|
// Convert the data to json
|
||||||
val adapter = moshi.adapter(Backup::class.java).indent("\t")
|
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 {
|
try {
|
||||||
// Create the directory and file
|
// Create the directory and file
|
||||||
@@ -129,7 +140,7 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
protocol = 0,
|
protocol = 0,
|
||||||
serviceCenter = null,
|
serviceCenter = null,
|
||||||
locked = message.locked,
|
locked = message.locked,
|
||||||
subId = message.subId)
|
subId = message.subId)
|
||||||
|
|
||||||
override fun getBackupProgress(): Observable<BackupRepository.Progress> = backupProgress
|
override fun getBackupProgress(): Observable<BackupRepository.Progress> = backupProgress
|
||||||
|
|
||||||
@@ -139,9 +150,14 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
.observeOn(Schedulers.computation())
|
.observeOn(Schedulers.computation())
|
||||||
.map { files ->
|
.map { files ->
|
||||||
files.map { file ->
|
files.map { file ->
|
||||||
|
val backup = Okio.buffer(Okio.source(file)).use { source ->
|
||||||
|
moshi.adapter(BackupMetadata::class.java).fromJson(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
val path = file.path
|
val path = file.path
|
||||||
val date = file.lastModified()
|
val date = file.lastModified()
|
||||||
val messages = 0
|
val messages = backup?.messageCount ?: 0
|
||||||
val size = file.length()
|
val size = file.length()
|
||||||
BackupFile(path, date, messages, size)
|
BackupFile(path, date, messages, size)
|
||||||
}
|
}
|
||||||
@@ -155,9 +171,10 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
restoreProgress.onNext(BackupRepository.Progress.Running(0, "Parsing backup"))
|
restoreProgress.onNext(BackupRepository.Progress.Running(0, "Parsing backup"))
|
||||||
|
|
||||||
val file = File(filePath)
|
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
|
val messageCount = backup?.messages?.size ?: 0
|
||||||
|
|
||||||
backup?.messages?.forEachIndexed { index, message ->
|
backup?.messages?.forEachIndexed { index, message ->
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import io.reactivex.Observable
|
|||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
import java.io.File
|
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<String>(path).toSerialized()
|
private val subject = BehaviorSubject.createDefault<String>(path).toSerialized()
|
||||||
|
|
||||||
|
|||||||
@@ -113,10 +113,7 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backup.summary = when (state.lastBackup) {
|
backup.summary = state.lastBackup
|
||||||
null -> activity?.getString(R.string.backup_never)
|
|
||||||
else -> dateFormatter.getDetailedTimestamp(state.lastBackup)
|
|
||||||
}
|
|
||||||
|
|
||||||
adapter.data = state.backups
|
adapter.data = state.backups
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import com.moez.QKSMS.R
|
|||||||
import com.moez.QKSMS.common.Navigator
|
import com.moez.QKSMS.common.Navigator
|
||||||
import com.moez.QKSMS.common.base.QkPresenter
|
import com.moez.QKSMS.common.base.QkPresenter
|
||||||
import com.moez.QKSMS.common.util.BillingManager
|
import com.moez.QKSMS.common.util.BillingManager
|
||||||
|
import com.moez.QKSMS.common.util.DateFormatter
|
||||||
import com.moez.QKSMS.interactor.PerformBackup
|
import com.moez.QKSMS.interactor.PerformBackup
|
||||||
import com.moez.QKSMS.repository.BackupRepository
|
import com.moez.QKSMS.repository.BackupRepository
|
||||||
import com.moez.QKSMS.service.RestoreBackupService
|
import com.moez.QKSMS.service.RestoreBackupService
|
||||||
@@ -37,6 +38,7 @@ class BackupPresenter @Inject constructor(
|
|||||||
private val backupRepo: BackupRepository,
|
private val backupRepo: BackupRepository,
|
||||||
private val billingManager: BillingManager,
|
private val billingManager: BillingManager,
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
|
private val dateFormatter: DateFormatter,
|
||||||
private val navigator: Navigator,
|
private val navigator: Navigator,
|
||||||
private val performBackup: PerformBackup
|
private val performBackup: PerformBackup
|
||||||
) : QkPresenter<BackupView, BackupState>(BackupState()) {
|
) : QkPresenter<BackupView, BackupState>(BackupState()) {
|
||||||
@@ -53,7 +55,16 @@ class BackupPresenter @Inject constructor(
|
|||||||
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
|
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
|
||||||
|
|
||||||
disposables += backupRepo.getBackups()
|
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
|
disposables += billingManager.upgradeStatus
|
||||||
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
|
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import com.moez.QKSMS.repository.BackupRepository
|
|||||||
data class BackupState(
|
data class BackupState(
|
||||||
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||||
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||||
val lastBackup: Long? = null,
|
val lastBackup: String = "",
|
||||||
val backups: List<BackupFile> = listOf(),
|
val backups: List<BackupFile> = listOf(),
|
||||||
val upgraded: Boolean = false
|
val upgraded: Boolean = false
|
||||||
)
|
)
|
||||||
@@ -149,6 +149,7 @@
|
|||||||
<string name="backup_backing_up">Backing up messages</string>
|
<string name="backup_backing_up">Backing up messages</string>
|
||||||
<string name="backup_restoring">Restoring from backup</string>
|
<string name="backup_restoring">Restoring from backup</string>
|
||||||
<string name="backup_last">Last backup</string>
|
<string name="backup_last">Last backup</string>
|
||||||
|
<string name="backup_loading">Loading…</string>
|
||||||
<string name="backup_never">Never</string>
|
<string name="backup_never">Never</string>
|
||||||
<string name="backup_restore_title">Restore</string>
|
<string name="backup_restore_title">Restore</string>
|
||||||
<string name="backup_restore_summary">Select a backup</string>
|
<string name="backup_restore_summary">Select a backup</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user