Compare commits
90 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45dbd5aa44 | ||
|
|
ee65251bf5 | ||
|
|
4d838d290d | ||
|
|
048efdf59f | ||
|
|
af2adeba13 | ||
|
|
93c6bec452 | ||
|
|
c944044465 | ||
|
|
8a63ca2310 | ||
|
|
12e5e3b35e | ||
|
|
553a85ef86 | ||
|
|
de7012cabf | ||
|
|
46f0d3ef74 | ||
|
|
c27c785ac2 | ||
|
|
4186c36f30 | ||
|
|
757e33dfb4 | ||
|
|
ab9bdf9f07 | ||
|
|
2e561697ac | ||
|
|
d242acd502 | ||
|
|
d37b44d3f6 | ||
|
|
e4c4d2bbf0 | ||
|
|
040d3e4433 | ||
|
|
b4f93fc0a5 | ||
|
|
c4e7807d18 | ||
|
|
8e55a4d824 | ||
|
|
c1e9fde6e8 | ||
|
|
32e80c7e95 | ||
|
|
c07a3b9d0d | ||
|
|
893d1a881d | ||
|
|
43ef130052 | ||
|
|
d5bea0ca53 | ||
|
|
9c740c5cc1 | ||
|
|
cf7535e2ba | ||
|
|
87afad29ce | ||
|
|
436233e735 | ||
|
|
6e367ddd74 | ||
|
|
fcdfaf5564 | ||
|
|
dff17fd11f | ||
|
|
85af73df99 | ||
|
|
c7a97711c0 | ||
|
|
ffbe05b2ae | ||
|
|
14f5d5daa4 | ||
|
|
f342cd6b56 | ||
|
|
8faacab53a | ||
|
|
659c327a6d | ||
|
|
bcc2f531c3 | ||
|
|
020df5c1f7 | ||
|
|
d6781e1d14 | ||
|
|
d42cd59880 | ||
|
|
be19c32fea | ||
|
|
8da0e98d23 | ||
|
|
73a2f05509 | ||
|
|
bb23f998e0 | ||
|
|
75915ff366 | ||
|
|
517e801580 | ||
|
|
12474e23f9 | ||
|
|
00bdd859a7 | ||
|
|
3a3af9ea00 | ||
|
|
1803b1a2ee | ||
|
|
4175c84363 | ||
|
|
1840d7b50e | ||
|
|
37b69833b3 | ||
|
|
093f766d1d | ||
|
|
69d8459b1c | ||
|
|
fa8a526642 | ||
|
|
1d35d951e6 | ||
|
|
3c0420f42f | ||
|
|
d000a825d3 | ||
|
|
23b28672d4 | ||
|
|
a076c9f420 | ||
|
|
bdc7a8f5ed | ||
|
|
bdcc3bb1f5 | ||
|
|
18d45aa1a3 | ||
|
|
b5bb8efe0a | ||
|
|
f18c18230b | ||
|
|
2fd1e998f4 | ||
|
|
c5a1980e0d | ||
|
|
d470ca4b47 | ||
|
|
35f450e444 | ||
|
|
206fb4e584 | ||
|
|
62088b36a4 | ||
|
|
aa5fd530d3 | ||
|
|
f0ee64bafa | ||
|
|
dfa413da6f | ||
|
|
9eb5e699e1 | ||
|
|
2d4c1b751e | ||
|
|
91b17ef4a2 | ||
|
|
9b748f7334 | ||
|
|
2deaed2067 | ||
|
|
fb608ed30a | ||
|
|
8e43afe408 |
@@ -15,5 +15,6 @@ disabled_rules=no-wildcard-imports,no-unused-imports
|
||||
ij_continuation_indent_size = 4
|
||||
|
||||
[{*.kt,*.kts}]
|
||||
ij_kotlin_allow_trailing_comma_on_call_site = true
|
||||
ij_kotlin_allow_trailing_comma = true
|
||||
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,6 +11,7 @@
|
||||
/.idea/navEditor.xml
|
||||
/.idea/assetWizardSettings.xml
|
||||
/.idea/kotlinScripting.xml
|
||||
/.idea/kotlinc.xml
|
||||
/.idea/deploymentTargetDropDown.xml
|
||||
/.idea/androidTestResultsUserPreferences.xml
|
||||
/.idea/render.experimental.xml
|
||||
|
||||
2
.idea/compiler.xml
generated
2
.idea/compiler.xml
generated
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="11" />
|
||||
<bytecodeTargetLevel target="17" />
|
||||
</component>
|
||||
</project>
|
||||
3
.idea/kotlinc.xml
generated
3
.idea/kotlinc.xml
generated
@@ -3,4 +3,7 @@
|
||||
<component name="Kotlin2JvmCompilerArguments">
|
||||
<option name="jvmTarget" value="1.8" />
|
||||
</component>
|
||||
<component name="KotlinJpsPluginSettings">
|
||||
<option name="version" value="1.6.21" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -14,8 +14,8 @@ android {
|
||||
applicationId 'org.koitharu.kotatsu'
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 32
|
||||
versionCode 415
|
||||
versionName '3.4.3'
|
||||
versionCode 430
|
||||
versionName '3.5'
|
||||
generatedDensities = []
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
@@ -28,6 +28,8 @@ android {
|
||||
// define this values in your local.properties file
|
||||
buildConfigField 'String', 'SHIKIMORI_CLIENT_ID', "\"${localProperty('shikimori.clientId')}\""
|
||||
buildConfigField 'String', 'SHIKIMORI_CLIENT_SECRET', "\"${localProperty('shikimori.clientSecret')}\""
|
||||
resValue "string", "acra_login", "${localProperty('acra.login')}"
|
||||
resValue "string", "acra_password", "${localProperty('acra.password')}"
|
||||
}
|
||||
buildTypes {
|
||||
debug {
|
||||
@@ -64,8 +66,11 @@ android {
|
||||
disable 'MissingTranslation', 'PrivateResource', 'NotifyDataSetChanged'
|
||||
}
|
||||
testOptions {
|
||||
unitTests.includeAndroidResources = true
|
||||
unitTests.returnDefaultValues = false
|
||||
unitTests.includeAndroidResources true
|
||||
unitTests.returnDefaultValues false
|
||||
kotlinOptions {
|
||||
freeCompilerArgs += ['-opt-in=org.koitharu.kotatsu.parsers.InternalParsersApi']
|
||||
}
|
||||
}
|
||||
}
|
||||
afterEvaluate {
|
||||
@@ -76,19 +81,19 @@ afterEvaluate {
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
implementation('com.github.nv95:kotatsu-parsers:2d1907569b') {
|
||||
implementation('com.github.KotatsuApp:kotatsu-parsers:5cb953eb86') {
|
||||
exclude group: 'org.json', module: 'json'
|
||||
}
|
||||
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.3'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.8.0'
|
||||
implementation 'androidx.activity:activity-ktx:1.5.0'
|
||||
implementation 'androidx.fragment:fragment-ktx:1.5.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.5.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-process:2.5.0'
|
||||
implementation 'androidx.activity:activity-ktx:1.5.1'
|
||||
implementation 'androidx.fragment:fragment-ktx:1.5.3'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.5.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-process:2.5.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.2.1'
|
||||
@@ -96,13 +101,13 @@ dependencies {
|
||||
implementation 'androidx.preference:preference-ktx:1.2.0'
|
||||
implementation 'androidx.work:work-runtime-ktx:2.7.1'
|
||||
implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha04'
|
||||
implementation 'com.google.android.material:material:1.7.0-alpha02'
|
||||
implementation 'com.google.android.material:material:1.7.0-rc01'
|
||||
//noinspection LifecycleAnnotationProcessorWithJava8
|
||||
kapt 'androidx.lifecycle:lifecycle-compiler:2.5.0'
|
||||
kapt 'androidx.lifecycle:lifecycle-compiler:2.5.1'
|
||||
|
||||
implementation 'androidx.room:room-runtime:2.4.2'
|
||||
implementation 'androidx.room:room-ktx:2.4.2'
|
||||
kapt 'androidx.room:room-compiler:2.4.2'
|
||||
implementation 'androidx.room:room-runtime:2.4.3'
|
||||
implementation 'androidx.room:room-ktx:2.4.3'
|
||||
kapt 'androidx.room:room-compiler:2.4.3'
|
||||
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.3'
|
||||
@@ -112,28 +117,28 @@ dependencies {
|
||||
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.3.2'
|
||||
|
||||
implementation 'io.insert-koin:koin-android:3.2.0'
|
||||
implementation 'io.coil-kt:coil-base:2.1.0'
|
||||
implementation 'io.coil-kt:coil-base:2.2.1'
|
||||
implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'
|
||||
implementation 'com.github.solkin:disk-lru-cache:1.4'
|
||||
|
||||
implementation 'ch.acra:acra-mail:5.9.3'
|
||||
implementation 'ch.acra:acra-dialog:5.9.3'
|
||||
implementation 'ch.acra:acra-http:5.9.6'
|
||||
implementation 'ch.acra:acra-dialog:5.9.6'
|
||||
|
||||
debugImplementation 'org.jsoup:jsoup:1.15.1'
|
||||
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.3'
|
||||
testImplementation 'org.json:json:20220320'
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
|
||||
|
||||
androidTestImplementation 'androidx.test:runner:1.4.0'
|
||||
androidTestImplementation 'androidx.test:rules:1.4.0'
|
||||
androidTestImplementation 'androidx.test:core-ktx:1.4.0'
|
||||
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'
|
||||
|
||||
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.3'
|
||||
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
|
||||
androidTestImplementation 'io.insert-koin:koin-test:3.2.0'
|
||||
androidTestImplementation 'io.insert-koin:koin-test-junit4:3.2.0'
|
||||
|
||||
androidTestImplementation 'androidx.room:room-testing:2.4.2'
|
||||
androidTestImplementation 'com.squareup.moshi:moshi-kotlin:1.13.0'
|
||||
androidTestImplementation 'androidx.room:room-testing:2.4.3'
|
||||
androidTestImplementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
|
||||
}
|
||||
4
app/proguard-rules.pro
vendored
4
app/proguard-rules.pro
vendored
@@ -10,4 +10,6 @@
|
||||
}
|
||||
-keep public class ** extends org.koitharu.kotatsu.base.ui.BaseFragment
|
||||
-keep class org.koitharu.kotatsu.core.db.entity.* { *; }
|
||||
-dontwarn okhttp3.internal.platform.ConscryptPlatform
|
||||
-dontwarn okhttp3.internal.platform.ConscryptPlatform
|
||||
-keep class org.koitharu.kotatsu.core.exceptions.* { *; }
|
||||
-keep class org.koitharu.kotatsu.settings.NotificationSettingsLegacyFragment
|
||||
8
app/src/androidTest/assets/categories/simple.json
Normal file
8
app/src/androidTest/assets/categories/simple.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"id": 4,
|
||||
"title": "Read later",
|
||||
"sortKey": 1,
|
||||
"order": "NEWEST",
|
||||
"createdAt": 1335906000000,
|
||||
"isTrackingEnabled": true
|
||||
}
|
||||
35
app/src/androidTest/assets/manga/header.json
Normal file
35
app/src/androidTest/assets/manga/header.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"id": -2096681732556647985,
|
||||
"title": "Странствия Эманон",
|
||||
"url": "/stranstviia_emanon",
|
||||
"publicUrl": "https://readmanga.io/stranstviia_emanon",
|
||||
"rating": 0.9400894,
|
||||
"isNsfw": true,
|
||||
"coverUrl": "https://staticrm.rmr.rocks/uploads/pics/01/12/559_p.jpg",
|
||||
"tags": [
|
||||
{
|
||||
"title": "Сверхъестественное",
|
||||
"key": "supernatural",
|
||||
"source": "READMANGA_RU"
|
||||
},
|
||||
{
|
||||
"title": "Сэйнэн",
|
||||
"key": "seinen",
|
||||
"source": "READMANGA_RU"
|
||||
},
|
||||
{
|
||||
"title": "Повседневность",
|
||||
"key": "slice_of_life",
|
||||
"source": "READMANGA_RU"
|
||||
},
|
||||
{
|
||||
"title": "Приключения",
|
||||
"key": "adventure",
|
||||
"source": "READMANGA_RU"
|
||||
}
|
||||
],
|
||||
"state": "FINISHED",
|
||||
"largeCoverUrl": "https://staticrm.rmr.rocks/uploads/pics/01/12/559_o.jpg",
|
||||
"description": null,
|
||||
"source": "READMANGA_RU"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.koitharu.kotatsu
|
||||
|
||||
import android.app.Instrumentation
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
suspend fun Instrumentation.awaitForIdle() = suspendCoroutine<Unit> { cont ->
|
||||
waitForIdle { cont.resume(Unit) }
|
||||
}
|
||||
54
app/src/androidTest/java/org/koitharu/kotatsu/SampleData.kt
Normal file
54
app/src/androidTest/java/org/koitharu/kotatsu/SampleData.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package org.koitharu.kotatsu
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.squareup.moshi.*
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
object SampleData {
|
||||
|
||||
private val moshi = Moshi.Builder()
|
||||
.add(DateAdapter())
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
|
||||
val manga: Manga = loadAsset("manga/header.json", Manga::class)
|
||||
|
||||
val mangaDetails: Manga = loadAsset("manga/full.json", Manga::class)
|
||||
|
||||
val tag = mangaDetails.tags.elementAt(2)
|
||||
|
||||
val chapter = checkNotNull(mangaDetails.chapters)[2]
|
||||
|
||||
val favouriteCategory: FavouriteCategory = loadAsset("categories/simple.json", FavouriteCategory::class)
|
||||
|
||||
fun <T : Any> loadAsset(name: String, cls: KClass<T>): T {
|
||||
val assets = InstrumentationRegistry.getInstrumentation().context.assets
|
||||
return assets.open(name).use {
|
||||
moshi.adapter(cls.java).fromJson(it.source().buffer())
|
||||
} ?: throw RuntimeException("Cannot read asset from json \"$name\"")
|
||||
}
|
||||
|
||||
private class DateAdapter : JsonAdapter<Date>() {
|
||||
|
||||
@FromJson
|
||||
override fun fromJson(reader: JsonReader): Date? {
|
||||
val ms = reader.nextLong()
|
||||
return if (ms == 0L) {
|
||||
null
|
||||
} else {
|
||||
Date(ms)
|
||||
}
|
||||
}
|
||||
|
||||
@ToJson
|
||||
override fun toJson(writer: JsonWriter, value: Date?) {
|
||||
writer.value(value?.time ?: 0L)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,10 @@ package org.koitharu.kotatsu.core.db
|
||||
import androidx.room.testing.MigrationTestHelper
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import java.io.IOException
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koitharu.kotatsu.core.db.migrations.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class MangaDatabaseTest {
|
||||
@@ -18,38 +17,41 @@ class MangaDatabaseTest {
|
||||
MangaDatabase::class.java,
|
||||
)
|
||||
|
||||
private val migrations = databaseMigrations
|
||||
|
||||
@Test
|
||||
@Throws(IOException::class)
|
||||
fun migrateAll() {
|
||||
helper.createDatabase(TEST_DB, 1).apply {
|
||||
// TODO execSQL("")
|
||||
close()
|
||||
fun versions() {
|
||||
assertEquals(1, migrations.first().startVersion)
|
||||
repeat(migrations.size) { i ->
|
||||
assertEquals(i + 1, migrations[i].startVersion)
|
||||
assertEquals(i + 2, migrations[i].endVersion)
|
||||
}
|
||||
assertEquals(DATABASE_VERSION, migrations.last().endVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun migrateAll() {
|
||||
helper.createDatabase(TEST_DB, 1).close()
|
||||
for (migration in migrations) {
|
||||
helper.runMigrationsAndValidate(
|
||||
TEST_DB,
|
||||
migration.endVersion,
|
||||
true,
|
||||
migration
|
||||
)
|
||||
).close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun prePopulate() {
|
||||
val resources = InstrumentationRegistry.getInstrumentation().targetContext.resources
|
||||
helper.createDatabase(TEST_DB, DATABASE_VERSION).use {
|
||||
DatabasePrePopulateCallback(resources).onCreate(it)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
|
||||
const val TEST_DB = "test-db"
|
||||
|
||||
val migrations = arrayOf(
|
||||
Migration1To2(),
|
||||
Migration2To3(),
|
||||
Migration3To4(),
|
||||
Migration4To5(),
|
||||
Migration5To6(),
|
||||
Migration6To7(),
|
||||
Migration7To8(),
|
||||
Migration8To9(),
|
||||
Migration9To10(),
|
||||
Migration10To11(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.koitharu.kotatsu.core.os
|
||||
|
||||
import android.content.pm.ShortcutInfo
|
||||
import android.content.pm.ShortcutManager
|
||||
import android.os.Build
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.inject
|
||||
import org.koitharu.kotatsu.SampleData
|
||||
import org.koitharu.kotatsu.awaitForIdle
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ShortcutsUpdaterTest : KoinTest {
|
||||
|
||||
private val historyRepository by inject<HistoryRepository>()
|
||||
private val shortcutsUpdater by inject<ShortcutsUpdater>()
|
||||
private val database by inject<MangaDatabase>()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
database.clearAllTables()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateShortcuts() = runTest {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
|
||||
return@runTest
|
||||
}
|
||||
awaitUpdate()
|
||||
assertTrue(getShortcuts().isEmpty())
|
||||
historyRepository.addOrUpdate(
|
||||
manga = SampleData.manga,
|
||||
chapterId = SampleData.chapter.id,
|
||||
page = 4,
|
||||
scroll = 2,
|
||||
percent = 0.3f
|
||||
)
|
||||
awaitUpdate()
|
||||
|
||||
val shortcuts = getShortcuts()
|
||||
assertEquals(1, shortcuts.size)
|
||||
}
|
||||
|
||||
private fun getShortcuts(): List<ShortcutInfo> {
|
||||
val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
val manager = checkNotNull(context.getSystemService<ShortcutManager>())
|
||||
return manager.dynamicShortcuts.filterNot { it.id == "com.squareup.leakcanary.dynamic_shortcut" }
|
||||
}
|
||||
|
||||
private suspend fun awaitUpdate() {
|
||||
val instrumentation = InstrumentationRegistry.getInstrumentation()
|
||||
instrumentation.awaitForIdle()
|
||||
shortcutsUpdater.await()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.koitharu.kotatsu.settings.backup
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.get
|
||||
import org.koin.test.inject
|
||||
import org.koitharu.kotatsu.SampleData
|
||||
import org.koitharu.kotatsu.core.backup.BackupRepository
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import kotlin.test.*
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class AppBackupAgentTest : KoinTest {
|
||||
|
||||
private val historyRepository by inject<HistoryRepository>()
|
||||
private val favouritesRepository by inject<FavouritesRepository>()
|
||||
private val backupRepository by inject<BackupRepository>()
|
||||
private val database by inject<MangaDatabase>()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
database.clearAllTables()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBackupRestore() = runTest {
|
||||
val category = favouritesRepository.createCategory(
|
||||
title = SampleData.favouriteCategory.title,
|
||||
sortOrder = SampleData.favouriteCategory.order,
|
||||
isTrackerEnabled = SampleData.favouriteCategory.isTrackingEnabled,
|
||||
)
|
||||
favouritesRepository.addToCategory(categoryId = category.id, mangas = listOf(SampleData.manga))
|
||||
historyRepository.addOrUpdate(
|
||||
manga = SampleData.mangaDetails,
|
||||
chapterId = SampleData.mangaDetails.chapters!![2].id,
|
||||
page = 3,
|
||||
scroll = 40,
|
||||
percent = 0.2f,
|
||||
)
|
||||
val history = checkNotNull(historyRepository.getOne(SampleData.manga))
|
||||
|
||||
val agent = AppBackupAgent()
|
||||
val backup = agent.createBackupFile(get(), backupRepository)
|
||||
|
||||
database.clearAllTables()
|
||||
assertTrue(favouritesRepository.getAllManga().isEmpty())
|
||||
assertNull(historyRepository.getLastOrNull())
|
||||
|
||||
backup.inputStream().use {
|
||||
agent.restoreBackupFile(it.fd, backup.length(), backupRepository)
|
||||
}
|
||||
|
||||
assertEquals(category, favouritesRepository.getCategory(category.id))
|
||||
assertEquals(history, historyRepository.getOne(SampleData.manga))
|
||||
assertContentEquals(listOf(SampleData.manga), favouritesRepository.getManga(category.id))
|
||||
|
||||
val allTags = database.tagsDao.findTags(SampleData.tag.source.name).toMangaTags()
|
||||
assertContains(allTags, SampleData.tag)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,21 @@
|
||||
package org.koitharu.kotatsu.tracker.domain
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.inject
|
||||
import org.koitharu.kotatsu.SampleData
|
||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class TrackerTest : KoinTest {
|
||||
|
||||
private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
private val mangaAdapter = moshi.adapter(Manga::class.java)
|
||||
private val historyRegistry by inject<HistoryRepository>()
|
||||
private val repository by inject<TrackingRepository>()
|
||||
private val dataRepository by inject<MangaDataRepository>()
|
||||
private val tracker by inject<Tracker>()
|
||||
@@ -166,22 +158,25 @@ class TrackerTest : KoinTest {
|
||||
}
|
||||
assertEquals(3, repository.getNewChaptersCount(mangaFirst.id))
|
||||
|
||||
val chapter = requireNotNull(mangaFull.chapters).run { get(lastIndex - 1) }
|
||||
var chapter = requireNotNull(mangaFull.chapters).run { get(lastIndex - 1) }
|
||||
repository.syncWithHistory(mangaFull, chapter.id)
|
||||
|
||||
assertEquals(1, repository.getNewChaptersCount(mangaFirst.id))
|
||||
|
||||
chapter = requireNotNull(mangaFull.chapters).run { get(lastIndex) }
|
||||
repository.syncWithHistory(mangaFull, chapter.id)
|
||||
|
||||
assertEquals(0, repository.getNewChaptersCount(mangaFirst.id))
|
||||
|
||||
tracker.checkUpdates(mangaFull, commit = true).apply {
|
||||
assertTrue(isValid)
|
||||
assert(newChapters.isEmpty())
|
||||
}
|
||||
assertEquals(1, repository.getNewChaptersCount(mangaFirst.id))
|
||||
assertEquals(0, repository.getNewChaptersCount(mangaFirst.id))
|
||||
}
|
||||
|
||||
private suspend fun loadManga(name: String): Manga {
|
||||
val assets = InstrumentationRegistry.getInstrumentation().context.assets
|
||||
val manga = assets.open("manga/$name").use {
|
||||
mangaAdapter.fromJson(it.source().buffer())
|
||||
} ?: throw RuntimeException("Cannot read manga from json \"$name\"")
|
||||
val manga = SampleData.loadAsset("manga/$name", Manga::class)
|
||||
dataRepository.storeManga(manga)
|
||||
return manga
|
||||
}
|
||||
|
||||
@@ -111,6 +111,7 @@
|
||||
|
||||
<service
|
||||
android:name="org.koitharu.kotatsu.download.ui.service.DownloadService"
|
||||
android:stopWithTask="false"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
<service android:name="org.koitharu.kotatsu.local.ui.LocalChaptersRemoveService" />
|
||||
<service
|
||||
|
||||
@@ -5,16 +5,19 @@ import android.content.Context
|
||||
import android.os.StrictMode
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.fragment.app.strictmode.FragmentStrictMode
|
||||
import androidx.room.InvalidationTracker
|
||||
import org.acra.ReportField
|
||||
import org.acra.config.dialog
|
||||
import org.acra.config.mailSender
|
||||
import org.acra.config.httpSender
|
||||
import org.acra.data.StringFormat
|
||||
import org.acra.ktx.initAcra
|
||||
import org.acra.sender.HttpSender
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koin.android.ext.android.getKoin
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koitharu.kotatsu.base.ui.util.ActivityRecreationHandle
|
||||
import org.koitharu.kotatsu.bookmarks.bookmarksModule
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.databaseModule
|
||||
import org.koitharu.kotatsu.core.github.githubModule
|
||||
import org.koitharu.kotatsu.core.network.networkModule
|
||||
@@ -27,7 +30,6 @@ import org.koitharu.kotatsu.local.data.PagesCache
|
||||
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.local.localModule
|
||||
import org.koitharu.kotatsu.main.mainModule
|
||||
import org.koitharu.kotatsu.main.ui.protect.AppProtectHelper
|
||||
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.reader.readerModule
|
||||
import org.koitharu.kotatsu.remotelist.remoteListModule
|
||||
@@ -36,7 +38,6 @@ import org.koitharu.kotatsu.search.searchModule
|
||||
import org.koitharu.kotatsu.settings.settingsModule
|
||||
import org.koitharu.kotatsu.suggestions.suggestionsModule
|
||||
import org.koitharu.kotatsu.tracker.trackerModule
|
||||
import org.koitharu.kotatsu.widget.WidgetUpdater
|
||||
import org.koitharu.kotatsu.widget.appWidgetModule
|
||||
|
||||
class KotatsuApp : Application() {
|
||||
@@ -48,11 +49,8 @@ class KotatsuApp : Application() {
|
||||
}
|
||||
initKoin()
|
||||
AppCompatDelegate.setDefaultNightMode(get<AppSettings>().theme)
|
||||
registerActivityLifecycleCallbacks(get<AppProtectHelper>())
|
||||
registerActivityLifecycleCallbacks(get<ActivityRecreationHandle>())
|
||||
val widgetUpdater = WidgetUpdater(applicationContext)
|
||||
widgetUpdater.subscribeToFavourites(get())
|
||||
widgetUpdater.subscribeToHistory(get())
|
||||
setupActivityLifecycleCallbacks()
|
||||
setupDatabaseObservers()
|
||||
}
|
||||
|
||||
private fun initKoin() {
|
||||
@@ -85,16 +83,24 @@ class KotatsuApp : Application() {
|
||||
super.attachBaseContext(base)
|
||||
initAcra {
|
||||
buildConfigClass = BuildConfig::class.java
|
||||
reportFormat = StringFormat.KEY_VALUE_LIST
|
||||
reportFormat = StringFormat.JSON
|
||||
excludeMatchingSharedPreferencesKeys = listOf(
|
||||
"sources_\\w+",
|
||||
)
|
||||
httpSender {
|
||||
uri = getString(R.string.url_error_report)
|
||||
basicAuthLogin = getString(R.string.acra_login)
|
||||
basicAuthPassword = getString(R.string.acra_password)
|
||||
httpMethod = HttpSender.Method.POST
|
||||
}
|
||||
reportContent = listOf(
|
||||
ReportField.PACKAGE_NAME,
|
||||
ReportField.APP_VERSION_CODE,
|
||||
ReportField.APP_VERSION_NAME,
|
||||
ReportField.ANDROID_VERSION,
|
||||
ReportField.PHONE_MODEL,
|
||||
ReportField.CRASH_CONFIGURATION,
|
||||
ReportField.STACK_TRACE,
|
||||
ReportField.CUSTOM_DATA,
|
||||
ReportField.CRASH_CONFIGURATION,
|
||||
ReportField.SHARED_PREFERENCES,
|
||||
)
|
||||
dialog {
|
||||
@@ -104,11 +110,22 @@ class KotatsuApp : Application() {
|
||||
resIcon = R.drawable.ic_alert_outline
|
||||
resTheme = android.R.style.Theme_Material_Light_Dialog_Alert
|
||||
}
|
||||
mailSender {
|
||||
mailTo = getString(R.string.email_error_report)
|
||||
reportAsFile = true
|
||||
reportFileName = "stacktrace.txt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupDatabaseObservers() {
|
||||
val observers = getKoin().getAll<InvalidationTracker.Observer>()
|
||||
val database = get<MangaDatabase>()
|
||||
val tracker = database.invalidationTracker
|
||||
observers.forEach {
|
||||
tracker.addObserver(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupActivityLifecycleCallbacks() {
|
||||
val callbacks = getKoin().getAll<ActivityLifecycleCallbacks>()
|
||||
callbacks.forEach {
|
||||
registerActivityLifecycleCallbacks(it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +134,7 @@ class KotatsuApp : Application() {
|
||||
StrictMode.ThreadPolicy.Builder()
|
||||
.detectAll()
|
||||
.penaltyLog()
|
||||
.build()
|
||||
.build(),
|
||||
)
|
||||
StrictMode.setVmPolicy(
|
||||
StrictMode.VmPolicy.Builder()
|
||||
@@ -126,7 +143,7 @@ class KotatsuApp : Application() {
|
||||
.setClassInstanceLimit(PagesCache::class.java, 1)
|
||||
.setClassInstanceLimit(MangaLoaderContext::class.java, 1)
|
||||
.penaltyLog()
|
||||
.build()
|
||||
.build(),
|
||||
)
|
||||
FragmentStrictMode.defaultPolicy = FragmentStrictMode.Policy.Builder()
|
||||
.penaltyDeath()
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package org.koitharu.kotatsu.base.domain
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.processLifecycleScope
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
fun interface ReversibleHandle {
|
||||
|
||||
@@ -10,7 +14,13 @@ fun interface ReversibleHandle {
|
||||
}
|
||||
|
||||
fun ReversibleHandle.reverseAsync() = processLifecycleScope.launch(Dispatchers.Default) {
|
||||
reverse()
|
||||
runCatchingCancellable {
|
||||
withContext(NonCancellable) {
|
||||
reverse()
|
||||
}
|
||||
}.onFailure {
|
||||
it.printStackTraceDebug()
|
||||
}
|
||||
}
|
||||
|
||||
operator fun ReversibleHandle.plus(other: ReversibleHandle) = ReversibleHandle {
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.widget.Checkable
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.appcompat.widget.AppCompatImageView
|
||||
import androidx.core.os.ParcelCompat
|
||||
import androidx.customview.view.AbsSavedState
|
||||
|
||||
class CheckableImageView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
@@ -73,7 +74,7 @@ class CheckableImageView @JvmOverloads constructor(
|
||||
fun onCheckedChanged(view: CheckableImageView, isChecked: Boolean)
|
||||
}
|
||||
|
||||
private class SavedState : BaseSavedState {
|
||||
private class SavedState : AbsSavedState {
|
||||
|
||||
val isChecked: Boolean
|
||||
|
||||
@@ -81,7 +82,7 @@ class CheckableImageView @JvmOverloads constructor(
|
||||
isChecked = checked
|
||||
}
|
||||
|
||||
constructor(source: Parcel) : super(source) {
|
||||
constructor(source: Parcel, classLoader: ClassLoader?) : super(source, classLoader) {
|
||||
isChecked = ParcelCompat.readBoolean(source)
|
||||
}
|
||||
|
||||
@@ -91,9 +92,10 @@ class CheckableImageView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Suppress("unused")
|
||||
@JvmField
|
||||
val CREATOR: Creator<SavedState> = object : Creator<SavedState> {
|
||||
override fun createFromParcel(`in`: Parcel) = SavedState(`in`)
|
||||
override fun createFromParcel(`in`: Parcel) = SavedState(`in`, SavedState::class.java.classLoader)
|
||||
|
||||
override fun newArray(size: Int): Array<SavedState?> = arrayOfNulls(size)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.koitharu.kotatsu.base.ui.widgets
|
||||
|
||||
import android.content.Context
|
||||
import android.text.Selection
|
||||
import android.text.Spannable
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import androidx.annotation.AttrRes
|
||||
import com.google.android.material.textview.MaterialTextView
|
||||
|
||||
class SelectableTextView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
@AttrRes defStyleAttr: Int = android.R.attr.textViewStyle,
|
||||
) : MaterialTextView(context, attrs, defStyleAttr) {
|
||||
|
||||
override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
|
||||
fixSelectionRange()
|
||||
return super.dispatchTouchEvent(event)
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/22810147/error-when-selecting-text-from-textview-java-lang-indexoutofboundsexception-se
|
||||
private fun fixSelectionRange() {
|
||||
if (selectionStart < 0 || selectionEnd < 0) {
|
||||
val spannableText = text as? Spannable ?: return
|
||||
Selection.setSelection(spannableText, text.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
),
|
||||
]
|
||||
)
|
||||
class BookmarkEntity(
|
||||
data class BookmarkEntity(
|
||||
@ColumnInfo(name = "manga_id", index = true) val mangaId: Long,
|
||||
@ColumnInfo(name = "page_id", index = true) val pageId: Long,
|
||||
@ColumnInfo(name = "chapter_id") val chapterId: Long,
|
||||
|
||||
@@ -2,15 +2,13 @@ package org.koitharu.kotatsu.bookmarks.ui
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import coil.ImageLoader
|
||||
import coil.request.Disposable
|
||||
import coil.size.Scale
|
||||
import coil.util.CoilUtils
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.ui.list.AdapterDelegateClickListenerAdapter
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
import org.koitharu.kotatsu.databinding.ItemBookmarkBinding
|
||||
import org.koitharu.kotatsu.utils.ext.disposeImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
@@ -23,29 +21,24 @@ fun bookmarkListAD(
|
||||
{ inflater, parent -> ItemBookmarkBinding.inflate(inflater, parent, false) }
|
||||
) {
|
||||
|
||||
var imageRequest: Disposable? = null
|
||||
val listener = AdapterDelegateClickListenerAdapter(this, clickListener)
|
||||
|
||||
binding.root.setOnClickListener(listener)
|
||||
binding.root.setOnLongClickListener(listener)
|
||||
|
||||
bind {
|
||||
imageRequest?.dispose()
|
||||
imageRequest = binding.imageViewThumb.newImageRequest(item.imageUrl)
|
||||
.referer(item.manga.publicUrl)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.allowRgb565(true)
|
||||
.scale(Scale.FILL)
|
||||
.lifecycle(lifecycleOwner)
|
||||
.enqueueWith(coil)
|
||||
binding.imageViewThumb.newImageRequest(item.imageUrl)?.run {
|
||||
referer(item.manga.publicUrl)
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
allowRgb565(true)
|
||||
lifecycle(lifecycleOwner)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
}
|
||||
|
||||
onViewRecycled {
|
||||
imageRequest?.dispose()
|
||||
imageRequest = null
|
||||
CoilUtils.dispose(binding.imageViewThumb)
|
||||
binding.imageViewThumb.setImageDrawable(null)
|
||||
binding.imageViewThumb.disposeImageRequest()
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,9 @@ package org.koitharu.kotatsu.browser
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.webkit.WebView
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koitharu.kotatsu.core.network.WebViewClientCompat
|
||||
import android.webkit.WebViewClient
|
||||
|
||||
class BrowserClient(private val callback: BrowserCallback) : WebViewClientCompat(), KoinComponent {
|
||||
class BrowserClient(private val callback: BrowserCallback) : WebViewClient() {
|
||||
|
||||
override fun onPageFinished(webView: WebView, url: String) {
|
||||
super.onPageFinished(webView, url)
|
||||
|
||||
@@ -2,13 +2,14 @@ package org.koitharu.kotatsu.browser
|
||||
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebView
|
||||
import android.widget.ProgressBar
|
||||
import androidx.core.view.isVisible
|
||||
import com.google.android.material.progressindicator.BaseProgressIndicator
|
||||
import org.koitharu.kotatsu.utils.ext.setProgressCompat
|
||||
|
||||
private const val PROGRESS_MAX = 100
|
||||
|
||||
class ProgressChromeClient(
|
||||
private val progressIndicator: BaseProgressIndicator<*>,
|
||||
private val progressIndicator: ProgressBar,
|
||||
) : WebChromeClient() {
|
||||
|
||||
init {
|
||||
@@ -24,7 +25,7 @@ class ProgressChromeClient(
|
||||
progressIndicator.isIndeterminate = false
|
||||
progressIndicator.setProgressCompat(newProgress.coerceAtMost(PROGRESS_MAX), true)
|
||||
} else {
|
||||
progressIndicator.setIndeterminate(true)
|
||||
progressIndicator.isIndeterminate = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,19 @@ package org.koitharu.kotatsu.browser.cloudflare
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.koitharu.kotatsu.core.network.AndroidCookieJar
|
||||
import org.koitharu.kotatsu.core.network.WebViewClientCompat
|
||||
|
||||
private const val CF_CLEARANCE = "cf_clearance"
|
||||
|
||||
class CloudFlareClient(
|
||||
private val cookieJar: AndroidCookieJar,
|
||||
private val callback: CloudFlareCallback,
|
||||
private val targetUrl: String
|
||||
) : WebViewClientCompat() {
|
||||
private val targetUrl: String,
|
||||
) : WebViewClient() {
|
||||
|
||||
private val oldClearance = getCookieValue(CF_CLEARANCE)
|
||||
private val oldClearance = getClearance()
|
||||
|
||||
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
|
||||
super.onPageStarted(view, url, favicon)
|
||||
@@ -32,14 +32,14 @@ class CloudFlareClient(
|
||||
}
|
||||
|
||||
private fun checkClearance() {
|
||||
val clearance = getCookieValue(CF_CLEARANCE)
|
||||
val clearance = getClearance()
|
||||
if (clearance != null && clearance != oldClearance) {
|
||||
callback.onCheckPassed()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCookieValue(name: String): String? {
|
||||
private fun getClearance(): String? {
|
||||
return cookieJar.loadForRequest(targetUrl.toHttpUrl())
|
||||
.find { it.name == name }?.value
|
||||
.find { it.name == CF_CLEARANCE }?.value
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ class CloudFlareDialog : AlertDialogFragment<FragmentCloudflareBinding>(), Cloud
|
||||
|
||||
override fun onInflateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?
|
||||
container: ViewGroup?,
|
||||
) = FragmentCloudflareBinding.inflate(inflater, container, false)
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
@@ -49,6 +49,7 @@ class CloudFlareDialog : AlertDialogFragment<FragmentCloudflareBinding>(), Cloud
|
||||
|
||||
override fun onDestroyView() {
|
||||
binding.webView.stopLoading()
|
||||
binding.webView.destroy()
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ class CloudFlareDialog : AlertDialogFragment<FragmentCloudflareBinding>(), Cloud
|
||||
|
||||
override fun onCheckPassed() {
|
||||
pendingResult.putBoolean(EXTRA_RESULT, true)
|
||||
dismiss()
|
||||
dismissAllowingStateLoss()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package org.koitharu.kotatsu.core.backup
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
|
||||
import org.koitharu.kotatsu.history.data.HistoryEntity
|
||||
import org.koitharu.kotatsu.parsers.util.json.JSONIterator
|
||||
import org.koitharu.kotatsu.parsers.util.json.mapJSON
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
private const val PAGE_SIZE = 10
|
||||
|
||||
@@ -24,11 +23,11 @@ class BackupRepository(private val db: MangaDatabase) {
|
||||
}
|
||||
offset += history.size
|
||||
for (item in history) {
|
||||
val manga = item.manga.toJson()
|
||||
val manga = JsonSerializer(item.manga).toJson()
|
||||
val tags = JSONArray()
|
||||
item.tags.forEach { tags.put(it.toJson()) }
|
||||
item.tags.forEach { tags.put(JsonSerializer(it).toJson()) }
|
||||
manga.put("tags", tags)
|
||||
val json = item.history.toJson()
|
||||
val json = JsonSerializer(item.history).toJson()
|
||||
json.put("manga", manga)
|
||||
entry.data.put(json)
|
||||
}
|
||||
@@ -40,7 +39,7 @@ class BackupRepository(private val db: MangaDatabase) {
|
||||
val entry = BackupEntry(BackupEntry.CATEGORIES, JSONArray())
|
||||
val categories = db.favouriteCategoriesDao.findAll()
|
||||
for (item in categories) {
|
||||
entry.data.put(item.toJson())
|
||||
entry.data.put(JsonSerializer(item).toJson())
|
||||
}
|
||||
return entry
|
||||
}
|
||||
@@ -55,11 +54,11 @@ class BackupRepository(private val db: MangaDatabase) {
|
||||
}
|
||||
offset += favourites.size
|
||||
for (item in favourites) {
|
||||
val manga = item.manga.toJson()
|
||||
val manga = JsonSerializer(item.manga).toJson()
|
||||
val tags = JSONArray()
|
||||
item.tags.forEach { tags.put(it.toJson()) }
|
||||
item.tags.forEach { tags.put(JsonSerializer(it).toJson()) }
|
||||
manga.put("tags", tags)
|
||||
val json = item.favourite.toJson()
|
||||
val json = JsonSerializer(item.favourite).toJson()
|
||||
json.put("manga", manga)
|
||||
entry.data.put(json)
|
||||
}
|
||||
@@ -77,60 +76,54 @@ class BackupRepository(private val db: MangaDatabase) {
|
||||
return entry
|
||||
}
|
||||
|
||||
private fun MangaEntity.toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
jo.put("id", id)
|
||||
jo.put("title", title)
|
||||
jo.put("alt_title", altTitle)
|
||||
jo.put("url", url)
|
||||
jo.put("public_url", publicUrl)
|
||||
jo.put("rating", rating)
|
||||
jo.put("nsfw", isNsfw)
|
||||
jo.put("cover_url", coverUrl)
|
||||
jo.put("large_cover_url", largeCoverUrl)
|
||||
jo.put("state", state)
|
||||
jo.put("author", author)
|
||||
jo.put("source", source)
|
||||
return jo
|
||||
suspend fun restoreHistory(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val mangaJson = item.getJSONObject("manga")
|
||||
val manga = JsonDeserializer(mangaJson).toMangaEntity()
|
||||
val tags = mangaJson.getJSONArray("tags").mapJSON {
|
||||
JsonDeserializer(it).toTagEntity()
|
||||
}
|
||||
val history = JsonDeserializer(item).toHistoryEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.historyDao.upsert(history)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun TagEntity.toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
jo.put("id", id)
|
||||
jo.put("title", title)
|
||||
jo.put("key", key)
|
||||
jo.put("source", source)
|
||||
return jo
|
||||
suspend fun restoreCategories(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val category = JsonDeserializer(item).toFavouriteCategoryEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.favouriteCategoriesDao.upsert(category)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun HistoryEntity.toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
jo.put("manga_id", mangaId)
|
||||
jo.put("created_at", createdAt)
|
||||
jo.put("updated_at", updatedAt)
|
||||
jo.put("chapter_id", chapterId)
|
||||
jo.put("page", page)
|
||||
jo.put("scroll", scroll)
|
||||
jo.put("percent", percent)
|
||||
return jo
|
||||
}
|
||||
|
||||
private fun FavouriteCategoryEntity.toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
jo.put("category_id", categoryId)
|
||||
jo.put("created_at", createdAt)
|
||||
jo.put("sort_key", sortKey)
|
||||
jo.put("title", title)
|
||||
jo.put("order", order)
|
||||
jo.put("track", track)
|
||||
return jo
|
||||
}
|
||||
|
||||
private fun FavouriteEntity.toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
jo.put("manga_id", mangaId)
|
||||
jo.put("category_id", categoryId)
|
||||
jo.put("created_at", createdAt)
|
||||
return jo
|
||||
suspend fun restoreFavourites(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val mangaJson = item.getJSONObject("manga")
|
||||
val manga = JsonDeserializer(mangaJson).toMangaEntity()
|
||||
val tags = mangaJson.getJSONArray("tags").mapJSON {
|
||||
JsonDeserializer(it).toTagEntity()
|
||||
}
|
||||
val favourite = JsonDeserializer(item).toFavouriteEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.favouritesDao.upsert(favourite)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.koitharu.kotatsu.core.backup
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
|
||||
import org.koitharu.kotatsu.history.data.HistoryEntity
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.parsers.util.json.getBooleanOrDefault
|
||||
import org.koitharu.kotatsu.parsers.util.json.getFloatOrDefault
|
||||
import org.koitharu.kotatsu.parsers.util.json.getStringOrNull
|
||||
|
||||
class JsonDeserializer(private val json: JSONObject) {
|
||||
|
||||
fun toFavouriteEntity() = FavouriteEntity(
|
||||
mangaId = json.getLong("manga_id"),
|
||||
categoryId = json.getLong("category_id"),
|
||||
createdAt = json.getLong("created_at"),
|
||||
)
|
||||
|
||||
fun toMangaEntity() = MangaEntity(
|
||||
id = json.getLong("id"),
|
||||
title = json.getString("title"),
|
||||
altTitle = json.getStringOrNull("alt_title"),
|
||||
url = json.getString("url"),
|
||||
publicUrl = json.getStringOrNull("public_url").orEmpty(),
|
||||
rating = json.getDouble("rating").toFloat(),
|
||||
isNsfw = json.getBooleanOrDefault("nsfw", false),
|
||||
coverUrl = json.getString("cover_url"),
|
||||
largeCoverUrl = json.getStringOrNull("large_cover_url"),
|
||||
state = json.getStringOrNull("state"),
|
||||
author = json.getStringOrNull("author"),
|
||||
source = json.getString("source")
|
||||
)
|
||||
|
||||
fun toTagEntity() = TagEntity(
|
||||
id = json.getLong("id"),
|
||||
title = json.getString("title"),
|
||||
key = json.getString("key"),
|
||||
source = json.getString("source")
|
||||
)
|
||||
|
||||
fun toHistoryEntity() = HistoryEntity(
|
||||
mangaId = json.getLong("manga_id"),
|
||||
createdAt = json.getLong("created_at"),
|
||||
updatedAt = json.getLong("updated_at"),
|
||||
chapterId = json.getLong("chapter_id"),
|
||||
page = json.getInt("page"),
|
||||
scroll = json.getDouble("scroll").toFloat(),
|
||||
percent = json.getFloatOrDefault("percent", -1f),
|
||||
)
|
||||
|
||||
fun toFavouriteCategoryEntity() = FavouriteCategoryEntity(
|
||||
categoryId = json.getInt("category_id"),
|
||||
createdAt = json.getLong("created_at"),
|
||||
sortKey = json.getInt("sort_key"),
|
||||
title = json.getString("title"),
|
||||
order = json.getStringOrNull("order") ?: SortOrder.NEWEST.name,
|
||||
track = json.getBooleanOrDefault("track", true),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.koitharu.kotatsu.core.backup
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
|
||||
import org.koitharu.kotatsu.history.data.HistoryEntity
|
||||
|
||||
class JsonSerializer private constructor(private val json: JSONObject) {
|
||||
|
||||
constructor(e: FavouriteEntity) : this(
|
||||
JSONObject().apply {
|
||||
put("manga_id", e.mangaId)
|
||||
put("category_id", e.categoryId)
|
||||
put("created_at", e.createdAt)
|
||||
}
|
||||
)
|
||||
|
||||
constructor(e: FavouriteCategoryEntity) : this(
|
||||
JSONObject().apply {
|
||||
put("category_id", e.categoryId)
|
||||
put("created_at", e.createdAt)
|
||||
put("sort_key", e.sortKey)
|
||||
put("title", e.title)
|
||||
put("order", e.order)
|
||||
put("track", e.track)
|
||||
}
|
||||
)
|
||||
|
||||
constructor(e: HistoryEntity) : this(
|
||||
JSONObject().apply {
|
||||
put("manga_id", e.mangaId)
|
||||
put("created_at", e.createdAt)
|
||||
put("updated_at", e.updatedAt)
|
||||
put("chapter_id", e.chapterId)
|
||||
put("page", e.page)
|
||||
put("scroll", e.scroll)
|
||||
put("percent", e.percent)
|
||||
}
|
||||
)
|
||||
|
||||
constructor(e: TagEntity) : this(
|
||||
JSONObject().apply {
|
||||
put("id", e.id)
|
||||
put("title", e.title)
|
||||
put("key", e.key)
|
||||
put("source", e.source)
|
||||
}
|
||||
)
|
||||
|
||||
constructor(e: MangaEntity) : this(
|
||||
JSONObject().apply {
|
||||
put("id", e.id)
|
||||
put("title", e.title)
|
||||
put("alt_title", e.altTitle)
|
||||
put("url", e.url)
|
||||
put("public_url", e.publicUrl)
|
||||
put("rating", e.rating)
|
||||
put("nsfw", e.isNsfw)
|
||||
put("cover_url", e.coverUrl)
|
||||
put("large_cover_url", e.largeCoverUrl)
|
||||
put("state", e.state)
|
||||
put("author", e.author)
|
||||
put("source", e.source)
|
||||
}
|
||||
)
|
||||
|
||||
fun toJson(): JSONObject = json
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
package org.koitharu.kotatsu.core.backup
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import org.json.JSONObject
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
|
||||
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
|
||||
import org.koitharu.kotatsu.history.data.HistoryEntity
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.parsers.util.json.*
|
||||
|
||||
class RestoreRepository(private val db: MangaDatabase) {
|
||||
|
||||
suspend fun upsertHistory(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val mangaJson = item.getJSONObject("manga")
|
||||
val manga = parseManga(mangaJson)
|
||||
val tags = mangaJson.getJSONArray("tags").mapJSON {
|
||||
parseTag(it)
|
||||
}
|
||||
val history = parseHistory(item)
|
||||
result += runCatching {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.historyDao.upsert(history)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
suspend fun upsertCategories(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val category = parseCategory(item)
|
||||
result += runCatching {
|
||||
db.favouriteCategoriesDao.upsert(category)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
suspend fun upsertFavourites(entry: BackupEntry): CompositeResult {
|
||||
val result = CompositeResult()
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val mangaJson = item.getJSONObject("manga")
|
||||
val manga = parseManga(mangaJson)
|
||||
val tags = mangaJson.getJSONArray("tags").mapJSON {
|
||||
parseTag(it)
|
||||
}
|
||||
val favourite = parseFavourite(item)
|
||||
result += runCatching {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.favouritesDao.upsert(favourite)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun parseManga(json: JSONObject) = MangaEntity(
|
||||
id = json.getLong("id"),
|
||||
title = json.getString("title"),
|
||||
altTitle = json.getStringOrNull("alt_title"),
|
||||
url = json.getString("url"),
|
||||
publicUrl = json.getStringOrNull("public_url").orEmpty(),
|
||||
rating = json.getDouble("rating").toFloat(),
|
||||
isNsfw = json.getBooleanOrDefault("nsfw", false),
|
||||
coverUrl = json.getString("cover_url"),
|
||||
largeCoverUrl = json.getStringOrNull("large_cover_url"),
|
||||
state = json.getStringOrNull("state"),
|
||||
author = json.getStringOrNull("author"),
|
||||
source = json.getString("source")
|
||||
)
|
||||
|
||||
private fun parseTag(json: JSONObject) = TagEntity(
|
||||
id = json.getLong("id"),
|
||||
title = json.getString("title"),
|
||||
key = json.getString("key"),
|
||||
source = json.getString("source")
|
||||
)
|
||||
|
||||
private fun parseHistory(json: JSONObject) = HistoryEntity(
|
||||
mangaId = json.getLong("manga_id"),
|
||||
createdAt = json.getLong("created_at"),
|
||||
updatedAt = json.getLong("updated_at"),
|
||||
chapterId = json.getLong("chapter_id"),
|
||||
page = json.getInt("page"),
|
||||
scroll = json.getDouble("scroll").toFloat(),
|
||||
percent = json.getFloatOrDefault("percent", -1f),
|
||||
)
|
||||
|
||||
private fun parseCategory(json: JSONObject) = FavouriteCategoryEntity(
|
||||
categoryId = json.getInt("category_id"),
|
||||
createdAt = json.getLong("created_at"),
|
||||
sortKey = json.getInt("sort_key"),
|
||||
title = json.getString("title"),
|
||||
order = json.getStringOrNull("order") ?: SortOrder.NEWEST.name,
|
||||
track = json.getBooleanOrDefault("track", true),
|
||||
)
|
||||
|
||||
private fun parseFavourite(json: JSONObject) = FavouriteEntity(
|
||||
mangaId = json.getLong("manga_id"),
|
||||
categoryId = json.getLong("category_id"),
|
||||
createdAt = json.getLong("created_at")
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.migration.Migration
|
||||
import org.koitharu.kotatsu.bookmarks.data.BookmarkEntity
|
||||
import org.koitharu.kotatsu.bookmarks.data.BookmarksDao
|
||||
import org.koitharu.kotatsu.core.db.dao.MangaDao
|
||||
@@ -29,6 +30,8 @@ import org.koitharu.kotatsu.tracker.data.TrackEntity
|
||||
import org.koitharu.kotatsu.tracker.data.TrackLogEntity
|
||||
import org.koitharu.kotatsu.tracker.data.TracksDao
|
||||
|
||||
const val DATABASE_VERSION = 12
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class,
|
||||
@@ -36,7 +39,7 @@ import org.koitharu.kotatsu.tracker.data.TracksDao
|
||||
TrackEntity::class, TrackLogEntity::class, SuggestionEntity::class, BookmarkEntity::class,
|
||||
ScrobblingEntity::class,
|
||||
],
|
||||
version = 12,
|
||||
version = DATABASE_VERSION,
|
||||
)
|
||||
abstract class MangaDatabase : RoomDatabase() {
|
||||
|
||||
@@ -63,22 +66,23 @@ abstract class MangaDatabase : RoomDatabase() {
|
||||
abstract val scrobblingDao: ScrobblingDao
|
||||
}
|
||||
|
||||
fun MangaDatabase(context: Context): MangaDatabase = Room.databaseBuilder(
|
||||
context,
|
||||
MangaDatabase::class.java,
|
||||
"kotatsu-db"
|
||||
).addMigrations(
|
||||
Migration1To2(),
|
||||
Migration2To3(),
|
||||
Migration3To4(),
|
||||
Migration4To5(),
|
||||
Migration5To6(),
|
||||
Migration6To7(),
|
||||
Migration7To8(),
|
||||
Migration8To9(),
|
||||
Migration9To10(),
|
||||
Migration10To11(),
|
||||
Migration11To12(),
|
||||
).addCallback(
|
||||
DatabasePrePopulateCallback(context.resources)
|
||||
).build()
|
||||
val databaseMigrations: Array<Migration>
|
||||
get() = arrayOf(
|
||||
Migration1To2(),
|
||||
Migration2To3(),
|
||||
Migration3To4(),
|
||||
Migration4To5(),
|
||||
Migration5To6(),
|
||||
Migration6To7(),
|
||||
Migration7To8(),
|
||||
Migration8To9(),
|
||||
Migration9To10(),
|
||||
Migration10To11(),
|
||||
Migration11To12(),
|
||||
)
|
||||
|
||||
fun MangaDatabase(context: Context): MangaDatabase = Room
|
||||
.databaseBuilder(context, MangaDatabase::class.java, "kotatsu-db")
|
||||
.addMigrations(*databaseMigrations)
|
||||
.addCallback(DatabasePrePopulateCallback(context.resources))
|
||||
.build()
|
||||
9
app/src/main/java/org/koitharu/kotatsu/core/db/Tables.kt
Normal file
9
app/src/main/java/org/koitharu/kotatsu/core/db/Tables.kt
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.koitharu.kotatsu.core.db
|
||||
|
||||
|
||||
const val TABLE_FAVOURITES = "favourites"
|
||||
const val TABLE_MANGA = "manga"
|
||||
const val TABLE_TAGS = "tags"
|
||||
const val TABLE_FAVOURITE_CATEGORIES = "favourite_categories"
|
||||
const val TABLE_HISTORY = "history"
|
||||
const val TABLE_MANGA_TAGS = "manga_tags"
|
||||
@@ -3,9 +3,10 @@ package org.koitharu.kotatsu.core.db.entity
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_MANGA
|
||||
|
||||
@Entity(tableName = "manga")
|
||||
class MangaEntity(
|
||||
@Entity(tableName = TABLE_MANGA)
|
||||
data class MangaEntity(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
@ColumnInfo(name = "manga_id") val id: Long,
|
||||
@ColumnInfo(name = "title") val title: String,
|
||||
@@ -18,5 +19,5 @@ class MangaEntity(
|
||||
@ColumnInfo(name = "large_cover_url") val largeCoverUrl: String?,
|
||||
@ColumnInfo(name = "state") val state: String?,
|
||||
@ColumnInfo(name = "author") val author: String?,
|
||||
@ColumnInfo(name = "source") val source: String
|
||||
@ColumnInfo(name = "source") val source: String,
|
||||
)
|
||||
@@ -3,9 +3,11 @@ package org.koitharu.kotatsu.core.db.entity
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_MANGA_TAGS
|
||||
|
||||
@Entity(
|
||||
tableName = "manga_tags", primaryKeys = ["manga_id", "tag_id"],
|
||||
tableName = TABLE_MANGA_TAGS,
|
||||
primaryKeys = ["manga_id", "tag_id"],
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = MangaEntity::class,
|
||||
@@ -23,5 +25,5 @@ import androidx.room.ForeignKey
|
||||
)
|
||||
class MangaTagsEntity(
|
||||
@ColumnInfo(name = "manga_id", index = true) val mangaId: Long,
|
||||
@ColumnInfo(name = "tag_id", index = true) val tagId: Long
|
||||
@ColumnInfo(name = "tag_id", index = true) val tagId: Long,
|
||||
)
|
||||
@@ -3,12 +3,13 @@ package org.koitharu.kotatsu.core.db.entity
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_TAGS
|
||||
|
||||
@Entity(tableName = "tags")
|
||||
class TagEntity(
|
||||
@Entity(tableName = TABLE_TAGS)
|
||||
data class TagEntity(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
@ColumnInfo(name = "tag_id") val id: Long,
|
||||
@ColumnInfo(name = "title") val title: String,
|
||||
@ColumnInfo(name = "key") val key: String,
|
||||
@ColumnInfo(name = "source") val source: String
|
||||
@ColumnInfo(name = "source") val source: String,
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
package org.koitharu.kotatsu.core.exceptions
|
||||
|
||||
class CaughtException(cause: Throwable, override val message: String?) : RuntimeException(cause)
|
||||
@@ -1,3 +0,0 @@
|
||||
package org.koitharu.kotatsu.core.exceptions
|
||||
|
||||
class MangaNotFoundException(s: String? = null) : RuntimeException(s)
|
||||
@@ -8,9 +8,11 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.browser.BrowserActivity
|
||||
import org.koitharu.kotatsu.browser.cloudflare.CloudFlareDialog
|
||||
import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException
|
||||
import org.koitharu.kotatsu.parsers.exception.AuthRequiredException
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity
|
||||
import org.koitharu.kotatsu.utils.TaggedActivityResult
|
||||
@@ -43,6 +45,10 @@ class ExceptionResolver private constructor(
|
||||
suspend fun resolve(e: Throwable): Boolean = when (e) {
|
||||
is CloudFlareProtectedException -> resolveCF(e.url)
|
||||
is AuthRequiredException -> resolveAuthException(e.source)
|
||||
is NotFoundException -> {
|
||||
openInBrowser(e.url)
|
||||
false
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
|
||||
@@ -69,6 +75,11 @@ class ExceptionResolver private constructor(
|
||||
sourceAuthContract.launch(source)
|
||||
}
|
||||
|
||||
private fun openInBrowser(url: String) {
|
||||
val context = activity ?: fragment?.activity ?: return
|
||||
context.startActivity(BrowserActivity.newIntent(context, url, null))
|
||||
}
|
||||
|
||||
private fun getFragmentManager() = checkNotNull(fragment?.childFragmentManager ?: activity?.supportFragmentManager)
|
||||
|
||||
companion object {
|
||||
@@ -77,6 +88,7 @@ class ExceptionResolver private constructor(
|
||||
fun getResolveStringId(e: Throwable) = when (e) {
|
||||
is CloudFlareProtectedException -> R.string.captcha_solve
|
||||
is AuthRequiredException -> R.string.sign_in
|
||||
is NotFoundException -> if (e.url.isNotEmpty()) R.string.open_in_browser else 0
|
||||
else -> 0
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class GithubRepository(private val okHttp: OkHttpClient) {
|
||||
suspend fun getLatestVersion(): AppVersion {
|
||||
val request = Request.Builder()
|
||||
.get()
|
||||
.url("https://api.github.com/repos/nv95/Kotatsu/releases/latest")
|
||||
.url("https://api.github.com/repos/KotatsuApp/Kotatsu/releases/latest")
|
||||
val json = okHttp.newCall(request.build()).await().parseJson()
|
||||
val asset = json.getJSONArray("assets").getJSONObject(0)
|
||||
return AppVersion(
|
||||
|
||||
@@ -1,6 +1,34 @@
|
||||
package org.koitharu.kotatsu.core.model
|
||||
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||
import org.koitharu.kotatsu.parsers.util.toTitleCase
|
||||
import org.koitharu.kotatsu.utils.ext.iterator
|
||||
|
||||
fun Collection<Manga>.ids() = mapToSet { it.id }
|
||||
fun Collection<Manga>.ids() = mapToSet { it.id }
|
||||
|
||||
fun Manga.getPreferredBranch(history: MangaHistory?): String? {
|
||||
val ch = chapters
|
||||
if (ch.isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
if (history != null) {
|
||||
val currentChapter = ch.find { it.id == history.chapterId }
|
||||
if (currentChapter != null) {
|
||||
return currentChapter.branch
|
||||
}
|
||||
}
|
||||
val groups = ch.groupBy { it.branch }
|
||||
for (locale in LocaleListCompat.getAdjustedDefault()) {
|
||||
var language = locale.getDisplayLanguage(locale).toTitleCase(locale)
|
||||
if (groups.containsKey(language)) {
|
||||
return language
|
||||
}
|
||||
language = locale.getDisplayName(locale).toTitleCase(locale)
|
||||
if (groups.containsKey(language)) {
|
||||
return language
|
||||
}
|
||||
}
|
||||
return groups.maxByOrNull { it.value.size }?.key
|
||||
}
|
||||
@@ -1,10 +1,18 @@
|
||||
package org.koitharu.kotatsu.core.model
|
||||
|
||||
import java.util.*
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.toTitleCase
|
||||
import java.util.*
|
||||
|
||||
fun MangaSource.getLocaleTitle(): String? {
|
||||
val lc = Locale(locale ?: return null)
|
||||
return lc.getDisplayLanguage(lc).toTitleCase(lc)
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
fun MangaSource(name: String): MangaSource? {
|
||||
MangaSource.values().forEach {
|
||||
if (it.name == name) return it
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.koitharu.kotatsu.parsers.model.Manga
|
||||
|
||||
// Limits to avoid TransactionTooLargeException
|
||||
private const val MAX_SAFE_SIZE = 1024 * 512 // Assume that 512 kb is safe parcel size
|
||||
private const val MAX_SAFE_CHAPTERS_COUNT = 40 // this is 100% safe
|
||||
private const val MAX_SAFE_CHAPTERS_COUNT = 32 // this is 100% safe
|
||||
|
||||
class ParcelableManga(
|
||||
val manga: Manga,
|
||||
|
||||
@@ -21,7 +21,12 @@ class DoHManager(
|
||||
private var cachedProvider: DoHProvider? = null
|
||||
|
||||
override fun lookup(hostname: String): List<InetAddress> {
|
||||
return getDelegate().lookup(hostname)
|
||||
return try {
|
||||
getDelegate().lookup(hostname)
|
||||
} catch (e: UnknownHostException) {
|
||||
// fallback
|
||||
Dns.SYSTEM.lookup(hostname)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
@@ -40,6 +45,7 @@ class DoHManager(
|
||||
DoHProvider.NONE -> Dns.SYSTEM
|
||||
DoHProvider.GOOGLE -> DnsOverHttps.Builder().client(bootstrapClient)
|
||||
.url("https://dns.google/dns-query".toHttpUrl())
|
||||
.resolvePrivateAddresses(true)
|
||||
.bootstrapDnsHosts(
|
||||
listOfNotNull(
|
||||
tryGetByIp("8.8.4.4"),
|
||||
@@ -50,6 +56,7 @@ class DoHManager(
|
||||
).build()
|
||||
DoHProvider.CLOUDFLARE -> DnsOverHttps.Builder().client(bootstrapClient)
|
||||
.url("https://cloudflare-dns.com/dns-query".toHttpUrl())
|
||||
.resolvePrivateAddresses(true)
|
||||
.bootstrapDnsHosts(
|
||||
listOfNotNull(
|
||||
tryGetByIp("162.159.36.1"),
|
||||
@@ -65,6 +72,7 @@ class DoHManager(
|
||||
).build()
|
||||
DoHProvider.ADGUARD -> DnsOverHttps.Builder().client(bootstrapClient)
|
||||
.url("https://dns-unfiltered.adguard.com/dns-query".toHttpUrl())
|
||||
.resolvePrivateAddresses(true)
|
||||
.bootstrapDnsHosts(
|
||||
listOfNotNull(
|
||||
tryGetByIp("94.140.14.140"),
|
||||
@@ -81,4 +89,4 @@ class DoHManager(
|
||||
e.printStackTraceDebug()
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package org.koitharu.kotatsu.core.network
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.os.Build
|
||||
import android.webkit.*
|
||||
|
||||
@Suppress("OverridingDeprecatedMember")
|
||||
abstract class WebViewClientCompat : WebViewClient() {
|
||||
|
||||
open fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
open fun shouldInterceptRequestCompat(view: WebView, url: String): WebResourceResponse? {
|
||||
return null
|
||||
}
|
||||
|
||||
open fun onReceivedErrorCompat(
|
||||
view: WebView,
|
||||
errorCode: Int,
|
||||
description: String?,
|
||||
failingUrl: String,
|
||||
isMainFrame: Boolean
|
||||
) {
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
final override fun shouldOverrideUrlLoading(
|
||||
view: WebView,
|
||||
request: WebResourceRequest
|
||||
): Boolean = shouldOverrideUrlCompat(view, request.url.toString())
|
||||
|
||||
final override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
|
||||
return shouldOverrideUrlCompat(view, url)
|
||||
}
|
||||
|
||||
final override fun shouldInterceptRequest(
|
||||
view: WebView,
|
||||
request: WebResourceRequest
|
||||
): WebResourceResponse? = shouldInterceptRequestCompat(view, request.url.toString())
|
||||
|
||||
final override fun shouldInterceptRequest(
|
||||
view: WebView,
|
||||
url: String
|
||||
): WebResourceResponse? = shouldInterceptRequestCompat(view, url)
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
final override fun onReceivedError(
|
||||
view: WebView,
|
||||
request: WebResourceRequest,
|
||||
error: WebResourceError
|
||||
) {
|
||||
onReceivedErrorCompat(
|
||||
view,
|
||||
error.errorCode,
|
||||
error.description?.toString(),
|
||||
request.url.toString(),
|
||||
request.isForMainFrame
|
||||
)
|
||||
}
|
||||
|
||||
final override fun onReceivedError(
|
||||
view: WebView,
|
||||
errorCode: Int,
|
||||
description: String?,
|
||||
failingUrl: String
|
||||
) {
|
||||
onReceivedErrorCompat(view, errorCode, description, failingUrl, failingUrl == view.url)
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
final override fun onReceivedHttpError(
|
||||
view: WebView,
|
||||
request: WebResourceRequest,
|
||||
error: WebResourceResponse
|
||||
) {
|
||||
onReceivedErrorCompat(
|
||||
view,
|
||||
error.statusCode,
|
||||
error.reasonPhrase,
|
||||
request.url
|
||||
.toString(),
|
||||
request.isForMainFrame
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,62 +6,84 @@ import android.content.pm.ShortcutManager
|
||||
import android.media.ThumbnailUtils
|
||||
import android.os.Build
|
||||
import android.util.Size
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.core.content.pm.ShortcutInfoCompat
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import androidx.room.InvalidationTracker
|
||||
import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.db.TABLE_HISTORY
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.reader.ui.ReaderActivity
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.processLifecycleScope
|
||||
import org.koitharu.kotatsu.utils.ext.requireBitmap
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
class ShortcutsRepository(
|
||||
class ShortcutsUpdater(
|
||||
private val context: Context,
|
||||
private val coil: ImageLoader,
|
||||
private val historyRepository: HistoryRepository,
|
||||
private val mangaRepository: MangaDataRepository,
|
||||
) {
|
||||
) : InvalidationTracker.Observer(TABLE_HISTORY) {
|
||||
|
||||
private val iconSize by lazy {
|
||||
getIconSize(context)
|
||||
}
|
||||
private val iconSize by lazy { getIconSize(context) }
|
||||
private var shortcutsUpdateJob: Job? = null
|
||||
|
||||
suspend fun updateShortcuts() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
val shortcuts = historyRepository.getList(0, manager.maxShortcutCountPerActivity)
|
||||
.filter { x -> x.title.isNotEmpty() }
|
||||
.map { buildShortcutInfo(it).build().toShortcutInfo() }
|
||||
manager.dynamicShortcuts = shortcuts
|
||||
override fun onInvalidated(tables: MutableSet<String>) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
val prevJob = shortcutsUpdateJob
|
||||
shortcutsUpdateJob = processLifecycleScope.launch(Dispatchers.Default) {
|
||||
prevJob?.join()
|
||||
updateShortcutsImpl()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun requestPinShortcut(manga: Manga): Boolean {
|
||||
return ShortcutManagerCompat.requestPinShortcut(
|
||||
context,
|
||||
buildShortcutInfo(manga).build(),
|
||||
null
|
||||
null,
|
||||
)
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
suspend fun await(): Boolean {
|
||||
return shortcutsUpdateJob?.join() != null
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
private suspend fun updateShortcutsImpl() = runCatchingCancellable {
|
||||
val manager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
|
||||
val shortcuts = historyRepository.getList(0, manager.maxShortcutCountPerActivity)
|
||||
.filter { x -> x.title.isNotEmpty() }
|
||||
.map { buildShortcutInfo(it).build().toShortcutInfo() }
|
||||
manager.dynamicShortcuts = shortcuts
|
||||
}.onFailure {
|
||||
it.printStackTraceDebug()
|
||||
}
|
||||
|
||||
private suspend fun buildShortcutInfo(manga: Manga): ShortcutInfoCompat.Builder {
|
||||
val icon = runCatching {
|
||||
withContext(Dispatchers.IO) {
|
||||
val bmp = coil.execute(
|
||||
ImageRequest.Builder(context)
|
||||
.data(manga.coverUrl)
|
||||
.size(iconSize.width, iconSize.height)
|
||||
.build()
|
||||
).requireBitmap()
|
||||
ThumbnailUtils.extractThumbnail(bmp, iconSize.width, iconSize.height, 0)
|
||||
}
|
||||
val icon = runCatchingCancellable {
|
||||
val bmp = coil.execute(
|
||||
ImageRequest.Builder(context)
|
||||
.data(manga.coverUrl)
|
||||
.size(iconSize.width, iconSize.height)
|
||||
.build(),
|
||||
).requireBitmap()
|
||||
ThumbnailUtils.extractThumbnail(bmp, iconSize.width, iconSize.height, 0)
|
||||
}.fold(
|
||||
onSuccess = { IconCompat.createWithAdaptiveBitmap(it) },
|
||||
onFailure = { IconCompat.createWithResource(context, R.drawable.ic_shortcut_default) }
|
||||
onFailure = { IconCompat.createWithResource(context, R.drawable.ic_shortcut_default) },
|
||||
)
|
||||
mangaRepository.storeManga(manga)
|
||||
return ShortcutInfoCompat.Builder(context, manga.id.toString())
|
||||
@@ -70,7 +92,7 @@ class ShortcutsRepository(
|
||||
.setIcon(icon)
|
||||
.setIntent(
|
||||
ReaderActivity.newIntent(context, manga.id)
|
||||
.setAction(ReaderActivity.ACTION_MANGA_READ)
|
||||
.setAction(ReaderActivity.ACTION_MANGA_READ),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import coil.map.Mapper
|
||||
import coil.request.Options
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.core.model.MangaSource
|
||||
|
||||
class FaviconMapper : Mapper<Uri, HttpUrl> {
|
||||
|
||||
@@ -13,7 +13,7 @@ class FaviconMapper : Mapper<Uri, HttpUrl> {
|
||||
if (data.scheme != "favicon") {
|
||||
return null
|
||||
}
|
||||
val mangaSource = MangaSource.valueOf(data.schemeSpecificPart)
|
||||
val mangaSource = MangaSource(data.schemeSpecificPart) ?: return null
|
||||
val repo = MangaRepository(mangaSource) as RemoteMangaRepository
|
||||
return repo.getFaviconUrl().toHttpUrl()
|
||||
}
|
||||
|
||||
@@ -14,9 +14,6 @@ import java.io.File
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.channels.trySendBlocking
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.ZoomMode
|
||||
import org.koitharu.kotatsu.core.network.DoHProvider
|
||||
@@ -64,6 +61,9 @@ class AppSettings(context: Context) {
|
||||
val readerPageSwitch: Set<String>
|
||||
get() = prefs.getStringSet(KEY_READER_SWITCHERS, null) ?: setOf(PAGE_SWITCH_TAPS)
|
||||
|
||||
val isReaderTapsAdaptive: Boolean
|
||||
get() = !prefs.getBoolean(KEY_READER_TAPS_LTR, false)
|
||||
|
||||
var isTrafficWarningEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_TRAFFIC_WARNING, true)
|
||||
set(value) = prefs.edit { putBoolean(KEY_TRAFFIC_WARNING, value) }
|
||||
@@ -314,6 +314,7 @@ class AppSettings(context: Context) {
|
||||
const val KEY_DOWNLOADS_SLOWDOWN = "downloads_slowdown"
|
||||
const val KEY_ALL_FAVOURITES_VISIBLE = "all_favourites_visible"
|
||||
const val KEY_DOH = "doh"
|
||||
const val KEY_READER_TAPS_LTR = "reader_taps_ltr"
|
||||
|
||||
// About
|
||||
const val KEY_APP_UPDATE = "app_update"
|
||||
|
||||
@@ -12,6 +12,7 @@ import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updatePadding
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlin.math.roundToInt
|
||||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.ui.BaseFragment
|
||||
@@ -29,7 +30,6 @@ import org.koitharu.kotatsu.reader.ui.ReaderActivity
|
||||
import org.koitharu.kotatsu.reader.ui.ReaderState
|
||||
import org.koitharu.kotatsu.utils.RecyclerViewScrollCallback
|
||||
import org.koitharu.kotatsu.utils.ext.addMenuProvider
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class ChaptersFragment :
|
||||
BaseFragment<FragmentChaptersBinding>(),
|
||||
@@ -46,7 +46,7 @@ class ChaptersFragment :
|
||||
|
||||
override fun onInflateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?
|
||||
container: ViewGroup?,
|
||||
) = FragmentChaptersBinding.inflate(inflater, container, false)
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@@ -98,7 +98,7 @@ class ChaptersFragment :
|
||||
manga = viewModel.manga.value ?: return,
|
||||
state = ReaderState(item.chapter.id, 0, 0),
|
||||
),
|
||||
options.toBundle()
|
||||
options.toBundle(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -128,13 +128,33 @@ class ChaptersFragment :
|
||||
Snackbar.make(
|
||||
binding.recyclerViewChapters,
|
||||
R.string.chapters_will_removed_background,
|
||||
Snackbar.LENGTH_LONG
|
||||
Snackbar.LENGTH_LONG,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
mode.finish()
|
||||
true
|
||||
}
|
||||
R.id.action_select_range -> {
|
||||
val controller = selectionController ?: return false
|
||||
val items = chaptersAdapter?.items ?: return false
|
||||
val ids = HashSet(controller.peekCheckedIds())
|
||||
val buffer = HashSet<Long>()
|
||||
var isAdding = false
|
||||
for (x in items) {
|
||||
if (x.chapter.id in ids) {
|
||||
isAdding = true
|
||||
if (buffer.isNotEmpty()) {
|
||||
ids.addAll(buffer)
|
||||
buffer.clear()
|
||||
}
|
||||
} else if (isAdding) {
|
||||
buffer.add(x.chapter.id)
|
||||
}
|
||||
}
|
||||
controller.addAll(ids)
|
||||
true
|
||||
}
|
||||
R.id.action_select_all -> {
|
||||
val ids = chaptersAdapter?.items?.map { it.chapter.id } ?: return false
|
||||
selectionController?.addAll(ids)
|
||||
@@ -158,14 +178,24 @@ class ChaptersFragment :
|
||||
|
||||
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||
val selectedIds = selectionController?.peekCheckedIds() ?: return false
|
||||
val items = chaptersAdapter?.items?.filter { x -> x.chapter.id in selectedIds }.orEmpty()
|
||||
menu.findItem(R.id.action_save).isVisible = items.none { x ->
|
||||
val allItems = chaptersAdapter?.items.orEmpty()
|
||||
val items = allItems.withIndex().filter { (_, x) -> x.chapter.id in selectedIds }
|
||||
menu.findItem(R.id.action_save).isVisible = items.none { (_, x) ->
|
||||
x.chapter.source == MangaSource.LOCAL
|
||||
}
|
||||
menu.findItem(R.id.action_delete).isVisible = items.all { x ->
|
||||
menu.findItem(R.id.action_delete).isVisible = items.all { (_, x) ->
|
||||
x.chapter.source == MangaSource.LOCAL
|
||||
}
|
||||
menu.findItem(R.id.action_select_all).isVisible = items.size < allItems.size
|
||||
mode.title = items.size.toString()
|
||||
var hasGap = false
|
||||
for (i in 0 until items.size - 1) {
|
||||
if (items[i].index + 1 != items[i + 1].index) {
|
||||
hasGap = true
|
||||
break
|
||||
}
|
||||
}
|
||||
menu.findItem(R.id.action_select_range).isVisible = hasGap
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -241,8 +271,8 @@ class ChaptersFragment :
|
||||
}
|
||||
|
||||
override fun onPrepareMenu(menu: Menu) {
|
||||
menu.findItem(R.id.action_reversed).isChecked = viewModel.isChaptersReversed.value == true
|
||||
menu.findItem(R.id.action_search).isVisible = viewModel.isChaptersEmpty.value == false
|
||||
menu.findItem(R.id.action_reversed)?.isChecked = viewModel.isChaptersReversed.value == true
|
||||
menu.findItem(R.id.action_search)?.isVisible = viewModel.isChaptersEmpty.value == false
|
||||
}
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean = when (menuItem.itemId) {
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.browser.BrowserActivity
|
||||
import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver
|
||||
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsUpdater
|
||||
import org.koitharu.kotatsu.databinding.ActivityDetailsBinding
|
||||
import org.koitharu.kotatsu.details.ui.adapter.BranchesAdapter
|
||||
import org.koitharu.kotatsu.download.ui.service.DownloadService
|
||||
@@ -103,8 +103,9 @@ class DetailsActivity :
|
||||
|
||||
private fun onMangaRemoved(manga: Manga) {
|
||||
Toast.makeText(
|
||||
this, getString(R.string._s_deleted_from_local_storage, manga.title),
|
||||
Toast.LENGTH_SHORT
|
||||
this,
|
||||
getString(R.string._s_deleted_from_local_storage, manga.title),
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
finishAfterTransition()
|
||||
}
|
||||
@@ -130,7 +131,7 @@ class DetailsActivity :
|
||||
onActionClick = {
|
||||
e.report("DetailsActivity::onError")
|
||||
dismiss()
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
@@ -141,14 +142,14 @@ class DetailsActivity :
|
||||
|
||||
override fun onWindowInsetsChanged(insets: Insets) {
|
||||
binding.snackbar.updatePadding(
|
||||
bottom = insets.bottom
|
||||
bottom = insets.bottom,
|
||||
)
|
||||
binding.toolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
topMargin = insets.top
|
||||
}
|
||||
binding.root.updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right
|
||||
right = insets.right,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -158,6 +159,7 @@ class DetailsActivity :
|
||||
tab.removeBadge()
|
||||
} else {
|
||||
val badge = tab.orCreateBadge
|
||||
badge.maxCharacterCount = 3
|
||||
badge.number = newChapters
|
||||
badge.isVisible = true
|
||||
}
|
||||
@@ -224,7 +226,7 @@ class DetailsActivity :
|
||||
R.id.action_shortcut -> {
|
||||
viewModel.manga.value?.let {
|
||||
lifecycleScope.launch {
|
||||
if (!get<ShortcutsRepository>().requestPinShortcut(it)) {
|
||||
if (!get<ShortcutsUpdater>().requestPinShortcut(it)) {
|
||||
binding.snackbar.show(getString(R.string.operation_not_supported))
|
||||
}
|
||||
}
|
||||
@@ -274,8 +276,8 @@ class DetailsActivity :
|
||||
ReaderActivity.newIntent(
|
||||
context = this@DetailsActivity,
|
||||
manga = remoteManga,
|
||||
state = ReaderState(chapterId, 0, 0)
|
||||
)
|
||||
state = ReaderState(chapterId, 0, 0),
|
||||
),
|
||||
)
|
||||
}
|
||||
setNeutralButton(R.string.download) { _, _ ->
|
||||
@@ -349,8 +351,8 @@ class DetailsActivity :
|
||||
dialogBuilder.setMessage(
|
||||
getString(
|
||||
R.string.large_manga_save_confirm,
|
||||
resources.getQuantityString(R.plurals.chapters, chaptersCount, chaptersCount)
|
||||
)
|
||||
resources.getQuantityString(R.plurals.chapters, chaptersCount, chaptersCount),
|
||||
),
|
||||
).setPositiveButton(R.string.save) { _, _ ->
|
||||
DownloadService.start(this, manga)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.koitharu.kotatsu.details.ui
|
||||
|
||||
import android.app.ActivityOptions
|
||||
import android.os.Bundle
|
||||
import android.text.Spanned
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.*
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
@@ -10,18 +9,15 @@ import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.net.toFile
|
||||
import androidx.core.net.toUri
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.isGone
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updatePadding
|
||||
import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
import coil.size.Scale
|
||||
import coil.util.CoilUtils
|
||||
import com.google.android.material.chip.Chip
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koin.android.ext.android.inject
|
||||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
|
||||
import org.koitharu.kotatsu.R
|
||||
@@ -33,6 +29,7 @@ import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
import org.koitharu.kotatsu.bookmarks.ui.BookmarksAdapter
|
||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||
import org.koitharu.kotatsu.databinding.FragmentDetailsBinding
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
|
||||
import org.koitharu.kotatsu.details.ui.scrobbling.ScrobblingInfoBottomSheet
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.select.FavouriteCategoriesBottomSheet
|
||||
import org.koitharu.kotatsu.history.domain.PROGRESS_NONE
|
||||
@@ -49,6 +46,7 @@ import org.koitharu.kotatsu.search.ui.SearchActivity
|
||||
import org.koitharu.kotatsu.utils.FileSize
|
||||
import org.koitharu.kotatsu.utils.ShareHelper
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
import org.koitharu.kotatsu.utils.image.CoverSizeResolver
|
||||
|
||||
class DetailsFragment :
|
||||
BaseFragment<FragmentDetailsBinding>(),
|
||||
@@ -82,6 +80,7 @@ class DetailsFragment :
|
||||
viewModel.bookmarks.observe(viewLifecycleOwner, ::onBookmarksChanged)
|
||||
viewModel.scrobblingInfo.observe(viewLifecycleOwner, ::onScrobblingInfoChanged)
|
||||
viewModel.description.observe(viewLifecycleOwner, ::onDescriptionChanged)
|
||||
viewModel.chapters.observe(viewLifecycleOwner, ::onChaptersChanged)
|
||||
addMenuProvider(DetailsMenuProvider())
|
||||
}
|
||||
|
||||
@@ -126,18 +125,6 @@ class DetailsFragment :
|
||||
else -> textViewState.isVisible = false
|
||||
}
|
||||
|
||||
// Info containers
|
||||
val chapters = manga.chapters
|
||||
if (chapters.isNullOrEmpty()) {
|
||||
infoLayout.textViewChapters.isVisible = false
|
||||
} else {
|
||||
infoLayout.textViewChapters.isVisible = true
|
||||
infoLayout.textViewChapters.text = resources.getQuantityString(
|
||||
R.plurals.chapters,
|
||||
chapters.size,
|
||||
chapters.size,
|
||||
)
|
||||
}
|
||||
if (manga.hasRating) {
|
||||
infoLayout.textViewRating.text = String.format("%.1f", manga.rating * 5)
|
||||
infoLayout.ratingContainer.isVisible = true
|
||||
@@ -164,14 +151,27 @@ class DetailsFragment :
|
||||
|
||||
infoLayout.textViewNsfw.isVisible = manga.isNsfw
|
||||
|
||||
// Buttons
|
||||
buttonRead.isEnabled = !manga.chapters.isNullOrEmpty()
|
||||
|
||||
// Chips
|
||||
bindTags(manga)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onChaptersChanged(chapters: List<ChapterListItem>?) {
|
||||
val infoLayout = binding.infoLayout
|
||||
if (chapters.isNullOrEmpty()) {
|
||||
infoLayout.textViewChapters.isVisible = false
|
||||
} else {
|
||||
infoLayout.textViewChapters.isVisible = true
|
||||
infoLayout.textViewChapters.text = resources.getQuantityString(
|
||||
R.plurals.chapters,
|
||||
chapters.size,
|
||||
chapters.size,
|
||||
)
|
||||
}
|
||||
// Buttons
|
||||
binding.buttonRead.isEnabled = !chapters.isNullOrEmpty()
|
||||
}
|
||||
|
||||
private fun onDescriptionChanged(description: CharSequence?) {
|
||||
if (description.isNullOrBlank()) {
|
||||
binding.textViewDescription.setText(R.string.no_description)
|
||||
@@ -231,14 +231,13 @@ class DetailsFragment :
|
||||
CoilUtils.dispose(imageViewCover)
|
||||
return
|
||||
}
|
||||
imageViewCover.newImageRequest(scrobbling.coverUrl)
|
||||
.crossfade(true)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.scale(Scale.FILL)
|
||||
.lifecycle(viewLifecycleOwner)
|
||||
.enqueueWith(coil)
|
||||
imageViewCover.newImageRequest(scrobbling.coverUrl)?.run {
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
lifecycle(viewLifecycleOwner)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
textViewTitle.text = scrobbling.title
|
||||
textViewTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, scrobbling.scrobbler.iconResId, 0)
|
||||
ratingBar.rating = scrobbling.rating * ratingBar.numStars
|
||||
@@ -267,7 +266,7 @@ class DetailsFragment :
|
||||
context = context ?: return,
|
||||
manga = manga,
|
||||
branch = viewModel.selectedBranchValue,
|
||||
)
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -277,14 +276,14 @@ class DetailsFragment :
|
||||
context = v.context,
|
||||
source = manga.source,
|
||||
query = manga.author ?: return,
|
||||
)
|
||||
),
|
||||
)
|
||||
}
|
||||
R.id.imageView_cover -> {
|
||||
val options = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.width, v.height)
|
||||
startActivity(
|
||||
ImageActivity.newIntent(v.context, manga.largeCoverUrl.ifNullOrEmpty { manga.coverUrl }),
|
||||
options.toBundle()
|
||||
options.toBundle(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -310,8 +309,8 @@ class DetailsFragment :
|
||||
c.chapter.branch == branch
|
||||
}?.let { c ->
|
||||
ReaderState(c.chapter.id, 0, 0)
|
||||
}
|
||||
)
|
||||
},
|
||||
),
|
||||
)
|
||||
true
|
||||
}
|
||||
@@ -344,7 +343,7 @@ class DetailsFragment :
|
||||
icon = 0,
|
||||
data = tag,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -356,13 +355,22 @@ class DetailsFragment :
|
||||
}
|
||||
val request = ImageRequest.Builder(context ?: return)
|
||||
.target(binding.imageViewCover)
|
||||
.size(CoverSizeResolver(binding.imageViewCover))
|
||||
.data(imageUrl)
|
||||
.crossfade(true)
|
||||
.referer(manga.publicUrl)
|
||||
.lifecycle(viewLifecycleOwner)
|
||||
lastResult?.drawable?.let {
|
||||
request.fallback(it)
|
||||
} ?: request.fallback(R.drawable.ic_placeholder)
|
||||
.placeholderMemoryCacheKey(manga.coverUrl)
|
||||
val previousDrawable = lastResult?.drawable
|
||||
if (previousDrawable != null) {
|
||||
request.fallback(previousDrawable)
|
||||
.placeholder(previousDrawable)
|
||||
.error(previousDrawable)
|
||||
} else {
|
||||
request.fallback(R.drawable.ic_placeholder)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
}
|
||||
request.enqueueWith(coil)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package org.koitharu.kotatsu.details.ui
|
||||
|
||||
import android.text.Html
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import androidx.core.text.getSpans
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.asFlow
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import java.io.IOException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -33,7 +38,7 @@ import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
||||
import org.koitharu.kotatsu.utils.SingleLiveEvent
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import java.io.IOException
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
class DetailsViewModel(
|
||||
intent: MangaIntent,
|
||||
@@ -91,8 +96,8 @@ class DetailsViewModel(
|
||||
if (description.isNullOrEmpty()) {
|
||||
emit(null)
|
||||
} else {
|
||||
emit(description.parseAsHtml())
|
||||
emit(description.parseAsHtml(imageGetter = imageGetter))
|
||||
emit(description.parseAsHtml().filterSpans())
|
||||
emit(description.parseAsHtml(imageGetter = imageGetter).filterSpans())
|
||||
}
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, null)
|
||||
|
||||
@@ -110,7 +115,7 @@ class DetailsViewModel(
|
||||
|
||||
val selectedBranchIndex = combine(
|
||||
branches.asFlow(),
|
||||
delegate.selectedBranch
|
||||
delegate.selectedBranch,
|
||||
) { branches, selected ->
|
||||
branches.indexOf(selected)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||
@@ -161,7 +166,7 @@ class DetailsViewModel(
|
||||
checkNotNull(manga) { "Cannot find saved manga for ${m.title}" }
|
||||
val original = localMangaRepository.getRemoteManga(manga)
|
||||
localMangaRepository.delete(manga) || throw IOException("Unable to delete file")
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
historyRepository.deleteOrSwap(manga, original)
|
||||
}
|
||||
onMangaRemoved.postCall(manga)
|
||||
@@ -200,7 +205,7 @@ class DetailsViewModel(
|
||||
reload()
|
||||
} else {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
localMangaRepository.getDetails(downloadedManga)
|
||||
}.onSuccess {
|
||||
delegate.relatedManga.value = it
|
||||
@@ -225,7 +230,7 @@ class DetailsViewModel(
|
||||
fun unregisterScrobbling() {
|
||||
launchJob(Dispatchers.Default) {
|
||||
scrobbler.unregisterScrobbling(
|
||||
mangaId = delegate.mangaId
|
||||
mangaId = delegate.mangaId,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -242,4 +247,13 @@ class DetailsViewModel(
|
||||
it.chapter.name.contains(query, ignoreCase = true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Spanned.filterSpans(): CharSequence {
|
||||
val spannable = SpannableString.valueOf(this)
|
||||
val spans = spannable.getSpans<ForegroundColorSpan>()
|
||||
for (span in spans) {
|
||||
spannable.removeSpan(span)
|
||||
}
|
||||
return spannable.trim()
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,23 @@
|
||||
package org.koitharu.kotatsu.details.ui
|
||||
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import org.acra.ACRA
|
||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||
import org.koitharu.kotatsu.base.domain.MangaIntent
|
||||
import org.koitharu.kotatsu.core.exceptions.MangaNotFoundException
|
||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
|
||||
import org.koitharu.kotatsu.details.ui.model.toListItem
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.exception.ParseException
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||
import org.koitharu.kotatsu.parsers.util.toTitleCase
|
||||
import org.koitharu.kotatsu.utils.ext.iterator
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.setCurrentManga
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
class MangaDetailsDelegate(
|
||||
private val intent: MangaIntent,
|
||||
@@ -43,23 +38,16 @@ class MangaDetailsDelegate(
|
||||
val mangaId = intent.manga?.id ?: intent.mangaId
|
||||
|
||||
suspend fun doLoad() {
|
||||
var manga = mangaDataRepository.resolveIntent(intent)
|
||||
?: throw MangaNotFoundException("Cannot find manga")
|
||||
ACRA.setCurrentManga(manga)
|
||||
var manga = mangaDataRepository.resolveIntent(intent) ?: throw NotFoundException("Cannot find manga", "")
|
||||
mangaData.value = manga
|
||||
manga = MangaRepository(manga.source).getDetails(manga)
|
||||
// find default branch
|
||||
val hist = historyRepository.getOne(manga)
|
||||
selectedBranch.value = if (hist != null) {
|
||||
val currentChapter = manga.chapters?.find { it.id == hist.chapterId }
|
||||
if (currentChapter != null) currentChapter.branch else predictBranch(manga.chapters)
|
||||
} else {
|
||||
predictBranch(manga.chapters)
|
||||
}
|
||||
selectedBranch.value = manga.getPreferredBranch(hist)
|
||||
mangaData.value = manga
|
||||
relatedManga.value = runCatching {
|
||||
relatedManga.value = runCatchingCancellable {
|
||||
if (manga.source == MangaSource.LOCAL) {
|
||||
val m = localMangaRepository.getRemoteManga(manga) ?: return@runCatching null
|
||||
val m = localMangaRepository.getRemoteManga(manga) ?: return@runCatchingCancellable null
|
||||
MangaRepository(m.source).getDetails(m)
|
||||
} else {
|
||||
localMangaRepository.findSavedManga(manga)
|
||||
@@ -96,7 +84,7 @@ class MangaDetailsDelegate(
|
||||
val dateFormat = settings.getDateFormat()
|
||||
val currentIndex = chapters.indexOfFirst { it.id == currentId }
|
||||
val firstNewIndex = chapters.size - newCount
|
||||
val downloadedIds = downloadedChapters?.mapToSet { it.id }
|
||||
val downloadedIds = downloadedChapters?.mapTo(HashSet(downloadedChapters.size)) { it.id }
|
||||
for (i in chapters.indices) {
|
||||
val chapter = chapters[i]
|
||||
if (chapter.branch != branch) {
|
||||
@@ -111,6 +99,9 @@ class MangaDetailsDelegate(
|
||||
dateFormat = dateFormat,
|
||||
)
|
||||
}
|
||||
if (result.size < chapters.size / 2) {
|
||||
result.trimToSize()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -166,24 +157,9 @@ class MangaDetailsDelegate(
|
||||
}
|
||||
result.sortBy { it.chapter.number }
|
||||
}
|
||||
if (result.size < sourceChapters.size / 2) {
|
||||
result.trimToSize()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun predictBranch(chapters: List<MangaChapter>?): String? {
|
||||
if (chapters.isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
val groups = chapters.groupBy { it.branch }
|
||||
for (locale in LocaleListCompat.getAdjustedDefault()) {
|
||||
var language = locale.getDisplayLanguage(locale).toTitleCase(locale)
|
||||
if (groups.containsKey(language)) {
|
||||
return language
|
||||
}
|
||||
language = locale.getDisplayName(locale).toTitleCase(locale)
|
||||
if (groups.containsKey(language)) {
|
||||
return language
|
||||
}
|
||||
}
|
||||
return groups.maxByOrNull { it.value.size }?.key
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,24 @@
|
||||
package org.koitharu.kotatsu.details.ui.model
|
||||
|
||||
import java.text.DateFormat
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
|
||||
class ChapterListItem(
|
||||
val chapter: MangaChapter,
|
||||
val flags: Int,
|
||||
val uploadDate: String?,
|
||||
private val uploadDateMs: Long,
|
||||
private val dateFormat: DateFormat,
|
||||
) {
|
||||
|
||||
var uploadDate: String? = null
|
||||
private set
|
||||
get() {
|
||||
if (field != null) return field
|
||||
if (uploadDateMs == 0L) return null
|
||||
field = dateFormat.format(uploadDateMs)
|
||||
return field
|
||||
}
|
||||
|
||||
val status: Int
|
||||
get() = flags and MASK_STATUS
|
||||
|
||||
@@ -32,7 +43,8 @@ class ChapterListItem(
|
||||
|
||||
if (chapter != other.chapter) return false
|
||||
if (flags != other.flags) return false
|
||||
if (uploadDate != other.uploadDate) return false
|
||||
if (uploadDateMs != other.uploadDateMs) return false
|
||||
if (dateFormat != other.dateFormat) return false
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -40,7 +52,8 @@ class ChapterListItem(
|
||||
override fun hashCode(): Int {
|
||||
var result = chapter.hashCode()
|
||||
result = 31 * result + flags
|
||||
result = 31 * result + (uploadDate?.hashCode() ?: 0)
|
||||
result = 31 * result + uploadDateMs.hashCode()
|
||||
result = 31 * result + dateFormat.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -53,4 +66,4 @@ class ChapterListItem(
|
||||
const val FLAG_DOWNLOADED = 32
|
||||
const val MASK_STATUS = FLAG_UNREAD or FLAG_CURRENT
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.koitharu.kotatsu.details.ui.model
|
||||
|
||||
import java.text.DateFormat
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_CURRENT
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_DOWNLOADED
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_MISSING
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_NEW
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_UNREAD
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import java.text.DateFormat
|
||||
|
||||
fun MangaChapter.toListItem(
|
||||
isCurrent: Boolean,
|
||||
@@ -25,6 +25,7 @@ fun MangaChapter.toListItem(
|
||||
return ChapterListItem(
|
||||
chapter = this,
|
||||
flags = flags,
|
||||
uploadDate = if (uploadDate != 0L) dateFormat.format(uploadDate) else null
|
||||
uploadDateMs = uploadDate,
|
||||
dateFormat = dateFormat,
|
||||
)
|
||||
}
|
||||
@@ -1,21 +1,32 @@
|
||||
package org.koitharu.kotatsu.download.domain
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.webkit.MimeTypeMap
|
||||
import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
import coil.size.Scale
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.internal.closeQuietly
|
||||
import okio.IOException
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.download.ui.service.PausingHandle
|
||||
import org.koitharu.kotatsu.local.data.PagesCache
|
||||
import org.koitharu.kotatsu.local.domain.CbzMangaOutput
|
||||
import org.koitharu.kotatsu.local.domain.LocalMangaRepository
|
||||
@@ -25,11 +36,10 @@ import org.koitharu.kotatsu.parsers.util.await
|
||||
import org.koitharu.kotatsu.utils.ext.deleteAwait
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
import org.koitharu.kotatsu.utils.ext.waitForNetwork
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
import java.io.File
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
||||
|
||||
private const val MAX_DOWNLOAD_ATTEMPTS = 3
|
||||
private const val MAX_FAILSAFE_ATTEMPTS = 2
|
||||
private const val DOWNLOAD_ERROR_DELAY = 500L
|
||||
private const val SLOWDOWN_DELAY = 200L
|
||||
|
||||
@@ -43,14 +53,11 @@ class DownloadManager(
|
||||
private val settings: AppSettings,
|
||||
) {
|
||||
|
||||
private val connectivityManager = context.getSystemService(
|
||||
Context.CONNECTIVITY_SERVICE
|
||||
) as ConnectivityManager
|
||||
private val coverWidth = context.resources.getDimensionPixelSize(
|
||||
androidx.core.R.dimen.compat_notification_large_icon_max_width
|
||||
androidx.core.R.dimen.compat_notification_large_icon_max_width,
|
||||
)
|
||||
private val coverHeight = context.resources.getDimensionPixelSize(
|
||||
androidx.core.R.dimen.compat_notification_large_icon_max_height
|
||||
androidx.core.R.dimen.compat_notification_large_icon_max_height,
|
||||
)
|
||||
private val semaphore = Semaphore(settings.downloadsParallelism)
|
||||
|
||||
@@ -58,114 +65,142 @@ class DownloadManager(
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
startId: Int,
|
||||
): ProgressJob<DownloadState> {
|
||||
): PausingProgressJob<DownloadState> {
|
||||
val stateFlow = MutableStateFlow<DownloadState>(
|
||||
DownloadState.Queued(startId = startId, manga = manga, cover = null)
|
||||
DownloadState.Queued(startId = startId, manga = manga, cover = null),
|
||||
)
|
||||
val job = downloadMangaImpl(manga, chaptersIds?.takeUnless { it.isEmpty() }, stateFlow, startId)
|
||||
return ProgressJob(job, stateFlow)
|
||||
val pausingHandle = PausingHandle()
|
||||
val job = coroutineScope.launch(Dispatchers.Default + errorStateHandler(stateFlow)) {
|
||||
try {
|
||||
downloadMangaImpl(manga, chaptersIds?.takeUnless { it.isEmpty() }, stateFlow, pausingHandle, startId)
|
||||
} catch (e: CancellationException) { // handle cancellation if not handled already
|
||||
val state = stateFlow.value
|
||||
if (state !is DownloadState.Cancelled) {
|
||||
stateFlow.value = DownloadState.Cancelled(startId, state.manga, state.cover)
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return PausingProgressJob(job, stateFlow, pausingHandle)
|
||||
}
|
||||
|
||||
private fun downloadMangaImpl(
|
||||
private suspend fun downloadMangaImpl(
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
outState: MutableStateFlow<DownloadState>,
|
||||
pausingHandle: PausingHandle,
|
||||
startId: Int,
|
||||
): Job = coroutineScope.launch(Dispatchers.Default + errorStateHandler(outState)) {
|
||||
@Suppress("NAME_SHADOWING") var manga = manga
|
||||
) {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
var manga = manga
|
||||
val chaptersIdsSet = chaptersIds?.toMutableSet()
|
||||
val cover = loadCover(manga)
|
||||
outState.value = DownloadState.Queued(startId, manga, cover)
|
||||
localMangaRepository.lockManga(manga.id)
|
||||
semaphore.acquire()
|
||||
coroutineContext[WakeLockNode]?.acquire()
|
||||
outState.value = DownloadState.Preparing(startId, manga, null)
|
||||
val destination = localMangaRepository.getOutputDir()
|
||||
checkNotNull(destination) { context.getString(R.string.cannot_find_available_storage) }
|
||||
val tempFileName = "${manga.id}_$startId.tmp"
|
||||
var output: CbzMangaOutput? = null
|
||||
try {
|
||||
if (manga.source == MangaSource.LOCAL) {
|
||||
manga = localMangaRepository.getRemoteManga(manga) ?: error("Cannot obtain remote manga instance")
|
||||
}
|
||||
val repo = MangaRepository(manga.source)
|
||||
outState.value = DownloadState.Preparing(startId, manga, cover)
|
||||
val data = if (manga.chapters.isNullOrEmpty()) repo.getDetails(manga) else manga
|
||||
output = CbzMangaOutput.get(destination, data)
|
||||
val coverUrl = data.largeCoverUrl ?: data.coverUrl
|
||||
downloadFile(coverUrl, data.publicUrl, destination, tempFileName).let { file ->
|
||||
output.addCover(file, MimeTypeMap.getFileExtensionFromUrl(coverUrl))
|
||||
}
|
||||
val chapters = checkNotNull(
|
||||
if (chaptersIdsSet == null) {
|
||||
data.chapters
|
||||
} else {
|
||||
data.chapters?.filter { x -> chaptersIdsSet.remove(x.id) }
|
||||
}
|
||||
) { "Chapters list must not be null" }
|
||||
check(chapters.isNotEmpty()) { "Chapters list must not be empty" }
|
||||
check(chaptersIdsSet.isNullOrEmpty()) {
|
||||
"${chaptersIdsSet?.size} of ${chaptersIds?.size} requested chapters not found in manga"
|
||||
}
|
||||
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
||||
val pages = repo.getPages(chapter)
|
||||
for ((pageIndex, page) in pages.withIndex()) {
|
||||
var retryCounter = 0
|
||||
failsafe@ while (true) {
|
||||
try {
|
||||
val url = repo.getPageUrl(page)
|
||||
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
||||
output.addPage(
|
||||
chapter = chapter,
|
||||
file = file,
|
||||
pageNumber = pageIndex,
|
||||
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
||||
withMangaLock(manga) {
|
||||
semaphore.withPermit {
|
||||
outState.value = DownloadState.Preparing(startId, manga, null)
|
||||
val destination = localMangaRepository.getOutputDir()
|
||||
checkNotNull(destination) { context.getString(R.string.cannot_find_available_storage) }
|
||||
val tempFileName = "${manga.id}_$startId.tmp"
|
||||
var output: CbzMangaOutput? = null
|
||||
try {
|
||||
if (manga.source == MangaSource.LOCAL) {
|
||||
manga = localMangaRepository.getRemoteManga(manga)
|
||||
?: error("Cannot obtain remote manga instance")
|
||||
}
|
||||
val repo = MangaRepository(manga.source)
|
||||
outState.value = DownloadState.Preparing(startId, manga, cover)
|
||||
val data = if (manga.chapters.isNullOrEmpty()) repo.getDetails(manga) else manga
|
||||
output = CbzMangaOutput.get(destination, data)
|
||||
val coverUrl = data.largeCoverUrl ?: data.coverUrl
|
||||
downloadFile(coverUrl, data.publicUrl, destination, tempFileName).let { file ->
|
||||
output.addCover(file, MimeTypeMap.getFileExtensionFromUrl(coverUrl))
|
||||
}
|
||||
val chapters = checkNotNull(
|
||||
if (chaptersIdsSet == null) {
|
||||
data.chapters
|
||||
} else {
|
||||
data.chapters?.filter { x -> chaptersIdsSet.remove(x.id) }
|
||||
},
|
||||
) { "Chapters list must not be null" }
|
||||
check(chapters.isNotEmpty()) { "Chapters list must not be empty" }
|
||||
check(chaptersIdsSet.isNullOrEmpty()) {
|
||||
"${chaptersIdsSet?.size} of ${chaptersIds?.size} requested chapters not found in manga"
|
||||
}
|
||||
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
||||
val pages = runFailsafe(outState, pausingHandle) {
|
||||
repo.getPages(chapter)
|
||||
}
|
||||
for ((pageIndex, page) in pages.withIndex()) {
|
||||
runFailsafe(outState, pausingHandle) {
|
||||
val url = repo.getPageUrl(page)
|
||||
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
||||
output.addPage(
|
||||
chapter = chapter,
|
||||
file = file,
|
||||
pageNumber = pageIndex,
|
||||
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
||||
)
|
||||
}
|
||||
outState.value = DownloadState.Progress(
|
||||
startId = startId,
|
||||
manga = data,
|
||||
cover = cover,
|
||||
totalChapters = chapters.size,
|
||||
currentChapter = chapterIndex,
|
||||
totalPages = pages.size,
|
||||
currentPage = pageIndex,
|
||||
)
|
||||
break@failsafe
|
||||
} catch (e: IOException) {
|
||||
if (retryCounter < MAX_DOWNLOAD_ATTEMPTS) {
|
||||
outState.value = DownloadState.WaitingForNetwork(startId, data, cover)
|
||||
delay(DOWNLOAD_ERROR_DELAY)
|
||||
connectivityManager.waitForNetwork()
|
||||
retryCounter++
|
||||
} else {
|
||||
throw e
|
||||
|
||||
if (settings.isDownloadsSlowdownEnabled) {
|
||||
delay(SLOWDOWN_DELAY)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outState.value = DownloadState.Progress(
|
||||
startId, data, cover,
|
||||
totalChapters = chapters.size,
|
||||
currentChapter = chapterIndex,
|
||||
totalPages = pages.size,
|
||||
currentPage = pageIndex,
|
||||
)
|
||||
|
||||
if (settings.isDownloadsSlowdownEnabled) {
|
||||
delay(SLOWDOWN_DELAY)
|
||||
outState.value = DownloadState.PostProcessing(startId, data, cover)
|
||||
output.mergeWithExisting()
|
||||
output.finish()
|
||||
val localManga = localMangaRepository.getFromFile(output.file)
|
||||
outState.value = DownloadState.Done(startId, data, cover, localManga)
|
||||
} catch (e: CancellationException) {
|
||||
outState.value = DownloadState.Cancelled(startId, manga, cover)
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTraceDebug()
|
||||
outState.value = DownloadState.Error(startId, manga, cover, e, false)
|
||||
} finally {
|
||||
withContext(NonCancellable) {
|
||||
output?.closeQuietly()
|
||||
output?.cleanup()
|
||||
File(destination, tempFileName).deleteAwait()
|
||||
}
|
||||
}
|
||||
}
|
||||
outState.value = DownloadState.PostProcessing(startId, data, cover)
|
||||
output.mergeWithExisting()
|
||||
output.finalize()
|
||||
val localManga = localMangaRepository.getFromFile(output.file)
|
||||
outState.value = DownloadState.Done(startId, data, cover, localManga)
|
||||
} catch (e: CancellationException) {
|
||||
outState.value = DownloadState.Cancelled(startId, manga, cover)
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTraceDebug()
|
||||
outState.value = DownloadState.Error(startId, manga, cover, e)
|
||||
} finally {
|
||||
withContext(NonCancellable) {
|
||||
output?.cleanup()
|
||||
File(destination, tempFileName).deleteAwait()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <R> runFailsafe(
|
||||
outState: MutableStateFlow<DownloadState>,
|
||||
pausingHandle: PausingHandle,
|
||||
block: suspend () -> R,
|
||||
): R {
|
||||
var countDown = MAX_FAILSAFE_ATTEMPTS
|
||||
failsafe@ while (true) {
|
||||
try {
|
||||
return block()
|
||||
} catch (e: IOException) {
|
||||
if (countDown <= 0) {
|
||||
val state = outState.value
|
||||
outState.value = DownloadState.Error(state.startId, state.manga, state.cover, e, true)
|
||||
countDown = MAX_FAILSAFE_ATTEMPTS
|
||||
pausingHandle.pause()
|
||||
pausingHandle.awaitResumed()
|
||||
outState.value = state
|
||||
} else {
|
||||
countDown--
|
||||
delay(DOWNLOAD_ERROR_DELAY)
|
||||
}
|
||||
}
|
||||
coroutineContext[WakeLockNode]?.release()
|
||||
semaphore.release()
|
||||
localMangaRepository.unlockManga(manga.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,26 +224,35 @@ class DownloadManager(
|
||||
|
||||
private fun errorStateHandler(outState: MutableStateFlow<DownloadState>) =
|
||||
CoroutineExceptionHandler { _, throwable ->
|
||||
throwable.printStackTraceDebug()
|
||||
val prevValue = outState.value
|
||||
outState.value = DownloadState.Error(
|
||||
startId = prevValue.startId,
|
||||
manga = prevValue.manga,
|
||||
cover = prevValue.cover,
|
||||
error = throwable,
|
||||
canRetry = false,
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun loadCover(manga: Manga) = runCatching {
|
||||
private suspend fun loadCover(manga: Manga) = runCatchingCancellable {
|
||||
imageLoader.execute(
|
||||
ImageRequest.Builder(context)
|
||||
.data(manga.coverUrl)
|
||||
.referer(manga.publicUrl)
|
||||
.size(coverWidth, coverHeight)
|
||||
.scale(Scale.FILL)
|
||||
.build()
|
||||
.build(),
|
||||
).drawable
|
||||
}.getOrNull()
|
||||
|
||||
private suspend inline fun <T> withMangaLock(manga: Manga, block: () -> T) = try {
|
||||
localMangaRepository.lockManga(manga.id)
|
||||
block()
|
||||
} finally {
|
||||
localMangaRepository.unlockManga(manga.id)
|
||||
}
|
||||
|
||||
class Factory(
|
||||
private val context: Context,
|
||||
private val imageLoader: ImageLoader,
|
||||
|
||||
@@ -108,33 +108,6 @@ sealed interface DownloadState {
|
||||
}
|
||||
}
|
||||
|
||||
class WaitingForNetwork(
|
||||
override val startId: Int,
|
||||
override val manga: Manga,
|
||||
override val cover: Drawable?,
|
||||
) : DownloadState {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as WaitingForNetwork
|
||||
|
||||
if (startId != other.startId) return false
|
||||
if (manga != other.manga) return false
|
||||
if (cover != other.cover) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = startId
|
||||
result = 31 * result + manga.hashCode()
|
||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class Done(
|
||||
override val startId: Int,
|
||||
override val manga: Manga,
|
||||
@@ -170,6 +143,7 @@ sealed interface DownloadState {
|
||||
override val manga: Manga,
|
||||
override val cover: Drawable?,
|
||||
val error: Throwable,
|
||||
val canRetry: Boolean,
|
||||
) : DownloadState {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
@@ -182,6 +156,7 @@ sealed interface DownloadState {
|
||||
if (manga != other.manga) return false
|
||||
if (cover != other.cover) return false
|
||||
if (error != other.error) return false
|
||||
if (canRetry != other.canRetry) return false
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -191,6 +166,7 @@ sealed interface DownloadState {
|
||||
result = 31 * result + manga.hashCode()
|
||||
result = 31 * result + (cover?.hashCode() ?: 0)
|
||||
result = 31 * result + error.hashCode()
|
||||
result = 31 * result + canRetry.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package org.koitharu.kotatsu.download.domain
|
||||
|
||||
import android.os.PowerManager
|
||||
import kotlin.coroutines.AbstractCoroutineContextElement
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class WakeLockNode(
|
||||
private val wakeLock: PowerManager.WakeLock,
|
||||
private val timeout: Long,
|
||||
) : AbstractCoroutineContextElement(Key) {
|
||||
|
||||
init {
|
||||
wakeLock.setReferenceCounted(true)
|
||||
}
|
||||
|
||||
fun acquire() {
|
||||
wakeLock.acquire(timeout)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
wakeLock.release()
|
||||
}
|
||||
|
||||
companion object Key : CoroutineContext.Key<WakeLockNode>
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.koitharu.kotatsu.download.ui
|
||||
|
||||
import android.view.View
|
||||
import androidx.core.view.isVisible
|
||||
import coil.ImageLoader
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
@@ -9,31 +10,44 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.databinding.ItemDownloadBinding
|
||||
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||
import org.koitharu.kotatsu.parsers.util.format
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
|
||||
fun downloadItemAD(
|
||||
scope: CoroutineScope,
|
||||
coil: ImageLoader,
|
||||
) = adapterDelegateViewBinding<ProgressJob<DownloadState>, ProgressJob<DownloadState>, ItemDownloadBinding>(
|
||||
{ inflater, parent -> ItemDownloadBinding.inflate(inflater, parent, false) }
|
||||
) = adapterDelegateViewBinding<DownloadItem, DownloadItem, ItemDownloadBinding>(
|
||||
{ inflater, parent -> ItemDownloadBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
|
||||
var job: Job? = null
|
||||
val percentPattern = context.resources.getString(R.string.percent_string_pattern)
|
||||
|
||||
val clickListener = View.OnClickListener { v ->
|
||||
when (v.id) {
|
||||
R.id.button_cancel -> item.cancel()
|
||||
R.id.button_resume -> item.resume()
|
||||
else -> context.startActivity(
|
||||
DetailsActivity.newIntent(context, item.progressValue.manga),
|
||||
)
|
||||
}
|
||||
}
|
||||
binding.buttonCancel.setOnClickListener(clickListener)
|
||||
binding.buttonResume.setOnClickListener(clickListener)
|
||||
itemView.setOnClickListener(clickListener)
|
||||
|
||||
bind {
|
||||
job?.cancel()
|
||||
job = item.progressAsFlow().onFirst { state ->
|
||||
binding.imageViewCover.newImageRequest(state.manga.coverUrl)
|
||||
.referer(state.manga.publicUrl)
|
||||
.placeholder(state.cover)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.allowRgb565(true)
|
||||
.enqueueWith(coil)
|
||||
binding.imageViewCover.newImageRequest(state.manga.coverUrl)?.run {
|
||||
referer(state.manga.publicUrl)
|
||||
placeholder(state.cover)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
allowRgb565(true)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
}.onEach { state ->
|
||||
binding.textViewTitle.text = state.manga.title
|
||||
when (state) {
|
||||
@@ -43,6 +57,8 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = true
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = false
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
is DownloadState.Done -> {
|
||||
binding.textViewStatus.setText(R.string.download_complete)
|
||||
@@ -50,6 +66,8 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = false
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = false
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
is DownloadState.Error -> {
|
||||
binding.textViewStatus.setText(R.string.error_occurred)
|
||||
@@ -58,6 +76,8 @@ fun downloadItemAD(
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.text = state.error.getDisplayMessage(context.resources)
|
||||
binding.textViewDetails.isVisible = true
|
||||
binding.buttonCancel.isVisible = state.canRetry
|
||||
binding.buttonResume.isVisible = state.canRetry
|
||||
}
|
||||
is DownloadState.PostProcessing -> {
|
||||
binding.textViewStatus.setText(R.string.processing_)
|
||||
@@ -65,6 +85,8 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = true
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = false
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
is DownloadState.Preparing -> {
|
||||
binding.textViewStatus.setText(R.string.preparing_)
|
||||
@@ -72,6 +94,8 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = true
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = true
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
is DownloadState.Progress -> {
|
||||
binding.textViewStatus.setText(R.string.manga_downloading_)
|
||||
@@ -82,6 +106,8 @@ fun downloadItemAD(
|
||||
binding.textViewPercent.text = percentPattern.format((state.percent * 100f).format(1))
|
||||
binding.textViewPercent.isVisible = true
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = true
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
is DownloadState.Queued -> {
|
||||
binding.textViewStatus.setText(R.string.queued)
|
||||
@@ -89,13 +115,8 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = false
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
}
|
||||
is DownloadState.WaitingForNetwork -> {
|
||||
binding.textViewStatus.setText(R.string.waiting_for_network)
|
||||
binding.progressBar.isIndeterminate = false
|
||||
binding.progressBar.isVisible = false
|
||||
binding.textViewPercent.isVisible = false
|
||||
binding.textViewDetails.isVisible = false
|
||||
binding.buttonCancel.isVisible = true
|
||||
binding.buttonResume.isVisible = false
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
package org.koitharu.kotatsu.download.ui
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.databinding.ActivityDownloadsBinding
|
||||
import org.koitharu.kotatsu.download.ui.service.DownloadService
|
||||
import org.koitharu.kotatsu.utils.bindServiceWithLifecycle
|
||||
|
||||
class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>() {
|
||||
|
||||
@@ -26,30 +28,63 @@ class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>() {
|
||||
val adapter = DownloadsAdapter(lifecycleScope, get())
|
||||
binding.recyclerView.setHasFixedSize(true)
|
||||
binding.recyclerView.adapter = adapter
|
||||
bindServiceWithLifecycle(
|
||||
owner = this,
|
||||
service = Intent(this, DownloadService::class.java),
|
||||
flags = 0,
|
||||
).service.flatMapLatest { binder ->
|
||||
(binder as? DownloadService.DownloadBinder)?.downloads ?: flowOf(null)
|
||||
}.onEach {
|
||||
adapter.items = it?.toList().orEmpty()
|
||||
binding.textViewHolder.isVisible = it.isNullOrEmpty()
|
||||
}.launchIn(lifecycleScope)
|
||||
val connection = DownloadServiceConnection(adapter)
|
||||
bindService(Intent(this, DownloadService::class.java), connection, 0)
|
||||
lifecycle.addObserver(connection)
|
||||
}
|
||||
|
||||
override fun onWindowInsetsChanged(insets: Insets) {
|
||||
binding.recyclerView.updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right,
|
||||
bottom = insets.bottom
|
||||
bottom = insets.bottom,
|
||||
)
|
||||
binding.toolbar.updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right
|
||||
right = insets.right,
|
||||
)
|
||||
}
|
||||
|
||||
private inner class DownloadServiceConnection(
|
||||
private val adapter: DownloadsAdapter,
|
||||
) : ServiceConnection, DefaultLifecycleObserver {
|
||||
|
||||
private var collectJob: Job? = null
|
||||
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
collectJob?.cancel()
|
||||
val binder = (service as? DownloadService.DownloadBinder)
|
||||
collectJob = if (binder == null) {
|
||||
null
|
||||
} else {
|
||||
lifecycleScope.launch {
|
||||
binder.downloads.collect {
|
||||
setItems(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
collectJob?.cancel()
|
||||
collectJob = null
|
||||
setItems(null)
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
super.onDestroy(owner)
|
||||
collectJob?.cancel()
|
||||
collectJob = null
|
||||
owner.lifecycle.removeObserver(this)
|
||||
unbindService(this)
|
||||
}
|
||||
|
||||
private fun setItems(items: Collection<DownloadItem>?) {
|
||||
adapter.items = items?.toList().orEmpty()
|
||||
binding.textViewHolder.isVisible = items.isNullOrEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun newIntent(context: Context) = Intent(context, DownloadsActivity::class.java)
|
||||
|
||||
@@ -5,12 +5,14 @@ import coil.ImageLoader
|
||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
||||
|
||||
typealias DownloadItem = PausingProgressJob<DownloadState>
|
||||
|
||||
class DownloadsAdapter(
|
||||
scope: CoroutineScope,
|
||||
coil: ImageLoader,
|
||||
) : AsyncListDifferDelegationAdapter<ProgressJob<DownloadState>>(DiffCallback()) {
|
||||
) : AsyncListDifferDelegationAdapter<DownloadItem>(DiffCallback()) {
|
||||
|
||||
init {
|
||||
delegatesManager.addDelegate(downloadItemAD(scope, coil))
|
||||
@@ -21,18 +23,18 @@ class DownloadsAdapter(
|
||||
return items[position].progressValue.startId.toLong()
|
||||
}
|
||||
|
||||
private class DiffCallback : DiffUtil.ItemCallback<ProgressJob<DownloadState>>() {
|
||||
private class DiffCallback : DiffUtil.ItemCallback<DownloadItem>() {
|
||||
|
||||
override fun areItemsTheSame(
|
||||
oldItem: ProgressJob<DownloadState>,
|
||||
newItem: ProgressJob<DownloadState>,
|
||||
oldItem: DownloadItem,
|
||||
newItem: DownloadItem,
|
||||
): Boolean {
|
||||
return oldItem.progressValue.startId == newItem.progressValue.startId
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(
|
||||
oldItem: ProgressJob<DownloadState>,
|
||||
newItem: ProgressJob<DownloadState>,
|
||||
oldItem: DownloadItem,
|
||||
newItem: DownloadItem,
|
||||
): Boolean {
|
||||
return oldItem.progressValue == newItem.progressValue
|
||||
}
|
||||
|
||||
@@ -7,33 +7,34 @@ import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.text.format.DateUtils
|
||||
import android.util.SparseArray
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.core.text.HtmlCompat
|
||||
import androidx.core.text.htmlEncode
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.core.util.forEach
|
||||
import androidx.core.util.isNotEmpty
|
||||
import androidx.core.util.size
|
||||
import com.google.android.material.R as materialR
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||
import org.koitharu.kotatsu.download.ui.DownloadsActivity
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.ellipsize
|
||||
import org.koitharu.kotatsu.parsers.util.format
|
||||
import org.koitharu.kotatsu.utils.PendingIntentCompat
|
||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
|
||||
class DownloadNotification(private val context: Context, startId: Int) {
|
||||
class DownloadNotification(private val context: Context) {
|
||||
|
||||
private val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val states = SparseArray<DownloadState>()
|
||||
private val groupBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
|
||||
|
||||
private val builder = NotificationCompat.Builder(context, CHANNEL_ID)
|
||||
private val cancelAction = NotificationCompat.Action(
|
||||
materialR.drawable.material_ic_clear_black_24dp,
|
||||
context.getString(android.R.string.cancel),
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
startId,
|
||||
DownloadService.getCancelIntent(startId),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
private val listIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
REQUEST_LIST,
|
||||
@@ -42,114 +43,271 @@ class DownloadNotification(private val context: Context, startId: Int) {
|
||||
)
|
||||
|
||||
init {
|
||||
builder.setOnlyAlertOnce(true)
|
||||
builder.setDefaults(0)
|
||||
builder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
||||
builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
||||
builder.setSilent(true)
|
||||
groupBuilder.setOnlyAlertOnce(true)
|
||||
groupBuilder.setDefaults(0)
|
||||
groupBuilder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
||||
groupBuilder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
||||
groupBuilder.setSilent(true)
|
||||
groupBuilder.setGroup(GROUP_ID)
|
||||
groupBuilder.setContentIntent(listIntent)
|
||||
groupBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
||||
groupBuilder.setGroupSummary(true)
|
||||
groupBuilder.setContentTitle(context.getString(R.string.downloading_manga))
|
||||
}
|
||||
|
||||
fun create(state: DownloadState, timeLeft: Long): Notification {
|
||||
builder.setContentTitle(state.manga.title)
|
||||
builder.setContentText(context.getString(R.string.manga_downloading_))
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
builder.setContentIntent(listIntent)
|
||||
builder.setStyle(null)
|
||||
builder.setLargeIcon(state.cover?.toBitmap())
|
||||
builder.clearActions()
|
||||
builder.setVisibility(
|
||||
fun buildGroupNotification(): Notification {
|
||||
val style = NotificationCompat.InboxStyle(groupBuilder)
|
||||
var progress = 0f
|
||||
var isAllDone = true
|
||||
var isInProgress = false
|
||||
groupBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
states.forEach { _, state ->
|
||||
if (state.manga.isNsfw) {
|
||||
NotificationCompat.VISIBILITY_PRIVATE
|
||||
} else {
|
||||
NotificationCompat.VISIBILITY_PUBLIC
|
||||
groupBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
||||
}
|
||||
)
|
||||
when (state) {
|
||||
is DownloadState.Cancelled -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.cancelling_))
|
||||
builder.setContentIntent(null)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
}
|
||||
is DownloadState.Done -> {
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setContentText(context.getString(R.string.download_complete))
|
||||
builder.setContentIntent(createMangaIntent(context, state.localManga))
|
||||
builder.setAutoCancel(true)
|
||||
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||
builder.setCategory(null)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(false)
|
||||
}
|
||||
is DownloadState.Error -> {
|
||||
val message = state.error.getDisplayMessage(context.resources)
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||
builder.setSubText(context.getString(R.string.error))
|
||||
builder.setContentText(message)
|
||||
builder.setAutoCancel(true)
|
||||
builder.setOngoing(false)
|
||||
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
||||
}
|
||||
is DownloadState.PostProcessing -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.processing_))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
}
|
||||
is DownloadState.Queued -> {
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setContentText(context.getString(R.string.queued))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
}
|
||||
is DownloadState.Preparing -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.preparing_))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
}
|
||||
is DownloadState.Progress -> {
|
||||
builder.setProgress(state.max, state.progress, false)
|
||||
if (timeLeft > 0L) {
|
||||
val eta = DateUtils.getRelativeTimeSpanString(timeLeft, 0L, DateUtils.SECOND_IN_MILLIS)
|
||||
builder.setContentText(eta)
|
||||
} else {
|
||||
val percent = (state.percent * 100).format()
|
||||
builder.setContentText(context.getString(R.string.percent_string_pattern, percent))
|
||||
val summary = when (state) {
|
||||
is DownloadState.Cancelled -> {
|
||||
progress++
|
||||
context.getString(R.string.cancelling_)
|
||||
}
|
||||
is DownloadState.Done -> {
|
||||
progress++
|
||||
context.getString(R.string.download_complete)
|
||||
}
|
||||
is DownloadState.Error -> {
|
||||
isAllDone = false
|
||||
context.getString(R.string.error)
|
||||
}
|
||||
is DownloadState.PostProcessing -> {
|
||||
progress++
|
||||
isInProgress = true
|
||||
isAllDone = false
|
||||
context.getString(R.string.processing_)
|
||||
}
|
||||
is DownloadState.Preparing -> {
|
||||
isAllDone = false
|
||||
isInProgress = true
|
||||
context.getString(R.string.preparing_)
|
||||
}
|
||||
is DownloadState.Progress -> {
|
||||
isAllDone = false
|
||||
isInProgress = true
|
||||
progress += state.percent
|
||||
context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
||||
}
|
||||
is DownloadState.Queued -> {
|
||||
isAllDone = false
|
||||
isInProgress = true
|
||||
context.getString(R.string.queued)
|
||||
}
|
||||
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
}
|
||||
is DownloadState.WaitingForNetwork -> {
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setContentText(context.getString(R.string.waiting_for_network))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
}
|
||||
style.addLine(
|
||||
context.getString(
|
||||
R.string.download_summary_pattern,
|
||||
state.manga.title.ellipsize(16).htmlEncode(),
|
||||
summary.htmlEncode(),
|
||||
).parseAsHtml(HtmlCompat.FROM_HTML_MODE_LEGACY),
|
||||
)
|
||||
}
|
||||
return builder.build()
|
||||
progress = if (isInProgress) {
|
||||
progress / states.size.toFloat()
|
||||
} else {
|
||||
1f
|
||||
}
|
||||
style.setBigContentTitle(
|
||||
context.getString(if (isAllDone) R.string.download_complete else R.string.downloading_manga),
|
||||
)
|
||||
groupBuilder.setContentText(context.resources.getQuantityString(R.plurals.items, states.size, states.size()))
|
||||
groupBuilder.setNumber(states.size)
|
||||
groupBuilder.setSmallIcon(
|
||||
if (isInProgress) android.R.drawable.stat_sys_download else android.R.drawable.stat_sys_download_done,
|
||||
)
|
||||
groupBuilder.setAutoCancel(isAllDone)
|
||||
when (progress) {
|
||||
1f -> groupBuilder.setProgress(0, 0, false)
|
||||
0f -> groupBuilder.setProgress(1, 0, true)
|
||||
else -> groupBuilder.setProgress(100, (progress * 100f).toInt(), false)
|
||||
}
|
||||
return groupBuilder.build()
|
||||
}
|
||||
|
||||
fun detach() {
|
||||
if (states.isNotEmpty()) {
|
||||
val notification = buildGroupNotification()
|
||||
manager.notify(ID_GROUP_DETACHED, notification)
|
||||
}
|
||||
manager.cancel(ID_GROUP)
|
||||
}
|
||||
|
||||
fun newItem(startId: Int) = Item(startId)
|
||||
|
||||
inner class Item(
|
||||
private val startId: Int,
|
||||
) {
|
||||
|
||||
private val builder = NotificationCompat.Builder(context, CHANNEL_ID)
|
||||
private val cancelAction = NotificationCompat.Action(
|
||||
materialR.drawable.material_ic_clear_black_24dp,
|
||||
context.getString(android.R.string.cancel),
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
startId * 2,
|
||||
DownloadService.getCancelIntent(startId),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE,
|
||||
),
|
||||
)
|
||||
private val retryAction = NotificationCompat.Action(
|
||||
R.drawable.ic_restart_black,
|
||||
context.getString(R.string.try_again),
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
startId * 2 + 1,
|
||||
DownloadService.getResumeIntent(startId),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE,
|
||||
),
|
||||
)
|
||||
|
||||
init {
|
||||
builder.setOnlyAlertOnce(true)
|
||||
builder.setDefaults(0)
|
||||
builder.color = ContextCompat.getColor(context, R.color.blue_primary)
|
||||
builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
||||
builder.setSilent(true)
|
||||
builder.setGroup(GROUP_ID)
|
||||
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
||||
}
|
||||
|
||||
fun notify(state: DownloadState, timeLeft: Long) {
|
||||
builder.setContentTitle(state.manga.title)
|
||||
builder.setContentText(context.getString(R.string.manga_downloading_))
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
builder.setContentIntent(listIntent)
|
||||
builder.setStyle(null)
|
||||
builder.setLargeIcon(state.cover?.toBitmap())
|
||||
builder.clearActions()
|
||||
builder.setSubText(null)
|
||||
builder.setShowWhen(false)
|
||||
builder.setVisibility(
|
||||
if (state.manga.isNsfw) {
|
||||
NotificationCompat.VISIBILITY_PRIVATE
|
||||
} else {
|
||||
NotificationCompat.VISIBILITY_PUBLIC
|
||||
},
|
||||
)
|
||||
when (state) {
|
||||
is DownloadState.Cancelled -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.cancelling_))
|
||||
builder.setContentIntent(null)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
is DownloadState.Done -> {
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setContentText(context.getString(R.string.download_complete))
|
||||
builder.setContentIntent(createMangaIntent(context, state.localManga))
|
||||
builder.setAutoCancel(true)
|
||||
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||
builder.setCategory(null)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(false)
|
||||
builder.setShowWhen(true)
|
||||
builder.setWhen(System.currentTimeMillis())
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
is DownloadState.Error -> {
|
||||
val message = state.error.getDisplayMessage(context.resources)
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||
builder.setSubText(context.getString(R.string.error))
|
||||
builder.setContentText(message)
|
||||
builder.setAutoCancel(!state.canRetry)
|
||||
builder.setOngoing(state.canRetry)
|
||||
builder.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||
builder.setShowWhen(true)
|
||||
builder.setWhen(System.currentTimeMillis())
|
||||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(message))
|
||||
if (state.canRetry) {
|
||||
builder.addAction(cancelAction)
|
||||
builder.addAction(retryAction)
|
||||
}
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
is DownloadState.PostProcessing -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.processing_))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
is DownloadState.Queued -> {
|
||||
builder.setProgress(0, 0, false)
|
||||
builder.setContentText(context.getString(R.string.queued))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
builder.priority = NotificationCompat.PRIORITY_LOW
|
||||
}
|
||||
is DownloadState.Preparing -> {
|
||||
builder.setProgress(1, 0, true)
|
||||
builder.setContentText(context.getString(R.string.preparing_))
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
is DownloadState.Progress -> {
|
||||
builder.setProgress(state.max, state.progress, false)
|
||||
val percent = context.getString(R.string.percent_string_pattern, (state.percent * 100).format())
|
||||
if (timeLeft > 0L) {
|
||||
val eta = DateUtils.getRelativeTimeSpanString(timeLeft, 0L, DateUtils.SECOND_IN_MILLIS)
|
||||
builder.setContentText(eta)
|
||||
builder.setSubText(percent)
|
||||
} else {
|
||||
builder.setContentText(percent)
|
||||
}
|
||||
builder.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||
builder.setStyle(null)
|
||||
builder.setOngoing(true)
|
||||
builder.addAction(cancelAction)
|
||||
builder.priority = NotificationCompat.PRIORITY_DEFAULT
|
||||
}
|
||||
}
|
||||
val notification = builder.build()
|
||||
states.append(startId, state)
|
||||
updateGroupNotification()
|
||||
manager.notify(TAG, startId, notification)
|
||||
}
|
||||
|
||||
fun dismiss() {
|
||||
manager.cancel(TAG, startId)
|
||||
states.remove(startId)
|
||||
updateGroupNotification()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateGroupNotification() {
|
||||
val notification = buildGroupNotification()
|
||||
manager.notify(ID_GROUP, notification)
|
||||
}
|
||||
|
||||
private fun createMangaIntent(context: Context, manga: Manga) = PendingIntent.getActivity(
|
||||
context,
|
||||
manga.hashCode(),
|
||||
DetailsActivity.newIntent(context, manga),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE
|
||||
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE,
|
||||
)
|
||||
|
||||
companion object {
|
||||
|
||||
private const val TAG = "download"
|
||||
private const val CHANNEL_ID = "download"
|
||||
private const val GROUP_ID = "downloads"
|
||||
private const val REQUEST_LIST = 6
|
||||
const val ID_GROUP = 9999
|
||||
private const val ID_GROUP_DETACHED = 9998
|
||||
|
||||
fun createChannel(context: Context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
@@ -158,7 +316,7 @@ class DownloadNotification(private val context: Context, startId: Int) {
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
context.getString(R.string.downloads),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
NotificationManager.IMPORTANCE_LOW,
|
||||
)
|
||||
channel.enableVibration(false)
|
||||
channel.enableLights(false)
|
||||
|
||||
@@ -8,12 +8,16 @@ import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.MainThread
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.plus
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koin.core.context.GlobalContext
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
@@ -24,35 +28,37 @@ import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.download.domain.DownloadManager
|
||||
import org.koitharu.kotatsu.download.domain.DownloadState
|
||||
import org.koitharu.kotatsu.download.domain.WakeLockNode
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.utils.ext.connectivityManager
|
||||
import org.koitharu.kotatsu.utils.ext.throttle
|
||||
import org.koitharu.kotatsu.utils.progress.PausingProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressJob
|
||||
import org.koitharu.kotatsu.utils.progress.TimeLeftEstimator
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class DownloadService : BaseService() {
|
||||
|
||||
private lateinit var downloadManager: DownloadManager
|
||||
private lateinit var notificationSwitcher: ForegroundNotificationSwitcher
|
||||
private lateinit var downloadNotification: DownloadNotification
|
||||
private lateinit var wakeLock: PowerManager.WakeLock
|
||||
|
||||
private val jobs = LinkedHashMap<Int, ProgressJob<DownloadState>>()
|
||||
private val jobs = LinkedHashMap<Int, PausingProgressJob<DownloadState>>()
|
||||
private val jobCount = MutableStateFlow(0)
|
||||
private val controlReceiver = ControlReceiver()
|
||||
private var binder: DownloadBinder? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
isRunning = true
|
||||
notificationSwitcher = ForegroundNotificationSwitcher(this)
|
||||
val wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager)
|
||||
downloadNotification = DownloadNotification(this)
|
||||
wakeLock = (applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager)
|
||||
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "kotatsu:downloading")
|
||||
downloadManager = get<DownloadManager.Factory>().create(
|
||||
coroutineScope = lifecycleScope + WakeLockNode(wakeLock, TimeUnit.HOURS.toMillis(1)),
|
||||
)
|
||||
wakeLock.acquire(TimeUnit.HOURS.toMillis(8))
|
||||
downloadManager = get<DownloadManager.Factory>().create(lifecycleScope)
|
||||
DownloadNotification.createChannel(this)
|
||||
registerReceiver(controlReceiver, IntentFilter(ACTION_DOWNLOAD_CANCEL))
|
||||
startForeground(DownloadNotification.ID_GROUP, downloadNotification.buildGroupNotification())
|
||||
val intentFilter = IntentFilter()
|
||||
intentFilter.addAction(ACTION_DOWNLOAD_CANCEL)
|
||||
intentFilter.addAction(ACTION_DOWNLOAD_RESUME)
|
||||
registerReceiver(controlReceiver, intentFilter)
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
@@ -64,24 +70,19 @@ class DownloadService : BaseService() {
|
||||
jobCount.value = jobs.size
|
||||
START_REDELIVER_INTENT
|
||||
} else {
|
||||
stopSelf(startId)
|
||||
stopSelfIfIdle()
|
||||
START_NOT_STICKY
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
super.onBind(intent)
|
||||
return binder ?: DownloadBinder(this).also { binder = it }
|
||||
}
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
binder = null
|
||||
return super.onUnbind(intent)
|
||||
return DownloadBinder(this)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
unregisterReceiver(controlReceiver)
|
||||
binder = null
|
||||
wakeLock.release()
|
||||
isRunning = false
|
||||
super.onDestroy()
|
||||
}
|
||||
@@ -90,7 +91,7 @@ class DownloadService : BaseService() {
|
||||
startId: Int,
|
||||
manga: Manga,
|
||||
chaptersIds: LongArray?,
|
||||
): ProgressJob<DownloadState> {
|
||||
): PausingProgressJob<DownloadState> {
|
||||
val job = downloadManager.downloadManga(manga, chaptersIds, startId)
|
||||
listenJob(job)
|
||||
return job
|
||||
@@ -99,10 +100,10 @@ class DownloadService : BaseService() {
|
||||
private fun listenJob(job: ProgressJob<DownloadState>) {
|
||||
lifecycleScope.launch {
|
||||
val startId = job.progressValue.startId
|
||||
val notification = DownloadNotification(this@DownloadService, startId)
|
||||
val notificationItem = downloadNotification.newItem(startId)
|
||||
try {
|
||||
val timeLeftEstimator = TimeLeftEstimator()
|
||||
notificationSwitcher.notify(startId, notification.create(job.progressValue, -1L))
|
||||
notificationItem.notify(job.progressValue, -1L)
|
||||
job.progressAsFlow()
|
||||
.onEach { state ->
|
||||
if (state is DownloadState.Progress) {
|
||||
@@ -115,26 +116,27 @@ class DownloadService : BaseService() {
|
||||
.whileActive()
|
||||
.collect { state ->
|
||||
val timeLeft = timeLeftEstimator.getEstimatedTimeLeft()
|
||||
notificationSwitcher.notify(startId, notification.create(state, timeLeft))
|
||||
notificationItem.notify(state, timeLeft)
|
||||
}
|
||||
job.join()
|
||||
} finally {
|
||||
(job.progressValue as? DownloadState.Done)?.let {
|
||||
sendBroadcast(
|
||||
Intent(ACTION_DOWNLOAD_COMPLETE)
|
||||
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false))
|
||||
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false)),
|
||||
)
|
||||
}
|
||||
notificationSwitcher.detach(
|
||||
startId,
|
||||
if (job.isCancelled) {
|
||||
null
|
||||
} else {
|
||||
notification.create(job.progressValue, -1L)
|
||||
if (job.isCancelled) {
|
||||
notificationItem.dismiss()
|
||||
if (jobs.remove(startId) != null) {
|
||||
jobCount.value = jobs.size
|
||||
}
|
||||
)
|
||||
stopSelf(startId)
|
||||
} else {
|
||||
notificationItem.notify(job.progressValue, -1L)
|
||||
}
|
||||
}
|
||||
}.invokeOnCompletion {
|
||||
stopSelfIfIdle()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +146,17 @@ class DownloadService : BaseService() {
|
||||
}
|
||||
|
||||
private val DownloadState.isTerminal: Boolean
|
||||
get() = this is DownloadState.Done || this is DownloadState.Error || this is DownloadState.Cancelled
|
||||
get() = this is DownloadState.Done || this is DownloadState.Cancelled || (this is DownloadState.Error && !canRetry)
|
||||
|
||||
@MainThread
|
||||
private fun stopSelfIfIdle() {
|
||||
if (jobs.any { (_, job) -> job.isActive }) {
|
||||
return
|
||||
}
|
||||
downloadNotification.detach()
|
||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
inner class ControlReceiver : BroadcastReceiver() {
|
||||
|
||||
@@ -152,17 +164,37 @@ class DownloadService : BaseService() {
|
||||
when (intent?.action) {
|
||||
ACTION_DOWNLOAD_CANCEL -> {
|
||||
val cancelId = intent.getIntExtra(EXTRA_CANCEL_ID, 0)
|
||||
jobs.remove(cancelId)?.cancel()
|
||||
jobCount.value = jobs.size
|
||||
jobs[cancelId]?.cancel()
|
||||
// jobs.remove(cancelId)?.cancel()
|
||||
// jobCount.value = jobs.size
|
||||
}
|
||||
ACTION_DOWNLOAD_RESUME -> {
|
||||
val cancelId = intent.getIntExtra(EXTRA_CANCEL_ID, 0)
|
||||
jobs[cancelId]?.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DownloadBinder(private val service: DownloadService) : Binder() {
|
||||
class DownloadBinder(service: DownloadService) : Binder(), DefaultLifecycleObserver {
|
||||
|
||||
val downloads: Flow<Collection<ProgressJob<DownloadState>>>
|
||||
get() = service.jobCount.mapLatest { service.jobs.values }
|
||||
private var downloadsStateFlow = MutableStateFlow<List<PausingProgressJob<DownloadState>>>(emptyList())
|
||||
|
||||
init {
|
||||
service.lifecycle.addObserver(this)
|
||||
service.jobCount.onEach {
|
||||
downloadsStateFlow.value = service.jobs.values.toList()
|
||||
}.launchIn(service.lifecycleScope)
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
owner.lifecycle.removeObserver(this)
|
||||
downloadsStateFlow.value = emptyList()
|
||||
super.onDestroy(owner)
|
||||
}
|
||||
|
||||
val downloads
|
||||
get() = downloadsStateFlow.asStateFlow()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -173,6 +205,7 @@ class DownloadService : BaseService() {
|
||||
const val ACTION_DOWNLOAD_COMPLETE = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_COMPLETE"
|
||||
|
||||
private const val ACTION_DOWNLOAD_CANCEL = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_CANCEL"
|
||||
private const val ACTION_DOWNLOAD_RESUME = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_RESUME"
|
||||
|
||||
private const val EXTRA_MANGA = "manga"
|
||||
private const val EXTRA_CHAPTERS_IDS = "chapters_ids"
|
||||
@@ -219,6 +252,9 @@ class DownloadService : BaseService() {
|
||||
fun getCancelIntent(startId: Int) = Intent(ACTION_DOWNLOAD_CANCEL)
|
||||
.putExtra(EXTRA_CANCEL_ID, startId)
|
||||
|
||||
fun getResumeIntent(startId: Int) = Intent(ACTION_DOWNLOAD_RESUME)
|
||||
.putExtra(EXTRA_CANCEL_ID, startId)
|
||||
|
||||
fun getDownloadedManga(intent: Intent?): Manga? {
|
||||
if (intent?.action == ACTION_DOWNLOAD_COMPLETE) {
|
||||
return intent.getParcelableExtra<ParcelableManga>(EXTRA_MANGA)?.manga
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.koitharu.kotatsu.download.ui.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.SparseArray
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.core.util.isEmpty
|
||||
import androidx.core.util.size
|
||||
|
||||
private const val DEFAULT_DELAY = 500L
|
||||
|
||||
class ForegroundNotificationSwitcher(
|
||||
private val service: Service,
|
||||
) {
|
||||
|
||||
private val notificationManager = service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val notifications = SparseArray<Notification>()
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
@Synchronized
|
||||
fun notify(startId: Int, notification: Notification) {
|
||||
if (notifications.isEmpty()) {
|
||||
service.startForeground(startId, notification)
|
||||
} else {
|
||||
notificationManager.notify(startId, notification)
|
||||
}
|
||||
notifications[startId] = notification
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun detach(startId: Int, notification: Notification?) {
|
||||
notifications.remove(startId)
|
||||
if (notifications.isEmpty()) {
|
||||
ServiceCompat.stopForeground(service, ServiceCompat.STOP_FOREGROUND_DETACH)
|
||||
}
|
||||
val nextIndex = notifications.size - 1
|
||||
if (nextIndex >= 0) {
|
||||
val nextStartId = notifications.keyAt(nextIndex)
|
||||
val nextNotification = notifications.valueAt(nextIndex)
|
||||
service.startForeground(nextStartId, nextNotification)
|
||||
}
|
||||
handler.postDelayed(NotifyRunnable(startId, notification), DEFAULT_DELAY)
|
||||
}
|
||||
|
||||
private inner class NotifyRunnable(
|
||||
private val startId: Int,
|
||||
private val notification: Notification?,
|
||||
) : Runnable {
|
||||
|
||||
override fun run() {
|
||||
if (notification != null) {
|
||||
notificationManager.notify(startId, notification)
|
||||
} else {
|
||||
notificationManager.cancel(startId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.koitharu.kotatsu.download.ui.service
|
||||
|
||||
import androidx.annotation.AnyThread
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
class PausingHandle {
|
||||
|
||||
private val paused = MutableStateFlow(false)
|
||||
|
||||
@get:AnyThread
|
||||
val isPaused: Boolean
|
||||
get() = paused.value
|
||||
|
||||
@AnyThread
|
||||
suspend fun awaitResumed() {
|
||||
paused.filter { !it }.first()
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
fun pause() {
|
||||
paused.value = true
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
fun resume() {
|
||||
paused.value = false
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package org.koitharu.kotatsu.favourites.data
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES
|
||||
|
||||
@Entity(tableName = "favourite_categories")
|
||||
class FavouriteCategoryEntity(
|
||||
@Entity(tableName = TABLE_FAVOURITE_CATEGORIES)
|
||||
data class FavouriteCategoryEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "category_id") val categoryId: Int,
|
||||
@ColumnInfo(name = "created_at") val createdAt: Long,
|
||||
|
||||
@@ -3,10 +3,13 @@ package org.koitharu.kotatsu.favourites.data
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
|
||||
@Entity(
|
||||
tableName = "favourites", primaryKeys = ["manga_id", "category_id"], foreignKeys = [
|
||||
tableName = TABLE_FAVOURITES,
|
||||
primaryKeys = ["manga_id", "category_id"],
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = MangaEntity::class,
|
||||
parentColumns = ["manga_id"],
|
||||
@@ -21,8 +24,8 @@ import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
)
|
||||
]
|
||||
)
|
||||
class FavouriteEntity(
|
||||
data class FavouriteEntity(
|
||||
@ColumnInfo(name = "manga_id", index = true) val mangaId: Long,
|
||||
@ColumnInfo(name = "category_id", index = true) val categoryId: Long,
|
||||
@ColumnInfo(name = "created_at") val createdAt: Long
|
||||
@ColumnInfo(name = "created_at") val createdAt: Long,
|
||||
)
|
||||
@@ -148,7 +148,12 @@ class FavouritesContainerFragment :
|
||||
menu.setOnMenuItemClickListener {
|
||||
when (it.itemId) {
|
||||
R.id.action_remove -> editDelegate.deleteCategory(category)
|
||||
R.id.action_edit -> startActivity(FavouritesCategoryEditActivity.newIntent(tabView.context, category.id))
|
||||
R.id.action_edit -> startActivity(
|
||||
FavouritesCategoryEditActivity.newIntent(
|
||||
tabView.context,
|
||||
category.id
|
||||
)
|
||||
)
|
||||
else -> return@setOnMenuItemClickListener false
|
||||
}
|
||||
true
|
||||
@@ -172,7 +177,7 @@ class FavouritesContainerFragment :
|
||||
private fun showStub() {
|
||||
val stub = stubBinding ?: ItemEmptyStateBinding.bind(binding.stubEmptyState.inflate())
|
||||
stub.root.isVisible = true
|
||||
stub.icon.setImageResource(R.drawable.ic_heart_outline)
|
||||
stub.icon.setImageResource(R.drawable.ic_empty_favourites)
|
||||
stub.textPrimary.setText(R.string.text_empty_holder_primary)
|
||||
stub.textSecondary.setText(R.string.empty_favourite_categories)
|
||||
stub.buttonRetry.setText(R.string.add)
|
||||
|
||||
@@ -3,6 +3,8 @@ package org.koitharu.kotatsu.favourites.ui.categories.edit
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.AdapterView
|
||||
@@ -24,7 +26,7 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
|
||||
class FavouritesCategoryEditActivity : BaseActivity<ActivityCategoryEditBinding>(), AdapterView.OnItemClickListener,
|
||||
View.OnClickListener {
|
||||
View.OnClickListener, TextWatcher {
|
||||
|
||||
private val viewModel by viewModel<FavouritesCategoryEditViewModel> {
|
||||
parametersOf(intent.getLongExtra(EXTRA_ID, NO_ID))
|
||||
@@ -40,6 +42,8 @@ class FavouritesCategoryEditActivity : BaseActivity<ActivityCategoryEditBinding>
|
||||
}
|
||||
initSortSpinner()
|
||||
binding.buttonDone.setOnClickListener(this)
|
||||
binding.editName.addTextChangedListener(this)
|
||||
afterTextChanged(binding.editName.text)
|
||||
|
||||
viewModel.onSaved.observe(this) { finishAfterTransition() }
|
||||
viewModel.category.observe(this, ::onCategoryChanged)
|
||||
@@ -66,13 +70,21 @@ class FavouritesCategoryEditActivity : BaseActivity<ActivityCategoryEditBinding>
|
||||
override fun onClick(v: View) {
|
||||
when (v.id) {
|
||||
R.id.button_done -> viewModel.save(
|
||||
title = binding.editName.text?.toString().orEmpty(),
|
||||
title = binding.editName.text?.toString()?.trim().orEmpty(),
|
||||
sortOrder = getSelectedSortOrder(),
|
||||
isTrackerEnabled = binding.switchTracker.isChecked,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
binding.buttonDone.isEnabled = !s.isNullOrBlank()
|
||||
}
|
||||
|
||||
override fun onWindowInsetsChanged(insets: Insets) {
|
||||
binding.scrollView.updatePadding(
|
||||
left = insets.left,
|
||||
|
||||
@@ -42,6 +42,7 @@ class FavouritesCategoryEditViewModel(
|
||||
isTrackerEnabled: Boolean,
|
||||
) {
|
||||
launchLoadingJob {
|
||||
check(title.isNotEmpty())
|
||||
if (categoryId == NO_ID) {
|
||||
repository.createCategory(title, sortOrder, isTrackerEnabled)
|
||||
} else {
|
||||
|
||||
@@ -2,9 +2,11 @@ package org.koitharu.kotatsu.favourites.ui.categories.select
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
import org.koin.core.parameter.parametersOf
|
||||
@@ -26,7 +28,8 @@ class FavouriteCategoriesBottomSheet :
|
||||
BaseBottomSheet<DialogFavoriteCategoriesBinding>(),
|
||||
OnListItemClickListener<MangaCategoryItem>,
|
||||
CategoriesEditDelegate.CategoriesEditCallback,
|
||||
View.OnClickListener {
|
||||
View.OnClickListener,
|
||||
Toolbar.OnMenuItemClickListener {
|
||||
|
||||
private val viewModel by viewModel<MangaCategoriesViewModel> {
|
||||
parametersOf(requireNotNull(arguments?.getParcelableArrayList<ParcelableManga>(KEY_MANGA_LIST)).map { it.manga })
|
||||
@@ -44,7 +47,7 @@ class FavouriteCategoriesBottomSheet :
|
||||
adapter = MangaCategoriesAdapter(this)
|
||||
binding.recyclerViewCategories.adapter = adapter
|
||||
binding.buttonDone.setOnClickListener(this)
|
||||
binding.itemCreate.setOnClickListener(this)
|
||||
binding.toolbar.setOnMenuItemClickListener(this)
|
||||
|
||||
viewModel.content.observe(viewLifecycleOwner, this::onContentChanged)
|
||||
viewModel.onError.observe(viewLifecycleOwner, ::onError)
|
||||
@@ -57,11 +60,18 @@ class FavouriteCategoriesBottomSheet :
|
||||
|
||||
override fun onClick(v: View) {
|
||||
when (v.id) {
|
||||
R.id.item_create -> startActivity(FavouritesCategoryEditActivity.newIntent(requireContext()))
|
||||
R.id.button_done -> dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.action_create -> startActivity(FavouritesCategoryEditActivity.newIntent(requireContext()))
|
||||
else -> return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onItemClick(item: MangaCategoryItem, view: View) {
|
||||
viewModel.setChecked(item.id, !item.isChecked)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.koitharu.kotatsu.list.ui.model.toUi
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
class FavouritesListViewModel(
|
||||
private val categoryId: Long,
|
||||
@@ -45,7 +46,7 @@ class FavouritesListViewModel(
|
||||
} else {
|
||||
repository.observeAll(categoryId)
|
||||
},
|
||||
createListModeFlow()
|
||||
createListModeFlow(),
|
||||
) { list, mode ->
|
||||
when {
|
||||
list.isEmpty() -> listOf(
|
||||
@@ -58,8 +59,9 @@ class FavouritesListViewModel(
|
||||
R.string.favourites_category_empty
|
||||
},
|
||||
actionStringRes = 0,
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
else -> list.toUi(mode, this)
|
||||
}
|
||||
}.catch {
|
||||
|
||||
@@ -10,5 +10,5 @@ val historyModule
|
||||
|
||||
single { HistoryRepository(get(), get(), get(), getAll()) }
|
||||
|
||||
viewModel { HistoryListViewModel(get(), get(), get(), get()) }
|
||||
viewModel { HistoryListViewModel(get(), get(), get()) }
|
||||
}
|
||||
@@ -46,7 +46,7 @@ abstract class HistoryDao {
|
||||
abstract fun observeCount(): Flow<Int>
|
||||
|
||||
@Query("SELECT percent FROM history WHERE manga_id = :id")
|
||||
abstract fun findProgress(id: Long): Float?
|
||||
abstract suspend fun findProgress(id: Long): Float?
|
||||
|
||||
@Query("DELETE FROM history")
|
||||
abstract suspend fun clear()
|
||||
|
||||
@@ -4,10 +4,11 @@ import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.PrimaryKey
|
||||
import org.koitharu.kotatsu.core.db.TABLE_HISTORY
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
|
||||
@Entity(
|
||||
tableName = "history",
|
||||
tableName = TABLE_HISTORY,
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = MangaEntity::class,
|
||||
@@ -17,7 +18,7 @@ import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
)
|
||||
]
|
||||
)
|
||||
class HistoryEntity(
|
||||
data class HistoryEntity(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
@ColumnInfo(name = "manga_id") val mangaId: Long,
|
||||
@ColumnInfo(name = "created_at") val createdAt: Long,
|
||||
|
||||
@@ -5,9 +5,9 @@ import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import androidx.core.view.MenuProvider
|
||||
import com.google.android.material.R as materialR
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import org.koitharu.kotatsu.R
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
class HistoryListMenuProvider(
|
||||
private val context: Context,
|
||||
@@ -38,6 +38,6 @@ class HistoryListMenuProvider(
|
||||
}
|
||||
|
||||
override fun onPrepareMenu(menu: Menu) {
|
||||
menu.findItem(R.id.action_history_grouping).isChecked = viewModel.isGroupingEnabled.value == true
|
||||
menu.findItem(R.id.action_history_grouping)?.isChecked = viewModel.isGroupingEnabled.value == true
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,6 @@ import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.domain.ReversibleHandle
|
||||
import org.koitharu.kotatsu.base.domain.plus
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
@@ -31,7 +29,6 @@ import java.util.concurrent.TimeUnit
|
||||
class HistoryListViewModel(
|
||||
private val repository: HistoryRepository,
|
||||
private val settings: AppSettings,
|
||||
private val shortcutsRepository: ShortcutsRepository,
|
||||
private val trackingRepository: TrackingRepository,
|
||||
) : MangaListViewModel(settings) {
|
||||
|
||||
@@ -72,7 +69,6 @@ class HistoryListViewModel(
|
||||
fun clearHistory() {
|
||||
launchLoadingJob {
|
||||
repository.clear()
|
||||
shortcutsRepository.updateShortcuts()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,10 +77,7 @@ class HistoryListViewModel(
|
||||
return
|
||||
}
|
||||
launchJob(Dispatchers.Default) {
|
||||
val handle = repository.deleteReversible(ids) + ReversibleHandle {
|
||||
shortcutsRepository.updateShortcuts()
|
||||
}
|
||||
shortcutsRepository.updateShortcuts()
|
||||
val handle = repository.deleteReversible(ids)
|
||||
onItemsRemoved.postCall(handle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ abstract class MangaListFragment :
|
||||
|
||||
override fun onInflateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?
|
||||
container: ViewGroup?,
|
||||
) = FragmentListBinding.inflate(inflater, container, false)
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@@ -76,13 +76,13 @@ abstract class MangaListFragment :
|
||||
listAdapter = MangaListAdapter(
|
||||
coil = get(),
|
||||
lifecycleOwner = viewLifecycleOwner,
|
||||
listener = this,
|
||||
listener = this
|
||||
)
|
||||
selectionController = ListSelectionController(
|
||||
activity = requireActivity(),
|
||||
decoration = MangaSelectionDecoration(view.context),
|
||||
registryOwner = this,
|
||||
callback = this,
|
||||
callback = this
|
||||
)
|
||||
paginationListener = PaginationScrollListener(4, this)
|
||||
with(binding.recyclerView) {
|
||||
@@ -97,7 +97,7 @@ abstract class MangaListFragment :
|
||||
setOnRefreshListener(this@MangaListFragment)
|
||||
isEnabled = isSwipeRefreshEnabled
|
||||
}
|
||||
addMenuProvider(MangaListMenuProvider(childFragmentManager))
|
||||
addMenuProvider(MangaListMenuProvider(this))
|
||||
|
||||
viewModel.listMode.observe(viewLifecycleOwner, ::onListModeChanged)
|
||||
viewModel.gridScale.observe(viewLifecycleOwner, ::onGridScaleChanged)
|
||||
@@ -171,21 +171,21 @@ abstract class MangaListFragment :
|
||||
val headerHeight = (activity as? AppBarOwner)?.appBar?.measureHeight() ?: insets.top
|
||||
binding.root.updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right,
|
||||
right = insets.right
|
||||
)
|
||||
if (activity is MainActivity) {
|
||||
binding.recyclerView.updatePadding(
|
||||
top = headerHeight,
|
||||
bottom = insets.bottom,
|
||||
bottom = insets.bottom
|
||||
)
|
||||
binding.swipeRefreshLayout.setProgressViewOffset(
|
||||
true,
|
||||
headerHeight + resources.resolveDp(-72),
|
||||
headerHeight + resources.resolveDp(10),
|
||||
headerHeight + resources.resolveDp(10)
|
||||
)
|
||||
} else {
|
||||
binding.recyclerView.updatePadding(
|
||||
bottom = insets.bottom,
|
||||
bottom = insets.bottom
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import androidx.fragment.app.Fragment
|
||||
import org.koitharu.kotatsu.R
|
||||
|
||||
class MangaListMenuProvider(
|
||||
private val fragmentManager: FragmentManager,
|
||||
private val fragment: Fragment,
|
||||
) : MenuProvider {
|
||||
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
@@ -17,7 +17,7 @@ class MangaListMenuProvider(
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean = when (menuItem.itemId) {
|
||||
R.id.action_list_mode -> {
|
||||
ListModeSelectDialog.show(fragmentManager)
|
||||
ListModeSelectDialog.show(fragment.childFragmentManager)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
@file:SuppressLint("UnsafeOptInUsageError")
|
||||
|
||||
package org.koitharu.kotatsu.list.ui.adapter
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.View
|
||||
import androidx.annotation.CheckResult
|
||||
import androidx.core.view.doOnNextLayout
|
||||
import com.google.android.material.badge.BadgeDrawable
|
||||
import com.google.android.material.badge.BadgeUtils
|
||||
import org.koitharu.kotatsu.R
|
||||
|
||||
@CheckResult
|
||||
fun View.bindBadge(badge: BadgeDrawable?, counter: Int): BadgeDrawable? {
|
||||
return if (counter > 0) {
|
||||
val badgeDrawable = badge ?: initBadge(this)
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
package org.koitharu.kotatsu.list.ui.adapter
|
||||
|
||||
import coil.ImageLoader
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.databinding.ItemEmptyStateBinding
|
||||
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.utils.ext.disposeImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.setTextAndVisible
|
||||
|
||||
fun emptyStateListAD(
|
||||
coil: ImageLoader,
|
||||
listener: MangaListListener,
|
||||
) = adapterDelegateViewBinding<EmptyState, ListModel, ItemEmptyStateBinding>(
|
||||
{ inflater, parent -> ItemEmptyStateBinding.inflate(inflater, parent, false) }
|
||||
{ inflater, parent -> ItemEmptyStateBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
|
||||
binding.buttonRetry.setOnClickListener { listener.onEmptyActionClick() }
|
||||
|
||||
bind {
|
||||
binding.icon.setImageResource(item.icon)
|
||||
binding.icon.newImageRequest(item.icon)?.enqueueWith(coil)
|
||||
binding.textPrimary.setText(item.textPrimary)
|
||||
binding.textSecondary.setTextAndVisible(item.textSecondary)
|
||||
binding.buttonRetry.setTextAndVisible(item.actionStringRes)
|
||||
}
|
||||
|
||||
onViewRecycled {
|
||||
binding.icon.disposeImageRequest()
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,6 @@ package org.koitharu.kotatsu.list.ui.adapter
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import coil.ImageLoader
|
||||
import coil.request.Disposable
|
||||
import coil.size.Scale
|
||||
import coil.util.CoilUtils
|
||||
import com.google.android.material.badge.BadgeDrawable
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
@@ -16,9 +13,11 @@ import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.list.ui.model.MangaGridModel
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.search.ui.multi.adapter.ItemSizeResolver
|
||||
import org.koitharu.kotatsu.utils.ext.disposeImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
import org.koitharu.kotatsu.utils.image.CoverSizeResolver
|
||||
|
||||
fun mangaGridItemAD(
|
||||
coil: ImageLoader,
|
||||
@@ -26,10 +25,8 @@ fun mangaGridItemAD(
|
||||
clickListener: OnListItemClickListener<Manga>,
|
||||
sizeResolver: ItemSizeResolver?,
|
||||
) = adapterDelegateViewBinding<MangaGridModel, ListModel, ItemMangaGridBinding>(
|
||||
{ inflater, parent -> ItemMangaGridBinding.inflate(inflater, parent, false) }
|
||||
{ inflater, parent -> ItemMangaGridBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
|
||||
var imageRequest: Disposable? = null
|
||||
var badge: BadgeDrawable? = null
|
||||
|
||||
itemView.setOnClickListener {
|
||||
@@ -47,16 +44,16 @@ fun mangaGridItemAD(
|
||||
bind { payloads ->
|
||||
binding.textViewTitle.text = item.title
|
||||
binding.progressView.setPercent(item.progress, MangaListAdapter.PAYLOAD_PROGRESS in payloads)
|
||||
imageRequest?.dispose()
|
||||
imageRequest = binding.imageViewCover.newImageRequest(item.coverUrl)
|
||||
.referer(item.manga.publicUrl)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.allowRgb565(true)
|
||||
.scale(Scale.FILL)
|
||||
.lifecycle(lifecycleOwner)
|
||||
.enqueueWith(coil)
|
||||
binding.imageViewCover.newImageRequest(item.coverUrl)?.run {
|
||||
referer(item.manga.publicUrl)
|
||||
size(CoverSizeResolver(binding.imageViewCover))
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
allowRgb565(true)
|
||||
lifecycle(lifecycleOwner)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
badge = itemView.bindBadge(badge, item.counter)
|
||||
}
|
||||
|
||||
@@ -64,9 +61,6 @@ fun mangaGridItemAD(
|
||||
itemView.clearBadge(badge)
|
||||
binding.progressView.percent = PROGRESS_NONE
|
||||
badge = null
|
||||
imageRequest?.dispose()
|
||||
imageRequest = null
|
||||
CoilUtils.dispose(binding.imageViewCover)
|
||||
binding.imageViewCover.setImageDrawable(null)
|
||||
binding.imageViewCover.disposeImageRequest()
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,9 @@ import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import coil.ImageLoader
|
||||
import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter
|
||||
import kotlin.jvm.internal.Intrinsics
|
||||
import org.koitharu.kotatsu.core.ui.DateTimeAgo
|
||||
import org.koitharu.kotatsu.list.ui.model.*
|
||||
import kotlin.jvm.internal.Intrinsics
|
||||
|
||||
class MangaListAdapter(
|
||||
coil: ImageLoader,
|
||||
@@ -24,7 +24,7 @@ class MangaListAdapter(
|
||||
.addDelegate(ITEM_TYPE_DATE, relatedDateItemAD())
|
||||
.addDelegate(ITEM_TYPE_ERROR_STATE, errorStateListAD(listener))
|
||||
.addDelegate(ITEM_TYPE_ERROR_FOOTER, errorFooterAD(listener))
|
||||
.addDelegate(ITEM_TYPE_EMPTY, emptyStateListAD(listener))
|
||||
.addDelegate(ITEM_TYPE_EMPTY, emptyStateListAD(coil, listener))
|
||||
.addDelegate(ITEM_TYPE_HEADER, listHeaderAD())
|
||||
.addDelegate(ITEM_TYPE_FILTER, currentFilterAD(listener))
|
||||
.addDelegate(ITEM_TYPE_HEADER_FILTER, listHeaderWithFilterAD(listener))
|
||||
|
||||
@@ -2,9 +2,6 @@ package org.koitharu.kotatsu.list.ui.adapter
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import coil.ImageLoader
|
||||
import coil.request.Disposable
|
||||
import coil.size.Scale
|
||||
import coil.util.CoilUtils
|
||||
import com.google.android.material.badge.BadgeDrawable
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
@@ -14,20 +11,16 @@ import org.koitharu.kotatsu.history.domain.PROGRESS_NONE
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.list.ui.model.MangaListDetailedModel
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
import org.koitharu.kotatsu.utils.ext.textAndVisible
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
import org.koitharu.kotatsu.utils.image.CoverSizeResolver
|
||||
|
||||
fun mangaListDetailedItemAD(
|
||||
coil: ImageLoader,
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
clickListener: OnListItemClickListener<Manga>,
|
||||
) = adapterDelegateViewBinding<MangaListDetailedModel, ListModel, ItemMangaListDetailsBinding>(
|
||||
{ inflater, parent -> ItemMangaListDetailsBinding.inflate(inflater, parent, false) }
|
||||
{ inflater, parent -> ItemMangaListDetailsBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
|
||||
var imageRequest: Disposable? = null
|
||||
var badge: BadgeDrawable? = null
|
||||
|
||||
itemView.setOnClickListener {
|
||||
@@ -38,31 +31,28 @@ fun mangaListDetailedItemAD(
|
||||
}
|
||||
|
||||
bind { payloads ->
|
||||
imageRequest?.dispose()
|
||||
binding.textViewTitle.text = item.title
|
||||
binding.textViewSubtitle.textAndVisible = item.subtitle
|
||||
binding.progressView.setPercent(item.progress, MangaListAdapter.PAYLOAD_PROGRESS in payloads)
|
||||
imageRequest = binding.imageViewCover.newImageRequest(item.coverUrl)
|
||||
.referer(item.manga.publicUrl)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.scale(Scale.FILL)
|
||||
.allowRgb565(true)
|
||||
.lifecycle(lifecycleOwner)
|
||||
.enqueueWith(coil)
|
||||
binding.imageViewCover.newImageRequest(item.coverUrl)?.run {
|
||||
referer(item.manga.publicUrl)
|
||||
size(CoverSizeResolver(binding.imageViewCover))
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
allowRgb565(true)
|
||||
lifecycle(lifecycleOwner)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
binding.textViewRating.textAndVisible = item.rating
|
||||
binding.textViewTags.text = item.tags
|
||||
itemView.bindBadge(badge, item.counter)
|
||||
badge = itemView.bindBadge(badge, item.counter)
|
||||
}
|
||||
|
||||
onViewRecycled {
|
||||
itemView.clearBadge(badge)
|
||||
binding.progressView.percent = PROGRESS_NONE
|
||||
badge = null
|
||||
imageRequest?.dispose()
|
||||
imageRequest = null
|
||||
CoilUtils.dispose(binding.imageViewCover)
|
||||
binding.imageViewCover.setImageDrawable(null)
|
||||
binding.imageViewCover.disposeImageRequest()
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,6 @@ package org.koitharu.kotatsu.list.ui.adapter
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import coil.ImageLoader
|
||||
import coil.request.Disposable
|
||||
import coil.size.Scale
|
||||
import coil.util.CoilUtils
|
||||
import com.google.android.material.badge.BadgeDrawable
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
@@ -13,20 +10,15 @@ import org.koitharu.kotatsu.databinding.ItemMangaListBinding
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.list.ui.model.MangaListModel
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.utils.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.utils.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.utils.ext.referer
|
||||
import org.koitharu.kotatsu.utils.ext.textAndVisible
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
|
||||
fun mangaListItemAD(
|
||||
coil: ImageLoader,
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
clickListener: OnListItemClickListener<Manga>,
|
||||
) = adapterDelegateViewBinding<MangaListModel, ListModel, ItemMangaListBinding>(
|
||||
{ inflater, parent -> ItemMangaListBinding.inflate(inflater, parent, false) }
|
||||
{ inflater, parent -> ItemMangaListBinding.inflate(inflater, parent, false) },
|
||||
) {
|
||||
|
||||
var imageRequest: Disposable? = null
|
||||
var badge: BadgeDrawable? = null
|
||||
|
||||
itemView.setOnClickListener {
|
||||
@@ -37,27 +29,23 @@ fun mangaListItemAD(
|
||||
}
|
||||
|
||||
bind {
|
||||
imageRequest?.dispose()
|
||||
binding.textViewTitle.text = item.title
|
||||
binding.textViewSubtitle.textAndVisible = item.subtitle
|
||||
imageRequest = binding.imageViewCover.newImageRequest(item.coverUrl)
|
||||
.referer(item.manga.publicUrl)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
.scale(Scale.FILL)
|
||||
.allowRgb565(true)
|
||||
.lifecycle(lifecycleOwner)
|
||||
.enqueueWith(coil)
|
||||
itemView.bindBadge(badge, item.counter)
|
||||
binding.imageViewCover.newImageRequest(item.coverUrl)?.run {
|
||||
referer(item.manga.publicUrl)
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_placeholder)
|
||||
allowRgb565(true)
|
||||
lifecycle(lifecycleOwner)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
badge = itemView.bindBadge(badge, item.counter)
|
||||
}
|
||||
|
||||
onViewRecycled {
|
||||
itemView.clearBadge(badge)
|
||||
badge = null
|
||||
imageRequest?.dispose()
|
||||
imageRequest = null
|
||||
CoilUtils.dispose(binding.imageViewCover)
|
||||
binding.imageViewCover.setImageDrawable(null)
|
||||
binding.imageViewCover.disposeImageRequest()
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,22 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
import java.text.Collator
|
||||
import java.util.*
|
||||
import java.util.Locale
|
||||
import java.util.TreeSet
|
||||
|
||||
class FilterCoordinator(
|
||||
private val repository: RemoteMangaRepository,
|
||||
@@ -152,7 +159,7 @@ class FilterCoordinator(
|
||||
}
|
||||
|
||||
private fun loadTagsAsync() = coroutineScope.async(Dispatchers.Default, CoroutineStart.LAZY) {
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
repository.getTags()
|
||||
}.onFailure { error ->
|
||||
error.printStackTraceDebug()
|
||||
@@ -203,4 +210,4 @@ class FilterCoordinator(
|
||||
return collator?.compare(t1, t2) ?: compareValues(t1, t2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,5 +16,5 @@ val localModule
|
||||
|
||||
factory { DownloadManager.Factory(androidContext(), get(), get(), get(), get(), get()) }
|
||||
|
||||
viewModel { LocalListViewModel(get(), get(), get(), get()) }
|
||||
viewModel { LocalListViewModel(get(), get(), get()) }
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.koitharu.kotatsu.local.domain
|
||||
|
||||
import androidx.annotation.WorkerThread
|
||||
import java.io.File
|
||||
import java.util.zip.ZipFile
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import okio.Closeable
|
||||
@@ -11,8 +13,6 @@ import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.util.toFileNameSafe
|
||||
import org.koitharu.kotatsu.utils.ext.deleteAwait
|
||||
import org.koitharu.kotatsu.utils.ext.readText
|
||||
import java.io.File
|
||||
import java.util.zip.ZipFile
|
||||
|
||||
class CbzMangaOutput(
|
||||
val file: File,
|
||||
@@ -62,7 +62,7 @@ class CbzMangaOutput(
|
||||
index.addChapter(chapter)
|
||||
}
|
||||
|
||||
suspend fun finalize() {
|
||||
suspend fun finish() {
|
||||
runInterruptible(Dispatchers.IO) {
|
||||
output.put(ENTRY_NAME_INDEX, index.toString())
|
||||
output.finish()
|
||||
@@ -89,7 +89,7 @@ class CbzMangaOutput(
|
||||
otherIndex = MangaIndex(
|
||||
zip.getInputStream(entry).use {
|
||||
it.reader().readText()
|
||||
}
|
||||
},
|
||||
)
|
||||
} else {
|
||||
output.copyEntryFrom(zip, entry)
|
||||
|
||||
@@ -8,19 +8,31 @@ import androidx.collection.ArraySet
|
||||
import androidx.core.net.toFile
|
||||
import androidx.core.net.toUri
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.Enumeration
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.IOException
|
||||
import org.koitharu.kotatsu.core.exceptions.UnsupportedFileException
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.local.data.CbzFilter
|
||||
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
||||
import org.koitharu.kotatsu.local.data.MangaIndex
|
||||
import org.koitharu.kotatsu.local.data.TempFileFilter
|
||||
import org.koitharu.kotatsu.parsers.model.*
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.model.MangaPage
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.parsers.util.toCamelCase
|
||||
import org.koitharu.kotatsu.utils.AlphanumComparator
|
||||
import org.koitharu.kotatsu.utils.CompositeMutex
|
||||
@@ -28,6 +40,7 @@ import org.koitharu.kotatsu.utils.ext.deleteAwait
|
||||
import org.koitharu.kotatsu.utils.ext.longHashCode
|
||||
import org.koitharu.kotatsu.utils.ext.readText
|
||||
import org.koitharu.kotatsu.utils.ext.resolveName
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
private const val MAX_PARALLELISM = 4
|
||||
|
||||
@@ -48,6 +61,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
x.altTitle?.contains(query, ignoreCase = true) == true
|
||||
}
|
||||
}
|
||||
list.sortWith(compareBy(AlphanumComparator()) { x -> x.title })
|
||||
return list
|
||||
}
|
||||
|
||||
@@ -61,6 +75,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
x.tags.containsAll(tags)
|
||||
}
|
||||
}
|
||||
list.sortWith(compareBy(AlphanumComparator()) { x -> x.title })
|
||||
return list
|
||||
}
|
||||
|
||||
@@ -68,6 +83,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
manga.source != MangaSource.LOCAL -> requireNotNull(findSavedManga(manga)) {
|
||||
"Manga is not local or saved"
|
||||
}
|
||||
|
||||
else -> getFromFile(Uri.parse(manga.url).toFile())
|
||||
}
|
||||
|
||||
@@ -86,7 +102,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
entries.filter { x ->
|
||||
!x.isDirectory && x.name.substringBeforeLast(
|
||||
File.separatorChar,
|
||||
""
|
||||
"",
|
||||
) == parent
|
||||
}
|
||||
}
|
||||
@@ -138,11 +154,11 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
url = fileUri,
|
||||
coverUrl = zipUri(
|
||||
file,
|
||||
entryName = index.getCoverEntry() ?: findFirstImageEntry(zip.entries())?.name.orEmpty()
|
||||
entryName = index.getCoverEntry() ?: findFirstImageEntry(zip.entries())?.name.orEmpty(),
|
||||
),
|
||||
chapters = info.chapters?.map { c ->
|
||||
c.copy(url = fileUri, source = MangaSource.LOCAL)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
// fallback
|
||||
@@ -211,7 +227,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
return@runInterruptible info.copy2(
|
||||
source = MangaSource.LOCAL,
|
||||
url = fileUri,
|
||||
chapters = info.chapters?.map { c -> c.copy(url = fileUri) }
|
||||
chapters = info.chapters?.map { c -> c.copy(url = fileUri) },
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -224,7 +240,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
context: CoroutineContext,
|
||||
): Deferred<Manga?> = async(context) {
|
||||
runInterruptible {
|
||||
runCatching { getFromFile(file) }.getOrNull()
|
||||
runCatchingCancellable { getFromFile(file) }.getOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,7 +304,7 @@ class LocalMangaRepository(private val storageManager: LocalStorageManager) : Ma
|
||||
locks.lock(id)
|
||||
}
|
||||
|
||||
suspend fun unlockManga(id: Long) {
|
||||
fun unlockManga(id: Long) {
|
||||
locks.unlock(id)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.local.ui
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -10,8 +11,8 @@ import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.IOException
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.download.ui.service.DownloadService
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
@@ -22,14 +23,13 @@ import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.utils.SingleLiveEvent
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
import org.koitharu.kotatsu.utils.progress.Progress
|
||||
import java.io.IOException
|
||||
|
||||
class LocalListViewModel(
|
||||
private val repository: LocalMangaRepository,
|
||||
private val historyRepository: HistoryRepository,
|
||||
settings: AppSettings,
|
||||
private val shortcutsRepository: ShortcutsRepository,
|
||||
) : MangaListViewModel(settings) {
|
||||
|
||||
val onMangaRemoved = SingleLiveEvent<Unit>()
|
||||
@@ -42,7 +42,7 @@ class LocalListViewModel(
|
||||
override val content = combine(
|
||||
mangaList,
|
||||
createListModeFlow(),
|
||||
listError
|
||||
listError,
|
||||
) { list, mode, error ->
|
||||
when {
|
||||
error != null -> listOf(error.toErrorState(canRetry = true))
|
||||
@@ -53,8 +53,9 @@ class LocalListViewModel(
|
||||
textPrimary = R.string.text_local_holder_primary,
|
||||
textSecondary = R.string.text_local_holder_secondary,
|
||||
actionStringRes = R.string._import,
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
else -> ArrayList<ListModel>(list.size + 1).apply {
|
||||
add(headerModel)
|
||||
list.toUi(this, mode)
|
||||
@@ -62,7 +63,7 @@ class LocalListViewModel(
|
||||
}
|
||||
}.asLiveDataDistinct(
|
||||
viewModelScope.coroutineContext + Dispatchers.Default,
|
||||
listOf(LoadingState)
|
||||
listOf(LoadingState),
|
||||
)
|
||||
|
||||
init {
|
||||
@@ -99,7 +100,7 @@ class LocalListViewModel(
|
||||
for (manga in itemsToRemove) {
|
||||
val original = repository.getRemoteManga(manga)
|
||||
repository.delete(manga) || throw IOException("Unable to delete file")
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
historyRepository.deleteOrSwap(manga, original)
|
||||
}
|
||||
mangaList.update { list ->
|
||||
@@ -107,7 +108,6 @@ class LocalListViewModel(
|
||||
}
|
||||
}
|
||||
}
|
||||
shortcutsRepository.updateShortcuts()
|
||||
onMangaRemoved.call(Unit)
|
||||
}
|
||||
}
|
||||
@@ -116,6 +116,8 @@ class LocalListViewModel(
|
||||
try {
|
||||
listError.value = null
|
||||
mangaList.value = repository.getList(0, null, null)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
listError.value = e
|
||||
}
|
||||
@@ -124,7 +126,7 @@ class LocalListViewModel(
|
||||
private fun cleanup() {
|
||||
if (!DownloadService.isRunning) {
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
repository.cleanup()
|
||||
}.onFailure { error ->
|
||||
error.printStackTraceDebug()
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
package org.koitharu.kotatsu.main
|
||||
|
||||
import android.app.Application
|
||||
import android.os.Build
|
||||
import androidx.room.InvalidationTracker
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
import org.koitharu.kotatsu.base.ui.util.ActivityRecreationHandle
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsUpdater
|
||||
import org.koitharu.kotatsu.main.ui.MainViewModel
|
||||
import org.koitharu.kotatsu.main.ui.protect.AppProtectHelper
|
||||
import org.koitharu.kotatsu.main.ui.protect.ProtectViewModel
|
||||
|
||||
val mainModule
|
||||
get() = module {
|
||||
single { AppProtectHelper(get()) }
|
||||
single { ActivityRecreationHandle() }
|
||||
factory { ShortcutsRepository(androidContext(), get(), get(), get()) }
|
||||
single { AppProtectHelper(get()) } bind Application.ActivityLifecycleCallbacks::class
|
||||
single { ActivityRecreationHandle() } bind Application.ActivityLifecycleCallbacks::class
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
single { ShortcutsUpdater(androidContext(), get(), get(), get()) } bind InvalidationTracker.Observer::class
|
||||
} else {
|
||||
factory { ShortcutsUpdater(androidContext(), get(), get(), get()) }
|
||||
}
|
||||
|
||||
viewModel { MainViewModel(get(), get()) }
|
||||
viewModel { ProtectViewModel(get(), get()) }
|
||||
}
|
||||
@@ -12,19 +12,24 @@ import androidx.appcompat.view.ActionMode
|
||||
import androidx.core.app.ActivityOptionsCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.view.*
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.fragment.app.commit
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.transition.TransitionManager
|
||||
import com.google.android.material.R as materialR
|
||||
import com.google.android.material.appbar.AppBarLayout
|
||||
import com.google.android.material.appbar.AppBarLayout.LayoutParams.*
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.navigation.NavigationView
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
@@ -59,7 +64,6 @@ import org.koitharu.kotatsu.tracker.ui.FeedFragment
|
||||
import org.koitharu.kotatsu.tracker.work.TrackWorker
|
||||
import org.koitharu.kotatsu.utils.VoiceInputContract
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
private const val TAG_PRIMARY = "primary"
|
||||
private const val TAG_SEARCH = "search"
|
||||
@@ -94,10 +98,10 @@ class MainActivity :
|
||||
it,
|
||||
binding.toolbar,
|
||||
R.string.open_menu,
|
||||
R.string.close_menu
|
||||
R.string.close_menu,
|
||||
).apply {
|
||||
setHomeAsUpIndicator(
|
||||
ContextCompat.getDrawable(this@MainActivity, materialR.drawable.abc_ic_ab_back_material)
|
||||
ContextCompat.getDrawable(this@MainActivity, materialR.drawable.abc_ic_ab_back_material),
|
||||
)
|
||||
setToolbarNavigationClickListener {
|
||||
binding.searchView.hideKeyboard()
|
||||
@@ -429,7 +433,12 @@ class MainActivity :
|
||||
}
|
||||
|
||||
private fun onFirstStart() {
|
||||
lifecycleScope.launchWhenResumed {
|
||||
lifecycleScope.launch(Dispatchers.Main) { // not a default `Main.immediate` dispatcher
|
||||
val settings = get<AppSettings>()
|
||||
when {
|
||||
!settings.isSourcesSelected -> OnboardDialogFragment.showWelcome(supportFragmentManager)
|
||||
settings.newSources.isNotEmpty() -> NewSourcesDialogFragment.show(supportFragmentManager)
|
||||
}
|
||||
val isUpdateSupported = withContext(Dispatchers.Default) {
|
||||
TrackWorker.setup(applicationContext)
|
||||
SuggestionsWorker.setup(applicationContext)
|
||||
@@ -438,11 +447,6 @@ class MainActivity :
|
||||
if (isUpdateSupported) {
|
||||
AppUpdateChecker(this@MainActivity).checkIfNeeded()
|
||||
}
|
||||
val settings = get<AppSettings>()
|
||||
when {
|
||||
!settings.isSourcesSelected -> OnboardDialogFragment.showWelcome(supportFragmentManager)
|
||||
settings.newSources.isNotEmpty() -> NewSourcesDialogFragment.show(supportFragmentManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,7 +473,7 @@ class MainActivity :
|
||||
val drawer = drawer ?: return
|
||||
val isLocked = actionModeDelegate.isActionModeStarted || isSearchOpened
|
||||
drawer.setDrawerLockMode(
|
||||
if (isLocked) DrawerLayout.LOCK_MODE_LOCKED_CLOSED else DrawerLayout.LOCK_MODE_UNLOCKED
|
||||
if (isLocked) DrawerLayout.LOCK_MODE_LOCKED_CLOSED else DrawerLayout.LOCK_MODE_UNLOCKED,
|
||||
)
|
||||
drawerToggle?.isDrawerIndicatorEnabled = !isLocked
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import org.acra.dialog.CrashReportDialog
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
|
||||
class AppProtectHelper(private val settings: AppSettings) : Application.ActivityLifecycleCallbacks {
|
||||
@@ -11,7 +12,7 @@ class AppProtectHelper(private val settings: AppSettings) : Application.Activity
|
||||
private var isUnlocked = settings.appPassword.isNullOrEmpty()
|
||||
|
||||
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
|
||||
if (activity !is ProtectActivity && !isUnlocked) {
|
||||
if (!isUnlocked && activity !is ProtectActivity && activity !is CrashReportDialog) {
|
||||
val sourceIntent = Intent(activity, activity.javaClass)
|
||||
activity.intent?.let {
|
||||
sourceIntent.putExtras(it)
|
||||
|
||||
@@ -46,7 +46,10 @@ class ProtectActivity :
|
||||
startActivity(intent)
|
||||
finishAfterTransition()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
if (!useFingerprint()) {
|
||||
binding.editPassword.requestFocus()
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ val readerModule
|
||||
factory { MangaDataRepository(get()) }
|
||||
single { PagesCache(get()) }
|
||||
|
||||
factory { PageSaveHelper(get(), androidContext()) }
|
||||
factory { PageSaveHelper(androidContext()) }
|
||||
|
||||
viewModel { params ->
|
||||
ReaderViewModel(
|
||||
@@ -23,10 +23,9 @@ val readerModule
|
||||
preselectedBranch = params[2],
|
||||
dataRepository = get(),
|
||||
historyRepository = get(),
|
||||
shortcutsRepository = get(),
|
||||
settings = get(),
|
||||
pageSaveHelper = get(),
|
||||
bookmarksRepository = get(),
|
||||
bookmarksRepository = get()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,22 @@ import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import androidx.collection.LongSparseArray
|
||||
import androidx.collection.set
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
import java.util.LinkedList
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.coroutines.AbstractCoroutineContextElement
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import okhttp3.OkHttpClient
|
||||
@@ -26,18 +39,15 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.await
|
||||
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
|
||||
import org.koitharu.kotatsu.utils.ext.connectivityManager
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.progress.ProgressDeferred
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.zip.ZipFile
|
||||
|
||||
private const val PROGRESS_UNDEFINED = -1f
|
||||
private const val PREFETCH_LIMIT_DEFAULT = 10
|
||||
|
||||
class PageLoader : KoinComponent, Closeable {
|
||||
|
||||
val loaderScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
val loaderScope = CoroutineScope(SupervisorJob() + InternalErrorHandler() + Dispatchers.Default)
|
||||
|
||||
private val okHttp = get<OkHttpClient>()
|
||||
private val cache = get<PagesCache>()
|
||||
@@ -194,4 +204,13 @@ class PageLoader : KoinComponent, Closeable {
|
||||
val deferred = CompletableDeferred(file)
|
||||
return ProgressDeferred(deferred, emptyProgressFlow)
|
||||
}
|
||||
|
||||
private class InternalErrorHandler :
|
||||
AbstractCoroutineContextElement(CoroutineExceptionHandler),
|
||||
CoroutineExceptionHandler {
|
||||
|
||||
override fun handleException(context: CoroutineContext, exception: Throwable) {
|
||||
exception.printStackTraceDebug()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,10 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.core.net.toUri
|
||||
import java.io.File
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
@@ -11,19 +15,14 @@ import kotlinx.coroutines.withContext
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okio.IOException
|
||||
import org.koitharu.kotatsu.base.domain.MangaUtils
|
||||
import org.koitharu.kotatsu.local.data.PagesCache
|
||||
import org.koitharu.kotatsu.parsers.model.MangaPage
|
||||
import org.koitharu.kotatsu.parsers.util.toFileNameSafe
|
||||
import org.koitharu.kotatsu.reader.domain.PageLoader
|
||||
import java.io.File
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
private const val MAX_FILENAME_LENGTH = 10
|
||||
private const val EXTENSION_FALLBACK = "png"
|
||||
|
||||
class PageSaveHelper(
|
||||
private val cache: PagesCache,
|
||||
context: Context,
|
||||
) {
|
||||
|
||||
@@ -61,7 +60,11 @@ class PageSaveHelper(
|
||||
} != null
|
||||
|
||||
private suspend fun getProposedFileName(url: String, file: File): String {
|
||||
var name = url.toHttpUrl().pathSegments.last()
|
||||
var name = if (url.startsWith("cbz://")) {
|
||||
requireNotNull(url.toUri().fragment)
|
||||
} else {
|
||||
url.toHttpUrl().pathSegments.last()
|
||||
}
|
||||
var extension = name.substringAfterLast('.', "")
|
||||
name = name.substringBeforeLast('.')
|
||||
if (extension.length !in 2..4) {
|
||||
|
||||
@@ -19,6 +19,7 @@ import androidx.transition.TransitionManager
|
||||
import androidx.transition.TransitionSet
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
@@ -47,7 +48,6 @@ import org.koitharu.kotatsu.utils.GridTouchHelper
|
||||
import org.koitharu.kotatsu.utils.ScreenOrientationHelper
|
||||
import org.koitharu.kotatsu.utils.ShareHelper
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class ReaderActivity :
|
||||
BaseFullscreenActivity<ActivityReaderBinding>(),
|
||||
@@ -67,6 +67,9 @@ class ReaderActivity :
|
||||
)
|
||||
}
|
||||
|
||||
override val readerMode: ReaderMode?
|
||||
get() = readerManager.currentMode
|
||||
|
||||
private lateinit var touchHelper: GridTouchHelper
|
||||
private lateinit var orientationHelper: ScreenOrientationHelper
|
||||
private lateinit var controlDelegate: ReaderControlDelegate
|
||||
@@ -82,7 +85,7 @@ class ReaderActivity :
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
touchHelper = GridTouchHelper(this, this)
|
||||
orientationHelper = ScreenOrientationHelper(this)
|
||||
controlDelegate = ReaderControlDelegate(lifecycleScope, get(), this)
|
||||
controlDelegate = ReaderControlDelegate(get(), this, this)
|
||||
binding.toolbarBottom.inflateMenu(R.menu.opt_reader_bottom)
|
||||
binding.toolbarBottom.setOnMenuItemClickListener(::onOptionsItemSelected)
|
||||
insetsDelegate.interceptingWindowInsetsListener = this
|
||||
@@ -146,7 +149,7 @@ class ReaderActivity :
|
||||
ChaptersBottomSheet.show(
|
||||
supportFragmentManager,
|
||||
viewModel.manga?.chapters.orEmpty(),
|
||||
viewModel.getCurrentState()?.chapterId ?: 0L
|
||||
viewModel.getCurrentState()?.chapterId ?: 0L,
|
||||
)
|
||||
}
|
||||
R.id.action_screen_rotate -> {
|
||||
@@ -166,10 +169,9 @@ class ReaderActivity :
|
||||
}
|
||||
}
|
||||
R.id.action_save_page -> {
|
||||
viewModel.getCurrentPage()?.also { page ->
|
||||
viewModel.saveCurrentState(readerManager.currentReader?.getCurrentState())
|
||||
viewModel.saveCurrentPage(page, savePageRequest)
|
||||
} ?: return false
|
||||
viewModel.saveCurrentState(readerManager.currentReader?.getCurrentState())
|
||||
val page = viewModel.getCurrentPage() ?: return false
|
||||
viewModel.saveCurrentPage(page, savePageRequest)
|
||||
}
|
||||
R.id.action_bookmark -> {
|
||||
if (viewModel.isBookmarkAdded.value == true) {
|
||||
@@ -211,7 +213,7 @@ class ReaderActivity :
|
||||
val resolveTextId = ExceptionResolver.getResolveStringId(e)
|
||||
if (resolveTextId != 0) {
|
||||
dialog.setPositiveButton(resolveTextId, listener)
|
||||
} else {
|
||||
} else if (e.isReportable()) {
|
||||
dialog.setPositiveButton(R.string.report, listener)
|
||||
}
|
||||
dialog.show()
|
||||
@@ -318,12 +320,12 @@ class ReaderActivity :
|
||||
binding.appbarTop.updatePadding(
|
||||
top = systemBars.top,
|
||||
right = systemBars.right,
|
||||
left = systemBars.left
|
||||
left = systemBars.left,
|
||||
)
|
||||
binding.appbarBottom?.updatePadding(
|
||||
bottom = systemBars.bottom,
|
||||
right = systemBars.right,
|
||||
left = systemBars.left
|
||||
left = systemBars.left,
|
||||
)
|
||||
return WindowInsetsCompat.Builder(insets)
|
||||
.setInsets(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
|
||||
@@ -346,14 +348,14 @@ class ReaderActivity :
|
||||
menuItem.setIcon(if (isAdded) R.drawable.ic_bookmark_added else R.drawable.ic_bookmark)
|
||||
}
|
||||
|
||||
private fun onUiStateChanged(uiState: ReaderUiState, previous: ReaderUiState?) {
|
||||
title = uiState.chapterName ?: uiState.mangaName ?: getString(R.string.loading_)
|
||||
supportActionBar?.subtitle = if (uiState.chapterNumber in 1..uiState.chaptersTotal) {
|
||||
private fun onUiStateChanged(uiState: ReaderUiState?, previous: ReaderUiState?) {
|
||||
title = uiState?.chapterName ?: uiState?.mangaName ?: getString(R.string.loading_)
|
||||
supportActionBar?.subtitle = if (uiState != null && uiState.chapterNumber in 1..uiState.chaptersTotal) {
|
||||
getString(R.string.chapter_d_of_d, uiState.chapterNumber, uiState.chaptersTotal)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (previous?.chapterName != null && uiState.chapterName != previous.chapterName) {
|
||||
if (uiState != null && previous?.chapterName != null && uiState.chapterName != previous.chapterName) {
|
||||
if (!uiState.chapterName.isNullOrEmpty()) {
|
||||
binding.toastView.showTemporary(uiState.chapterName, TOAST_DURATION)
|
||||
}
|
||||
|
||||
@@ -1,33 +1,39 @@
|
||||
package org.koitharu.kotatsu.reader.ui
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.view.KeyEvent
|
||||
import android.view.SoundEffectConstants
|
||||
import android.view.View
|
||||
import androidx.lifecycle.LifecycleCoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
import org.koitharu.kotatsu.core.prefs.ReaderMode
|
||||
import org.koitharu.kotatsu.utils.GridTouchHelper
|
||||
|
||||
class ReaderControlDelegate(
|
||||
scope: LifecycleCoroutineScope,
|
||||
settings: AppSettings,
|
||||
private val listener: OnInteractionListener
|
||||
) {
|
||||
private val settings: AppSettings,
|
||||
private val listener: OnInteractionListener,
|
||||
owner: LifecycleOwner,
|
||||
) : DefaultLifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private var isTapSwitchEnabled: Boolean = true
|
||||
private var isVolumeKeysSwitchEnabled: Boolean = false
|
||||
private var isReaderTapsAdaptive: Boolean = true
|
||||
|
||||
init {
|
||||
settings.observeAsFlow(AppSettings.KEY_READER_SWITCHERS) { readerPageSwitch }
|
||||
.flowOn(Dispatchers.Default)
|
||||
.onEach {
|
||||
isTapSwitchEnabled = AppSettings.PAGE_SWITCH_TAPS in it
|
||||
isVolumeKeysSwitchEnabled = AppSettings.PAGE_SWITCH_VOLUME_KEYS in it
|
||||
}.launchIn(scope)
|
||||
owner.lifecycle.addObserver(this)
|
||||
settings.subscribe(this)
|
||||
updateSettings()
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
settings.unsubscribe(this)
|
||||
owner.lifecycle.removeObserver(this)
|
||||
super.onDestroy(owner)
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
updateSettings()
|
||||
}
|
||||
|
||||
fun onGridTouch(area: Int, view: View) {
|
||||
@@ -41,7 +47,7 @@ class ReaderControlDelegate(
|
||||
view.playSoundEffect(SoundEffectConstants.NAVIGATION_UP)
|
||||
}
|
||||
GridTouchHelper.AREA_LEFT -> if (isTapSwitchEnabled) {
|
||||
listener.switchPageBy(-1)
|
||||
listener.switchPageBy(if (isReaderTapsReversed()) 1 else -1)
|
||||
view.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT)
|
||||
}
|
||||
GridTouchHelper.AREA_BOTTOM -> if (isTapSwitchEnabled) {
|
||||
@@ -49,7 +55,7 @@ class ReaderControlDelegate(
|
||||
view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN)
|
||||
}
|
||||
GridTouchHelper.AREA_RIGHT -> if (isTapSwitchEnabled) {
|
||||
listener.switchPageBy(1)
|
||||
listener.switchPageBy(if (isReaderTapsReversed()) -1 else 1)
|
||||
view.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT)
|
||||
}
|
||||
}
|
||||
@@ -72,17 +78,25 @@ class ReaderControlDelegate(
|
||||
KeyEvent.KEYCODE_PAGE_DOWN,
|
||||
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN,
|
||||
KeyEvent.KEYCODE_DPAD_DOWN,
|
||||
KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
||||
-> {
|
||||
listener.switchPageBy(1)
|
||||
true
|
||||
}
|
||||
KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
||||
listener.switchPageBy(if (isReaderTapsReversed()) -1 else 1)
|
||||
true
|
||||
}
|
||||
KeyEvent.KEYCODE_PAGE_UP,
|
||||
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP,
|
||||
KeyEvent.KEYCODE_DPAD_UP,
|
||||
KeyEvent.KEYCODE_DPAD_LEFT -> {
|
||||
-> {
|
||||
listener.switchPageBy(-1)
|
||||
true
|
||||
}
|
||||
KeyEvent.KEYCODE_DPAD_LEFT -> {
|
||||
listener.switchPageBy(if (isReaderTapsReversed()) 1 else -1)
|
||||
true
|
||||
}
|
||||
KeyEvent.KEYCODE_DPAD_CENTER -> {
|
||||
listener.toggleUiVisibility()
|
||||
true
|
||||
@@ -97,8 +111,21 @@ class ReaderControlDelegate(
|
||||
)
|
||||
}
|
||||
|
||||
private fun updateSettings() {
|
||||
val switch = settings.readerPageSwitch
|
||||
isTapSwitchEnabled = AppSettings.PAGE_SWITCH_TAPS in switch
|
||||
isVolumeKeysSwitchEnabled = AppSettings.PAGE_SWITCH_VOLUME_KEYS in switch
|
||||
isReaderTapsAdaptive = settings.isReaderTapsAdaptive
|
||||
}
|
||||
|
||||
private fun isReaderTapsReversed(): Boolean {
|
||||
return isReaderTapsAdaptive && listener.readerMode == ReaderMode.REVERSED
|
||||
}
|
||||
|
||||
interface OnInteractionListener {
|
||||
|
||||
val readerMode: ReaderMode?
|
||||
|
||||
fun switchPageBy(delta: Int)
|
||||
|
||||
fun toggleUiVisibility()
|
||||
|
||||
@@ -6,9 +6,9 @@ import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import java.util.Date
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.acra.ACRA
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||
import org.koitharu.kotatsu.base.domain.MangaIntent
|
||||
@@ -16,12 +16,11 @@ import org.koitharu.kotatsu.base.domain.MangaUtils
|
||||
import org.koitharu.kotatsu.base.ui.BaseViewModel
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
|
||||
import org.koitharu.kotatsu.core.exceptions.MangaNotFoundException
|
||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.*
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import org.koitharu.kotatsu.history.domain.PROGRESS_NONE
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.model.MangaPage
|
||||
@@ -33,8 +32,7 @@ import org.koitharu.kotatsu.utils.SingleLiveEvent
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.utils.ext.processLifecycleScope
|
||||
import org.koitharu.kotatsu.utils.ext.setCurrentManga
|
||||
import java.util.*
|
||||
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
|
||||
|
||||
private const val BOUNDS_PAGE_OFFSET = 2
|
||||
private const val PAGES_TRIM_THRESHOLD = 120
|
||||
@@ -46,7 +44,6 @@ class ReaderViewModel(
|
||||
private val preselectedBranch: String?,
|
||||
private val dataRepository: MangaDataRepository,
|
||||
private val historyRepository: HistoryRepository,
|
||||
private val shortcutsRepository: ShortcutsRepository,
|
||||
private val bookmarksRepository: BookmarksRepository,
|
||||
private val settings: AppSettings,
|
||||
private val pageSaveHelper: PageSaveHelper,
|
||||
@@ -73,9 +70,9 @@ class ReaderViewModel(
|
||||
mangaName = manga?.title,
|
||||
chapterName = chapter?.name,
|
||||
chapterNumber = chapter?.number ?: 0,
|
||||
chaptersTotal = chapters.size()
|
||||
chaptersTotal = chapters.size(),
|
||||
)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, null)
|
||||
|
||||
val content = MutableLiveData(ReaderContent(emptyList(), null))
|
||||
val manga: Manga?
|
||||
@@ -84,7 +81,7 @@ class ReaderViewModel(
|
||||
val readerAnimation = settings.observeAsLiveData(
|
||||
context = viewModelScope.coroutineContext + Dispatchers.Default,
|
||||
key = AppSettings.KEY_READER_ANIMATION,
|
||||
valueProducer = { readerAnimation }
|
||||
valueProducer = { readerAnimation },
|
||||
)
|
||||
|
||||
val isScreenshotsBlockEnabled = combine(
|
||||
@@ -93,7 +90,7 @@ class ReaderViewModel(
|
||||
) { manga, policy ->
|
||||
policy == ScreenshotsPolicy.BLOCK_ALL ||
|
||||
(policy == ScreenshotsPolicy.BLOCK_NSFW && manga != null && manga.isNsfw)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, false)
|
||||
|
||||
val onZoomChanged = SingleLiveEvent<Unit>()
|
||||
|
||||
@@ -105,7 +102,7 @@ class ReaderViewModel(
|
||||
bookmarksRepository.observeBookmark(manga, state.chapterId, state.page)
|
||||
.map { it != null }
|
||||
}
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, false)
|
||||
|
||||
init {
|
||||
loadImpl()
|
||||
@@ -127,12 +124,12 @@ class ReaderViewModel(
|
||||
val manga = checkNotNull(mangaData.value)
|
||||
dataRepository.savePreferences(
|
||||
manga = manga,
|
||||
mode = newMode
|
||||
mode = newMode,
|
||||
)
|
||||
readerMode.value = newMode
|
||||
content.value?.run {
|
||||
content.value = copy(
|
||||
state = getCurrentState()
|
||||
state = getCurrentState(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -263,8 +260,7 @@ class ReaderViewModel(
|
||||
|
||||
private fun loadImpl() {
|
||||
loadingJob = launchLoadingJob(Dispatchers.Default) {
|
||||
var manga = dataRepository.resolveIntent(intent) ?: throw MangaNotFoundException("Cannot find manga")
|
||||
ACRA.setCurrentManga(manga)
|
||||
var manga = dataRepository.resolveIntent(intent) ?: throw NotFoundException("Cannot find manga", "")
|
||||
mangaData.value = manga
|
||||
val repo = MangaRepository(manga.source)
|
||||
manga = repo.getDetails(manga)
|
||||
@@ -289,7 +285,6 @@ class ReaderViewModel(
|
||||
currentState.value?.let {
|
||||
val percent = computePercent(it.chapterId, it.page)
|
||||
historyRepository.addOrUpdate(manga, it.chapterId, it.page, it.scroll, percent)
|
||||
shortcutsRepository.updateShortcuts()
|
||||
}
|
||||
|
||||
content.postValue(ReaderContent(pages, currentState.value))
|
||||
@@ -364,7 +359,7 @@ class ReaderViewModel(
|
||||
?: manga.chapters?.randomOrNull()
|
||||
?: error("There are no chapters in this manga")
|
||||
val pages = repo.getPages(chapter)
|
||||
return runCatching {
|
||||
return runCatchingCancellable {
|
||||
val isWebtoon = MangaUtils.determineMangaIsWebtoon(pages)
|
||||
if (isWebtoon) ReaderMode.WEBTOON else defaultMode
|
||||
}.onSuccess {
|
||||
@@ -395,7 +390,7 @@ class ReaderViewModel(
|
||||
*/
|
||||
private fun HistoryRepository.saveStateAsync(manga: Manga, state: ReaderState, percent: Float): Job {
|
||||
return processLifecycleScope.launch(Dispatchers.Default) {
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
addOrUpdate(
|
||||
manga = manga,
|
||||
chapterId = state.chapterId,
|
||||
|
||||
@@ -13,7 +13,7 @@ abstract class BasePageHolder<B : ViewBinding>(
|
||||
protected val binding: B,
|
||||
loader: PageLoader,
|
||||
settings: AppSettings,
|
||||
exceptionResolver: ExceptionResolver
|
||||
exceptionResolver: ExceptionResolver,
|
||||
) : RecyclerView.ViewHolder(binding.root), PageHolderDelegate.Callback {
|
||||
|
||||
@Suppress("LeakingThis")
|
||||
@@ -37,6 +37,14 @@ abstract class BasePageHolder<B : ViewBinding>(
|
||||
|
||||
protected abstract fun onBind(data: ReaderPage)
|
||||
|
||||
@CallSuper
|
||||
open fun onAttachedToWindow() {
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
open fun onDetachedFromWindow() {
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
open fun onRecycled() {
|
||||
delegate.onRecycle()
|
||||
|
||||
@@ -4,12 +4,12 @@ import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.AsyncListDiffer
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.reader.domain.PageLoader
|
||||
import org.koitharu.kotatsu.utils.ext.resetTransformations
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
@Suppress("LeakingThis")
|
||||
abstract class BaseReaderAdapter<H : BasePageHolder<*>>(
|
||||
@@ -35,6 +35,16 @@ abstract class BaseReaderAdapter<H : BasePageHolder<*>>(
|
||||
super.onViewRecycled(holder)
|
||||
}
|
||||
|
||||
override fun onViewAttachedToWindow(holder: H) {
|
||||
super.onViewAttachedToWindow(holder)
|
||||
holder.onAttachedToWindow()
|
||||
}
|
||||
|
||||
override fun onViewDetachedFromWindow(holder: H) {
|
||||
super.onViewDetachedFromWindow(holder)
|
||||
holder.onDetachedFromWindow()
|
||||
}
|
||||
|
||||
open fun getItem(position: Int): ReaderPage = differ.currentList[position]
|
||||
|
||||
open fun getItemOrNull(position: Int) = differ.currentList.getOrNull(position)
|
||||
@@ -45,7 +55,7 @@ abstract class BaseReaderAdapter<H : BasePageHolder<*>>(
|
||||
|
||||
final override fun onCreateViewHolder(
|
||||
parent: ViewGroup,
|
||||
viewType: Int
|
||||
viewType: Int,
|
||||
): H = onCreateViewHolder(parent, loader, settings, exceptionResolver)
|
||||
|
||||
suspend fun setItems(items: List<ReaderPage>) = suspendCoroutine<Unit> { cont ->
|
||||
@@ -58,7 +68,7 @@ abstract class BaseReaderAdapter<H : BasePageHolder<*>>(
|
||||
parent: ViewGroup,
|
||||
loader: PageLoader,
|
||||
settings: AppSettings,
|
||||
exceptionResolver: ExceptionResolver
|
||||
exceptionResolver: ExceptionResolver,
|
||||
): H
|
||||
|
||||
private class DiffCallback : DiffUtil.ItemCallback<ReaderPage>() {
|
||||
@@ -70,6 +80,5 @@ abstract class BaseReaderAdapter<H : BasePageHolder<*>>(
|
||||
override fun areContentsTheSame(oldItem: ReaderPage, newItem: ReaderPage): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user