From 9ef0a3a77c92a71873dc4ae317460766cd399979 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:10:24 +0800 Subject: [PATCH] up --- .../legado/app/ui/audio/AudioPlayActivity.kt | 64 +++++++++++++++++++ .../legado/app/ui/audio/AudioPlayViewModel.kt | 13 +++- .../app/ui/book/read/ReadBookActivity.kt | 2 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index 041226de0..87a01e5d5 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -4,10 +4,12 @@ import android.app.Activity import android.content.Intent import android.graphics.drawable.Drawable import android.os.Bundle +import android.view.KeyEvent import android.widget.SeekBar import androidx.lifecycle.Observer import com.bumptech.glide.RequestBuilder import com.bumptech.glide.request.RequestOptions +import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus @@ -15,15 +17,24 @@ import io.legado.app.constant.Status import io.legado.app.data.entities.BookChapter import io.legado.app.help.BlurTransformation import io.legado.app.help.ImageLoader +import io.legado.app.help.storage.Backup +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton import io.legado.app.service.help.AudioPlay import io.legado.app.ui.chapterlist.ChapterListActivity +import io.legado.app.ui.main.MainActivity +import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel import io.legado.app.utils.observeEvent +import io.legado.app.utils.snackbar import kotlinx.android.synthetic.main.activity_audio_play.* +import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.view_title_bar.* import org.apache.commons.lang3.time.DateFormatUtils import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onLongClick +import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult class AudioPlayActivity : VMBaseActivity(R.layout.activity_audio_play), @@ -136,6 +147,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } } observeEvent(Bus.AUDIO_PROGRESS) { + viewModel.durPageIndex = it if (!adjustProgress) player_progress.progress = it tv_dur_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") } @@ -145,6 +157,58 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } } + override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean { + when (keyCode) { + KeyEvent.KEYCODE_BACK -> { + page_view.snackbar(R.string.to_backstage, R.string.ok) { + startActivity() + } + return true + } + } + return super.onKeyLongPress(keyCode, event) + } + + override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { + when (keyCode) { + KeyEvent.KEYCODE_BACK -> { + event?.let { + if ((event.flags and KeyEvent.FLAG_CANCELED_LONG_PRESS == 0) + && event.isTracking + && !event.isCanceled + ) { + if (status == Status.PLAY) { + AudioPlay.pause(this) + return true + } + } + } + } + } + return super.onKeyUp(keyCode, event) + } + + override fun finish() { + viewModel.book?.let { + if (!viewModel.inBookshelf) { + this.alert(title = getString(R.string.add_to_shelf)) { + message = getString(R.string.check_add_bookshelf, it.name) + okButton { viewModel.inBookshelf = true } + noButton { viewModel.removeFromBookshelf { super.finish() } } + }.show().applyTint() + } else { + super.finish() + } + } ?: super.finish() + } + + override fun onDestroy() { + super.onDestroy() + if (!BuildConfig.DEBUG) { + Backup.autoBackup() + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index c960350e3..bcedc99f2 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -6,7 +6,6 @@ import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseViewModel -import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp @@ -24,7 +23,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) var callBack: CallBack? = null var durChapterIndex = 0 var durPageIndex = 0 - var isLocalBook = true var webBook: WebBook? = null private val loadingChapters = arrayListOf() @@ -42,7 +40,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) coverData.postValue(book.getDisplayCover()) durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos - isLocalBook = book.origin == BookType.local App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } @@ -223,6 +220,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } + fun removeFromBookshelf(success: (() -> Unit)?) { + execute { + book?.let { + App.db.bookDao().delete(it.bookUrl) + } + }.onSuccess { + success?.invoke() + } + } + interface CallBack { fun contentLoadFinish(bookChapter: BookChapter, content: String) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 9a4595a2b..f945a3197 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -199,7 +199,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean { when (keyCode) { KeyEvent.KEYCODE_BACK -> { - page_view.snackbar("转到后台", "确定") { + page_view.snackbar(R.string.to_backstage, R.string.ok) { startActivity() } return true diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index adf54661f..54eafa2b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -556,5 +556,6 @@ 类型: 文本 音频 + 转到后台