From d5142eda4c5e98cbaaae3e82998e61fbe4184bc6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 08:42:54 +0800 Subject: [PATCH 01/35] up --- app/src/debug/google-services .json | 69 ------------------- .../debug/google-services.json} | 0 build.gradle | 2 +- 3 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 app/src/debug/google-services .json rename app/{google-services .json => src/debug/google-services.json} (100%) diff --git a/app/src/debug/google-services .json b/app/src/debug/google-services .json deleted file mode 100644 index 8cc19b1d5..000000000 --- a/app/src/debug/google-services .json +++ /dev/null @@ -1,69 +0,0 @@ -{ - "project_info": { - "project_number": "453392274790", - "firebase_url": "https://legado-fca69.firebaseio.com", - "project_id": "legado-fca69", - "storage_bucket": "legado-fca69.appspot.com" - }, - "client": [ - { - "client_info": { - "mobilesdk_app_id": "1:453392274790:android:1d2b1eefbe0e78cff624a7", - "android_client_info": { - "package_name": "io.legado.app" - } - }, - "oauth_client": [ - { - "client_id": "453392274790-hnbpatpce9hbjiggj76hgo7queu86atq.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyD90mfNLhA7cAzzI9SonpSz5mrF5BnmyJA" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "453392274790-hnbpatpce9hbjiggj76hgo7queu86atq.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - }, - { - "client_info": { - "mobilesdk_app_id": "1:453392274790:android:c4eac14b1410eec5f624a7", - "android_client_info": { - "package_name": "io.legado.app.debug" - } - }, - "oauth_client": [ - { - "client_id": "453392274790-hnbpatpce9hbjiggj76hgo7queu86atq.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyD90mfNLhA7cAzzI9SonpSz5mrF5BnmyJA" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "453392274790-hnbpatpce9hbjiggj76hgo7queu86atq.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - } - ], - "configuration_version": "1" -} \ No newline at end of file diff --git a/app/google-services .json b/app/src/debug/google-services.json similarity index 100% rename from app/google-services .json rename to app/src/debug/google-services.json diff --git a/build.gradle b/build.gradle index f880bb0e5..dd59b4c6c 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { maven { url 'https://plugins.gradle.org/m2/' } } dependencies { - classpath 'com.android.tools.build:gradle:3.5.1' + classpath 'com.android.tools.build:gradle:3.5.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'de.timfreiheit.resourceplaceholders:placeholders:0.3' classpath 'com.google.gms:google-services:4.3.2' From ebc606cf47c10b5e163cb896eafc5a4f504287a9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 09:05:28 +0800 Subject: [PATCH 02/35] up --- .../java/io/legado/app/ui/audio/AudioPlayActivity.kt | 2 +- .../java/io/legado/app/ui/audio/AudioPlayViewModel.kt | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 d2a7e19ce..a1c3c145f 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 @@ -45,7 +45,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a true } iv_skip_next.onClick { - + viewModel.moveToNext() } iv_skip_previous.onClick { 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 458097f54..a3d3877d0 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 @@ -184,6 +184,17 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } + fun moveToPrev() { + if (durChapterIndex > 0) { + durChapterIndex-- + bookData.value?.let { + it.durChapterIndex = durChapterIndex + saveRead(it) + loadContent(it, durChapterIndex) + } + } + } + fun moveToNext() { if (durChapterIndex < chapterSize - 1) { durChapterIndex++ From e62d289f1b59ac63a6359d51f068342c9accd597 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 09:05:57 +0800 Subject: [PATCH 03/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a1c3c145f..6953ed891 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 @@ -48,7 +48,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a viewModel.moveToNext() } iv_skip_previous.onClick { - + viewModel.moveToPrev() } player_progress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { From f6f472ed4854fb653a7ef03d31bfb9061b1d13f7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 11:10:54 +0800 Subject: [PATCH 04/35] up --- .../app/receiver/MediaButtonReceiver.kt | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 1df892cda..330956351 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -4,11 +4,18 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.view.KeyEvent +import io.legado.app.App import io.legado.app.constant.Bus +import io.legado.app.data.entities.Book import io.legado.app.help.ActivityHelp import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.utils.postEvent +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** @@ -33,15 +40,24 @@ class MediaButtonReceiver : BroadcastReceiver() { } private fun readAloud(context: Context) { - if (ActivityHelp.isExist(AudioPlayActivity::class.java)) { - postEvent(Bus.AUDIO_PLAY_BUTTON, true) - } else if (!ActivityHelp.isExist(ReadBookActivity::class.java)) { - val intent = Intent(context, ReadBookActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - intent.putExtra("readAloud", true) - context.startActivity(intent) - } else { - postEvent(Bus.READ_ALOUD_BUTTON, true) + when { + ActivityHelp.isExist(AudioPlayActivity::class.java) -> + postEvent(Bus.AUDIO_PLAY_BUTTON, true) + ActivityHelp.isExist(ReadBookActivity::class.java) -> + postEvent(Bus.READ_ALOUD_BUTTON, true) + else -> { + GlobalScope.launch(Main) { + val lastBook: Book? = withContext(IO) { + App.db.bookDao().lastReadBook + } + lastBook?.let { + val intent = Intent(context, ReadBookActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putExtra("readAloud", true) + context.startActivity(intent) + } + } + } } } } From c3628cf44c9291fa519da701de501781ee62fc50 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 11:29:43 +0800 Subject: [PATCH 05/35] up --- .../io/legado/app/ui/book/read/ReadBookActivity.kt | 12 ++++++++++-- .../io/legado/app/ui/book/read/ReadBookViewModel.kt | 6 +++--- .../legado/app/ui/chapterlist/ChapterListFragment.kt | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) 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 b2dd3dae0..fdb8d1808 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 @@ -64,6 +64,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override val viewModel: ReadBookViewModel get() = getViewModel(ReadBookViewModel::class.java) + private val requestCodeChapterList = 568 private val requestCodeEditSource = 111 private var timeElectricityReceiver: TimeElectricityReceiver? = null override var readAloudStatus = Status.STOP @@ -436,7 +437,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun openChapterList() { viewModel.bookData.value?.let { - startActivity(Pair("bookUrl", it.bookUrl)) + startActivityForResult( + requestCodeChapterList, + Pair("bookUrl", it.bookUrl) + ) } } @@ -510,6 +514,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo if (resultCode == Activity.RESULT_OK) { when (requestCode) { requestCodeEditSource -> viewModel.upBookSource() + requestCodeChapterList -> + data?.getIntExtra("index", viewModel.durChapterIndex)?.let { + viewModel.openChapter(it) + } } } } @@ -552,7 +560,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo observeEvent(Bus.TIME_CHANGED) { page_view.upTime() } observeEvent(Bus.BATTERY_CHANGED) { page_view.upBattery(it) } observeEvent(Bus.OPEN_CHAPTER) { - viewModel.openChapter(it) + viewModel.openChapter(it.index) page_view.upContent() } observeEvent(Bus.READ_ALOUD_BUTTON) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index daf901f60..5880bec5c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -279,12 +279,12 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - fun openChapter(chapter: BookChapter) { + fun openChapter(index: Int) { prevTextChapter = null curTextChapter = null nextTextChapter = null - if (chapter.index != durChapterIndex) { - durChapterIndex = chapter.index + if (index != durChapterIndex) { + durChapterIndex = index durPageIndex = 0 } saveRead() diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 4d3e947b6..97092ab13 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -1,5 +1,7 @@ package io.legado.app.ui.chapterlist +import android.app.Activity.RESULT_OK +import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout @@ -9,11 +11,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment -import io.legado.app.constant.Bus import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.utils.getViewModelOfActivity -import io.legado.app.utils.postEvent import kotlinx.android.synthetic.main.fragment_chapter_list.* import org.jetbrains.anko.sdk27.listeners.onClick @@ -83,7 +83,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } override fun openChapter(bookChapter: BookChapter) { - postEvent(Bus.OPEN_CHAPTER, bookChapter) + activity?.setResult(RESULT_OK, Intent().putExtra("index", bookChapter.index)) activity?.finish() } From e6b613d92af33e651c577a3eb8150c7da8a20317 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 11:56:18 +0800 Subject: [PATCH 06/35] up --- .../io/legado/app/ui/audio/AudioPlayActivity.kt | 7 +++++-- app/src/main/res/layout/activity_audio_play.xml | 15 +++++++++------ 2 files changed, 14 insertions(+), 8 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 6953ed891..cd114bc68 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 @@ -16,6 +16,7 @@ import io.legado.app.utils.getViewModel import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_audio_play.* 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 @@ -52,7 +53,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } player_progress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - + tv_dur_time.text = DateFormatUtils.format(progress.toLong(), "mm:ss") } override fun onStartTrackingTouch(seekBar: SeekBar?) { @@ -67,7 +68,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } private fun upView(book: Book) { - actionBar?.title = book.name + title_bar.title = book.name ImageLoader.load(this, book.getDisplayCover()) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) @@ -121,9 +122,11 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } observeEvent(Bus.AUDIO_PROGRESS) { if (!adjustProgress) player_progress.progress = it + tv_dur_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") } observeEvent(Bus.AUDIO_SIZE) { player_progress.max = it + tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml index 3c2a02ca5..a4b06402e 100644 --- a/app/src/main/res/layout/activity_audio_play.xml +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -50,15 +50,16 @@ android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" - android:paddingStart="5dp" - android:paddingEnd="5dp" + android:paddingStart="16dp" + android:paddingEnd="16dp" app:layout_constraintBottom_toTopOf="@+id/ll_play_menu"> + android:text="00:00" + tools:ignore="HardcodedText,RtlSymmetry" /> + android:layout_height="wrap_content" + android:text="00:00" + tools:ignore="HardcodedText" /> @@ -78,8 +81,8 @@ android:id="@+id/ll_play_menu" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingLeft="15dp" - android:paddingRight="15dp" + android:paddingLeft="24dp" + android:paddingRight="24dp" android:gravity="center_vertical" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent"> From 63099be772d2ddd1870950ae59c29cbe981fc9d2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 12:23:44 +0800 Subject: [PATCH 07/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 2 +- app/src/main/res/layout/activity_audio_play.xml | 2 -- 2 files changed, 1 insertion(+), 3 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 cd114bc68..d2fbf7c67 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 @@ -77,8 +77,8 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a ImageLoader.load(this, book.getDisplayCover()) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) - .centerCrop() .bitmapTransform(BlurTransformation(this, 25)) + .centerCrop() .setAsDrawable(iv_bg) } diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml index a4b06402e..540fec706 100644 --- a/app/src/main/res/layout/activity_audio_play.xml +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -33,10 +33,8 @@ android:id="@+id/iv_cover" android:layout_width="260dp" android:layout_height="260dp" - android:layout_gravity="center" android:contentDescription="@string/img_cover" android:scaleType="centerCrop" - android:src="@drawable/image_cover_default" app:civ_border_width="2dp" app:civ_border_color="@color/colorAccent" app:layout_constraintBottom_toTopOf="@+id/ll_player_progress" From fda8c741cb25ca091b72ddc274e4dc3fcb71109b Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 12:27:24 +0800 Subject: [PATCH 08/35] up --- app/src/main/java/io/legado/app/service/AudioPlayService.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index fb7e85e68..5b5dc842e 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -109,6 +109,7 @@ class AudioPlayService : BaseService(), } catch (e: Exception) { launch { toast(e.localizedMessage + " " + url) + stopSelf() } } } From 132362f0e1052a25f4475bde61ee1c948eb8bd57 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 12:28:46 +0800 Subject: [PATCH 09/35] up --- app/src/main/java/io/legado/app/service/AudioPlayService.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 5b5dc842e..468a3d4ba 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -154,6 +154,9 @@ class AudioPlayService : BaseService(), } override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { + launch { + toast("error: $what $extra") + } return true } From 68a5889ff22234eba7589b2ab4f752703357345b Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 12:31:01 +0800 Subject: [PATCH 10/35] up --- app/src/main/java/io/legado/app/service/AudioPlayService.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 468a3d4ba..3b1c92eec 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -24,6 +24,7 @@ import io.legado.app.help.IntentHelp import io.legado.app.help.MediaHelp import io.legado.app.receiver.MediaButtonReceiver import io.legado.app.ui.book.read.ReadBookActivity +import io.legado.app.utils.LogUtils import io.legado.app.utils.postEvent import kotlinx.coroutines.launch import org.jetbrains.anko.toast @@ -107,6 +108,7 @@ class AudioPlayService : BaseService(), mediaPlayer.setDataSource(url) mediaPlayer.prepareAsync() } catch (e: Exception) { + LogUtils.d("AudioPlayService", e.localizedMessage + " " + url) launch { toast(e.localizedMessage + " " + url) stopSelf() From 37feed6768eec25b05b1deeddadeaee189f0e35d Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 14:03:43 +0800 Subject: [PATCH 11/35] up --- .../app/ui/book/read/ReadBookActivity.kt | 20 ++++---- .../app/ui/book/read/ReadBookViewModel.kt | 50 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) 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 fdb8d1808..69e252234 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 @@ -74,7 +74,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo setSupportActionBar(toolbar) initView() viewModel.callBack = this - viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) + viewModel.titleDate.observe(this, Observer { title_bar.title = it }) viewModel.initData(intent) } @@ -141,12 +141,12 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo when (item.itemId) { R.id.menu_change_source -> { read_menu.runMenuOut() - viewModel.bookData.value?.let { + viewModel.book?.let { ChangeSourceDialog.show(supportFragmentManager, it.name, it.author) } } R.id.menu_refresh -> { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.curTextChapter = null page_view.upContent() viewModel.refreshContent(it) @@ -255,7 +255,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 加载章节内容 */ override fun loadContent() { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(it, viewModel.durChapterIndex - 1) @@ -266,7 +266,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 加载章节内容, index章节序号 */ override fun loadContent(index: Int) { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.loadContent(it, index) } } @@ -337,10 +337,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override val curOrigin: String? - get() = viewModel.bookData.value?.origin + get() = viewModel.book?.origin override val oldBook: Book? - get() = viewModel.bookData.value + get() = viewModel.book override fun changeTo(book: Book) { viewModel.changeTo(book) @@ -436,7 +436,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override fun openChapterList() { - viewModel.bookData.value?.let { + viewModel.book?.let { startActivityForResult( requestCodeChapterList, Pair("bookUrl", it.bookUrl) @@ -475,7 +475,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 朗读 */ private fun readAloud(play: Boolean = true) { - val book = viewModel.bookData.value + val book = viewModel.book val textChapter = viewModel.curTextChapter if (book != null && textChapter != null) { val key = IntentDataHelp.putData(textChapter) @@ -523,7 +523,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override fun finish() { - viewModel.bookData.value?.let { + viewModel.book?.let { if (!viewModel.inBookshelf) { this.alert(title = getString(R.string.add_to_shelf)) { message = getString(R.string.check_add_bookshelf, it.name) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 5880bec5c..4144bee78 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -21,8 +21,9 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class ReadBookViewModel(application: Application) : BaseViewModel(application) { + var titleDate = MutableLiveData() + var book: Book? = null var inBookshelf = false - var bookData = MutableLiveData() var chapterSize = 0 var callBack: CallBack? = null var durChapterIndex = 0 @@ -38,22 +39,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { inBookshelf = intent.getBooleanExtra("inBookshelf", true) IntentDataHelp.getData(intent.getStringExtra("key"))?.let { - setBook(it) + initBook(it) } ?: intent.getStringExtra("bookUrl")?.let { App.db.bookDao().getBook(it)?.let { book -> - setBook(book) + initBook(book) } } ?: App.db.bookDao().lastReadBook?.let { - setBook(it) + initBook(it) } } } - private fun setBook(book: Book) { + private fun initBook(book: Book) { durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local - bookData.postValue(book) + this.book = book + titleDate.postValue(book.name) App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } @@ -72,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { callBack?.loadContent() } if (inBookshelf) { - saveRead(book) + saveRead() } } @@ -117,7 +119,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { prevTextChapter = curTextChapter curTextChapter = nextTextChapter nextTextChapter = null - bookData.value?.let { + book?.let { if (curTextChapter == null) { loadContent(it, durChapterIndex) } else if (upContent) { @@ -127,7 +129,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { launch(IO) { for (i in 2..10) { delay(100) - bookData.value?.let { book -> + book?.let { book -> download(book, durChapterIndex + i) } } @@ -140,7 +142,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { nextTextChapter = curTextChapter curTextChapter = prevTextChapter prevTextChapter = null - bookData.value?.let { + book?.let { if (curTextChapter == null) { loadContent(it, durChapterIndex) } else if (upContent) { @@ -150,7 +152,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { launch(IO) { for (i in -5..-2) { delay(100) - bookData.value?.let { book -> + book?.let { book -> download(book, durChapterIndex + i) } } @@ -225,19 +227,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { val c = BookHelp.disposeContent( chapter.title, - bookData.value!!.name, + book!!.name, webBook?.bookSource?.bookSourceUrl, content, - bookData.value!!.useReplaceRule + book!!.useReplaceRule ) callBack?.contentLoadFinish(chapter, c) } } } - fun changeTo(book: Book) { + fun changeTo(book1: Book) { execute { - bookData.value?.let { + book?.let { App.db.bookDao().delete(it.bookUrl) } prevTextChapter = null @@ -246,15 +248,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { withContext(Main) { callBack?.upContent() } - App.db.bookDao().insert(book) - bookData.postValue(book) - App.db.bookSourceDao().getBookSource(book.origin)?.let { + App.db.bookDao().insert(book1) + book = book1 + App.db.bookSourceDao().getBookSource(book1.origin)?.let { webBook = WebBook(it) } - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) { upChangeDurChapterIndex(book, it) } + if (book1.tocUrl.isEmpty()) { + loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } } else { - loadChapterList(book) { upChangeDurChapterIndex(book, it) } + loadChapterList(book1) { upChangeDurChapterIndex(book1, it) } } } } @@ -291,7 +293,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { callBack?.loadContent() } - fun saveRead(book: Book? = bookData.value) { + fun saveRead() { execute { book?.let { book -> book.lastCheckCount = 0 @@ -308,7 +310,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun removeFromBookshelf(success: (() -> Unit)?) { execute { - bookData.value?.let { + book?.let { App.db.bookDao().delete(it.bookUrl) } }.onSuccess { @@ -318,7 +320,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun upBookSource() { execute { - bookData.value?.let { book -> + book?.let { book -> App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } From 539164a94976dde55d298a2ee82e3f3e695e3c43 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 14:19:28 +0800 Subject: [PATCH 12/35] up --- .../legado/app/ui/audio/AudioPlayActivity.kt | 17 ++++---- .../legado/app/ui/audio/AudioPlayViewModel.kt | 40 ++++++++++--------- 2 files changed, 29 insertions(+), 28 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 d2fbf7c67..d9f36d6a5 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 @@ -7,7 +7,6 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus import io.legado.app.constant.Status -import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BlurTransformation import io.legado.app.help.ImageLoader @@ -32,7 +31,8 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) viewModel.callBack = this - viewModel.bookData.observe(this, Observer { upView(it) }) + viewModel.titleData.observe(this, Observer { title_bar.title = it }) + viewModel.coverData.observe(this, Observer { upCover(it) }) viewModel.initData(intent) initView() } @@ -67,14 +67,13 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a }) } - private fun upView(book: Book) { - title_bar.title = book.name - ImageLoader.load(this, book.getDisplayCover()) + private fun upCover(path: String) { + ImageLoader.load(this, path) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() .setAsDrawable(iv_cover) - ImageLoader.load(this, book.getDisplayCover()) + ImageLoader.load(this, path) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .bitmapTransform(BlurTransformation(this, 25)) @@ -86,7 +85,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a when (status) { Status.PLAY -> AudioPlay.pause(this) Status.PAUSE -> AudioPlay.resume(this) - else -> viewModel.bookData.value?.let { + else -> viewModel.book?.let { viewModel.loadContent(it, viewModel.durChapterIndex) } } @@ -95,12 +94,12 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a override fun contentLoadFinish(bookChapter: BookChapter, content: String) { AudioPlay.play( this, - viewModel.bookData.value?.name, + viewModel.book?.name, bookChapter.title, content, viewModel.durPageIndex ) - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.loadContent(it, viewModel.durChapterIndex + 1) } } 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 a3d3877d0..48f022f76 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 @@ -16,8 +16,10 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext class AudioPlayViewModel(application: Application) : BaseViewModel(application) { + var titleData = MutableLiveData() + var coverData = MutableLiveData() + var book: Book? = null var inBookshelf = false - var bookData = MutableLiveData() var chapterSize = 0 var callBack: CallBack? = null var durChapterIndex = 0 @@ -30,16 +32,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) execute { inBookshelf = intent.getBooleanExtra("inBookshelf", true) val bookUrl = intent.getStringExtra("bookUrl") - val book = if (!bookUrl.isNullOrEmpty()) { + book = if (!bookUrl.isNullOrEmpty()) { App.db.bookDao().getBook(bookUrl) } else { App.db.bookDao().lastReadBook } - book?.let { + book?.let { book -> + titleData.postValue(book.name) durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local - bookData.postValue(book) App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } @@ -57,7 +59,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) chapterSize = count } } - saveRead(book) + saveRead() } } @@ -148,23 +150,23 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun changeTo(book: Book) { + fun changeTo(book1: Book) { execute { - bookData.value?.let { + book?.let { App.db.bookDao().delete(it.bookUrl) } withContext(Dispatchers.Main) { } - App.db.bookDao().insert(book) - bookData.postValue(book) - App.db.bookSourceDao().getBookSource(book.origin)?.let { + App.db.bookDao().insert(book1) + book = book1 + App.db.bookSourceDao().getBookSource(book1.origin)?.let { webBook = WebBook(it) } - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) { upChangeDurChapterIndex(book, it) } + if (book1.tocUrl.isEmpty()) { + loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } } else { - loadChapterList(book) { upChangeDurChapterIndex(book, it) } + loadChapterList(book1) { upChangeDurChapterIndex(book1, it) } } } } @@ -187,9 +189,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun moveToPrev() { if (durChapterIndex > 0) { durChapterIndex-- - bookData.value?.let { - it.durChapterIndex = durChapterIndex - saveRead(it) + book?.durChapterIndex = durChapterIndex + saveRead() + book?.let { loadContent(it, durChapterIndex) } } @@ -198,9 +200,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun moveToNext() { if (durChapterIndex < chapterSize - 1) { durChapterIndex++ - bookData.value?.let { + book?.let { it.durChapterIndex = durChapterIndex - saveRead(it) + saveRead() loadContent(it, durChapterIndex) } } else { @@ -208,7 +210,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun saveRead(book: Book? = bookData.value) { + fun saveRead() { execute { book?.let { book -> book.lastCheckCount = 0 From 071e7c424892e993f70556903a176ffe1a9695b4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 14:25:04 +0800 Subject: [PATCH 13/35] up --- .../main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 4144bee78..cc4032a00 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -51,11 +51,11 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } private fun initBook(book: Book) { + this.book = book + titleDate.postValue(book.name) durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local - this.book = book - titleDate.postValue(book.name) App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } From 973a5ea16354961b71a5548b63b5ddad8ba63073 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 14:43:09 +0800 Subject: [PATCH 14/35] up --- .../legado/app/ui/audio/AudioPlayActivity.kt | 34 +++++-- .../legado/app/ui/audio/AudioPlayViewModel.kt | 64 ++++++------- .../app/ui/book/read/ReadBookActivity.kt | 12 +-- .../app/ui/book/read/ReadBookViewModel.kt | 96 ++++++++++--------- 4 files changed, 113 insertions(+), 93 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 d9f36d6a5..91ec8bd10 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 @@ -1,5 +1,7 @@ package io.legado.app.ui.audio +import android.app.Activity +import android.content.Intent import android.os.Bundle import android.widget.SeekBar import androidx.lifecycle.Observer @@ -11,6 +13,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.help.BlurTransformation import io.legado.app.help.ImageLoader import io.legado.app.service.help.AudioPlay +import io.legado.app.ui.chapterlist.ChapterListActivity import io.legado.app.utils.getViewModel import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_audio_play.* @@ -18,6 +21,7 @@ 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.startActivityForResult class AudioPlayActivity : VMBaseActivity(R.layout.activity_audio_play), AudioPlayViewModel.CallBack { @@ -25,6 +29,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a override val viewModel: AudioPlayViewModel get() = getViewModel(AudioPlayViewModel::class.java) + private var requestCodeChapter = 8461 private var adjustProgress = false private var status = Status.STOP @@ -65,6 +70,14 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a AudioPlay.adjustProgress(this@AudioPlayActivity, player_progress.progress) } }) + iv_chapter.onClick { + viewModel.book?.let { + startActivityForResult( + requestCodeChapter, + Pair("bookUrl", it.bookUrl) + ) + } + } } private fun upCover(path: String) { @@ -85,9 +98,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a when (status) { Status.PLAY -> AudioPlay.pause(this) Status.PAUSE -> AudioPlay.resume(this) - else -> viewModel.book?.let { - viewModel.loadContent(it, viewModel.durChapterIndex) - } + else -> viewModel.loadContent(viewModel.durChapterIndex) } } @@ -99,9 +110,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a content, viewModel.durPageIndex ) - viewModel.book?.let { - viewModel.loadContent(it, viewModel.durChapterIndex + 1) - } + viewModel.loadContent(viewModel.durChapterIndex + 1) } override fun observeLiveBus() { @@ -128,4 +137,17 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") } } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + if (resultCode == Activity.RESULT_OK) { + when (requestCode) { + requestCodeChapter -> data?.getIntExtra("index", viewModel.durChapterIndex)?.let { + if (it != viewModel.durChapterIndex) { + viewModel.loadContent(it) + } + } + } + } + } } \ No newline at end of file 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 48f022f76..ebeae4801 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 @@ -98,36 +98,40 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun loadContent(book: Book, index: Int) { - if (addLoading(index)) { - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - BookHelp.getContent(book, chapter)?.let { - contentLoadFinish(chapter, it) - removeLoading(chapter.index) - } ?: download(book, chapter) - } ?: removeLoading(index) - }.onError { - removeLoading(index) + fun loadContent(index: Int) { + book?.let { book -> + if (addLoading(index)) { + execute { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + BookHelp.getContent(book, chapter)?.let { + contentLoadFinish(chapter, it) + removeLoading(chapter.index) + } ?: download(chapter) + } ?: removeLoading(index) + }.onError { + removeLoading(index) + } } } } - private fun download(book: Book, chapter: BookChapter) { - webBook?.getContent(book, chapter, scope = this) - ?.onSuccess(Dispatchers.IO) { content -> - if (content.isNullOrEmpty()) { - contentLoadFinish(chapter, context.getString(R.string.content_empty)) - removeLoading(chapter.index) - } else { - BookHelp.saveContent(book, chapter, content) - contentLoadFinish(chapter, content) + private fun download(chapter: BookChapter) { + book?.let { book -> + webBook?.getContent(book, chapter, scope = this) + ?.onSuccess(Dispatchers.IO) { content -> + if (content.isNullOrEmpty()) { + contentLoadFinish(chapter, context.getString(R.string.content_empty)) + removeLoading(chapter.index) + } else { + BookHelp.saveContent(book, chapter, content) + contentLoadFinish(chapter, content) + removeLoading(chapter.index) + } + }?.onError { + contentLoadFinish(chapter, it.localizedMessage) removeLoading(chapter.index) } - }?.onError { - contentLoadFinish(chapter, it.localizedMessage) - removeLoading(chapter.index) - } + } } private fun addLoading(index: Int): Boolean { @@ -191,20 +195,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) durChapterIndex-- book?.durChapterIndex = durChapterIndex saveRead() - book?.let { - loadContent(it, durChapterIndex) - } + loadContent(durChapterIndex) } } fun moveToNext() { if (durChapterIndex < chapterSize - 1) { durChapterIndex++ - book?.let { - it.durChapterIndex = durChapterIndex - saveRead() - loadContent(it, durChapterIndex) - } + book?.durChapterIndex = durChapterIndex + saveRead() + loadContent(durChapterIndex) } else { AudioPlay.stop(context) } 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 69e252234..9a4595a2b 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 @@ -255,20 +255,16 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 加载章节内容 */ override fun loadContent() { - viewModel.book?.let { - viewModel.loadContent(it, viewModel.durChapterIndex) - viewModel.loadContent(it, viewModel.durChapterIndex + 1) - viewModel.loadContent(it, viewModel.durChapterIndex - 1) - } + viewModel.loadContent(viewModel.durChapterIndex) + viewModel.loadContent(viewModel.durChapterIndex + 1) + viewModel.loadContent(viewModel.durChapterIndex - 1) } /** * 加载章节内容, index章节序号 */ override fun loadContent(index: Int) { - viewModel.book?.let { - viewModel.loadContent(it, index) - } + viewModel.loadContent(index) } /** diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index cc4032a00..e84a7020d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -121,17 +121,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { nextTextChapter = null book?.let { if (curTextChapter == null) { - loadContent(it, durChapterIndex) + loadContent(durChapterIndex) } else if (upContent) { callBack?.upContent() } - loadContent(it, durChapterIndex.plus(1)) + loadContent(durChapterIndex.plus(1)) launch(IO) { for (i in 2..10) { delay(100) - book?.let { book -> - download(book, durChapterIndex + i) - } + download(durChapterIndex + i) } } } @@ -144,68 +142,72 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { prevTextChapter = null book?.let { if (curTextChapter == null) { - loadContent(it, durChapterIndex) + loadContent(durChapterIndex) } else if (upContent) { callBack?.upContent() } - loadContent(it, durChapterIndex.minus(1)) + loadContent(durChapterIndex.minus(1)) launch(IO) { for (i in -5..-2) { delay(100) - book?.let { book -> - download(book, durChapterIndex + i) - } + download(durChapterIndex + i) } } } } - fun loadContent(book: Book, index: Int) { - if (addLoading(index)) { - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - BookHelp.getContent(book, chapter)?.let { - contentLoadFinish(chapter, it) - removeLoading(chapter.index) - } ?: download(book, chapter) - } ?: removeLoading(index) - }.onError { - removeLoading(index) + fun loadContent(index: Int) { + book?.let { book -> + if (addLoading(index)) { + execute { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + BookHelp.getContent(book, chapter)?.let { + contentLoadFinish(chapter, it) + removeLoading(chapter.index) + } ?: download(chapter) + } ?: removeLoading(index) + }.onError { + removeLoading(index) + } } } } - private fun download(book: Book, index: Int) { - if (addLoading(index)) { - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - if (BookHelp.hasContent(book, chapter)) { - removeLoading(chapter.index) - } else { - download(book, chapter) - } - } ?: removeLoading(index) - }.onError { - removeLoading(index) + private fun download(index: Int) { + book?.let { book -> + if (addLoading(index)) { + execute { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + if (BookHelp.hasContent(book, chapter)) { + removeLoading(chapter.index) + } else { + download(chapter) + } + } ?: removeLoading(index) + }.onError { + removeLoading(index) + } } } } - private fun download(book: Book, chapter: BookChapter) { - webBook?.getContent(book, chapter, scope = this) - ?.onSuccess(IO) { content -> - if (content.isNullOrEmpty()) { - contentLoadFinish(chapter, context.getString(R.string.content_empty)) - removeLoading(chapter.index) - } else { - BookHelp.saveContent(book, chapter, content) - contentLoadFinish(chapter, content) + private fun download(chapter: BookChapter) { + book?.let { book -> + webBook?.getContent(book, chapter, scope = this) + ?.onSuccess(IO) { content -> + if (content.isNullOrEmpty()) { + contentLoadFinish(chapter, context.getString(R.string.content_empty)) + removeLoading(chapter.index) + } else { + BookHelp.saveContent(book, chapter, content) + contentLoadFinish(chapter, content) + removeLoading(chapter.index) + } + }?.onError { + contentLoadFinish(chapter, it.localizedMessage) removeLoading(chapter.index) } - }?.onError { - contentLoadFinish(chapter, it.localizedMessage) - removeLoading(chapter.index) - } + } } private fun addLoading(index: Int): Boolean { @@ -332,7 +334,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> BookHelp.delContent(book, chapter) - loadContent(book, durChapterIndex) + loadContent(durChapterIndex) } } } From b08d7b8752a4e42722b6f2f77bc14d968e0c0127 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 14:47:46 +0800 Subject: [PATCH 15/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt | 1 + 1 file changed, 1 insertion(+) 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 ebeae4801..c960350e3 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 @@ -39,6 +39,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } book?.let { book -> titleData.postValue(book.name) + coverData.postValue(book.getDisplayCover()) durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local From 9303f0aab592faecd86f958fb511903895dd1cb4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 16:56:45 +0800 Subject: [PATCH 16/35] up --- .../java/io/legado/app/help/ImageLoader.kt | 259 ++---------------- .../legado/app/ui/audio/AudioPlayActivity.kt | 6 +- .../app/ui/book/info/BookInfoActivity.kt | 2 +- .../ui/book/read/config/BgTextConfigDialog.kt | 2 +- .../ui/book/read/config/ReadStyleDialog.kt | 2 +- .../app/ui/book/search/SearchAdapter.kt | 6 +- .../app/ui/explore/ExploreShowAdapter.kt | 2 +- .../app/ui/main/bookshelf/BooksAdapter.kt | 2 +- .../io/legado/app/ui/main/rss/RssAdapter.kt | 2 +- .../app/ui/rss/article/RssArticlesAdapter.kt | 2 +- 10 files changed, 33 insertions(+), 252 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ImageLoader.kt b/app/src/main/java/io/legado/app/help/ImageLoader.kt index 836cb6e1d..26a9063d6 100644 --- a/app/src/main/java/io/legado/app/help/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -4,264 +4,45 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri -import android.widget.ImageView import androidx.annotation.DrawableRes import com.bumptech.glide.Glide import com.bumptech.glide.RequestBuilder -import com.bumptech.glide.RequestManager -import com.bumptech.glide.load.Transformation -import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions -import com.bumptech.glide.load.resource.bitmap.CenterCrop -import com.bumptech.glide.load.resource.bitmap.RoundedCorners -import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions -import com.bumptech.glide.load.resource.gif.GifDrawable -import com.bumptech.glide.request.RequestOptions -import com.bumptech.glide.request.transition.Transition import java.io.File object ImageLoader { - fun load(context: Context, url: String?): ImageLoadBuilder { - return ImageLoadBuilder(context, url) - } - - fun load(context: Context, @DrawableRes resId: Int?): ImageLoadBuilder { - return ImageLoadBuilder(context, resId) - } - - fun load(context: Context, file: File?): ImageLoadBuilder { - return ImageLoadBuilder(context, file) - } - - fun load(context: Context, uri: Uri?): ImageLoadBuilder { - return ImageLoadBuilder(context, uri) - } - - fun load(context: Context, drawable: Drawable?): ImageLoadBuilder { - return ImageLoadBuilder(context, drawable) + fun load(context: Context, path: String?): RequestBuilder { + if (path?.startsWith("http", true) == true) { + return Glide.with(context).load(path) + } + kotlin.runCatching { + return Glide.with(context).load(File(path)) + } + return Glide.with(context).load(path) } - fun load(context: Context, bitmap: Bitmap?): ImageLoadBuilder { - return ImageLoadBuilder(context, bitmap) + fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder { + return Glide.with(context).load(resId) } - fun load(context: Context, bytes: ByteArray?): ImageLoadBuilder { - return ImageLoadBuilder(context, bytes) + fun load(context: Context, file: File?): RequestBuilder { + return Glide.with(context).load(file) } - - fun with(context: Context): ImageLoadBuilder { - return ImageLoadBuilder(context) + fun load(context: Context, uri: Uri?): RequestBuilder { + return Glide.with(context).load(uri) } - fun clear(imageView: ImageView) { - with(imageView.context).clear(imageView) + fun load(context: Context, drawable: Drawable?): RequestBuilder { + return Glide.with(context).load(drawable) } - class ImageLoadBuilder constructor(context: Context, private var source: S? = null) { - - private val manager: RequestManager = Glide.with(context) - private var requestOptions: RequestOptions = RequestOptions() - private var crossFade: Boolean = false - private var noCache: Boolean = false - - fun load(source: S): ImageLoadBuilder { - this.source = source - return this - } - - fun bitmapTransform(transformation: Transformation): ImageLoadBuilder { - requestOptions = requestOptions.apply(RequestOptions.bitmapTransform(transformation)) - return this - } - - fun toRound(corner: Int): ImageLoadBuilder { - requestOptions = requestOptions.transform(RoundedCorners(corner)) - return this - } - - fun toCropRound(corner: Int): ImageLoadBuilder { - requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner)) - return this - } - - fun toCircle(): ImageLoadBuilder { - requestOptions = requestOptions.circleCrop() - return this - } - - fun centerInside(): ImageLoadBuilder { - requestOptions = requestOptions.centerInside() - return this - } - - fun fitCenter(): ImageLoadBuilder { - requestOptions = requestOptions.fitCenter() - return this - } - - fun centerCrop(): ImageLoadBuilder { - requestOptions = requestOptions.centerCrop() - return this - } - - fun crossFade(): ImageLoadBuilder { - crossFade = true - return this - } - - fun noCache(): ImageLoadBuilder { - noCache = true - return this - } - - fun placeholder(placeholder: Drawable): ImageLoadBuilder { - requestOptions = requestOptions.placeholder(placeholder) - return this - } - - fun placeholder(@DrawableRes resId: Int): ImageLoadBuilder { - requestOptions = requestOptions.placeholder(resId) - return this - } - - fun error(drawable: Drawable): ImageLoadBuilder { - requestOptions = requestOptions.error(drawable) - return this - } - - fun error(@DrawableRes resId: Int): ImageLoadBuilder { - requestOptions = requestOptions.error(resId) - return this - } - - fun override(width: Int, height: Int): ImageLoadBuilder { - requestOptions = requestOptions.override(width, height) - return this - } - - fun override(size: Int): ImageLoadBuilder { - requestOptions = requestOptions.override(size) - return this - } - - fun clear(imageView: ImageView) { - manager.clear(imageView) - } - - fun downloadOnly(target: ImageViewTarget) { - manager.downloadOnly().load(source).into(Target(target)) - } - - fun setAsDrawable(imageView: ImageView) { - asDrawable().into(imageView) - } - - fun setAsDrawable(target: ImageViewTarget) { - asDrawable().into(Target(target)) - } - - fun setAsBitmap(imageView: ImageView) { - asBitmap().into(imageView) - } - - fun setAsBitmap(target: ImageViewTarget) { - asBitmap().into(Target(target)) - } - - fun setAsGif(imageView: ImageView) { - asGif().into(imageView) - } - - fun setAsGif(target: ImageViewTarget) { - asGif().into(Target(target)) - } - - fun setAsFile(imageView: ImageView) { - asFile().into(imageView) - } - - fun setAsFile(target: ImageViewTarget) { - asFile().into(Target(target)) - } - - private fun asDrawable(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asDrawable().load(source)) - - if (crossFade) { - builder = builder.transition(DrawableTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asBitmap(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asBitmap().load(source)) - - if (crossFade) { - builder = builder.transition(BitmapTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asGif(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asGif().load(source)) - - if (crossFade) { - builder = builder.transition(DrawableTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asFile(): RequestBuilder { - return manager.asFile().load(source) - } - - private fun ensureOptions(builder: RequestBuilder): RequestBuilder { - return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.AUTOMATIC)) - } - - private inner class Target constructor(private val target: ImageViewTarget) : - com.bumptech.glide.request.target.ImageViewTarget(target.view) { - - init { - if (this.target.waitForLayout) { - waitForLayout() - } - } - - override fun onResourceReady(resource: R, transition: Transition?) { - if (!target.onResourceReady(resource)) { - super.onResourceReady(resource, transition) - } - } - - override fun setResource(resource: R?) { - target.setResource(resource) - } - } + fun load(context: Context, bitmap: Bitmap?): RequestBuilder { + return Glide.with(context).load(bitmap) } - abstract class ImageViewTarget(val view: ImageView) { - internal var waitForLayout: Boolean = false - - fun waitForLayout(): ImageViewTarget { - waitForLayout = true - return this - } - - fun setResource(resource: R?) { - - } - - fun onResourceReady(resource: R?): Boolean { - return false - } - + fun load(context: Context, bytes: ByteArray?): RequestBuilder { + return Glide.with(context).load(bytes) } - } 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 91ec8bd10..cec370757 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 @@ -10,7 +10,6 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus 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.service.help.AudioPlay import io.legado.app.ui.chapterlist.ChapterListActivity @@ -85,13 +84,12 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) ImageLoader.load(this, path) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) - .bitmapTransform(BlurTransformation(this, 25)) .centerCrop() - .setAsDrawable(iv_bg) + .into(iv_bg) } private fun playButton() { diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 53890274b..d6258f319 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -89,7 +89,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } val kinds = book.getKindList() if (kinds.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index d8112b538..194e89daf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -153,7 +153,7 @@ class BgTextConfigDialog : DialogFragment() { with(holder.itemView) { ImageLoader.load(context, context.assets.open("bg/$item").readBytes()) .centerCrop() - .setAsBitmap(iv_bg) + .into(iv_bg) tv_name.text = item.substringBeforeLast(".") this.onClick { ReadBookConfig.getConfig().setBg(1, item) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index d64fc4db9..f88a7a0e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -248,7 +248,7 @@ class ReadStyleDialog : DialogFragment() { } ReadBookConfig.getConfig(i).apply { when (bgType()) { - 2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().setAsDrawable(iv) + 2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().into(iv) else -> iv.setImageDrawable(bgDrawable(100, 150)) } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 7339ba392..04d2e46f8 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -72,7 +72,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.showBookInfo(searchBook.name, searchBook.author) @@ -89,7 +89,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } 3 -> { val kinds = searchBook.getKindList() @@ -136,6 +136,8 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } 5 -> tv_introduce.text = context.getString(R.string.intro_show, searchBook.intro) + else -> { + } } } } diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt index 5daa76472..d25e7f255 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt @@ -63,7 +63,7 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.showBookInfo(item.toBook()) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt index 25de68e9a..75b0f7787 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt @@ -30,7 +30,7 @@ class BooksAdapter(context: Context, private val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.open(item) } onLongClick { diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt index b490c8ec8..63bec82e2 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt @@ -19,7 +19,7 @@ class RssAdapter(context: Context, val callBack: CallBack) : .centerCrop() .placeholder(R.drawable.image_rss) .error(R.drawable.image_rss) - .setAsBitmap(iv_icon) + .into(iv_icon) onClick { callBack.openRss(item) } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt index 03546d957..4105e1958 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt @@ -28,7 +28,7 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) : } else { image_view.visible() ImageLoader.load(context, item.image) - .setAsBitmap(image_view) + .into(image_view) } if (item.read) { tv_title.textColorResource = R.color.tv_text_summary From c10e1602bdc2825552dc024f544d7c80c8bc1a2e Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 17:31:00 +0800 Subject: [PATCH 17/35] up --- app/build.gradle | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 6741567bb..dee80474e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,11 @@ android { versionName version testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" project.ext.set("archivesBaseName", name + "_" + version) + javaCompileOptions { + annotationProcessorOptions { + arguments = ["room.incremental":"true"] + } + } } buildTypes { release { From f9714259cbc90c239ce9dc8a89e96cbd18d0148a Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 17:45:08 +0800 Subject: [PATCH 18/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 3 +++ 1 file changed, 3 insertions(+) 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 cec370757..97d26e393 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 @@ -5,11 +5,13 @@ import android.content.Intent import android.os.Bundle import android.widget.SeekBar import androidx.lifecycle.Observer +import com.bumptech.glide.request.RequestOptions import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus 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.service.help.AudioPlay import io.legado.app.ui.chapterlist.ChapterListActivity @@ -89,6 +91,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() + .apply(RequestOptions.bitmapTransform(BlurTransformation(this, 20))) .into(iv_bg) } From 399433abf8e418eaeef9ff2a6b0ca10684713ca4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 18:28:34 +0800 Subject: [PATCH 19/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 97d26e393..16f83d4dd 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 @@ -91,7 +91,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .apply(RequestOptions.bitmapTransform(BlurTransformation(this, 20))) + .apply(RequestOptions.bitmapTransform(BlurTransformation(this, 25))) .into(iv_bg) } From a6d56e8ddd47befc1797b0dd7ed536469f8be713 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 18:32:59 +0800 Subject: [PATCH 20/35] up --- app/src/main/java/io/legado/app/help/BlurTransformation.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BlurTransformation.kt b/app/src/main/java/io/legado/app/help/BlurTransformation.kt index 9d60a9047..4d19a47a8 100644 --- a/app/src/main/java/io/legado/app/help/BlurTransformation.kt +++ b/app/src/main/java/io/legado/app/help/BlurTransformation.kt @@ -17,13 +17,12 @@ import java.security.MessageDigest /** * 模糊 */ -class BlurTransformation(context: Context, private val radius: Int) : BitmapTransformation() { - private val rs: RenderScript = RenderScript.create(context) +class BlurTransformation(val context: Context, private val radius: Int) : BitmapTransformation() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { val blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true) - + val rs: RenderScript = RenderScript.create(context) // Allocate memory for Renderscript to work with //分配用于渲染脚本的内存 val input = Allocation.createFromBitmap( @@ -50,6 +49,7 @@ class BlurTransformation(context: Context, private val radius: Int) : BitmapTran // Copy the output to the blurred bitmap //将输出复制到模糊的位图 output.copyTo(blurredBitmap) + rs.destroy() return blurredBitmap } From 6da0f994d63eb499536e05ba0b0b5afc6987fc60 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 18:46:47 +0800 Subject: [PATCH 21/35] up --- .../io/legado/app/help/BlurTransformation.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BlurTransformation.kt b/app/src/main/java/io/legado/app/help/BlurTransformation.kt index 4d19a47a8..fec7d0eef 100644 --- a/app/src/main/java/io/legado/app/help/BlurTransformation.kt +++ b/app/src/main/java/io/legado/app/help/BlurTransformation.kt @@ -13,16 +13,23 @@ import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool import com.bumptech.glide.load.resource.bitmap.BitmapTransformation import java.security.MessageDigest +import kotlin.math.min +import kotlin.math.roundToInt + /** * 模糊 + * @radius: 0..25 */ -class BlurTransformation(val context: Context, private val radius: Int) : BitmapTransformation() { +class BlurTransformation(context: Context, private val radius: Int) : BitmapTransformation() { + private val rs: RenderScript = RenderScript.create(context) @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { - val blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true) - val rs: RenderScript = RenderScript.create(context) + //图片缩小1/2 + val width = (min(outWidth, toTransform.width) / 2f).roundToInt() + val height = (min(outHeight, toTransform.height) / 2f).roundToInt() + val blurredBitmap = Bitmap.createScaledBitmap(toTransform, width, height, false); // Allocate memory for Renderscript to work with //分配用于渲染脚本的内存 val input = Allocation.createFromBitmap( @@ -39,7 +46,7 @@ class BlurTransformation(val context: Context, private val radius: Int) : Bitmap script.setInput(input) // Set the blur radius - //设置模糊半径 + //设置模糊半径0..25 script.setRadius(radius.toFloat()) // Start the ScriptIntrinsicBlur @@ -49,7 +56,6 @@ class BlurTransformation(val context: Context, private val radius: Int) : Bitmap // Copy the output to the blurred bitmap //将输出复制到模糊的位图 output.copyTo(blurredBitmap) - rs.destroy() return blurredBitmap } From 90e52cc3fe19fe089d94fd536d7d978dc6bd15cc Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 18:53:18 +0800 Subject: [PATCH 22/35] up --- .../java/io/legado/app/ui/audio/AudioPlayActivity.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 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 16f83d4dd..041226de0 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 @@ -2,9 +2,11 @@ package io.legado.app.ui.audio import android.app.Activity import android.content.Intent +import android.graphics.drawable.Drawable import android.os.Bundle import android.widget.SeekBar import androidx.lifecycle.Observer +import com.bumptech.glide.RequestBuilder import com.bumptech.glide.request.RequestOptions import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -88,13 +90,17 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a .centerCrop() .into(iv_cover) ImageLoader.load(this, path) - .placeholder(R.drawable.image_cover_default) - .error(R.drawable.image_cover_default) + .thumbnail(defaultCover()) .centerCrop() .apply(RequestOptions.bitmapTransform(BlurTransformation(this, 25))) .into(iv_bg) } + private fun defaultCover(): RequestBuilder { + return ImageLoader.load(this, R.drawable.image_cover_default) + .apply(RequestOptions.bitmapTransform(BlurTransformation(this, 25))) + } + private fun playButton() { when (status) { Status.PLAY -> AudioPlay.pause(this) From 9ef0a3a77c92a71873dc4ae317460766cd399979 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:10:24 +0800 Subject: [PATCH 23/35] 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 @@ 类型: 文本 音频 + 转到后台 From e5f3d2f28a7c8ee88d029097bf368c32c64401a8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:13:26 +0800 Subject: [PATCH 24/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 1 + .../main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) 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 87a01e5d5..352682664 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 @@ -204,6 +204,7 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a override fun onDestroy() { super.onDestroy() + AudioPlay.stop(this) if (!BuildConfig.DEBUG) { Backup.autoBackup() } 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 bcedc99f2..2c9493e29 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 @@ -191,6 +191,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun moveToPrev() { if (durChapterIndex > 0) { durChapterIndex-- + durPageIndex = 0 book?.durChapterIndex = durChapterIndex saveRead() loadContent(durChapterIndex) @@ -200,6 +201,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun moveToNext() { if (durChapterIndex < chapterSize - 1) { durChapterIndex++ + durPageIndex = 0 book?.durChapterIndex = durChapterIndex saveRead() loadContent(durChapterIndex) @@ -208,7 +210,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun saveRead() { + private fun saveRead() { execute { book?.let { book -> book.lastCheckCount = 0 From ebd9b09d087a2848112df336d67c1240e838e977 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:20:38 +0800 Subject: [PATCH 25/35] up --- .../java/io/legado/app/data/dao/BookDao.kt | 2 + .../legado/app/ui/audio/AudioPlayActivity.kt | 54 ++++++++++--------- .../legado/app/ui/audio/AudioPlayViewModel.kt | 8 +++ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index ba1d7e67e..ecd16d6ef 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -53,4 +53,6 @@ interface BookDao { @Query("delete from books where bookUrl = :bookUrl") fun delete(bookUrl: String) + @Query("update books set durChapterIndex = :pos where bookUrl = :bookUrl") + fun upProgress(bookUrl: String, pos: Int) } \ No newline at end of file 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 352682664..bd8ea22b0 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 @@ -131,32 +131,6 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a viewModel.loadContent(viewModel.durChapterIndex + 1) } - override fun observeLiveBus() { - observeEvent(Bus.AUDIO_PLAY_BUTTON) { - playButton() - } - observeEvent(Bus.AUDIO_NEXT) { - viewModel.moveToNext() - } - observeEvent(Bus.AUDIO_STATE) { - status = it - if (status == Status.PLAY) { - fab_play_stop.setImageResource(R.drawable.ic_pause_24dp) - } else { - fab_play_stop.setImageResource(R.drawable.ic_play_24dp) - } - } - observeEvent(Bus.AUDIO_PROGRESS) { - viewModel.durPageIndex = it - if (!adjustProgress) player_progress.progress = it - tv_dur_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") - } - observeEvent(Bus.AUDIO_SIZE) { - player_progress.max = it - tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") - } - } - override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean { when (keyCode) { KeyEvent.KEYCODE_BACK -> { @@ -222,4 +196,32 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } } } + + override fun observeLiveBus() { + observeEvent(Bus.AUDIO_PLAY_BUTTON) { + playButton() + } + observeEvent(Bus.AUDIO_NEXT) { + viewModel.moveToNext() + } + observeEvent(Bus.AUDIO_STATE) { + status = it + if (status == Status.PLAY) { + fab_play_stop.setImageResource(R.drawable.ic_pause_24dp) + } else { + fab_play_stop.setImageResource(R.drawable.ic_play_24dp) + } + } + observeEvent(Bus.AUDIO_PROGRESS) { + viewModel.durPageIndex = it + if (!adjustProgress) player_progress.progress = it + tv_dur_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") + viewModel.saveProgress() + } + observeEvent(Bus.AUDIO_SIZE) { + player_progress.max = it + tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") + } + } + } \ No newline at end of file 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 2c9493e29..7831eeda2 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 @@ -222,6 +222,14 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } + fun saveProgress() { + execute { + book?.let { + App.db.bookDao().upProgress(it.bookUrl, durPageIndex) + } + } + } + fun removeFromBookshelf(success: (() -> Unit)?) { execute { book?.let { From 16257b14cb6784b186f6645a5ec28107011055aa Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:21:23 +0800 Subject: [PATCH 26/35] up --- app/src/main/java/io/legado/app/data/dao/BookDao.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index ecd16d6ef..2a356e933 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -53,6 +53,6 @@ interface BookDao { @Query("delete from books where bookUrl = :bookUrl") fun delete(bookUrl: String) - @Query("update books set durChapterIndex = :pos where bookUrl = :bookUrl") + @Query("update books set durChapterPos = :pos where bookUrl = :bookUrl") fun upProgress(bookUrl: String, pos: Int) } \ No newline at end of file From 972c0ec0185dcd0848522fee3d7a4c394598dd09 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:43:59 +0800 Subject: [PATCH 27/35] up --- .../app/ui/chapterlist/ChapterListActivity.kt | 7 ++++--- .../app/ui/chapterlist/ChapterListFragment.kt | 19 ++++++------------- .../ui/chapterlist/ChapterListViewModel.kt | 12 +++++------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt index ef5be2761..514c2dc68 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt @@ -27,9 +27,10 @@ class ChapterListActivity : VMBaseActivity(R.layout.activi override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) viewModel.bookUrl = intent.getStringExtra("bookUrl") - viewModel.loadBook() - view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) - tab_layout.setupWithViewPager(view_pager) + viewModel.loadBook { + view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) + tab_layout.setupWithViewPager(view_pager) + } } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 97092ab13..18449147e 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -45,14 +45,13 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } private fun initData() { - viewModel.bookDate.observe(viewLifecycleOwner, Observer { - loadBookFinish(it) - }) viewModel.bookUrl?.let { bookUrl -> App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer { adapter.setItems(it) - viewModel.bookDate.value?.let { book -> - loadBookFinish(book) + viewModel.book?.let { + durChapterIndex = it.durChapterIndex + tv_current_chapter_info.text = it.durChapterTitle + recycler_view.scrollToPosition(durChapterIndex) } }) } @@ -66,18 +65,12 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } } tv_current_chapter_info.onClick { - viewModel.bookDate.value?.let { + viewModel.book?.let { recycler_view.scrollToPosition(it.durChapterIndex) } } } - private fun loadBookFinish(book: Book) { - durChapterIndex = book.durChapterIndex - tv_current_chapter_info.text = book.durChapterTitle - recycler_view.scrollToPosition(durChapterIndex) - } - override fun durChapterIndex(): Int { return durChapterIndex } @@ -88,6 +81,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } override fun book(): Book? { - return viewModel.bookDate.value + return viewModel.book } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt index 11d1b38e9..d11aa73dc 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt @@ -2,23 +2,21 @@ package io.legado.app.ui.chapterlist import android.app.Application -import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book class ChapterListViewModel(application: Application) : BaseViewModel(application) { - - var bookDate = MutableLiveData() var bookUrl: String? = null + var book: Book? = null - fun loadBook() { + fun loadBook(success: () -> Unit) { execute { bookUrl?.let { - App.db.bookDao().getBook(it)?.let { book -> - bookDate.postValue(book) - } + book = App.db.bookDao().getBook(it) } + }.onSuccess { + success() } } } \ No newline at end of file From 37d0377a1fc8192d0c158b651eb3668e71938617 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:50:01 +0800 Subject: [PATCH 28/35] up --- app/src/main/java/io/legado/app/service/AudioPlayService.kt | 2 +- app/src/main/java/io/legado/app/service/help/AudioPlay.kt | 2 +- app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 3b1c92eec..fb15d022d 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -74,7 +74,7 @@ class AudioPlayService : BaseService(), Action.play -> { title = intent.getStringExtra("title") ?: "" subtitle = intent.getStringExtra("subtitle") ?: "" - position = intent.getIntExtra("pageIndex", 0) + position = intent.getIntExtra("position", 0) play(intent.getStringExtra("url")) } Action.pause -> pause(true) diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt index 48d3f5b15..10d6d6026 100644 --- a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -13,7 +13,7 @@ object AudioPlay { intent.putExtra("title", title) intent.putExtra("subtitle", subtitle) intent.putExtra("url", url) - intent.putExtra("dataKey", position) + intent.putExtra("position", position) context.startService(intent) } 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 7831eeda2..191c03d1d 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 @@ -19,7 +19,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) var coverData = MutableLiveData() var book: Book? = null var inBookshelf = false - var chapterSize = 0 + private var chapterSize = 0 var callBack: CallBack? = null var durChapterIndex = 0 var durPageIndex = 0 From d0e64c821077e51b87d4f4b5cdc2fa55b8b80924 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 19:53:09 +0800 Subject: [PATCH 29/35] up --- app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt | 3 +-- 1 file changed, 1 insertion(+), 2 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 bd8ea22b0..d0cd7ae0b 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 @@ -29,7 +29,6 @@ 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 @@ -134,7 +133,7 @@ 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) { + title_bar.snackbar(R.string.to_backstage, R.string.ok) { startActivity() } return true From cc4258cbc1c44b82acbfa299f8c7371804c20963 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 20:14:20 +0800 Subject: [PATCH 30/35] up --- .../main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt | 2 +- app/src/main/res/layout/item_text.xml | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt index 205cf4e2c..c0d19851f 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt @@ -73,6 +73,7 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca kindList.map { kind -> val tv = LayoutInflater.from(context) .inflate(R.layout.item_text, gl_child, false) + gl_child.addView(tv) tv.text_view.text = kind.title tv.text_view.onClick { kind.url?.let { kindUrl -> @@ -83,7 +84,6 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca ) } } - gl_child.addView(tv) } } }.onFinally { diff --git a/app/src/main/res/layout/item_text.xml b/app/src/main/res/layout/item_text.xml index 87687a713..331bd8a2c 100644 --- a/app/src/main/res/layout/item_text.xml +++ b/app/src/main/res/layout/item_text.xml @@ -6,12 +6,10 @@ android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/selector_fillet_btn_bg" - android:clickable="true" android:ellipsize="end" - android:focusable="true" android:gravity="center" android:padding="5dp" - android:singleLine="true" + android:maxLines="1" android:textColor="@color/tv_text_default" android:textSize="14sp" tools:ignore="UnusedAttribute" /> From c34440aeb1bad9e97b7481f04e50bebe31aabf51 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 20:16:53 +0800 Subject: [PATCH 31/35] up --- app/src/main/res/layout/activity_audio_play.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml index 540fec706..4669f519a 100644 --- a/app/src/main/res/layout/activity_audio_play.xml +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -57,6 +57,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00" + android:textColor="@color/md_white_1000" tools:ignore="HardcodedText,RtlSymmetry" /> From 25d45b6b221c6c6486cd27dd6f7b83881f5ae688 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 20:27:22 +0800 Subject: [PATCH 32/35] up --- app/src/main/res/layout/activity_audio_play.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml index 4669f519a..9ef78bdd8 100644 --- a/app/src/main/res/layout/activity_audio_play.xml +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -65,7 +65,8 @@ android:layout_width="0dp" android:layout_height="25dp" android:layout_gravity="center_vertical" - android:layout_weight="1" /> + android:layout_weight="1" + android:progressBackgroundTint="@color/md_dark_secondary" /> Date: Tue, 5 Nov 2019 20:43:01 +0800 Subject: [PATCH 33/35] up --- app/src/main/AndroidManifest.xml | 32 +++++++++++---- .../legado/app/ui/audio/AudioPlayActivity.kt | 39 +++---------------- 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8ce35a34b..547deccb2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,7 +38,9 @@ android:resource="@xml/shortcuts" android:launchMode="singleTask" /> - + - - - - - - + + + + + + @@ -79,7 +93,9 @@ - + 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 d0cd7ae0b..5fadc7be7 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,7 +4,6 @@ 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 @@ -27,7 +26,6 @@ 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.view_title_bar.* import org.apache.commons.lang3.time.DateFormatUtils @@ -130,37 +128,6 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a viewModel.loadContent(viewModel.durChapterIndex + 1) } - override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean { - when (keyCode) { - KeyEvent.KEYCODE_BACK -> { - title_bar.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) { @@ -170,7 +137,11 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a noButton { viewModel.removeFromBookshelf { super.finish() } } }.show().applyTint() } else { - super.finish() + if (status == Status.PLAY) { + startActivity() + } else { + super.finish() + } } } ?: super.finish() } From fbe84f8df6343443afd2742b75d5b563fed59fc1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 20:48:11 +0800 Subject: [PATCH 34/35] up --- app/src/main/AndroidManifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 547deccb2..24d3cb609 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,7 +40,7 @@ + android:launchMode="singleTop" /> Date: Tue, 5 Nov 2019 20:52:56 +0800 Subject: [PATCH 35/35] up --- app/src/main/AndroidManifest.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 24d3cb609..230455575 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,9 +38,7 @@ android:resource="@xml/shortcuts" android:launchMode="singleTask" /> - +