瀏覽代碼

Refactor: CopyFileActivity

Wayne 4 年之前
父節點
當前提交
85f2bef002

+ 33 - 8
src/main/java/com/kdanmobile/reader/copyfile/CopyFileActivity.kt

@@ -1,12 +1,13 @@
 package com.kdanmobile.reader.copyfile
 
-import androidx.lifecycle.Observer
+import android.annotation.SuppressLint
 import android.content.Intent
+import android.os.Build
 import android.os.Bundle
+import androidx.lifecycle.Observer
 import com.kdanmobile.base.KdanBaseActivity
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.koin.ReadModuleKoinComponent
-import com.kdanmobile.reader.thumb.FileUtil
 import kotlinx.android.synthetic.main.activity_copy_file.*
 import org.koin.android.viewmodel.ext.android.viewModel
 import org.koin.core.parameter.parametersOf
@@ -17,7 +18,9 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
     abstract fun getKdanPdfReaderFolder(): File
     abstract fun provideReaderActivityIntent(filePath: String?): Intent
     open fun hasPermissionToAccessExternalStorage(): Boolean = true
-    open fun onFileCopyComplete(callback: () -> Unit) = callback.invoke()
+    open fun onFileCopyComplete() {
+        tryToStartReaderActivity()
+    }
 
     companion object {
         const val KEY_FILE_ABSOLUTE_PATH = "file_absolute_path"
@@ -39,18 +42,36 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
 
     private fun onCopyProgressUpdate(progress: Int?) {
         progress?.also {
-            progressBar.progress = it
-            tvProgressPercent.text = "$it%"
+            updateProgress(fileCopyProgressMap(progress))
+        }
+    }
+
+    /**
+     * You can change progress by overriding this method.
+     */
+    protected open fun fileCopyProgressMap(progress: Int): Int {
+        return progress
+    }
+
+    /**
+     * Force update progress value on screen.
+     */
+    @SuppressLint("SetTextI18n")
+    protected fun updateProgress(progress: Int) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+            progressBar.setProgress(progress, true)
+        } else {
+            progressBar.progress = progress
         }
+        tvProgressPercent.text = "$progress%"
     }
 
     private fun onEvent(event: CopyFileViewModel.Event?) {
         if (null == event) return
         when (event) {
             is CopyFileViewModel.Event.Success -> {
-                onFileCopyComplete {
-                    startReaderActivity(event.filePath)
-                }
+                onCopyProgressUpdate(100)
+                onFileCopyComplete()
             }
             is CopyFileViewModel.Event.Failed -> {
                 event.e.printStackTrace()
@@ -70,6 +91,10 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
         fileCannotOpenDialog.show(supportFragmentManager, fileCannotOpenDialog.tag)
     }
 
+    protected fun tryToStartReaderActivity() {
+        startReaderActivity(viewModel.dstFilePath)
+    }
+
     private fun startReaderActivity(filePath: String?) {
         startActivity(provideReaderActivityIntent(filePath))
         overridePendingTransition(0, 0)

+ 5 - 5
src/main/java/com/kdanmobile/reader/copyfile/CopyFileViewModel.kt

@@ -1,17 +1,16 @@
 package com.kdanmobile.reader.copyfile
 
+import android.content.Context
+import android.content.Intent
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
-import android.content.Context
-import android.content.Intent
 import com.kdanmobile.reader.event.EventBroadcaster
 import com.kdanmobile.reader.event.EventManager
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 import java.io.File
-import java.lang.Exception
 
 /**
  * 1. if info.shouldCopyFile == false,
@@ -36,7 +35,7 @@ class CopyFileViewModel(applicationContext: Context,
 
     sealed class Event {
         data class Success(val filePath: String?): Event()
-        data class Failed(val filePath: String?, val e: Throwable): Event()
+        data class Failed(val filePath: String?, val e: Throwable) : Event()
     }
 
     val copyProgressLiveData: LiveData<Int>
@@ -46,7 +45,8 @@ class CopyFileViewModel(applicationContext: Context,
 
     private var copyFileDisposable: Disposable? = null
     private var srcFilePath: String? = null
-    private var dstFilePath: String? = null
+    var dstFilePath: String? = null
+        private set
     private var hasCompleteCopy = false
 
     init {