Browse Source

Merge branch '150-refactor-copy-file-activity' into 'master'

Resolve "refactor copy file activity"

Closes #150

See merge request android/module/pdfreadermodule!78
黃笠維 4 years ago
parent
commit
9d467dde39

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

@@ -1,12 +1,13 @@
 package com.kdanmobile.reader.copyfile
 package com.kdanmobile.reader.copyfile
 
 
-import androidx.lifecycle.Observer
+import android.annotation.SuppressLint
 import android.content.Intent
 import android.content.Intent
+import android.os.Build
 import android.os.Bundle
 import android.os.Bundle
+import androidx.lifecycle.Observer
 import com.kdanmobile.base.KdanBaseActivity
 import com.kdanmobile.base.KdanBaseActivity
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.koin.ReadModuleKoinComponent
 import com.kdanmobile.reader.koin.ReadModuleKoinComponent
-import com.kdanmobile.reader.thumb.FileUtil
 import kotlinx.android.synthetic.main.activity_copy_file.*
 import kotlinx.android.synthetic.main.activity_copy_file.*
 import org.koin.android.viewmodel.ext.android.viewModel
 import org.koin.android.viewmodel.ext.android.viewModel
 import org.koin.core.parameter.parametersOf
 import org.koin.core.parameter.parametersOf
@@ -17,7 +18,9 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
     abstract fun getKdanPdfReaderFolder(): File
     abstract fun getKdanPdfReaderFolder(): File
     abstract fun provideReaderActivityIntent(filePath: String?): Intent
     abstract fun provideReaderActivityIntent(filePath: String?): Intent
     open fun hasPermissionToAccessExternalStorage(): Boolean = true
     open fun hasPermissionToAccessExternalStorage(): Boolean = true
-    open fun onFileCopyComplete(callback: () -> Unit) = callback.invoke()
+    open fun onFileCopyComplete() {
+        tryToStartReaderActivity()
+    }
 
 
     companion object {
     companion object {
         const val KEY_FILE_ABSOLUTE_PATH = "file_absolute_path"
         const val KEY_FILE_ABSOLUTE_PATH = "file_absolute_path"
@@ -39,18 +42,36 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
 
 
     private fun onCopyProgressUpdate(progress: Int?) {
     private fun onCopyProgressUpdate(progress: Int?) {
         progress?.also {
         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?) {
     private fun onEvent(event: CopyFileViewModel.Event?) {
         if (null == event) return
         if (null == event) return
         when (event) {
         when (event) {
             is CopyFileViewModel.Event.Success -> {
             is CopyFileViewModel.Event.Success -> {
-                onFileCopyComplete {
-                    startReaderActivity(event.filePath)
-                }
+                onCopyProgressUpdate(100)
+                onFileCopyComplete()
             }
             }
             is CopyFileViewModel.Event.Failed -> {
             is CopyFileViewModel.Event.Failed -> {
                 event.e.printStackTrace()
                 event.e.printStackTrace()
@@ -70,6 +91,10 @@ abstract class CopyFileActivity : KdanBaseActivity(), ReadModuleKoinComponent {
         fileCannotOpenDialog.show(supportFragmentManager, fileCannotOpenDialog.tag)
         fileCannotOpenDialog.show(supportFragmentManager, fileCannotOpenDialog.tag)
     }
     }
 
 
+    protected fun tryToStartReaderActivity() {
+        startReaderActivity(viewModel.dstFilePath)
+    }
+
     private fun startReaderActivity(filePath: String?) {
     private fun startReaderActivity(filePath: String?) {
         startActivity(provideReaderActivityIntent(filePath))
         startActivity(provideReaderActivityIntent(filePath))
         overridePendingTransition(0, 0)
         overridePendingTransition(0, 0)

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

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