Show list of backups on restore click

This commit is contained in:
Moez Bhatti
2018-08-22 22:11:00 -04:00
parent b7605cd362
commit cde31e31a5
15 changed files with 390 additions and 28 deletions
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.repository
import android.os.Environment
import com.moez.QKSMS.model.Backup
import com.moez.QKSMS.util.QkFileObserver
import io.reactivex.Observable
import java.io.File
import javax.inject.Inject
class BackupRepositoryImpl @Inject constructor() : BackupRepository {
companion object {
private val BACKUP_LOCATION = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups"
}
override fun getBackups(): Observable<List<Backup>> = QkFileObserver(BACKUP_LOCATION)
.observable
.map { path -> File(path).listFiles() }
.map { files ->
files.map { file ->
val date = file.lastModified()
val size = file.length()
Backup(date, listOf(), size)
}
}
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.util
import android.os.FileObserver
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
class QkFileObserver(path: String) : FileObserver(path) {
private val subject = BehaviorSubject.createDefault<String>(path).toSerialized()
val observable: Observable<String> = subject
.doOnSubscribe { startWatching() }
.doOnDispose { stopWatching() }
.share()
override fun onEvent(event: Int, path: String?) {
path?.let(subject::onNext)
}
}
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.model
data class Backup(
val date: Long,
val messages: List<Long>,
val size: Long
)
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.repository
import com.moez.QKSMS.model.Backup
import io.reactivex.Observable
interface BackupRepository {
/**
* Returns a list of all local backups
*/
fun getBackups(): Observable<List<Backup>>
}
@@ -19,6 +19,7 @@
package com.moez.QKSMS.common.base
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.moez.QKSMS.common.util.extensions.setVisible
@@ -50,7 +51,7 @@ abstract class QkAdapter<T> : RecyclerView.Adapter<QkViewHolder>() {
var emptyView: View? = null
set(value) {
field = value
value?.setVisible(false)
field?.isVisible = data.isEmpty()
}
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
@@ -25,6 +25,7 @@ abstract class QkPresenter<View : QkViewContract<State>, State>(initialState: St
}
open fun onCleared() {
disposables.dispose()
}
}
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.backup
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.FlowableAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.model.Backup
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.backup_list_item.view.*
import javax.inject.Inject
class BackupAdapter @Inject constructor(
private val context: Context,
private val dateFormatter: DateFormatter
) : FlowableAdapter<Backup>() {
val backupSelected: Subject<Backup> = PublishSubject.create()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.backup_list_item, parent, false)
return QkViewHolder(view).apply {
view.setOnClickListener { backupSelected.onNext(getItem(adapterPosition)) }
}
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val backup = getItem(position)
val view = holder.itemView
val count = backup.messages.size
view.title.text = dateFormatter.getDetailedTimestamp(backup.date)
view.messages.text = context.resources.getQuantityString(R.plurals.backup_message_count, count, count)
view.size.text = Formatter.formatFileSize(context, backup.size)
}
}
@@ -18,9 +18,7 @@
*/
package com.moez.QKSMS.feature.backup
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.app.AlertDialog
import android.view.View
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
@@ -28,21 +26,26 @@ import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.model.Backup
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.backup_controller.*
import kotlinx.android.synthetic.main.backup_list_dialog.view.*
import javax.inject.Inject
class BackupController : QkController<BackupView, BackupState, BackupPresenter>(), BackupView {
companion object {
private const val REQUEST_CODE = 427
}
@Inject lateinit var adapter: BackupAdapter
@Inject override lateinit var presenter: BackupPresenter
private val restoreFileSubject: Subject<Uri> = PublishSubject.create()
private val dialog by lazy {
val view = View.inflate(activity, R.layout.backup_list_dialog, null)
.apply { files.adapter = adapter.apply { emptyView = empty } }
AlertDialog.Builder(activity)
.setView(view)
.setCancelable(true)
.create()
}
init {
appComponent.inject(this)
@@ -66,17 +69,11 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
data?.data?.run(restoreFileSubject::onNext)
}
}
override fun render(state: BackupState) {
restore.isEnabled = state.upgraded
adapter.data = state.backups
fabIcon.setImageResource(when (state.upgraded) {
true -> R.drawable.ic_file_upload_black_24dp
false -> R.drawable.ic_star_black_24dp
@@ -90,14 +87,10 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
override fun restoreClicks(): Observable<*> = restore.clicks()
override fun restoreFileSelected(): Observable<Uri> = restoreFileSubject
override fun restoreFileSelected(): Observable<Backup> = adapter.backupSelected
override fun fabClicks(): Observable<*> = fab.clicks()
override fun selectFile() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "*/*"
startActivityForResult(Intent.createChooser(intent, null), REQUEST_CODE)
}
override fun selectFile() = dialog.show()
}
@@ -21,17 +21,22 @@ package com.moez.QKSMS.feature.backup
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.common.util.BillingManager
import com.moez.QKSMS.repository.BackupRepository
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import javax.inject.Inject
class BackupPresenter @Inject constructor(
backupRepo: BackupRepository,
private val billingManager: BillingManager,
private val navigator: Navigator
) : QkPresenter<BackupView, BackupState>(BackupState()) {
init {
disposables += backupRepo.getBackups()
.subscribe { backups -> newState { copy(backups = backups) } }
disposables += billingManager.upgradeStatus
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
}
@@ -18,7 +18,10 @@
*/
package com.moez.QKSMS.feature.backup
import com.moez.QKSMS.model.Backup
data class BackupState(
val upgraded: Boolean = false,
val lastBackup: Long = 0
val lastBackup: Long = 0,
val backups: List<Backup> = listOf()
)
@@ -18,14 +18,15 @@
*/
package com.moez.QKSMS.feature.backup
import android.net.Uri
import com.moez.QKSMS.common.base.QkViewContract
import com.moez.QKSMS.model.Backup
import io.reactivex.Observable
import java.io.File
interface BackupView : QkViewContract<BackupState> {
fun restoreClicks(): Observable<*>
fun restoreFileSelected(): Observable<Uri>
fun restoreFileSelected(): Observable<Backup>
fun fabClicks(): Observable<*>
fun selectFile()
@@ -53,6 +53,8 @@ import com.moez.QKSMS.mapper.CursorToPartImpl
import com.moez.QKSMS.mapper.CursorToRecipient
import com.moez.QKSMS.mapper.CursorToRecipientImpl
import com.moez.QKSMS.mapper.RatingManagerImpl
import com.moez.QKSMS.repository.BackupRepository
import com.moez.QKSMS.repository.BackupRepositoryImpl
import com.moez.QKSMS.repository.ContactRepository
import com.moez.QKSMS.repository.ContactRepositoryImpl
import com.moez.QKSMS.repository.ConversationRepository
@@ -138,6 +140,9 @@ class AppModule(private var application: Application) {
// Repository
@Provides
fun provideBackupRepository(repository: BackupRepositoryImpl): BackupRepository = repository
@Provides
fun provideContactRepository(repository: ContactRepositoryImpl): ContactRepository = repository
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.moez.QKSMS.common.widget.QkTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="@string/backup_restore_dialog_title"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"
app:textSize="primary" />
<com.moez.QKSMS.common.widget.QkTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="2dp"
android:text="@string/backup_restore_dialog_summary"
android:textColor="?android:attr/textColorTertiary"
app:textSize="primary" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:background="?android:attr/divider" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingBottom="8dp"
android:paddingTop="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/backup_list_item" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:gravity="center"
android:text="@string/backup_restore_dialog_empty"
android:textColor="?android:attr/textColorSecondary"
app:textSize="secondary" />
</LinearLayout>
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp">
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:textSize="secondary"
tools:text="5/7/18 4:18 PM" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/messages"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
app:textSize="secondary"
tools:text="72,302 messages" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:textSize="secondary"
tools:text="31.8 MB" />
</androidx.constraintlayout.widget.ConstraintLayout>
@@ -150,6 +150,14 @@
<string name="backup_never">Never</string>
<string name="backup_restore_title">Restore</string>
<string name="backup_restore_summary">Select a backup</string>
<string name="backup_restore_dialog_title">Backups</string>
<string name="backup_restore_dialog_summary" translatable="false">/QKSMS/Backups</string>
<string name="backup_restore_dialog_empty">No backups found</string>
<plurals name="backup_message_count">
<item quantity="zero">No messages</item>
<item quantity="one">%d message</item>
<item quantity="other">%d messages</item>
</plurals>
<string name="backup_disclaimer">Currently, only SMS is supported by Backup and Restore. MMS support and scheduled backups will be coming soon!</string>
<string name="backup_now">Backup now</string>