|
@@ -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)
|