Compare commits
140 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06ec145802 | ||
|
|
6624778f7f | ||
|
|
1af1f071ad | ||
|
|
f87db4e6d3 | ||
|
|
07bd66fb39 | ||
|
|
4bb0d52217 | ||
|
|
66de4bd49e | ||
|
|
ff12d63696 | ||
|
|
c168a841f3 | ||
|
|
8bfb676e6a | ||
|
|
d5c0ce280e | ||
|
|
b34627c361 | ||
|
|
cbc3be056a | ||
|
|
d9acc4ec18 | ||
|
|
577cc848ee | ||
|
|
8a64c88a07 | ||
|
|
1cd7745e38 | ||
|
|
395b3f7200 | ||
|
|
b8db4c81d8 | ||
|
|
98bd42f3ae | ||
|
|
db8835a7b8 | ||
|
|
afe50a9ed6 | ||
|
|
beba818f57 | ||
|
|
beb17ef442 | ||
|
|
24f1546019 | ||
|
|
1b0fed5c56 | ||
|
|
3d32bd9d58 | ||
|
|
590120433c | ||
|
|
4bd7656681 | ||
|
|
2c7438e64d | ||
|
|
665bebaa7b | ||
|
|
6ed5994726 | ||
|
|
311ed865b7 | ||
|
|
b59fb678fe | ||
|
|
ed9ebdcc55 | ||
|
|
74569615e3 | ||
|
|
f3c320a90f | ||
|
|
a3012ab458 | ||
|
|
6ec58879fd | ||
|
|
571cf08c53 | ||
|
|
fca53eee7a | ||
|
|
ed9e2eb4d2 | ||
|
|
c0e94f8415 | ||
|
|
e172d619a1 | ||
|
|
d6c64fc638 | ||
|
|
37404cb9a6 | ||
|
|
9d5271ff26 | ||
|
|
5f59432e48 | ||
|
|
5c082b5cdb | ||
|
|
32133d3358 | ||
|
|
366e4f0da8 | ||
|
|
3ef033c700 | ||
|
|
bef8e4652f | ||
|
|
8bfdf07a2f | ||
|
|
f3e597275b | ||
|
|
11feaae216 | ||
|
|
fe2c1f9634 | ||
|
|
0c7c6dc48a | ||
|
|
503652f024 | ||
|
|
0c4adc67ea | ||
|
|
c7f5ce30b5 | ||
|
|
59d538824f | ||
|
|
de79f39d16 | ||
|
|
9792da3a5c | ||
|
|
c2407e6e41 | ||
|
|
7321eeaed9 | ||
|
|
9876adf676 | ||
|
|
d29e979fbf | ||
|
|
35baf4b58d | ||
|
|
97524d66f2 | ||
|
|
5b53f8c27d | ||
|
|
d4588570e6 | ||
|
|
cc2f9d4529 | ||
|
|
3def71ccc1 | ||
|
|
b313c64648 | ||
|
|
f7e7c84317 | ||
|
|
ee1c532d53 | ||
|
|
6993cec85e | ||
|
|
0b19f56215 | ||
|
|
817ce7e8df | ||
|
|
2b2498cb38 | ||
|
|
e4efd0f696 | ||
|
|
fbb267e11c | ||
|
|
5740af05fa | ||
|
|
ae2cc1dffc | ||
|
|
a5b9712e9f | ||
|
|
c013e6e4f4 | ||
|
|
0249faa3f6 | ||
|
|
9c52423dc0 | ||
|
|
1f7e5458ae | ||
|
|
b4d487b398 | ||
|
|
0281f1eadb | ||
|
|
1bd9b655f9 | ||
|
|
ed87292921 | ||
|
|
861be7614e | ||
|
|
717fe8748a | ||
|
|
c7a1312cd6 | ||
|
|
b2927854d4 | ||
|
|
cfda150630 | ||
|
|
4fa1382ce9 | ||
|
|
43075c52d1 | ||
|
|
87942747fc | ||
|
|
bb6cd73acd | ||
|
|
6790e5b0d4 | ||
|
|
845c356a73 | ||
|
|
34499ea77d | ||
|
|
6210864280 | ||
|
|
19084419c7 | ||
|
|
84ce4c508c | ||
|
|
0db8fafe61 | ||
|
|
fed241215e | ||
|
|
761f24daf9 | ||
|
|
a435435496 | ||
|
|
81e8c25563 | ||
|
|
e3504c3b1e | ||
|
|
2601c12348 | ||
|
|
138cf44e37 | ||
|
|
65d83e0921 | ||
|
|
6e1cd05fa8 | ||
|
|
8398c01929 | ||
|
|
835c49ae79 | ||
|
|
36065ccf6c | ||
|
|
4ab40566f7 | ||
|
|
bf01a4d1ab | ||
|
|
8dce9dcc3f | ||
|
|
d872044252 | ||
|
|
f4313525c2 | ||
|
|
4eb4ec7de0 | ||
|
|
ecb4dd87d9 | ||
|
|
3d0f5f75cd | ||
|
|
c5462e8454 | ||
|
|
5039e324fb | ||
|
|
b251b3e654 | ||
|
|
5f10070564 | ||
|
|
3da6f80eb6 | ||
|
|
4b2cfdb972 | ||
|
|
51387ace7e | ||
|
|
2bdb83ff28 | ||
|
|
a1b85433ec | ||
|
|
ca5207c658 |
@@ -16,11 +16,12 @@ android {
|
||||
applicationId 'org.koitharu.kotatsu'
|
||||
minSdk = 21
|
||||
targetSdk = 34
|
||||
versionCode = 577
|
||||
versionName = '6.1'
|
||||
versionCode = 594
|
||||
versionName = '6.2.6'
|
||||
generatedDensities = []
|
||||
testInstrumentationRunner "org.koitharu.kotatsu.HiltTestRunner"
|
||||
ksp {
|
||||
// arg("room.generateKotlin", "true") TODO: enable later
|
||||
arg("room.schemaLocation", "$projectDir/schemas")
|
||||
}
|
||||
androidResources {
|
||||
@@ -81,29 +82,29 @@ afterEvaluate {
|
||||
}
|
||||
dependencies {
|
||||
//noinspection GradleDependency
|
||||
implementation('com.github.KotatsuApp:kotatsu-parsers:aae3fa3b05') {
|
||||
implementation('com.github.KotatsuApp:kotatsu-parsers:face1d5b26') {
|
||||
exclude group: 'org.json', module: 'json'
|
||||
}
|
||||
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.10'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.20'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'androidx.core:core-ktx:1.10.1'
|
||||
implementation 'androidx.activity:activity-ktx:1.7.2'
|
||||
implementation 'androidx.core:core-ktx:1.12.0'
|
||||
implementation 'androidx.activity:activity-ktx:1.8.0'
|
||||
implementation 'androidx.fragment:fragment-ktx:1.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-process:2.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
|
||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
|
||||
implementation 'androidx.lifecycle:lifecycle-service:2.6.2'
|
||||
implementation 'androidx.lifecycle:lifecycle-process:2.6.2'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.3.1'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.3.2'
|
||||
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta02'
|
||||
implementation 'androidx.preference:preference-ktx:1.2.1'
|
||||
implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05'
|
||||
implementation 'com.google.android.material:material:1.9.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-common-java8:2.6.1'
|
||||
implementation 'com.google.android.material:material:1.10.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-common-java8:2.6.2'
|
||||
|
||||
// TODO https://issuetracker.google.com/issues/254846063
|
||||
implementation 'androidx.work:work-runtime-ktx:2.8.1'
|
||||
@@ -114,35 +115,35 @@ dependencies {
|
||||
exclude group: 'com.google.j2objc', module: 'j2objc-annotations'
|
||||
}
|
||||
|
||||
implementation 'androidx.room:room-runtime:2.5.2'
|
||||
implementation 'androidx.room:room-ktx:2.5.2'
|
||||
ksp 'androidx.room:room-compiler:2.5.2'
|
||||
implementation 'androidx.room:room-runtime:2.6.0'
|
||||
implementation 'androidx.room:room-ktx:2.6.0'
|
||||
ksp 'androidx.room:room-compiler:2.6.0'
|
||||
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.11.0'
|
||||
implementation 'com.squareup.okio:okio:3.5.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.12.0'
|
||||
implementation 'com.squareup.okio:okio:3.6.0'
|
||||
|
||||
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl:4.3.2'
|
||||
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.3.2'
|
||||
|
||||
implementation 'com.google.dagger:hilt-android:2.47'
|
||||
kapt 'com.google.dagger:hilt-compiler:2.47'
|
||||
implementation 'com.google.dagger:hilt-android:2.48.1'
|
||||
kapt 'com.google.dagger:hilt-compiler:2.48.1'
|
||||
implementation 'androidx.hilt:hilt-work:1.0.0'
|
||||
kapt 'androidx.hilt:hilt-compiler:1.0.0'
|
||||
|
||||
implementation 'io.coil-kt:coil-base:2.4.0'
|
||||
implementation 'io.coil-kt:coil-svg:2.4.0'
|
||||
implementation 'com.github.KotatsuApp:subsampling-scale-image-view:9b1d20be67'
|
||||
implementation 'io.coil-kt:coil-base:2.5.0'
|
||||
implementation 'io.coil-kt:coil-svg:2.5.0'
|
||||
implementation 'com.github.KotatsuApp:subsampling-scale-image-view:cf089a264d'
|
||||
implementation 'com.github.solkin:disk-lru-cache:1.4'
|
||||
implementation 'io.noties.markwon:core:4.6.2'
|
||||
|
||||
implementation 'ch.acra:acra-http:5.11.1'
|
||||
implementation 'ch.acra:acra-dialog:5.11.1'
|
||||
implementation 'ch.acra:acra-http:5.11.3'
|
||||
implementation 'ch.acra:acra-dialog:5.11.3'
|
||||
|
||||
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'org.json:json:20230618'
|
||||
testImplementation 'org.json:json:20231013'
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
|
||||
|
||||
androidTestImplementation 'androidx.test:runner:1.5.2'
|
||||
@@ -152,9 +153,9 @@ dependencies {
|
||||
|
||||
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
|
||||
|
||||
androidTestImplementation 'androidx.room:room-testing:2.5.2'
|
||||
androidTestImplementation 'androidx.room:room-testing:2.6.0'
|
||||
androidTestImplementation 'com.squareup.moshi:moshi-kotlin:1.15.0'
|
||||
|
||||
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.47'
|
||||
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.47'
|
||||
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.48.1'
|
||||
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.48.1'
|
||||
}
|
||||
|
||||
1
app/proguard-rules.pro
vendored
1
app/proguard-rules.pro
vendored
@@ -18,3 +18,4 @@
|
||||
-keep class org.koitharu.kotatsu.core.exceptions.* { *; }
|
||||
-keep class org.koitharu.kotatsu.settings.NotificationSettingsLegacyFragment
|
||||
-keep class org.koitharu.kotatsu.core.prefs.ScreenshotsPolicy { *; }
|
||||
-keep class org.koitharu.kotatsu.settings.backup.PeriodicalBackupSettingsFragment { *; }
|
||||
|
||||
@@ -82,7 +82,7 @@ class AppBackupAgentTest {
|
||||
assertEquals(history, historyRepository.getOne(SampleData.manga))
|
||||
assertEquals(listOf(SampleData.manga), favouritesRepository.getManga(category.id))
|
||||
|
||||
val allTags = database.tagsDao.findTags(SampleData.tag.source.name).toMangaTags()
|
||||
val allTags = database.getTagsDao().findTags(SampleData.tag.source.name).toMangaTags()
|
||||
assertTrue(SampleData.tag in allTags)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
<uses-permission
|
||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="29" />
|
||||
@@ -82,6 +83,16 @@
|
||||
<data android:host="kotatsu.app" />
|
||||
<data android:path="/manga" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
|
||||
<data android:scheme="kotatsu" />
|
||||
<data android:host="manga" />
|
||||
<data android:host="kotatsu.app" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name="org.koitharu.kotatsu.reader.ui.ReaderActivity"
|
||||
@@ -95,7 +106,12 @@
|
||||
android:label="@string/search" />
|
||||
<activity
|
||||
android:name="org.koitharu.kotatsu.search.ui.MangaListActivity"
|
||||
android:label="@string/search_manga" />
|
||||
android:exported="true"
|
||||
android:label="@string/manga_list">
|
||||
<intent-filter>
|
||||
<action android:name="${applicationId}.action.EXPLORE_MANGA" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name="org.koitharu.kotatsu.history.ui.HistoryActivity"
|
||||
android:label="@string/history" />
|
||||
@@ -138,8 +154,8 @@
|
||||
android:windowSoftInputMode="adjustResize" />
|
||||
<activity
|
||||
android:name="org.koitharu.kotatsu.browser.cloudflare.CloudFlareActivity"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
|
||||
android:autoRemoveFromRecents="true"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
|
||||
android:windowSoftInputMode="adjustResize" />
|
||||
<activity
|
||||
android:name="org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity"
|
||||
@@ -314,6 +330,20 @@
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_recent" />
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name="org.koitharu.kotatsu.settings.about.UpdateDownloadReceiver"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name="org.koitharu.kotatsu.core.ErrorReporterReceiver"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="${applicationId}.action.REPORT_ERROR" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<meta-data
|
||||
android:name="android.webkit.WebView.EnableSafeBrowsing"
|
||||
|
||||
@@ -25,15 +25,15 @@ class BookmarksRepository @Inject constructor(
|
||||
) {
|
||||
|
||||
fun observeBookmark(manga: Manga, chapterId: Long, page: Int): Flow<Bookmark?> {
|
||||
return db.bookmarksDao.observe(manga.id, chapterId, page).map { it?.toBookmark(manga) }
|
||||
return db.getBookmarksDao().observe(manga.id, chapterId, page).map { it?.toBookmark(manga) }
|
||||
}
|
||||
|
||||
fun observeBookmarks(manga: Manga): Flow<List<Bookmark>> {
|
||||
return db.bookmarksDao.observe(manga.id).mapItems { it.toBookmark(manga) }
|
||||
return db.getBookmarksDao().observe(manga.id).mapItems { it.toBookmark(manga) }
|
||||
}
|
||||
|
||||
fun observeBookmarks(): Flow<Map<Manga, List<Bookmark>>> {
|
||||
return db.bookmarksDao.observe().map { map ->
|
||||
return db.getBookmarksDao().observe().map { map ->
|
||||
val res = LinkedHashMap<Manga, List<Bookmark>>(map.size)
|
||||
for ((k, v) in map) {
|
||||
val manga = k.toManga()
|
||||
@@ -46,9 +46,9 @@ class BookmarksRepository @Inject constructor(
|
||||
suspend fun addBookmark(bookmark: Bookmark) {
|
||||
db.withTransaction {
|
||||
val tags = bookmark.manga.tags.toEntities()
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(bookmark.manga.toEntity(), tags)
|
||||
db.bookmarksDao.insert(bookmark.toEntity())
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(bookmark.manga.toEntity(), tags)
|
||||
db.getBookmarksDao().insert(bookmark.toEntity())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,11 +56,11 @@ class BookmarksRepository @Inject constructor(
|
||||
val entity = bookmark.toEntity().copy(
|
||||
imageUrl = imageUrl,
|
||||
)
|
||||
db.bookmarksDao.upsert(listOf(entity))
|
||||
db.getBookmarksDao().upsert(listOf(entity))
|
||||
}
|
||||
|
||||
suspend fun removeBookmark(mangaId: Long, chapterId: Long, page: Int) {
|
||||
check(db.bookmarksDao.delete(mangaId, chapterId, page) != 0) {
|
||||
check(db.getBookmarksDao().delete(mangaId, chapterId, page) != 0) {
|
||||
"Bookmark not found"
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ class BookmarksRepository @Inject constructor(
|
||||
suspend fun removeBookmarks(ids: Set<Long>): ReversibleHandle {
|
||||
val entities = ArrayList<BookmarkEntity>(ids.size)
|
||||
db.withTransaction {
|
||||
val dao = db.bookmarksDao
|
||||
val dao = db.getBookmarksDao()
|
||||
for (pageId in ids) {
|
||||
val e = dao.find(pageId)
|
||||
if (e != null) {
|
||||
@@ -92,7 +92,7 @@ class BookmarksRepository @Inject constructor(
|
||||
db.withTransaction {
|
||||
for (e in entities) {
|
||||
try {
|
||||
db.bookmarksDao.insert(e)
|
||||
db.getBookmarksDao().insert(e)
|
||||
} catch (e: SQLException) {
|
||||
e.printStackTraceDebug()
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ class BookmarksActivity :
|
||||
val fm = supportFragmentManager
|
||||
if (fm.findFragmentById(R.id.container) == null) {
|
||||
fm.commit {
|
||||
val fragment = BookmarksFragment.newInstance()
|
||||
replace(R.id.container, fragment)
|
||||
setReorderingAllowed(true)
|
||||
replace(R.id.container, BookmarksFragment::class.java, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import androidx.core.view.updatePadding
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import coil.ImageLoader
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
@@ -25,8 +24,7 @@ import org.koitharu.kotatsu.core.ui.BaseFragment
|
||||
import org.koitharu.kotatsu.core.ui.list.ListSelectionController
|
||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.core.ui.list.fastscroll.FastScroller
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleAction
|
||||
import org.koitharu.kotatsu.core.ui.util.reverseAsync
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleActionObserver
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||
import org.koitharu.kotatsu.databinding.FragmentListSimpleBinding
|
||||
@@ -38,7 +36,6 @@ import org.koitharu.kotatsu.list.ui.adapter.ListStateHolderListener
|
||||
import org.koitharu.kotatsu.list.ui.adapter.TypedListSpacingDecoration
|
||||
import org.koitharu.kotatsu.list.ui.model.ListHeader
|
||||
import org.koitharu.kotatsu.main.ui.owners.AppBarOwner
|
||||
import org.koitharu.kotatsu.main.ui.owners.SnackbarOwner
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.reader.ui.ReaderActivity
|
||||
import javax.inject.Inject
|
||||
@@ -105,7 +102,7 @@ class BookmarksFragment :
|
||||
viewLifecycleOwner,
|
||||
SnackbarErrorObserver(binding.recyclerView, this)
|
||||
)
|
||||
viewModel.onActionDone.observeEvent(viewLifecycleOwner, ::onActionDone)
|
||||
viewModel.onActionDone.observeEvent(viewLifecycleOwner, ReversibleActionObserver(binding.recyclerView))
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
@@ -184,17 +181,6 @@ class BookmarksFragment :
|
||||
}
|
||||
}
|
||||
|
||||
private fun onActionDone(action: ReversibleAction) {
|
||||
val handle = action.handle
|
||||
val length = if (handle == null) Snackbar.LENGTH_SHORT else Snackbar.LENGTH_LONG
|
||||
val snackbar =
|
||||
Snackbar.make((activity as SnackbarOwner).snackbarHost, action.stringResId, length)
|
||||
if (handle != null) {
|
||||
snackbar.setAction(R.string.undo) { handle.reverseAsync() }
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
|
||||
private inner class SpanSizeLookup : GridLayoutManager.SpanSizeLookup(), Runnable {
|
||||
|
||||
init {
|
||||
|
||||
@@ -35,6 +35,7 @@ class BookmarksSheetViewModel @Inject constructor(
|
||||
|
||||
val content: StateFlow<List<ListModel>> = bookmarksRepository.observeBookmarks(manga)
|
||||
.map { mapList(it) }
|
||||
.withErrorHandling()
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Lazily, listOf(LoadingFooter()))
|
||||
|
||||
private suspend fun mapList(bookmarks: List<Bookmark>): List<ListModel> {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.koitharu.kotatsu.core
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.app.PendingIntentCompat
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.util.ext.getSerializableExtraCompat
|
||||
import org.koitharu.kotatsu.core.util.ext.report
|
||||
|
||||
class ErrorReporterReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
val e = intent?.getSerializableExtraCompat<Throwable>(EXTRA_ERROR) ?: return
|
||||
e.report()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val EXTRA_ERROR = "err"
|
||||
private const val ACTION_REPORT = "${BuildConfig.APPLICATION_ID}.action.REPORT_ERROR"
|
||||
|
||||
fun getPendingIntent(context: Context, e: Throwable): PendingIntent {
|
||||
val intent = Intent(context, ErrorReporterReceiver::class.java)
|
||||
intent.setAction(ACTION_REPORT)
|
||||
intent.setData(Uri.parse("err://${e.hashCode()}"))
|
||||
intent.putExtra(EXTRA_ERROR, e)
|
||||
return checkNotNull(PendingIntentCompat.getBroadcast(context, 0, intent, 0, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ class BackupRepository @Inject constructor(
|
||||
var offset = 0
|
||||
val entry = BackupEntry(BackupEntry.HISTORY, JSONArray())
|
||||
while (true) {
|
||||
val history = db.historyDao.findAll(offset, PAGE_SIZE)
|
||||
val history = db.getHistoryDao().findAll(offset, PAGE_SIZE)
|
||||
if (history.isEmpty()) {
|
||||
break
|
||||
}
|
||||
@@ -42,7 +42,7 @@ class BackupRepository @Inject constructor(
|
||||
|
||||
suspend fun dumpCategories(): BackupEntry {
|
||||
val entry = BackupEntry(BackupEntry.CATEGORIES, JSONArray())
|
||||
val categories = db.favouriteCategoriesDao.findAll()
|
||||
val categories = db.getFavouriteCategoriesDao().findAll()
|
||||
for (item in categories) {
|
||||
entry.data.put(JsonSerializer(item).toJson())
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class BackupRepository @Inject constructor(
|
||||
var offset = 0
|
||||
val entry = BackupEntry(BackupEntry.FAVOURITES, JSONArray())
|
||||
while (true) {
|
||||
val favourites = db.favouritesDao.findAll(offset, PAGE_SIZE)
|
||||
val favourites = db.getFavouritesDao().findAll(offset, PAGE_SIZE)
|
||||
if (favourites.isEmpty()) {
|
||||
break
|
||||
}
|
||||
@@ -73,7 +73,7 @@ class BackupRepository @Inject constructor(
|
||||
|
||||
suspend fun dumpBookmarks(): BackupEntry {
|
||||
val entry = BackupEntry(BackupEntry.BOOKMARKS, JSONArray())
|
||||
val all = db.bookmarksDao.findAll()
|
||||
val all = db.getBookmarksDao().findAll()
|
||||
for ((m, b) in all) {
|
||||
val json = JSONObject()
|
||||
val manga = JsonSerializer(m.manga).toJson()
|
||||
@@ -122,9 +122,9 @@ class BackupRepository @Inject constructor(
|
||||
val history = JsonDeserializer(item).toHistoryEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.historyDao.upsert(history)
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga, tags)
|
||||
db.getHistoryDao().upsert(history)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,7 +136,7 @@ class BackupRepository @Inject constructor(
|
||||
for (item in entry.data.JSONIterator()) {
|
||||
val category = JsonDeserializer(item).toFavouriteCategoryEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.favouriteCategoriesDao.upsert(category)
|
||||
db.getFavouriteCategoriesDao().upsert(category)
|
||||
}
|
||||
}
|
||||
return result
|
||||
@@ -153,9 +153,9 @@ class BackupRepository @Inject constructor(
|
||||
val favourite = JsonDeserializer(item).toFavouriteEntity()
|
||||
result += runCatchingCancellable {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.favouritesDao.upsert(favourite)
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga, tags)
|
||||
db.getFavouritesDao().upsert(favourite)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,9 +175,9 @@ class BackupRepository @Inject constructor(
|
||||
}
|
||||
result += runCatchingCancellable {
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga, tags)
|
||||
db.bookmarksDao.upsert(bookmarks)
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga, tags)
|
||||
db.getBookmarksDao().upsert(bookmarks)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class BackupZipOutput(val file: File) : Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private const val DIR_BACKUPS = "backups"
|
||||
const val DIR_BACKUPS = "backups"
|
||||
|
||||
suspend fun BackupZipOutput(context: Context): BackupZipOutput = runInterruptible(Dispatchers.IO) {
|
||||
val dir = context.run {
|
||||
|
||||
@@ -66,29 +66,29 @@ const val DATABASE_VERSION = 17
|
||||
)
|
||||
abstract class MangaDatabase : RoomDatabase() {
|
||||
|
||||
abstract val historyDao: HistoryDao
|
||||
abstract fun getHistoryDao(): HistoryDao
|
||||
|
||||
abstract val tagsDao: TagsDao
|
||||
abstract fun getTagsDao(): TagsDao
|
||||
|
||||
abstract val mangaDao: MangaDao
|
||||
abstract fun getMangaDao(): MangaDao
|
||||
|
||||
abstract val favouritesDao: FavouritesDao
|
||||
abstract fun getFavouritesDao(): FavouritesDao
|
||||
|
||||
abstract val preferencesDao: PreferencesDao
|
||||
abstract fun getPreferencesDao(): PreferencesDao
|
||||
|
||||
abstract val favouriteCategoriesDao: FavouriteCategoriesDao
|
||||
abstract fun getFavouriteCategoriesDao(): FavouriteCategoriesDao
|
||||
|
||||
abstract val tracksDao: TracksDao
|
||||
abstract fun getTracksDao(): TracksDao
|
||||
|
||||
abstract val trackLogsDao: TrackLogsDao
|
||||
abstract fun getTrackLogsDao(): TrackLogsDao
|
||||
|
||||
abstract val suggestionDao: SuggestionDao
|
||||
abstract fun getSuggestionDao(): SuggestionDao
|
||||
|
||||
abstract val bookmarksDao: BookmarksDao
|
||||
abstract fun getBookmarksDao(): BookmarksDao
|
||||
|
||||
abstract val scrobblingDao: ScrobblingDao
|
||||
abstract fun getScrobblingDao(): ScrobblingDao
|
||||
|
||||
abstract val sourcesDao: MangaSourcesDao
|
||||
abstract fun getSourcesDao(): MangaSourcesDao
|
||||
}
|
||||
|
||||
fun getDatabaseMigrations(context: Context): Array<Migration> = arrayOf(
|
||||
|
||||
@@ -51,6 +51,28 @@ abstract class TagsDao {
|
||||
)
|
||||
abstract suspend fun findTags(query: String, limit: Int): List<TagEntity>
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT tags.* FROM manga_tags
|
||||
LEFT JOIN tags ON tags.tag_id = manga_tags.tag_id
|
||||
WHERE manga_tags.manga_id IN (SELECT manga_id FROM manga_tags WHERE tag_id = :tagId)
|
||||
GROUP BY tags.tag_id
|
||||
ORDER BY COUNT(manga_id) DESC;
|
||||
""",
|
||||
)
|
||||
abstract suspend fun findRelatedTags(tagId: Long): List<TagEntity>
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT tags.* FROM manga_tags
|
||||
LEFT JOIN tags ON tags.tag_id = manga_tags.tag_id
|
||||
WHERE manga_tags.manga_id IN (SELECT manga_id FROM manga_tags WHERE tag_id IN (:ids))
|
||||
GROUP BY tags.tag_id
|
||||
ORDER BY COUNT(manga_id) DESC;
|
||||
""",
|
||||
)
|
||||
abstract suspend fun findRelatedTags(ids: Set<Long>): List<TagEntity>
|
||||
|
||||
@Upsert
|
||||
abstract suspend fun upsert(tags: Iterable<TagEntity>)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ fun TagEntity.toMangaTag() = MangaTag(
|
||||
|
||||
fun Collection<TagEntity>.toMangaTags() = mapToSet(TagEntity::toMangaTag)
|
||||
|
||||
fun Collection<TagEntity>.toMangaTagsList() = map(TagEntity::toMangaTag)
|
||||
|
||||
fun MangaEntity.toManga(tags: Set<MangaTag>) = Manga(
|
||||
id = this.id,
|
||||
title = this.title,
|
||||
|
||||
@@ -5,8 +5,8 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration10To11 : Migration(10, 11) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL(
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `bookmarks` (
|
||||
`manga_id` INTEGER NOT NULL,
|
||||
@@ -20,7 +20,7 @@ class Migration10To11 : Migration(10, 11) {
|
||||
FOREIGN KEY(`manga_id`) REFERENCES `manga`(`manga_id`) ON UPDATE NO ACTION ON DELETE CASCADE )
|
||||
""".trimIndent()
|
||||
)
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS `index_bookmarks_manga_id` ON `bookmarks` (`manga_id`)")
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS `index_bookmarks_page_id` ON `bookmarks` (`page_id`)")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS `index_bookmarks_manga_id` ON `bookmarks` (`manga_id`)")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS `index_bookmarks_page_id` ON `bookmarks` (`page_id`)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration11To12 : Migration(11, 12) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL(
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `scrobblings` (
|
||||
`scrobbler` INTEGER NOT NULL,
|
||||
@@ -21,7 +21,7 @@ class Migration11To12 : Migration(11, 12) {
|
||||
)
|
||||
""".trimIndent()
|
||||
)
|
||||
database.execSQL("ALTER TABLE history ADD COLUMN `percent` REAL NOT NULL DEFAULT -1")
|
||||
database.execSQL("ALTER TABLE bookmarks ADD COLUMN `percent` REAL NOT NULL DEFAULT -1")
|
||||
db.execSQL("ALTER TABLE history ADD COLUMN `percent` REAL NOT NULL DEFAULT -1")
|
||||
db.execSQL("ALTER TABLE bookmarks ADD COLUMN `percent` REAL NOT NULL DEFAULT -1")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration12To13 : Migration(12, 13) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE favourite_categories ADD COLUMN `show_in_lib` INTEGER NOT NULL DEFAULT 1")
|
||||
database.execSQL("ALTER TABLE favourites ADD COLUMN `sort_key` INTEGER NOT NULL DEFAULT 0")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE favourite_categories ADD COLUMN `show_in_lib` INTEGER NOT NULL DEFAULT 1")
|
||||
db.execSQL("ALTER TABLE favourites ADD COLUMN `sort_key` INTEGER NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration13To14 : Migration(13, 14) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE favourite_categories ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
database.execSQL("ALTER TABLE favourites ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
database.execSQL("ALTER TABLE history ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
database.execSQL("ALTER TABLE preferences ADD COLUMN `cf_brightness` REAL NOT NULL DEFAULT 0")
|
||||
database.execSQL("ALTER TABLE preferences ADD COLUMN `cf_contrast` REAL NOT NULL DEFAULT 0")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE favourite_categories ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE favourites ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE history ADD COLUMN `deleted_at` INTEGER NOT NULL DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE preferences ADD COLUMN `cf_brightness` REAL NOT NULL DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE preferences ADD COLUMN `cf_contrast` REAL NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,5 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration14To15 : Migration(14, 15) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) = Unit
|
||||
override fun migrate(db: SupportSQLiteDatabase) = Unit
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration15To16 : Migration(15, 16) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE preferences ADD COLUMN `cf_invert` INTEGER NOT NULL DEFAULT 0")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE preferences ADD COLUMN `cf_invert` INTEGER NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ class Migration16To17(context: Context) : Migration(16, 17) {
|
||||
|
||||
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("CREATE TABLE `sources` (`source` TEXT NOT NULL, `enabled` INTEGER NOT NULL, `sort_key` INTEGER NOT NULL, PRIMARY KEY(`source`))")
|
||||
database.execSQL("CREATE INDEX `index_sources_sort_key` ON `sources` (`sort_key`)")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE `sources` (`source` TEXT NOT NULL, `enabled` INTEGER NOT NULL, `sort_key` INTEGER NOT NULL, PRIMARY KEY(`source`))")
|
||||
db.execSQL("CREATE INDEX `index_sources_sort_key` ON `sources` (`sort_key`)")
|
||||
val hiddenSources = prefs.getStringSet("sources_hidden", null).orEmpty()
|
||||
val order = prefs.getString("sources_order_2", null)?.split('|').orEmpty()
|
||||
val sources = MangaSource.entries
|
||||
@@ -30,7 +30,7 @@ class Migration16To17(context: Context) : Migration(16, 17) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"INSERT INTO `sources` (`source`, `enabled`, `sort_key`) VALUES (?, ?, ?)",
|
||||
arrayOf(name, (!isHidden).toInt(), sortKey),
|
||||
)
|
||||
|
||||
@@ -7,48 +7,48 @@ class Migration1To2 : Migration(1, 2) {
|
||||
/**
|
||||
* Adding foreign keys
|
||||
*/
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
/* manga_tags */
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS manga_tags_tmp (manga_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, " +
|
||||
"PRIMARY KEY(manga_id, tag_id), " +
|
||||
"FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE, " +
|
||||
"FOREIGN KEY(tag_id) REFERENCES tags(tag_id) ON UPDATE NO ACTION ON DELETE CASCADE )"
|
||||
)
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_manga_tags_manga_id ON manga_tags_tmp (manga_id)")
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_manga_tags_tag_id ON manga_tags_tmp (tag_id)")
|
||||
database.execSQL("INSERT INTO manga_tags_tmp (manga_id, tag_id) SELECT manga_id, tag_id FROM manga_tags")
|
||||
database.execSQL("DROP TABLE manga_tags")
|
||||
database.execSQL("ALTER TABLE manga_tags_tmp RENAME TO manga_tags")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_manga_tags_manga_id ON manga_tags_tmp (manga_id)")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_manga_tags_tag_id ON manga_tags_tmp (tag_id)")
|
||||
db.execSQL("INSERT INTO manga_tags_tmp (manga_id, tag_id) SELECT manga_id, tag_id FROM manga_tags")
|
||||
db.execSQL("DROP TABLE manga_tags")
|
||||
db.execSQL("ALTER TABLE manga_tags_tmp RENAME TO manga_tags")
|
||||
/* favourites */
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS favourites_tmp (manga_id INTEGER NOT NULL, category_id INTEGER NOT NULL, created_at INTEGER NOT NULL, " +
|
||||
"PRIMARY KEY(manga_id, category_id), " +
|
||||
"FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE , " +
|
||||
"FOREIGN KEY(category_id) REFERENCES favourite_categories(category_id) ON UPDATE NO ACTION ON DELETE CASCADE )"
|
||||
)
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_favourites_manga_id ON favourites_tmp (manga_id)")
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_favourites_category_id ON favourites_tmp (category_id)")
|
||||
database.execSQL("INSERT INTO favourites_tmp (manga_id, category_id, created_at) SELECT manga_id, category_id, created_at FROM favourites")
|
||||
database.execSQL("DROP TABLE favourites")
|
||||
database.execSQL("ALTER TABLE favourites_tmp RENAME TO favourites")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_favourites_manga_id ON favourites_tmp (manga_id)")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_favourites_category_id ON favourites_tmp (category_id)")
|
||||
db.execSQL("INSERT INTO favourites_tmp (manga_id, category_id, created_at) SELECT manga_id, category_id, created_at FROM favourites")
|
||||
db.execSQL("DROP TABLE favourites")
|
||||
db.execSQL("ALTER TABLE favourites_tmp RENAME TO favourites")
|
||||
/* history */
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS history_tmp (manga_id INTEGER NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, chapter_id INTEGER NOT NULL, page INTEGER NOT NULL, " +
|
||||
"PRIMARY KEY(manga_id), " +
|
||||
"FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )"
|
||||
)
|
||||
database.execSQL("INSERT INTO history_tmp (manga_id, created_at, updated_at, chapter_id, page) SELECT manga_id, created_at, updated_at, chapter_id, page FROM history")
|
||||
database.execSQL("DROP TABLE history")
|
||||
database.execSQL("ALTER TABLE history_tmp RENAME TO history")
|
||||
db.execSQL("INSERT INTO history_tmp (manga_id, created_at, updated_at, chapter_id, page) SELECT manga_id, created_at, updated_at, chapter_id, page FROM history")
|
||||
db.execSQL("DROP TABLE history")
|
||||
db.execSQL("ALTER TABLE history_tmp RENAME TO history")
|
||||
/* preferences */
|
||||
database.execSQL(
|
||||
db.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS preferences_tmp (manga_id INTEGER NOT NULL, mode INTEGER NOT NULL," +
|
||||
" PRIMARY KEY(manga_id), " +
|
||||
"FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )"
|
||||
)
|
||||
database.execSQL("INSERT INTO preferences_tmp (manga_id, mode) SELECT manga_id, mode FROM preferences")
|
||||
database.execSQL("DROP TABLE preferences")
|
||||
database.execSQL("ALTER TABLE preferences_tmp RENAME TO preferences")
|
||||
db.execSQL("INSERT INTO preferences_tmp (manga_id, mode) SELECT manga_id, mode FROM preferences")
|
||||
db.execSQL("DROP TABLE preferences")
|
||||
db.execSQL("ALTER TABLE preferences_tmp RENAME TO preferences")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration2To3 : Migration(2, 3) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE history ADD COLUMN scroll REAL NOT NULL DEFAULT 0")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE history ADD COLUMN scroll REAL NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration3To4 : Migration(3, 4) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS tracks (manga_id INTEGER NOT NULL, chapters_total INTEGER NOT NULL, last_chapter_id INTEGER NOT NULL, chapters_new INTEGER NOT NULL, last_check INTEGER NOT NULL, last_notified_id INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS tracks (manga_id INTEGER NOT NULL, chapters_total INTEGER NOT NULL, last_chapter_id INTEGER NOT NULL, chapters_new INTEGER NOT NULL, last_check INTEGER NOT NULL, last_notified_id INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration4To5 : Migration(4, 5) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE favourite_categories ADD COLUMN sort_key INTEGER NOT NULL DEFAULT 0")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE favourite_categories ADD COLUMN sort_key INTEGER NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration5To6 : Migration(5, 6) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS track_logs (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, manga_id INTEGER NOT NULL, chapters TEXT NOT NULL, created_at INTEGER NOT NULL, FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE)")
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_track_logs_manga_id ON track_logs (manga_id)")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS track_logs (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, manga_id INTEGER NOT NULL, chapters TEXT NOT NULL, created_at INTEGER NOT NULL, FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE)")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_track_logs_manga_id ON track_logs (manga_id)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration6To7 : Migration(6, 7) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE manga ADD COLUMN public_url TEXT NOT NULL DEFAULT ''")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE manga ADD COLUMN public_url TEXT NOT NULL DEFAULT ''")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration7To8 : Migration(7, 8) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE manga ADD COLUMN nsfw INTEGER NOT NULL DEFAULT 0")
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS suggestions (manga_id INTEGER NOT NULL, relevance REAL NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
|
||||
database.execSQL("CREATE INDEX IF NOT EXISTS index_suggestions_manga_id ON suggestions (manga_id)")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE manga ADD COLUMN nsfw INTEGER NOT NULL DEFAULT 0")
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS suggestions (manga_id INTEGER NOT NULL, relevance REAL NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS index_suggestions_manga_id ON suggestions (manga_id)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
|
||||
class Migration8To9 : Migration(8, 9) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE favourite_categories ADD COLUMN `order` TEXT NOT NULL DEFAULT ${SortOrder.NEWEST.name}")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE favourite_categories ADD COLUMN `order` TEXT NOT NULL DEFAULT ${SortOrder.NEWEST.name}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration9To10 : Migration(9, 10) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE favourite_categories ADD COLUMN `track` INTEGER NOT NULL DEFAULT 1")
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE favourite_categories ADD COLUMN `track` INTEGER NOT NULL DEFAULT 1")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package org.koitharu.kotatsu.core.model
|
||||
|
||||
import android.os.Parcelable
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import java.util.Date
|
||||
|
||||
@Parcelize
|
||||
@@ -12,7 +12,7 @@ data class FavouriteCategory(
|
||||
val id: Long,
|
||||
val title: String,
|
||||
val sortKey: Int,
|
||||
val order: SortOrder,
|
||||
val order: ListSortOrder,
|
||||
val createdAt: Date,
|
||||
val isTrackingEnabled: Boolean,
|
||||
val isVisibleInLibrary: Boolean,
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.network
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import okio.IOException
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders.CONTENT_ENCODING
|
||||
|
||||
class GZipInterceptor : Interceptor {
|
||||
@@ -9,6 +10,10 @@ class GZipInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val newRequest = chain.request().newBuilder()
|
||||
newRequest.addHeader(CONTENT_ENCODING, "gzip")
|
||||
return chain.proceed(newRequest.build())
|
||||
return try {
|
||||
chain.proceed(newRequest.build())
|
||||
} catch (e: NullPointerException) {
|
||||
throw IOException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.koitharu.kotatsu.core.network
|
||||
|
||||
import androidx.collection.ArraySet
|
||||
import dagger.Lazy
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
@@ -13,6 +16,7 @@ import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import java.util.EnumMap
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -22,9 +26,15 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
private val settings: AppSettings,
|
||||
) : Interceptor {
|
||||
|
||||
private val locks = EnumMap<MangaSource, Any>(MangaSource::class.java)
|
||||
private val blacklist = EnumMap<MangaSource, MutableSet<String>>(MangaSource::class.java)
|
||||
|
||||
val isEnabled: Boolean
|
||||
get() = settings.isMirrorSwitchingAvailable
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
if (!settings.isMirrorSwitchingAvailable) {
|
||||
if (!isEnabled) {
|
||||
return chain.proceed(request)
|
||||
}
|
||||
return try {
|
||||
@@ -43,6 +53,30 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun trySwitchMirror(repository: RemoteMangaRepository): Boolean = runInterruptible(Dispatchers.Default) {
|
||||
if (!isEnabled) {
|
||||
return@runInterruptible false
|
||||
}
|
||||
val mirrors = repository.getAvailableMirrors()
|
||||
if (mirrors.size <= 1) {
|
||||
return@runInterruptible false
|
||||
}
|
||||
synchronized(obtainLock(repository.source)) {
|
||||
val currentMirror = repository.domain
|
||||
addToBlacklist(repository.source, currentMirror)
|
||||
val newMirror = mirrors.firstOrNull { x ->
|
||||
x != currentMirror && !isBlacklisted(repository.source, x)
|
||||
} ?: return@synchronized false
|
||||
repository.domain = newMirror
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun rollback(repository: RemoteMangaRepository, oldMirror: String) = synchronized(obtainLock(repository.source)) {
|
||||
blacklist[repository.source]?.remove(oldMirror)
|
||||
repository.domain = oldMirror
|
||||
}
|
||||
|
||||
private fun trySwitchMirror(request: Request, chain: Interceptor.Chain): Response? {
|
||||
val source = request.tag(MangaSource::class.java) ?: return null
|
||||
val repository = mangaRepositoryFactoryLazy.get().create(source) as? RemoteMangaRepository ?: return null
|
||||
@@ -50,7 +84,9 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
if (mirrors.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
return tryMirrors(repository, mirrors, chain, request)
|
||||
return synchronized(obtainLock(repository.source)) {
|
||||
tryMirrors(repository, mirrors, chain, request)
|
||||
}
|
||||
}
|
||||
|
||||
private fun tryMirrors(
|
||||
@@ -66,7 +102,7 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
}
|
||||
val urlBuilder = url.newBuilder()
|
||||
for (mirror in mirrors) {
|
||||
if (mirror == currentDomain) {
|
||||
if (mirror == currentDomain || isBlacklisted(repository.source, mirror)) {
|
||||
continue
|
||||
}
|
||||
val newHost = hostOf(url.host, mirror) ?: continue
|
||||
@@ -75,6 +111,7 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
.build()
|
||||
val response = chain.proceed(newRequest)
|
||||
if (response.isFailed) {
|
||||
addToBlacklist(repository.source, mirror)
|
||||
response.closeQuietly()
|
||||
} else {
|
||||
repository.domain = mirror
|
||||
@@ -104,4 +141,18 @@ class MirrorSwitchInterceptor @Inject constructor(
|
||||
private fun ResponseBody.copy(): ResponseBody {
|
||||
return source().readByteArray().toResponseBody(contentType())
|
||||
}
|
||||
|
||||
private fun obtainLock(source: MangaSource): Any = locks.getOrPut(source) {
|
||||
Any()
|
||||
}
|
||||
|
||||
private fun isBlacklisted(source: MangaSource, domain: String): Boolean {
|
||||
return blacklist[source]?.contains(domain) == true
|
||||
}
|
||||
|
||||
private fun addToBlacklist(source: MangaSource, domain: String) {
|
||||
blacklist.getOrPut(source) {
|
||||
ArraySet(2)
|
||||
}.add(domain)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import kotlinx.coroutines.launch
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.db.TABLE_HISTORY
|
||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.favicon.faviconUri
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.ui.image.ThumbnailTransformation
|
||||
import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow
|
||||
@@ -29,8 +30,10 @@ import org.koitharu.kotatsu.core.util.ext.processLifecycleScope
|
||||
import org.koitharu.kotatsu.core.util.ext.source
|
||||
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
import org.koitharu.kotatsu.reader.ui.ReaderActivity
|
||||
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -73,8 +76,18 @@ class AppShortcutManager @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun requestPinShortcut(manga: Manga): Boolean {
|
||||
return ShortcutManagerCompat.requestPinShortcut(context, buildShortcutInfo(manga), null)
|
||||
suspend fun requestPinShortcut(manga: Manga): Boolean = try {
|
||||
ShortcutManagerCompat.requestPinShortcut(context, buildShortcutInfo(manga), null)
|
||||
} catch (e: IllegalStateException) {
|
||||
e.printStackTraceDebug()
|
||||
false
|
||||
}
|
||||
|
||||
suspend fun requestPinShortcut(source: MangaSource): Boolean = try {
|
||||
ShortcutManagerCompat.requestPinShortcut(context, buildShortcutInfo(source), null)
|
||||
} catch (e: IllegalStateException) {
|
||||
e.printStackTraceDebug()
|
||||
false
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@@ -86,6 +99,11 @@ class AppShortcutManager @Inject constructor(
|
||||
ShortcutManagerCompat.reportShortcutUsed(context, mangaId.toString())
|
||||
}
|
||||
|
||||
fun isDynamicShortcutsAvailable(): Boolean {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 &&
|
||||
context.getSystemService(ShortcutManager::class.java).maxShortcutCountPerActivity > 0
|
||||
}
|
||||
|
||||
private suspend fun updateShortcutsImpl() = runCatchingCancellable {
|
||||
val maxShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context).coerceAtLeast(5)
|
||||
val shortcuts = historyRepository.getList(0, maxShortcuts)
|
||||
@@ -132,8 +150,25 @@ class AppShortcutManager @Inject constructor(
|
||||
.build()
|
||||
}
|
||||
|
||||
fun isDynamicShortcutsAvailable(): Boolean {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 &&
|
||||
context.getSystemService(ShortcutManager::class.java).maxShortcutCountPerActivity > 0
|
||||
private suspend fun buildShortcutInfo(source: MangaSource): ShortcutInfoCompat {
|
||||
val icon = runCatchingCancellable {
|
||||
coil.execute(
|
||||
ImageRequest.Builder(context)
|
||||
.data(source.faviconUri())
|
||||
.size(iconSize)
|
||||
.scale(Scale.FIT)
|
||||
.build(),
|
||||
).getDrawableOrThrow().toBitmap()
|
||||
}.fold(
|
||||
onSuccess = { IconCompat.createWithAdaptiveBitmap(it) },
|
||||
onFailure = { IconCompat.createWithResource(context, R.drawable.ic_shortcut_default) },
|
||||
)
|
||||
return ShortcutInfoCompat.Builder(context, source.name)
|
||||
.setShortLabel(source.title)
|
||||
.setLongLabel(source.title)
|
||||
.setIcon(icon)
|
||||
.setLongLived(true)
|
||||
.setIntent(MangaListActivity.newIntent(context, source))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,16 +28,16 @@ class MangaDataRepository @Inject constructor(
|
||||
suspend fun saveReaderMode(manga: Manga, mode: ReaderMode) {
|
||||
db.withTransaction {
|
||||
storeManga(manga)
|
||||
val entity = db.preferencesDao.find(manga.id) ?: newEntity(manga.id)
|
||||
db.preferencesDao.upsert(entity.copy(mode = mode.id))
|
||||
val entity = db.getPreferencesDao().find(manga.id) ?: newEntity(manga.id)
|
||||
db.getPreferencesDao().upsert(entity.copy(mode = mode.id))
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveColorFilter(manga: Manga, colorFilter: ReaderColorFilter?) {
|
||||
db.withTransaction {
|
||||
storeManga(manga)
|
||||
val entity = db.preferencesDao.find(manga.id) ?: newEntity(manga.id)
|
||||
db.preferencesDao.upsert(
|
||||
val entity = db.getPreferencesDao().find(manga.id) ?: newEntity(manga.id)
|
||||
db.getPreferencesDao().upsert(
|
||||
entity.copy(
|
||||
cfBrightness = colorFilter?.brightness ?: 0f,
|
||||
cfContrast = colorFilter?.contrast ?: 0f,
|
||||
@@ -48,25 +48,25 @@ class MangaDataRepository @Inject constructor(
|
||||
}
|
||||
|
||||
suspend fun getReaderMode(mangaId: Long): ReaderMode? {
|
||||
return db.preferencesDao.find(mangaId)?.let { ReaderMode.valueOf(it.mode) }
|
||||
return db.getPreferencesDao().find(mangaId)?.let { ReaderMode.valueOf(it.mode) }
|
||||
}
|
||||
|
||||
suspend fun getColorFilter(mangaId: Long): ReaderColorFilter? {
|
||||
return db.preferencesDao.find(mangaId)?.getColorFilterOrNull()
|
||||
return db.getPreferencesDao().find(mangaId)?.getColorFilterOrNull()
|
||||
}
|
||||
|
||||
fun observeColorFilter(mangaId: Long): Flow<ReaderColorFilter?> {
|
||||
return db.preferencesDao.observe(mangaId)
|
||||
return db.getPreferencesDao().observe(mangaId)
|
||||
.map { it?.getColorFilterOrNull() }
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
|
||||
suspend fun findMangaById(mangaId: Long): Manga? {
|
||||
return db.mangaDao.find(mangaId)?.toManga()
|
||||
return db.getMangaDao().find(mangaId)?.toManga()
|
||||
}
|
||||
|
||||
suspend fun findMangaByPublicUrl(publicUrl: String): Manga? {
|
||||
return db.mangaDao.findByPublicUrl(publicUrl)?.toManga()
|
||||
return db.getMangaDao().findByPublicUrl(publicUrl)?.toManga()
|
||||
}
|
||||
|
||||
suspend fun resolveIntent(intent: MangaIntent): Manga? = when {
|
||||
@@ -79,13 +79,13 @@ class MangaDataRepository @Inject constructor(
|
||||
suspend fun storeManga(manga: Manga) {
|
||||
val tags = manga.tags.toEntities()
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga.toEntity(), tags)
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga.toEntity(), tags)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun findTags(source: MangaSource): Set<MangaTag> {
|
||||
return db.tagsDao.findTags(source.name).toMangaTags()
|
||||
return db.getTagsDao().findTags(source.name).toMangaTags()
|
||||
}
|
||||
|
||||
private fun MangaPrefsEntity.getColorFilterOrNull(): ReaderColorFilter? {
|
||||
|
||||
@@ -43,5 +43,7 @@ class MangaIntent private constructor(
|
||||
|
||||
const val KEY_MANGA = "manga"
|
||||
const val KEY_ID = "id"
|
||||
|
||||
fun of(manga: Manga) = MangaIntent(manga, manga.id, null)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ class MangaLinkResolver @Inject constructor(
|
||||
) {
|
||||
|
||||
suspend fun resolve(uri: Uri): Manga {
|
||||
return if (uri.host == "kotatsu.app") {
|
||||
return if (uri.scheme == "kotatsu" || uri.host == "kotatsu.app") {
|
||||
resolveAppLink(uri)
|
||||
} else {
|
||||
resolveExternalLink(uri)
|
||||
} ?: throw NotFoundException("Manga not found", uri.toString())
|
||||
} ?: throw NotFoundException("Cannot resolve link", uri.toString())
|
||||
}
|
||||
|
||||
suspend fun resolveAppLink(uri: Uri): Manga? {
|
||||
private suspend fun resolveAppLink(uri: Uri): Manga? {
|
||||
require(uri.pathSegments.singleOrNull() == "manga") { "Invalid url" }
|
||||
val sourceName = requireNotNull(uri.getQueryParameter("source")) { "Source is not specified" }
|
||||
val source = MangaSource(sourceName)
|
||||
@@ -42,7 +42,7 @@ class MangaLinkResolver @Inject constructor(
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun resolveExternalLink(uri: Uri): Manga? {
|
||||
private suspend fun resolveExternalLink(uri: Uri): Manga? {
|
||||
dataRepository.findMangaByPublicUrl(uri.toString())?.let {
|
||||
return it
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.parsers.config.MangaSourceConfig
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.*
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.coroutines.resume
|
||||
@@ -50,7 +50,7 @@ class MangaLoaderContextImpl @Inject constructor(
|
||||
}
|
||||
|
||||
override fun encodeBase64(data: ByteArray): String {
|
||||
return Base64.encodeToString(data, Base64.NO_PADDING)
|
||||
return Base64.encodeToString(data, Base64.NO_WRAP)
|
||||
}
|
||||
|
||||
override fun decodeBase64(data: String): ByteArray {
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.parser
|
||||
|
||||
import androidx.annotation.AnyThread
|
||||
import org.koitharu.kotatsu.core.cache.ContentCache
|
||||
import org.koitharu.kotatsu.core.network.MirrorSwitchInterceptor
|
||||
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
@@ -43,6 +44,7 @@ interface MangaRepository {
|
||||
private val localMangaRepository: LocalMangaRepository,
|
||||
private val loaderContext: MangaLoaderContext,
|
||||
private val contentCache: ContentCache,
|
||||
private val mirrorSwitchInterceptor: MirrorSwitchInterceptor,
|
||||
) {
|
||||
|
||||
private val cache = EnumMap<MangaSource, WeakReference<RemoteMangaRepository>>(MangaSource::class.java)
|
||||
@@ -55,7 +57,11 @@ interface MangaRepository {
|
||||
cache[source]?.get()?.let { return it }
|
||||
return synchronized(cache) {
|
||||
cache[source]?.get()?.let { return it }
|
||||
val repository = RemoteMangaRepository(MangaParser(source, loaderContext), contentCache)
|
||||
val repository = RemoteMangaRepository(
|
||||
parser = MangaParser(source, loaderContext),
|
||||
cache = contentCache,
|
||||
mirrorSwitchInterceptor = mirrorSwitchInterceptor,
|
||||
)
|
||||
cache[source] = WeakReference(repository)
|
||||
repository
|
||||
}
|
||||
|
||||
@@ -13,11 +13,13 @@ import okhttp3.Response
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.cache.ContentCache
|
||||
import org.koitharu.kotatsu.core.cache.SafeDeferred
|
||||
import org.koitharu.kotatsu.core.network.MirrorSwitchInterceptor
|
||||
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
||||
import org.koitharu.kotatsu.core.util.ext.processLifecycleScope
|
||||
import org.koitharu.kotatsu.parsers.MangaParser
|
||||
import org.koitharu.kotatsu.parsers.MangaParserAuthProvider
|
||||
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||
import org.koitharu.kotatsu.parsers.exception.ParseException
|
||||
import org.koitharu.kotatsu.parsers.model.Favicons
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
@@ -31,6 +33,7 @@ import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
class RemoteMangaRepository(
|
||||
private val parser: MangaParser,
|
||||
private val cache: ContentCache,
|
||||
private val mirrorSwitchInterceptor: MirrorSwitchInterceptor,
|
||||
) : MangaRepository, Interceptor {
|
||||
|
||||
override val source: MangaSource
|
||||
@@ -66,11 +69,15 @@ class RemoteMangaRepository(
|
||||
}
|
||||
|
||||
override suspend fun getList(offset: Int, query: String): List<Manga> {
|
||||
return parser.getList(offset, query)
|
||||
return mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getList(offset, query)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getList(offset: Int, tags: Set<MangaTag>?, sortOrder: SortOrder?): List<Manga> {
|
||||
return parser.getList(offset, tags, sortOrder)
|
||||
return mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getList(offset, tags, sortOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getDetails(manga: Manga): Manga = getDetails(manga, withCache = true)
|
||||
@@ -78,17 +85,25 @@ class RemoteMangaRepository(
|
||||
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
|
||||
cache.getPages(source, chapter.url)?.let { return it }
|
||||
val pages = asyncSafe {
|
||||
parser.getPages(chapter).distinctById()
|
||||
mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getPages(chapter).distinctById()
|
||||
}
|
||||
}
|
||||
cache.putPages(source, chapter.url, pages)
|
||||
return pages.await()
|
||||
}
|
||||
|
||||
override suspend fun getPageUrl(page: MangaPage): String = parser.getPageUrl(page)
|
||||
override suspend fun getPageUrl(page: MangaPage): String = mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getPageUrl(page)
|
||||
}
|
||||
|
||||
override suspend fun getTags(): Set<MangaTag> = parser.getTags()
|
||||
override suspend fun getTags(): Set<MangaTag> = mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getTags()
|
||||
}
|
||||
|
||||
suspend fun getFavicons(): Favicons = parser.getFavicons()
|
||||
suspend fun getFavicons(): Favicons = mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getFavicons()
|
||||
}
|
||||
|
||||
override suspend fun getRelated(seed: Manga): List<Manga> {
|
||||
cache.getRelatedManga(source, seed.url)?.let { return it }
|
||||
@@ -105,12 +120,18 @@ class RemoteMangaRepository(
|
||||
}
|
||||
cache.getDetails(source, manga.url)?.let { return it }
|
||||
val details = asyncSafe {
|
||||
parser.getDetails(manga)
|
||||
mirrorSwitchInterceptor.withMirrorSwitching {
|
||||
parser.getDetails(manga)
|
||||
}
|
||||
}
|
||||
cache.putDetails(source, manga.url, details)
|
||||
return details.await()
|
||||
}
|
||||
|
||||
suspend fun peekDetails(manga: Manga): Manga? {
|
||||
return cache.getDetails(source, manga.url)
|
||||
}
|
||||
|
||||
suspend fun find(manga: Manga): Manga? {
|
||||
val list = getList(0, manga.title)
|
||||
return list.find { x -> x.id == manga.id }
|
||||
@@ -155,4 +176,33 @@ class RemoteMangaRepository(
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private suspend fun <R> MirrorSwitchInterceptor.withMirrorSwitching(block: suspend () -> R): R {
|
||||
if (!isEnabled) {
|
||||
return block()
|
||||
}
|
||||
val initialMirror = domain
|
||||
val result = runCatchingCancellable {
|
||||
block()
|
||||
}
|
||||
if (result.isValidResult()) {
|
||||
return result.getOrThrow()
|
||||
}
|
||||
return if (trySwitchMirror(this@RemoteMangaRepository)) {
|
||||
val newResult = runCatchingCancellable {
|
||||
block()
|
||||
}
|
||||
if (newResult.isValidResult()) {
|
||||
return newResult.getOrThrow()
|
||||
} else {
|
||||
rollback(this@RemoteMangaRepository, initialMirror)
|
||||
return result.getOrThrow()
|
||||
}
|
||||
} else {
|
||||
result.getOrThrow()
|
||||
}
|
||||
}
|
||||
|
||||
private fun Result<*>.isValidResult() = exceptionOrNull() !is ParseException
|
||||
&& (getOrNull() as? Collection<*>)?.isEmpty() != true
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.putEnumValue
|
||||
import org.koitharu.kotatsu.core.util.ext.takeIfReadable
|
||||
import org.koitharu.kotatsu.core.util.ext.toUriOrNull
|
||||
import org.koitharu.kotatsu.history.domain.model.HistoryOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.parsers.util.find
|
||||
import org.koitharu.kotatsu.parsers.util.mapNotNullToSet
|
||||
@@ -72,6 +72,18 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
get() = prefs.getInt(KEY_GRID_SIZE, 100)
|
||||
set(value) = prefs.edit { putInt(KEY_GRID_SIZE, value) }
|
||||
|
||||
var historyListMode: ListMode
|
||||
get() = prefs.getEnumValue(KEY_LIST_MODE_HISTORY, listMode)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_LIST_MODE_HISTORY, value) }
|
||||
|
||||
var suggestionsListMode: ListMode
|
||||
get() = prefs.getEnumValue(KEY_LIST_MODE_SUGGESTIONS, listMode)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_LIST_MODE_SUGGESTIONS, value) }
|
||||
|
||||
var favoritesListMode: ListMode
|
||||
get() = prefs.getEnumValue(KEY_LIST_MODE_FAVORITES, listMode)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_LIST_MODE_FAVORITES, value) }
|
||||
|
||||
var isNsfwContentDisabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_DISABLE_NSFW, false)
|
||||
set(value) = prefs.edit { putBoolean(KEY_DISABLE_NSFW, value) }
|
||||
@@ -90,6 +102,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
val readerPageSwitch: Set<String>
|
||||
get() = prefs.getStringSet(KEY_READER_SWITCHERS, null) ?: setOf(PAGE_SWITCH_TAPS)
|
||||
|
||||
val isReaderZoomButtonsEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_READER_ZOOM_BUTTONS, false)
|
||||
|
||||
val isReaderTapsAdaptive: Boolean
|
||||
get() = !prefs.getBoolean(KEY_READER_TAPS_LTR, false)
|
||||
|
||||
@@ -161,7 +176,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
get() = prefs.getString(KEY_APP_PASSWORD, null)
|
||||
set(value) = prefs.edit {
|
||||
if (value != null) putString(KEY_APP_PASSWORD, value) else remove(
|
||||
KEY_APP_PASSWORD
|
||||
KEY_APP_PASSWORD,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -173,7 +188,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
set(value) = prefs.edit { putBoolean(KEY_PROTECT_APP_BIOMETRIC, value) }
|
||||
|
||||
val isMirrorSwitchingAvailable: Boolean
|
||||
get() = prefs.getBoolean(KEY_MIRROR_SWITCHING, true)
|
||||
get() = prefs.getBoolean(KEY_MIRROR_SWITCHING, false)
|
||||
|
||||
val isExitConfirmationEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_EXIT_CONFIRM, false)
|
||||
@@ -198,6 +213,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
get() = prefs.getBoolean(KEY_SOURCES_GRID, false)
|
||||
set(value) = prefs.edit { putBoolean(KEY_SOURCES_GRID, value) }
|
||||
|
||||
val isNewSourcesTipEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_SOURCES_NEW, true)
|
||||
|
||||
val isPagesNumbersEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_PAGES_NUMBERS, false)
|
||||
|
||||
@@ -267,6 +285,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
val isReaderSliderEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_READER_SLIDER, true)
|
||||
|
||||
val isReaderKeepScreenOn: Boolean
|
||||
get() = prefs.getBoolean(KEY_READER_SCREEN_ON, true)
|
||||
|
||||
val isImagesProxyEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_IMAGES_PROXY, false)
|
||||
|
||||
@@ -298,8 +319,8 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
get() = prefs.getEnumValue(KEY_LOCAL_LIST_ORDER, SortOrder.NEWEST)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_LOCAL_LIST_ORDER, value) }
|
||||
|
||||
var historySortOrder: HistoryOrder
|
||||
get() = prefs.getEnumValue(KEY_HISTORY_ORDER, HistoryOrder.UPDATED)
|
||||
var historySortOrder: ListSortOrder
|
||||
get() = prefs.getEnumValue(KEY_HISTORY_ORDER, ListSortOrder.UPDATED)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_HISTORY_ORDER, value) }
|
||||
|
||||
val isRelatedMangaEnabled: Boolean
|
||||
@@ -314,7 +335,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
set(@FloatRange(from = 0.0, to = 1.0) value) = prefs.edit {
|
||||
putFloat(
|
||||
KEY_READER_AUTOSCROLL_SPEED,
|
||||
value
|
||||
value,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -325,11 +346,24 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
}
|
||||
val policy = NetworkPolicy.from(
|
||||
prefs.getString(KEY_PAGES_PRELOAD, null),
|
||||
NetworkPolicy.NON_METERED
|
||||
NetworkPolicy.NON_METERED,
|
||||
)
|
||||
return policy.isNetworkAllowed(connectivityManager)
|
||||
}
|
||||
|
||||
val is32BitColorsEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_32BIT_COLOR, false)
|
||||
|
||||
val isPeriodicalBackupEnabled: Boolean
|
||||
get() = prefs.getBoolean(KEY_BACKUP_PERIODICAL_ENABLED, false)
|
||||
|
||||
val periodicalBackupFrequency: Long
|
||||
get() = prefs.getString(KEY_BACKUP_PERIODICAL_FREQUENCY, null)?.toLongOrNull() ?: 7L
|
||||
|
||||
var periodicalBackupOutput: Uri?
|
||||
get() = prefs.getString(KEY_BACKUP_PERIODICAL_OUTPUT, null)?.toUriOrNull()
|
||||
set(value) = prefs.edit { putString(KEY_BACKUP_PERIODICAL_OUTPUT, value?.toString()) }
|
||||
|
||||
fun isTipEnabled(tip: String): Boolean {
|
||||
return prefs.getStringSet(KEY_TIPS_CLOSED, emptySet())?.contains(tip) != true
|
||||
}
|
||||
@@ -395,6 +429,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val TRACK_FAVOURITES = "favourites"
|
||||
|
||||
const val KEY_LIST_MODE = "list_mode_2"
|
||||
const val KEY_LIST_MODE_HISTORY = "list_mode_history"
|
||||
const val KEY_LIST_MODE_FAVORITES = "list_mode_favorites"
|
||||
const val KEY_LIST_MODE_SUGGESTIONS = "list_mode_suggestions"
|
||||
const val KEY_THEME = "theme"
|
||||
const val KEY_COLOR_THEME = "color_theme"
|
||||
const val KEY_THEME_AMOLED = "amoled_theme"
|
||||
@@ -409,6 +446,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val KEY_REMOTE_SOURCES = "remote_sources"
|
||||
const val KEY_LOCAL_STORAGE = "local_storage"
|
||||
const val KEY_READER_SWITCHERS = "reader_switchers"
|
||||
const val KEY_READER_ZOOM_BUTTONS = "reader_zoom_buttons"
|
||||
const val KEY_TRACKER_ENABLED = "tracker_enabled"
|
||||
const val KEY_TRACKER_WIFI_ONLY = "tracker_wifi"
|
||||
const val KEY_TRACK_SOURCES = "track_sources"
|
||||
@@ -430,6 +468,10 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val KEY_ZOOM_MODE = "zoom_mode"
|
||||
const val KEY_BACKUP = "backup"
|
||||
const val KEY_RESTORE = "restore"
|
||||
const val KEY_BACKUP_PERIODICAL_ENABLED = "backup_periodic"
|
||||
const val KEY_BACKUP_PERIODICAL_FREQUENCY = "backup_periodic_freq"
|
||||
const val KEY_BACKUP_PERIODICAL_OUTPUT = "backup_periodic_output"
|
||||
const val KEY_BACKUP_PERIODICAL_LAST = "backup_periodic_last"
|
||||
const val KEY_HISTORY_GROUPING = "history_grouping"
|
||||
const val KEY_READING_INDICATORS = "reading_indicators"
|
||||
const val KEY_REVERSE_CHAPTERS = "reverse_chapters"
|
||||
@@ -456,6 +498,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val KEY_READER_BAR = "reader_bar"
|
||||
const val KEY_READER_SLIDER = "reader_slider"
|
||||
const val KEY_READER_BACKGROUND = "reader_background"
|
||||
const val KEY_READER_SCREEN_ON = "reader_screen_on"
|
||||
const val KEY_SHORTCUTS = "dynamic_shortcuts"
|
||||
const val KEY_READER_TAPS_LTR = "reader_taps_ltr"
|
||||
const val KEY_LOCAL_LIST_ORDER = "local_order"
|
||||
@@ -466,6 +509,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val KEY_LOGGING_ENABLED = "logging"
|
||||
const val KEY_LOGS_SHARE = "logs_share"
|
||||
const val KEY_SOURCES_GRID = "sources_grid"
|
||||
const val KEY_SOURCES_NEW = "sources_new"
|
||||
const val KEY_UPDATES_UNSTABLE = "updates_unstable"
|
||||
const val KEY_TIPS_CLOSED = "tips_closed"
|
||||
const val KEY_SSL_BYPASS = "ssl_bypass"
|
||||
@@ -483,6 +527,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
|
||||
const val KEY_DISABLE_NSFW = "no_nsfw"
|
||||
const val KEY_RELATED_MANGA = "related_manga"
|
||||
const val KEY_NAV_MAIN = "nav_main"
|
||||
const val KEY_32BIT_COLOR = "enhanced_colors"
|
||||
|
||||
// About
|
||||
const val KEY_APP_UPDATE = "app_update"
|
||||
|
||||
@@ -6,7 +6,6 @@ import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.KeyEvent
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.CallSuper
|
||||
@@ -96,10 +95,10 @@ abstract class BaseActivity<B : ViewBinding> :
|
||||
insetsDelegate.onViewCreated(binding.root)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem) = if (item.itemId == android.R.id.home) {
|
||||
onBackPressed()
|
||||
true
|
||||
} else super.onOptionsItemSelected(item)
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
dispatchNavigateUp()
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
||||
if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
|
||||
@@ -126,10 +125,10 @@ abstract class BaseActivity<B : ViewBinding> :
|
||||
val actionModeColor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
ColorUtils.compositeColors(
|
||||
ContextCompat.getColor(this, com.google.android.material.R.color.m3_appbar_overlay_color),
|
||||
getThemeColor(com.google.android.material.R.attr.colorSurface),
|
||||
getThemeColor(R.attr.m3ColorBackground),
|
||||
)
|
||||
} else {
|
||||
ContextCompat.getColor(this, R.color.kotatsu_secondaryContainer)
|
||||
ContextCompat.getColor(this, R.color.kotatsu_m3_background)
|
||||
}
|
||||
val insets = ViewCompat.getRootWindowInsets(viewBinding.root)
|
||||
?.getInsets(WindowInsetsCompat.Type.systemBars()) ?: return
|
||||
@@ -150,6 +149,17 @@ abstract class BaseActivity<B : ViewBinding> :
|
||||
window.statusBarColor = defaultStatusBarColor
|
||||
}
|
||||
|
||||
protected open fun dispatchNavigateUp() {
|
||||
val upIntent = parentActivityIntent
|
||||
if (upIntent != null) {
|
||||
if (!navigateUpTo(upIntent)) {
|
||||
startActivity(upIntent)
|
||||
}
|
||||
} else {
|
||||
finishAfterTransition()
|
||||
}
|
||||
}
|
||||
|
||||
private fun putDataToExtras(intent: Intent?) {
|
||||
intent?.putExtra(EXTRA_DATA, intent.data)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.koitharu.kotatsu.core.ui.list
|
||||
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
|
||||
|
||||
class RecyclerScrollKeeper(
|
||||
private val rv: RecyclerView,
|
||||
) : AdapterDataObserver() {
|
||||
|
||||
private val scrollUpRunnable = Runnable {
|
||||
(rv.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(0, 0)
|
||||
}
|
||||
|
||||
fun attach() {
|
||||
rv.adapter?.registerAdapterDataObserver(this)
|
||||
}
|
||||
|
||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||
super.onItemRangeInserted(positionStart, itemCount)
|
||||
if (positionStart == 0 && isScrolledToTop()) {
|
||||
postScrollUp()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
|
||||
if (toPosition == 0 && isScrolledToTop()) {
|
||||
postScrollUp()
|
||||
}
|
||||
}
|
||||
|
||||
private fun postScrollUp() {
|
||||
rv.postDelayed(scrollUpRunnable, 500L)
|
||||
}
|
||||
|
||||
private fun isScrolledToTop(): Boolean {
|
||||
return (rv.layoutManager as? LinearLayoutManager)?.findFirstVisibleItemPosition() == 0
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,13 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.withStyledAttributes
|
||||
import androidx.core.view.GravityCompat
|
||||
import androidx.core.view.ancestors
|
||||
import androidx.core.view.isGone
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||
import org.koitharu.kotatsu.core.util.ext.isLayoutReversed
|
||||
import org.koitharu.kotatsu.core.util.ext.parents
|
||||
import org.koitharu.kotatsu.databinding.FastScrollerBinding
|
||||
import kotlin.math.roundToInt
|
||||
import com.google.android.material.R as materialR
|
||||
@@ -522,7 +522,7 @@ class FastScroller @JvmOverloads constructor(
|
||||
return BubbleSize.entries.getOrNull(ordinal) ?: defaultValue
|
||||
}
|
||||
|
||||
private fun findValidParent(view: View): ViewGroup? = view.parents.firstNotNullOfOrNull { p ->
|
||||
private fun findValidParent(view: View): ViewGroup? = view.ancestors.firstNotNullOfOrNull { p ->
|
||||
if (p is FrameLayout || p is ConstraintLayout || p is CoordinatorLayout || p is RelativeLayout) {
|
||||
p as ViewGroup
|
||||
} else {
|
||||
|
||||
@@ -104,6 +104,7 @@ sealed class AdaptiveSheetBehavior {
|
||||
companion object {
|
||||
|
||||
const val STATE_EXPANDED = SideSheetBehavior.STATE_EXPANDED
|
||||
const val STATE_COLLAPSED = BottomSheetBehavior.STATE_COLLAPSED
|
||||
const val STATE_SETTLING = SideSheetBehavior.STATE_SETTLING
|
||||
const val STATE_DRAGGING = SideSheetBehavior.STATE_DRAGGING
|
||||
const val STATE_HIDDEN = SideSheetBehavior.STATE_HIDDEN
|
||||
@@ -114,10 +115,11 @@ sealed class AdaptiveSheetBehavior {
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun from(lp: CoordinatorLayout.LayoutParams): AdaptiveSheetBehavior? = when (val behavior = lp.behavior) {
|
||||
is BottomSheetBehavior<*> -> Bottom(behavior)
|
||||
is SideSheetBehavior<*> -> Side(behavior)
|
||||
else -> null
|
||||
}
|
||||
fun from(lp: CoordinatorLayout.LayoutParams): AdaptiveSheetBehavior? =
|
||||
when (val behavior = lp.behavior) {
|
||||
is BottomSheetBehavior<*> -> Bottom(behavior)
|
||||
is SideSheetBehavior<*> -> Side(behavior)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,19 @@ package org.koitharu.kotatsu.core.ui.sheet
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.InputDevice
|
||||
import android.view.LayoutInflater
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
import androidx.core.content.withStyledAttributes
|
||||
import androidx.core.view.ancestors
|
||||
import androidx.core.view.isGone
|
||||
import androidx.core.view.isVisible
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.util.ext.parents
|
||||
import org.koitharu.kotatsu.databinding.LayoutSheetHeaderAdaptiveBinding
|
||||
|
||||
class AdaptiveSheetHeaderBar @JvmOverloads constructor(
|
||||
@@ -21,7 +23,8 @@ class AdaptiveSheetHeaderBar @JvmOverloads constructor(
|
||||
@AttrRes defStyleAttr: Int = 0,
|
||||
) : LinearLayout(context, attrs, defStyleAttr), AdaptiveSheetCallback {
|
||||
|
||||
private val binding = LayoutSheetHeaderAdaptiveBinding.inflate(LayoutInflater.from(context), this)
|
||||
private val binding =
|
||||
LayoutSheetHeaderAdaptiveBinding.inflate(LayoutInflater.from(context), this)
|
||||
private var sheetBehavior: AdaptiveSheetBehavior? = null
|
||||
|
||||
var title: CharSequence?
|
||||
@@ -60,6 +63,28 @@ class AdaptiveSheetHeaderBar @JvmOverloads constructor(
|
||||
super.onDetachedFromWindow()
|
||||
}
|
||||
|
||||
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
|
||||
val behavior = sheetBehavior ?: return super.onGenericMotionEvent(event)
|
||||
if (event.source and InputDevice.SOURCE_CLASS_POINTER != 0) {
|
||||
if (event.actionMasked == MotionEvent.ACTION_SCROLL) {
|
||||
if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0f) {
|
||||
behavior.state = if (
|
||||
behavior is AdaptiveSheetBehavior.Bottom
|
||||
&& behavior.state == AdaptiveSheetBehavior.STATE_EXPANDED
|
||||
) {
|
||||
AdaptiveSheetBehavior.STATE_COLLAPSED
|
||||
} else {
|
||||
AdaptiveSheetBehavior.STATE_HIDDEN
|
||||
}
|
||||
} else {
|
||||
behavior.state = AdaptiveSheetBehavior.STATE_EXPANDED
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return super.onGenericMotionEvent(event)
|
||||
}
|
||||
|
||||
override fun onStateChanged(sheet: View, newState: Int) {
|
||||
|
||||
}
|
||||
@@ -81,14 +106,9 @@ class AdaptiveSheetHeaderBar @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
private fun findParentSheetBehavior(): AdaptiveSheetBehavior? {
|
||||
for (p in parents) {
|
||||
val layoutParams = (p as? View)?.layoutParams
|
||||
if (layoutParams is CoordinatorLayout.LayoutParams) {
|
||||
AdaptiveSheetBehavior.from(layoutParams)?.let {
|
||||
return it
|
||||
}
|
||||
}
|
||||
return ancestors.firstNotNullOfOrNull {
|
||||
((it as? View)?.layoutParams as? CoordinatorLayout.LayoutParams)
|
||||
?.let { params -> AdaptiveSheetBehavior.from(params) }
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,9 @@ class SlidingBottomNavigationView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
fun show() {
|
||||
if (currentState == STATE_UP) {
|
||||
return
|
||||
}
|
||||
currentAnimator?.cancel()
|
||||
clearAnimation()
|
||||
|
||||
@@ -77,6 +80,9 @@ class SlidingBottomNavigationView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
fun hide() {
|
||||
if (currentState == STATE_DOWN) {
|
||||
return
|
||||
}
|
||||
currentAnimator?.cancel()
|
||||
clearAnimation()
|
||||
|
||||
@@ -117,6 +123,7 @@ class SlidingBottomNavigationView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
internal class SavedState : AbsSavedState {
|
||||
|
||||
var currentState = STATE_UP
|
||||
var translationY = 0F
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.koitharu.kotatsu.core.ui.widgets
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.databinding.ViewZoomBinding
|
||||
|
||||
class ZoomControl @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
) : LinearLayout(context, attrs), View.OnClickListener {
|
||||
|
||||
private val binding = ViewZoomBinding.inflate(LayoutInflater.from(context), this)
|
||||
|
||||
var listener: ZoomControlListener? = null
|
||||
|
||||
init {
|
||||
binding.buttonZoomIn.setOnClickListener(this)
|
||||
binding.buttonZoomOut.setOnClickListener(this)
|
||||
}
|
||||
|
||||
override fun onClick(v: View) {
|
||||
when (v.id) {
|
||||
R.id.button_zoom_in -> listener?.onZoomIn()
|
||||
R.id.button_zoom_out -> listener?.onZoomOut()
|
||||
}
|
||||
}
|
||||
|
||||
interface ZoomControlListener {
|
||||
|
||||
fun onZoomIn()
|
||||
|
||||
fun onZoomOut()
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
@Deprecated("", replaceWith = ReplaceWith("CompositeMutex2"))
|
||||
class CompositeMutex<T : Any> : Set<T> {
|
||||
|
||||
private val state = ArrayMap<T, MutableStateFlow<Boolean>>()
|
||||
@@ -25,7 +26,7 @@ class CompositeMutex<T : Any> : Set<T> {
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
return state.isEmpty
|
||||
return state.isEmpty()
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> {
|
||||
|
||||
@@ -19,7 +19,7 @@ class CompositeMutex2<T : Any> : Set<T> {
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
return delegates.isEmpty
|
||||
return delegates.isEmpty()
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> {
|
||||
|
||||
@@ -15,6 +15,7 @@ class ViewBadge(
|
||||
) : View.OnLayoutChangeListener, DefaultLifecycleObserver {
|
||||
|
||||
private var badgeDrawable: BadgeDrawable? = null
|
||||
private var maxCharacterCount: Int = -1
|
||||
|
||||
var counter: Int
|
||||
get() = badgeDrawable?.number ?: 0
|
||||
@@ -48,8 +49,16 @@ class ViewBadge(
|
||||
clearBadge()
|
||||
}
|
||||
|
||||
fun setMaxCharacterCount(value: Int) {
|
||||
maxCharacterCount = value
|
||||
badgeDrawable?.maxCharacterCount = value
|
||||
}
|
||||
|
||||
private fun initBadge(): BadgeDrawable {
|
||||
val badge = BadgeDrawable.create(anchor.context)
|
||||
if (maxCharacterCount > 0) {
|
||||
badge.maxCharacterCount = maxCharacterCount
|
||||
}
|
||||
anchor.addOnLayoutChangeListener(this)
|
||||
BadgeUtils.attachBadgeDrawable(badge, anchor)
|
||||
badgeDrawable = badge
|
||||
|
||||
@@ -83,7 +83,7 @@ fun <I> ActivityResultLauncher<I>.tryLaunch(
|
||||
e.printStackTraceDebug()
|
||||
}.isSuccess
|
||||
|
||||
fun SharedPreferences.observe() = callbackFlow<String?> {
|
||||
fun SharedPreferences.observe(): Flow<String?> = callbackFlow {
|
||||
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
|
||||
trySendBlocking(key)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ fun ImageView.newImageRequest(lifecycleOwner: LifecycleOwner, data: Any?): Image
|
||||
return null
|
||||
}
|
||||
}
|
||||
disposeImageRequest()
|
||||
// disposeImageRequest()
|
||||
return ImageRequest.Builder(context)
|
||||
.data(data)
|
||||
.lifecycle(lifecycleOwner)
|
||||
|
||||
@@ -55,3 +55,5 @@ inline fun <reified E : Enum<E>> Collection<E>.toEnumSet(): EnumSet<E> = if (isE
|
||||
} else {
|
||||
EnumSet.copyOf(this)
|
||||
}
|
||||
|
||||
fun <E : Enum<E>> Collection<E>.sortedByOrdinal() = sortedBy { it.ordinal }
|
||||
|
||||
@@ -14,6 +14,7 @@ import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import org.koitharu.kotatsu.core.util.RetainedLifecycleCoroutineScope
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
@@ -79,3 +80,11 @@ fun <T> Deferred<T>.getCompletionResultOrNull(): Result<T>? = if (isCompleted) {
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
fun <T> Deferred<T>.peek(): T? = if (isCompleted) {
|
||||
runCatchingCancellable {
|
||||
getCompleted()
|
||||
}.getOrNull()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -26,6 +26,17 @@ fun <T> Flow<T>.onFirst(action: suspend (T) -> Unit): Flow<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Flow<T>.onEachWhile(action: suspend (T) -> Boolean): Flow<T> {
|
||||
var isCalled = false
|
||||
return onEach {
|
||||
if (!isCalled) {
|
||||
isCalled = action(it)
|
||||
}
|
||||
}.onCompletion {
|
||||
isCalled = false
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R> Flow<List<T>>.mapItems(crossinline transform: (T) -> R): Flow<List<R>> {
|
||||
return map { list -> list.map(transform) }
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package org.koitharu.kotatsu.core.util.ext
|
||||
|
||||
import android.app.Activity
|
||||
import android.graphics.Rect
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.view.View.MeasureSpec
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewParent
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.Checkable
|
||||
import androidx.core.view.children
|
||||
import androidx.core.view.descendants
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewpager2.widget.ViewPager2
|
||||
@@ -89,23 +90,8 @@ fun Slider.setValueRounded(newValue: Float) {
|
||||
value = roundedValue.coerceIn(valueFrom, valueTo)
|
||||
}
|
||||
|
||||
fun <T : View> ViewGroup.findViewsByType(clazz: Class<T>): Sequence<T> {
|
||||
if (childCount == 0) {
|
||||
return emptySequence()
|
||||
}
|
||||
return sequence {
|
||||
for (view in children) {
|
||||
if (clazz.isInstance(view)) {
|
||||
yield(clazz.cast(view)!!)
|
||||
} else if (view is ViewGroup && view.childCount != 0) {
|
||||
yieldAll(view.findViewsByType(clazz))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun RecyclerView.invalidateNestedItemDecorations() {
|
||||
findViewsByType(RecyclerView::class.java).forEach {
|
||||
descendants.filterIsInstance<RecyclerView>().forEach {
|
||||
it.invalidateItemDecorations()
|
||||
}
|
||||
}
|
||||
@@ -113,15 +99,6 @@ fun RecyclerView.invalidateNestedItemDecorations() {
|
||||
val View.parentView: ViewGroup?
|
||||
get() = parent as? ViewGroup
|
||||
|
||||
val View.parents: Sequence<ViewParent>
|
||||
get() = sequence {
|
||||
var p: ViewParent? = parent
|
||||
while (p != null) {
|
||||
yield(p)
|
||||
p = p.parent
|
||||
}
|
||||
}
|
||||
|
||||
fun View.measureDimension(desiredSize: Int, measureSpec: Int): Int {
|
||||
var result: Int
|
||||
val specMode = MeasureSpec.getMode(measureSpec)
|
||||
@@ -164,3 +141,9 @@ fun BaseProgressIndicator<*>.showOrHide(value: Boolean) {
|
||||
if (isVisible) hide()
|
||||
}
|
||||
}
|
||||
|
||||
fun View.setOnContextClickListenerCompat(listener: View.OnLongClickListener) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
setOnContextClickListener(listener::onLongClick)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.koitharu.kotatsu.details.data
|
||||
|
||||
import org.koitharu.kotatsu.core.model.isLocal
|
||||
import org.koitharu.kotatsu.local.domain.model.LocalManga
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.reader.data.filterChapters
|
||||
|
||||
data class MangaDetails(
|
||||
private val manga: Manga,
|
||||
private val localManga: LocalManga?,
|
||||
val description: CharSequence?,
|
||||
val isLoaded: Boolean,
|
||||
) {
|
||||
|
||||
val id: Long
|
||||
get() = manga.id
|
||||
|
||||
val chapters: Map<String?, List<MangaChapter>> = manga.chapters?.groupBy { it.branch }.orEmpty()
|
||||
|
||||
val branches: Set<String?>
|
||||
get() = chapters.keys
|
||||
|
||||
val allChapters: List<MangaChapter>
|
||||
get() = manga.chapters.orEmpty()
|
||||
|
||||
val isLocal
|
||||
get() = manga.isLocal
|
||||
|
||||
val local: LocalManga?
|
||||
get() = localManga ?: if (manga.isLocal) LocalManga(manga) else null
|
||||
|
||||
fun toManga() = manga
|
||||
|
||||
fun filterChapters(branch: String?) = MangaDetails(
|
||||
manga = manga.filterChapters(branch),
|
||||
localManga = localManga?.run {
|
||||
copy(manga = manga.filterChapters(branch))
|
||||
},
|
||||
description = description,
|
||||
isLoaded = isLoaded,
|
||||
)
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
import org.koitharu.kotatsu.details.domain.model.DoubleManga
|
||||
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||
@@ -20,7 +20,7 @@ import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblingInfo
|
||||
import org.koitharu.kotatsu.tracker.domain.TrackingRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
@Deprecated("")
|
||||
/* TODO: remove */
|
||||
class DetailsInteractor @Inject constructor(
|
||||
private val historyRepository: HistoryRepository,
|
||||
private val favouritesRepository: FavouritesRepository,
|
||||
@@ -66,15 +66,26 @@ class DetailsInteractor @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateLocal(subject: DoubleManga?, localManga: LocalManga): DoubleManga? {
|
||||
return if (subject?.any?.id == localManga.manga.id) {
|
||||
subject.copy(
|
||||
localManga = runCatchingCancellable {
|
||||
localMangaRepository.getDetails(localManga.manga)
|
||||
},
|
||||
)
|
||||
suspend fun updateLocal(subject: MangaDetails?, localManga: LocalManga): MangaDetails? {
|
||||
subject ?: return null
|
||||
return if (subject.id == localManga.manga.id) {
|
||||
if (subject.isLocal) {
|
||||
subject.copy(
|
||||
manga = localManga.manga,
|
||||
)
|
||||
} else {
|
||||
subject.copy(
|
||||
localManga = runCatchingCancellable {
|
||||
localManga.copy(
|
||||
manga = localMangaRepository.getDetails(localManga.manga),
|
||||
)
|
||||
}.getOrNull() ?: subject.local,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
subject
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun findRemote(seed: Manga) = localMangaRepository.getRemoteManga(seed)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.koitharu.kotatsu.details.domain
|
||||
|
||||
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 kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import org.koitharu.kotatsu.core.model.isLocal
|
||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.util.ext.peek
|
||||
import org.koitharu.kotatsu.core.util.ext.sanitize
|
||||
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||
import org.koitharu.kotatsu.explore.domain.RecoverMangaUseCase
|
||||
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.recoverNotNull
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
import javax.inject.Inject
|
||||
|
||||
class DetailsLoadUseCase @Inject constructor(
|
||||
private val mangaDataRepository: MangaDataRepository,
|
||||
private val localMangaRepository: LocalMangaRepository,
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
private val recoverUseCase: RecoverMangaUseCase,
|
||||
private val imageGetter: Html.ImageGetter,
|
||||
) {
|
||||
|
||||
operator fun invoke(intent: MangaIntent): Flow<MangaDetails> = channelFlow {
|
||||
val manga = requireNotNull(mangaDataRepository.resolveIntent(intent)) {
|
||||
"Cannot resolve intent $intent"
|
||||
}
|
||||
val local = if (!manga.isLocal) {
|
||||
async {
|
||||
localMangaRepository.findSavedManga(manga)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
send(MangaDetails(manga, null, null, false))
|
||||
val details = getDetails(manga)
|
||||
send(MangaDetails(details, local?.peek(), details.description?.parseAsHtml(withImages = false), false))
|
||||
send(MangaDetails(details, local?.await(), details.description?.parseAsHtml(withImages = true), true))
|
||||
}
|
||||
|
||||
private suspend fun getDetails(seed: Manga) = runCatchingCancellable {
|
||||
val repository = mangaRepositoryFactory.create(seed.source)
|
||||
repository.getDetails(seed)
|
||||
}.recoverNotNull { e ->
|
||||
if (e is NotFoundException) {
|
||||
recoverUseCase(seed)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}.getOrThrow()
|
||||
|
||||
private suspend fun String.parseAsHtml(withImages: Boolean): CharSequence? {
|
||||
return if (withImages) {
|
||||
runInterruptible(Dispatchers.IO) {
|
||||
parseAsHtml(imageGetter = imageGetter)
|
||||
}.filterSpans()
|
||||
} else {
|
||||
runInterruptible(Dispatchers.Default) {
|
||||
parseAsHtml()
|
||||
}.filterSpans().sanitize()
|
||||
}.takeUnless { it.isBlank() }
|
||||
}
|
||||
|
||||
private fun Spanned.filterSpans(): Spanned {
|
||||
val spannable = SpannableString.valueOf(this)
|
||||
val spans = spannable.getSpans<ForegroundColorSpan>()
|
||||
for (span in spans) {
|
||||
spannable.removeSpan(span)
|
||||
}
|
||||
return spannable
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package org.koitharu.kotatsu.details.domain
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import org.koitharu.kotatsu.core.model.isLocal
|
||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.details.domain.model.DoubleManga
|
||||
import org.koitharu.kotatsu.explore.domain.RecoverMangaUseCase
|
||||
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.recoverNotNull
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
import javax.inject.Inject
|
||||
|
||||
class DoubleMangaLoadUseCase @Inject constructor(
|
||||
private val mangaDataRepository: MangaDataRepository,
|
||||
private val localMangaRepository: LocalMangaRepository,
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
private val recoverUseCase: RecoverMangaUseCase,
|
||||
) {
|
||||
|
||||
operator fun invoke(manga: Manga): Flow<DoubleManga> = flow<DoubleManga> {
|
||||
var lastValue: DoubleManga? = null
|
||||
var emitted = false
|
||||
invokeImpl(manga).collect {
|
||||
lastValue = it
|
||||
if (it.any != null) {
|
||||
emitted = true
|
||||
emit(it)
|
||||
}
|
||||
}
|
||||
if (!emitted) {
|
||||
lastValue?.requireAny()
|
||||
}
|
||||
}.flowOn(Dispatchers.Default)
|
||||
|
||||
operator fun invoke(mangaId: Long): Flow<DoubleManga> = flow {
|
||||
emit(mangaDataRepository.findMangaById(mangaId) ?: throwNFE())
|
||||
}.flatMapLatest { invoke(it) }
|
||||
|
||||
operator fun invoke(intent: MangaIntent): Flow<DoubleManga> = flow {
|
||||
emit(mangaDataRepository.resolveIntent(intent) ?: throwNFE())
|
||||
}.flatMapLatest { invoke(it) }
|
||||
|
||||
private suspend fun loadLocal(manga: Manga): Result<Manga>? {
|
||||
return runCatchingCancellable {
|
||||
if (manga.isLocal) {
|
||||
localMangaRepository.getDetails(manga)
|
||||
} else {
|
||||
localMangaRepository.findSavedManga(manga)?.manga
|
||||
} ?: return null
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadRemote(manga: Manga): Result<Manga>? {
|
||||
return runCatchingCancellable {
|
||||
val seed = if (manga.isLocal) {
|
||||
localMangaRepository.getRemoteManga(manga)
|
||||
} else {
|
||||
manga
|
||||
} ?: return null
|
||||
val repository = mangaRepositoryFactory.create(seed.source)
|
||||
repository.getDetails(seed)
|
||||
}.recoverNotNull { e ->
|
||||
if (e is NotFoundException) {
|
||||
recoverUseCase(manga)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun invokeImpl(manga: Manga): Flow<DoubleManga> = combine(
|
||||
flow { emit(null); emit(loadRemote(manga)) },
|
||||
flow { emit(null); emit(loadLocal(manga)) },
|
||||
) { remote, local ->
|
||||
DoubleManga(
|
||||
remoteManga = remote,
|
||||
localManga = local,
|
||||
)
|
||||
}
|
||||
|
||||
private fun throwNFE(): Nothing = throw NotFoundException("Cannot find manga", "")
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.koitharu.kotatsu.details.domain
|
||||
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.model.findChapter
|
||||
import org.koitharu.kotatsu.core.model.isLocal
|
||||
import org.koitharu.kotatsu.core.os.NetworkState
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
||||
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import javax.inject.Inject
|
||||
|
||||
class ProgressUpdateUseCase @Inject constructor(
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
private val database: MangaDatabase,
|
||||
private val localMangaRepository: LocalMangaRepository,
|
||||
private val networkState: NetworkState,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(manga: Manga): Float {
|
||||
val history = database.getHistoryDao().find(manga.id) ?: return PROGRESS_NONE
|
||||
val seed = if (manga.isLocal) {
|
||||
localMangaRepository.getRemoteManga(manga) ?: manga
|
||||
} else {
|
||||
manga
|
||||
}
|
||||
if (!seed.isLocal && !networkState.value) {
|
||||
return PROGRESS_NONE
|
||||
}
|
||||
val repo = mangaRepositoryFactory.create(seed.source)
|
||||
val details = if (manga.source != seed.source || seed.chapters.isNullOrEmpty()) {
|
||||
repo.getDetails(seed)
|
||||
} else {
|
||||
seed
|
||||
}
|
||||
val chapter = details.findChapter(history.chapterId) ?: return PROGRESS_NONE
|
||||
val chapters = details.getChapters(chapter.branch) ?: return PROGRESS_NONE
|
||||
val chaptersCount = chapters.size
|
||||
if (chaptersCount == 0) {
|
||||
return PROGRESS_NONE
|
||||
}
|
||||
val chapterIndex = chapters.indexOfFirst { x -> x.id == history.chapterId }
|
||||
val pagesCount = repo.getPages(chapter).size
|
||||
if (pagesCount == 0) {
|
||||
return PROGRESS_NONE
|
||||
}
|
||||
val pagePercent = (history.page + 1) / pagesCount.toFloat()
|
||||
val ppc = 1f / chaptersCount
|
||||
val result = ppc * chapterIndex + ppc * pagePercent
|
||||
if (result != history.percent) {
|
||||
database.getHistoryDao().update(
|
||||
history.copy(
|
||||
chapterId = chapter.id,
|
||||
percent = result,
|
||||
),
|
||||
)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package org.koitharu.kotatsu.details.domain.model
|
||||
|
||||
import org.koitharu.kotatsu.core.model.findById
|
||||
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.reader.data.filterChapters
|
||||
|
||||
data class DoubleManga(
|
||||
private val remoteManga: Result<Manga>?,
|
||||
private val localManga: Result<Manga>?,
|
||||
) {
|
||||
|
||||
constructor(manga: Manga) : this(
|
||||
remoteManga = if (manga.source != MangaSource.LOCAL) Result.success(manga) else null,
|
||||
localManga = if (manga.source == MangaSource.LOCAL) Result.success(manga) else null,
|
||||
)
|
||||
|
||||
val remote: Manga?
|
||||
get() = remoteManga?.getOrNull()
|
||||
|
||||
val local: Manga?
|
||||
get() = localManga?.getOrNull()
|
||||
|
||||
val any: Manga?
|
||||
get() = remote ?: local
|
||||
|
||||
val hasRemote: Boolean
|
||||
get() = remoteManga?.isSuccess == true
|
||||
|
||||
val hasLocal: Boolean
|
||||
get() = localManga?.isSuccess == true
|
||||
|
||||
val chapters: List<MangaChapter>? by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
mergeChapters()
|
||||
}
|
||||
|
||||
fun hasChapter(id: Long): Boolean {
|
||||
return local?.chapters?.findById(id) != null || remote?.chapters?.findById(id) != null
|
||||
}
|
||||
|
||||
fun requireAny(): Manga {
|
||||
val result = remoteManga?.getOrNull() ?: localManga?.getOrNull()
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
throw (
|
||||
remoteManga?.exceptionOrNull()
|
||||
?: localManga?.exceptionOrNull()
|
||||
?: IllegalStateException("No online either local manga available")
|
||||
)
|
||||
}
|
||||
|
||||
fun filterChapters(branch: String?) = DoubleManga(
|
||||
remoteManga?.map { it.filterChapters(branch) },
|
||||
localManga?.map { it.filterChapters(branch) },
|
||||
)
|
||||
|
||||
private fun mergeChapters(): List<MangaChapter>? {
|
||||
val remoteChapters = remote?.chapters
|
||||
val localChapters = local?.chapters
|
||||
if (localChapters == null && remoteChapters == null) {
|
||||
return null
|
||||
}
|
||||
val localMap = if (!localChapters.isNullOrEmpty()) {
|
||||
localChapters.associateByTo(LinkedHashMap(localChapters.size)) { it.id }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val result = ArrayList<MangaChapter>(maxOf(remoteChapters?.size ?: 0, localChapters?.size ?: 0))
|
||||
remoteChapters?.forEach { r ->
|
||||
localMap?.remove(r.id)?.let { l ->
|
||||
result.add(l)
|
||||
} ?: result.add(r)
|
||||
}
|
||||
localMap?.values?.let {
|
||||
result.addAll(it)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.koitharu.kotatsu.details.ui
|
||||
|
||||
import android.view.InputDevice
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.View.OnLayoutChangeListener
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
@@ -12,7 +14,7 @@ class ChaptersBottomSheetMediator(
|
||||
private val behavior: BottomSheetBehavior<*>,
|
||||
) : OnBackPressedCallback(false),
|
||||
ActionModeListener,
|
||||
OnLayoutChangeListener {
|
||||
OnLayoutChangeListener, View.OnGenericMotionListener {
|
||||
|
||||
private var lockCounter = 0
|
||||
|
||||
@@ -55,6 +57,20 @@ class ChaptersBottomSheetMediator(
|
||||
}
|
||||
}
|
||||
|
||||
override fun onGenericMotion(v: View?, event: MotionEvent): Boolean {
|
||||
if (event.source and InputDevice.SOURCE_CLASS_POINTER != 0) {
|
||||
if (event.actionMasked == MotionEvent.ACTION_SCROLL) {
|
||||
if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0f) {
|
||||
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
|
||||
} else {
|
||||
behavior.state = BottomSheetBehavior.STATE_EXPANDED
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun lock() {
|
||||
lockCounter++
|
||||
behavior.isDraggable = lockCounter <= 0
|
||||
|
||||
@@ -2,33 +2,30 @@ package org.koitharu.kotatsu.details.ui
|
||||
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
|
||||
import org.koitharu.kotatsu.details.ui.model.toListItem
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||
|
||||
fun mapChapters(
|
||||
remoteManga: Manga?,
|
||||
localManga: Manga?,
|
||||
fun MangaDetails.mapChapters(
|
||||
history: MangaHistory?,
|
||||
newCount: Int,
|
||||
branch: String?,
|
||||
bookmarks: List<Bookmark>,
|
||||
): List<ChapterListItem> {
|
||||
val remoteChapters = remoteManga?.getChapters(branch).orEmpty()
|
||||
val localChapters = localManga?.getChapters(branch).orEmpty()
|
||||
val remoteChapters = chapters[branch].orEmpty()
|
||||
val localChapters = local?.manga?.getChapters(branch).orEmpty()
|
||||
if (remoteChapters.isEmpty() && localChapters.isEmpty()) {
|
||||
return emptyList()
|
||||
}
|
||||
val bookmarked = bookmarks.mapToSet { it.chapterId }
|
||||
val currentId = history?.chapterId ?: 0L
|
||||
val newFrom = if (newCount == 0 || remoteChapters.isEmpty()) Int.MAX_VALUE else remoteChapters.size - newCount
|
||||
val chaptersSize = maxOf(remoteChapters.size, localChapters.size)
|
||||
val ids = buildSet(chaptersSize) {
|
||||
val ids = buildSet(maxOf(remoteChapters.size, localChapters.size)) {
|
||||
remoteChapters.mapTo(this) { it.id }
|
||||
localChapters.mapTo(this) { it.id }
|
||||
}
|
||||
val result = ArrayList<ChapterListItem>(chaptersSize)
|
||||
val result = ArrayList<ChapterListItem>(ids.size)
|
||||
val localMap = if (localChapters.isNotEmpty()) {
|
||||
localChapters.associateByTo(LinkedHashMap(localChapters.size)) { it.id }
|
||||
} else {
|
||||
@@ -40,7 +37,7 @@ fun mapChapters(
|
||||
if (chapter.id == currentId) {
|
||||
isUnread = true
|
||||
}
|
||||
result += chapter.toListItem(
|
||||
result += (local ?: chapter).toListItem(
|
||||
isCurrent = chapter.id == currentId,
|
||||
isUnread = isUnread,
|
||||
isNew = isUnread && result.size >= newFrom,
|
||||
@@ -57,7 +54,7 @@ fun mapChapters(
|
||||
isCurrent = chapter.id == currentId,
|
||||
isUnread = isUnread,
|
||||
isNew = false,
|
||||
isDownloaded = remoteManga != null,
|
||||
isDownloaded = !isLocal,
|
||||
isBookmarked = chapter.id in bookmarked,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -39,7 +39,6 @@ import org.koitharu.kotatsu.core.os.AppShortcutManager
|
||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||
import org.koitharu.kotatsu.core.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
||||
import org.koitharu.kotatsu.core.util.ViewBadge
|
||||
import org.koitharu.kotatsu.core.util.ext.doOnExpansionsChanged
|
||||
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
|
||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||
@@ -49,6 +48,7 @@ import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||
import org.koitharu.kotatsu.core.util.ext.setNavigationBarTransparentCompat
|
||||
import org.koitharu.kotatsu.core.util.ext.setNavigationIconSafe
|
||||
import org.koitharu.kotatsu.core.util.ext.setOnContextClickListenerCompat
|
||||
import org.koitharu.kotatsu.core.util.ext.textAndVisible
|
||||
import org.koitharu.kotatsu.databinding.ActivityDetailsBinding
|
||||
import org.koitharu.kotatsu.details.service.MangaPrefetchService
|
||||
@@ -74,7 +74,6 @@ class DetailsActivity :
|
||||
@Inject
|
||||
lateinit var appShortcutManager: AppShortcutManager
|
||||
|
||||
private lateinit var viewBadge: ViewBadge
|
||||
private var buttonTip: WeakReference<ButtonTip>? = null
|
||||
|
||||
private val viewModel: DetailsViewModel by viewModels()
|
||||
@@ -89,8 +88,8 @@ class DetailsActivity :
|
||||
}
|
||||
viewBinding.buttonRead.setOnClickListener(this)
|
||||
viewBinding.buttonRead.setOnLongClickListener(this)
|
||||
viewBinding.buttonRead.setOnContextClickListenerCompat(this)
|
||||
viewBinding.buttonDropdown.setOnClickListener(this)
|
||||
viewBadge = ViewBadge(viewBinding.buttonRead, this)
|
||||
|
||||
if (viewBinding.layoutBottom != null) {
|
||||
val behavior = BottomSheetBehavior.from(checkNotNull(viewBinding.layoutBottom))
|
||||
@@ -103,6 +102,7 @@ class DetailsActivity :
|
||||
viewBinding.toolbarChapters?.setNavigationOnClickListener {
|
||||
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
|
||||
}
|
||||
viewBinding.toolbarChapters?.setOnGenericMotionListener(bsMediator)
|
||||
} else {
|
||||
chaptersMenuProvider = ChaptersMenuProvider(viewModel, null)
|
||||
addMenuProvider(chaptersMenuProvider)
|
||||
@@ -110,7 +110,6 @@ class DetailsActivity :
|
||||
onBackPressedDispatcher.addCallback(chaptersMenuProvider)
|
||||
|
||||
viewModel.manga.filterNotNull().observe(this, ::onMangaUpdated)
|
||||
viewModel.newChaptersCount.observe(this, ::onNewChaptersChanged)
|
||||
viewModel.onMangaRemoved.observeEvent(this, ::onMangaRemoved)
|
||||
viewModel.onError.observeEvent(
|
||||
this,
|
||||
@@ -134,13 +133,21 @@ class DetailsActivity :
|
||||
viewBinding.toolbarChapters?.subtitle = it
|
||||
viewBinding.textViewSubtitle?.textAndVisible = it
|
||||
}
|
||||
viewModel.isChaptersReversed.observe(this, MenuInvalidator(viewBinding.toolbarChapters ?: this))
|
||||
viewModel.favouriteCategories.observe(this, MenuInvalidator(this))
|
||||
viewModel.isChaptersReversed.observe(
|
||||
this,
|
||||
MenuInvalidator(viewBinding.toolbarChapters ?: this),
|
||||
)
|
||||
val menuInvalidator = MenuInvalidator(this)
|
||||
viewModel.favouriteCategories.observe(this, menuInvalidator)
|
||||
viewModel.remoteManga.observe(this, menuInvalidator)
|
||||
viewModel.branches.observe(this) {
|
||||
viewBinding.buttonDropdown.isVisible = it.size > 1
|
||||
}
|
||||
viewModel.chapters.observe(this, PrefetchObserver(this))
|
||||
viewModel.onDownloadStarted.observeEvent(this, DownloadStartedObserver(viewBinding.containerDetails))
|
||||
viewModel.onDownloadStarted.observeEvent(
|
||||
this,
|
||||
DownloadStartedObserver(viewBinding.containerDetails),
|
||||
)
|
||||
|
||||
addMenuProvider(
|
||||
DetailsMenuProvider(
|
||||
@@ -243,7 +250,11 @@ class DetailsActivity :
|
||||
right = insets.right,
|
||||
)
|
||||
if (insets.bottom > 0) {
|
||||
window.setNavigationBarTransparentCompat(this, viewBinding.layoutBottom?.elevation ?: 0f, 0.9f)
|
||||
window.setNavigationBarTransparentCompat(
|
||||
this,
|
||||
viewBinding.layoutBottom?.elevation ?: 0f,
|
||||
0.9f,
|
||||
)
|
||||
}
|
||||
viewBinding.cardChapters?.updateLayoutParams<MarginLayoutParams> {
|
||||
bottomMargin = insets.bottom + marginEnd
|
||||
@@ -265,18 +276,23 @@ class DetailsActivity :
|
||||
}
|
||||
val text = when {
|
||||
!info.isValid -> getString(R.string.loading_)
|
||||
info.currentChapter >= 0 -> getString(R.string.chapter_d_of_d, info.currentChapter + 1, info.totalChapters)
|
||||
info.currentChapter >= 0 -> getString(
|
||||
R.string.chapter_d_of_d,
|
||||
info.currentChapter + 1,
|
||||
info.totalChapters,
|
||||
)
|
||||
|
||||
info.totalChapters == 0 -> getString(R.string.no_chapters)
|
||||
else -> resources.getQuantityString(R.plurals.chapters, info.totalChapters, info.totalChapters)
|
||||
else -> resources.getQuantityString(
|
||||
R.plurals.chapters,
|
||||
info.totalChapters,
|
||||
info.totalChapters,
|
||||
)
|
||||
}
|
||||
viewBinding.toolbarChapters?.title = text
|
||||
viewBinding.textViewTitle?.text = text
|
||||
}
|
||||
|
||||
private fun onNewChaptersChanged(newChapters: Int) {
|
||||
viewBadge.counter = newChapters
|
||||
}
|
||||
|
||||
private fun showBranchPopupMenu(v: View) {
|
||||
val menu = PopupMenu(v.context, v)
|
||||
val branches = viewModel.branches.value
|
||||
@@ -286,7 +302,12 @@ class DetailsActivity :
|
||||
append(' ')
|
||||
append(' ')
|
||||
inSpans(
|
||||
ForegroundColorSpan(v.context.getThemeColor(android.R.attr.textColorSecondary, Color.LTGRAY)),
|
||||
ForegroundColorSpan(
|
||||
v.context.getThemeColor(
|
||||
android.R.attr.textColorSecondary,
|
||||
Color.LTGRAY,
|
||||
),
|
||||
),
|
||||
RelativeSizeSpan(0.74f),
|
||||
) {
|
||||
append(branch.count.toString())
|
||||
@@ -305,7 +326,8 @@ class DetailsActivity :
|
||||
val manga = viewModel.manga.value ?: return
|
||||
val chapterId = viewModel.historyInfo.value.history?.chapterId
|
||||
if (chapterId != null && manga.chapters?.none { x -> x.id == chapterId } == true) {
|
||||
val snackbar = makeSnackbar(getString(R.string.chapter_is_missing), Snackbar.LENGTH_SHORT)
|
||||
val snackbar =
|
||||
makeSnackbar(getString(R.string.chapter_is_missing), Snackbar.LENGTH_SHORT)
|
||||
snackbar.show()
|
||||
} else {
|
||||
startActivity(
|
||||
@@ -331,7 +353,10 @@ class DetailsActivity :
|
||||
view.isVisible = isVisible
|
||||
}
|
||||
|
||||
private fun makeSnackbar(text: CharSequence, @BaseTransientBottomBar.Duration duration: Int): Snackbar {
|
||||
private fun makeSnackbar(
|
||||
text: CharSequence,
|
||||
@BaseTransientBottomBar.Duration duration: Int,
|
||||
): Snackbar {
|
||||
val sb = Snackbar.make(viewBinding.containerDetails, text, duration)
|
||||
if (viewBinding.layoutBottom?.isVisible == true) {
|
||||
sb.anchorView = viewBinding.toolbarChapters
|
||||
|
||||
@@ -11,6 +11,8 @@ import android.widget.Toast
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.text.buildSpannedString
|
||||
import androidx.core.text.color
|
||||
import androidx.core.view.isGone
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updatePadding
|
||||
@@ -21,6 +23,7 @@ import coil.request.SuccessResult
|
||||
import coil.util.CoilUtils
|
||||
import com.google.android.material.chip.Chip
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
@@ -37,6 +40,7 @@ import org.koitharu.kotatsu.core.util.FileSize
|
||||
import org.koitharu.kotatsu.core.util.ext.crossfade
|
||||
import org.koitharu.kotatsu.core.util.ext.drawableTop
|
||||
import org.koitharu.kotatsu.core.util.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
|
||||
import org.koitharu.kotatsu.core.util.ext.isTextTruncated
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
@@ -70,13 +74,14 @@ import org.koitharu.kotatsu.scrobbling.common.ui.selector.ScrobblingSelectorShee
|
||||
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
||||
import org.koitharu.kotatsu.search.ui.SearchActivity
|
||||
import javax.inject.Inject
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
@AndroidEntryPoint
|
||||
class DetailsFragment :
|
||||
BaseFragment<FragmentDetailsBinding>(),
|
||||
View.OnClickListener,
|
||||
ChipsView.OnChipClickListener,
|
||||
OnListItemClickListener<Bookmark>, ViewTreeObserver.OnDrawListener {
|
||||
OnListItemClickListener<Bookmark>, ViewTreeObserver.OnDrawListener, View.OnLayoutChangeListener {
|
||||
|
||||
@Inject
|
||||
lateinit var coil: ImageLoader
|
||||
@@ -100,6 +105,7 @@ class DetailsFragment :
|
||||
binding.buttonScrobblingMore.setOnClickListener(this)
|
||||
binding.buttonRelatedMore.setOnClickListener(this)
|
||||
binding.infoLayout.textViewSource.setOnClickListener(this)
|
||||
binding.textViewDescription.addOnLayoutChangeListener(this)
|
||||
binding.textViewDescription.viewTreeObserver.addOnDrawListener(this)
|
||||
binding.textViewDescription.movementMethod = LinkMovementMethod.getInstance()
|
||||
binding.chipsTags.onChipClickListener = this
|
||||
@@ -113,9 +119,9 @@ class DetailsFragment :
|
||||
viewModel.bookmarks.observe(viewLifecycleOwner, ::onBookmarksChanged)
|
||||
viewModel.scrobblingInfo.observe(viewLifecycleOwner, ::onScrobblingInfoChanged)
|
||||
viewModel.description.observe(viewLifecycleOwner, ::onDescriptionChanged)
|
||||
viewModel.chapters.observe(viewLifecycleOwner, ::onChaptersChanged)
|
||||
viewModel.localSize.observe(viewLifecycleOwner, ::onLocalSizeChanged)
|
||||
viewModel.relatedManga.observe(viewLifecycleOwner, ::onRelatedMangaChanged)
|
||||
combine(viewModel.chapters, viewModel.newChaptersCount, ::Pair).observe(viewLifecycleOwner, ::onChaptersChanged)
|
||||
}
|
||||
|
||||
override fun onItemClick(item: Bookmark, view: View) {
|
||||
@@ -145,6 +151,22 @@ class DetailsFragment :
|
||||
}
|
||||
}
|
||||
|
||||
override fun onLayoutChange(
|
||||
v: View?,
|
||||
left: Int,
|
||||
top: Int,
|
||||
right: Int,
|
||||
bottom: Int,
|
||||
oldLeft: Int,
|
||||
oldTop: Int,
|
||||
oldRight: Int,
|
||||
oldBottom: Int
|
||||
) {
|
||||
with(viewBinding ?: return) {
|
||||
buttonDescriptionMore.isVisible = textViewDescription.isTextTruncated
|
||||
}
|
||||
}
|
||||
|
||||
private fun onMangaUpdated(manga: Manga) {
|
||||
with(requireViewBinding()) {
|
||||
// Main
|
||||
@@ -160,21 +182,22 @@ class DetailsFragment :
|
||||
}
|
||||
|
||||
when (manga.state) {
|
||||
MangaState.FINISHED -> {
|
||||
infoLayout.textViewState.apply {
|
||||
textAndVisible = resources.getString(R.string.state_finished)
|
||||
drawableTop = ContextCompat.getDrawable(context, R.drawable.ic_state_finished)
|
||||
}
|
||||
MangaState.FINISHED -> infoLayout.textViewState.apply {
|
||||
textAndVisible = resources.getString(R.string.state_finished)
|
||||
drawableTop = ContextCompat.getDrawable(context, R.drawable.ic_state_finished)
|
||||
}
|
||||
|
||||
MangaState.ONGOING -> {
|
||||
infoLayout.textViewState.apply {
|
||||
textAndVisible = resources.getString(R.string.state_ongoing)
|
||||
drawableTop = ContextCompat.getDrawable(context, R.drawable.ic_state_ongoing)
|
||||
}
|
||||
MangaState.ONGOING -> infoLayout.textViewState.apply {
|
||||
textAndVisible = resources.getString(R.string.state_ongoing)
|
||||
drawableTop = ContextCompat.getDrawable(context, R.drawable.ic_state_ongoing)
|
||||
}
|
||||
|
||||
else -> infoLayout.textViewState.isVisible = false
|
||||
MangaState.ABANDONED -> infoLayout.textViewState.apply {
|
||||
textAndVisible = resources.getString(R.string.state_abandoned)
|
||||
drawableTop = ContextCompat.getDrawable(context, R.drawable.ic_state_abandoned)
|
||||
}
|
||||
|
||||
null -> infoLayout.textViewState.isVisible = false
|
||||
}
|
||||
if (manga.source == MangaSource.LOCAL) {
|
||||
infoLayout.textViewSource.isVisible = false
|
||||
@@ -190,14 +213,28 @@ class DetailsFragment :
|
||||
}
|
||||
}
|
||||
|
||||
private fun onChaptersChanged(chapters: List<ChapterListItem>?) {
|
||||
private fun onChaptersChanged(data: Pair<List<ChapterListItem>?, Int>) {
|
||||
val (chapters, newChapters) = data
|
||||
val infoLayout = requireViewBinding().infoLayout
|
||||
if (chapters.isNullOrEmpty()) {
|
||||
infoLayout.textViewChapters.isVisible = false
|
||||
} else {
|
||||
val count = chapters.countChaptersByBranch()
|
||||
infoLayout.textViewChapters.isVisible = true
|
||||
infoLayout.textViewChapters.text = resources.getQuantityString(R.plurals.chapters, count, count)
|
||||
val chaptersText = resources.getQuantityString(R.plurals.chapters, count, count)
|
||||
infoLayout.textViewChapters.text = if (newChapters == 0) {
|
||||
chaptersText
|
||||
} else {
|
||||
buildSpannedString {
|
||||
append(chaptersText)
|
||||
append(' ')
|
||||
color(infoLayout.textViewChapters.context.getThemeColor(materialR.attr.colorError)) {
|
||||
append("(+")
|
||||
append(newChapters.toString())
|
||||
append(')')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +245,6 @@ class DetailsFragment :
|
||||
} else {
|
||||
tv.text = description
|
||||
}
|
||||
requireViewBinding().buttonDescriptionMore.isVisible = tv.isTextTruncated
|
||||
}
|
||||
|
||||
private fun onLocalSizeChanged(size: Long) {
|
||||
|
||||
@@ -42,6 +42,7 @@ class DetailsMenuProvider(
|
||||
menu.findItem(R.id.action_browser).isVisible = manga?.source != MangaSource.LOCAL
|
||||
menu.findItem(R.id.action_shortcut).isVisible = ShortcutManagerCompat.isRequestPinShortcutSupported(activity)
|
||||
menu.findItem(R.id.action_scrobbling).isVisible = viewModel.isScrobblingAvailable
|
||||
menu.findItem(R.id.action_online).isVisible = viewModel.remoteManga.value != null
|
||||
menu.findItem(R.id.action_favourite).setIcon(
|
||||
if (viewModel.favouriteCategories.value) R.drawable.ic_heart else R.drawable.ic_heart_outline,
|
||||
)
|
||||
@@ -88,6 +89,12 @@ class DetailsMenuProvider(
|
||||
}
|
||||
}
|
||||
|
||||
R.id.action_online -> {
|
||||
viewModel.remoteManga.value?.let {
|
||||
activity.startActivity(DetailsActivity.newIntent(activity, it))
|
||||
}
|
||||
}
|
||||
|
||||
R.id.action_related -> {
|
||||
viewModel.manga.value?.let {
|
||||
activity.startActivity(MultiSearchActivity.newIntent(activity, it.title))
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
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.net.toUri
|
||||
import androidx.core.text.getSpans
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -17,22 +10,21 @@ import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filterNot
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.transformLatest
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.plus
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
|
||||
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
||||
import org.koitharu.kotatsu.core.os.NetworkState
|
||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||
@@ -40,17 +32,15 @@ import org.koitharu.kotatsu.core.prefs.observeAsStateFlow
|
||||
import org.koitharu.kotatsu.core.ui.BaseViewModel
|
||||
import org.koitharu.kotatsu.core.util.ext.MutableEventFlow
|
||||
import org.koitharu.kotatsu.core.util.ext.call
|
||||
import org.koitharu.kotatsu.core.util.ext.combine
|
||||
import org.koitharu.kotatsu.core.util.ext.computeSize
|
||||
import org.koitharu.kotatsu.core.util.ext.onFirst
|
||||
import org.koitharu.kotatsu.core.util.ext.onEachWhile
|
||||
import org.koitharu.kotatsu.core.util.ext.requireValue
|
||||
import org.koitharu.kotatsu.core.util.ext.sanitize
|
||||
import org.koitharu.kotatsu.core.util.ext.toFileOrNull
|
||||
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||
import org.koitharu.kotatsu.details.domain.BranchComparator
|
||||
import org.koitharu.kotatsu.details.domain.DetailsInteractor
|
||||
import org.koitharu.kotatsu.details.domain.DoubleMangaLoadUseCase
|
||||
import org.koitharu.kotatsu.details.domain.DetailsLoadUseCase
|
||||
import org.koitharu.kotatsu.details.domain.ProgressUpdateUseCase
|
||||
import org.koitharu.kotatsu.details.domain.RelatedMangaUseCase
|
||||
import org.koitharu.kotatsu.details.domain.model.DoubleManga
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
|
||||
import org.koitharu.kotatsu.details.ui.model.HistoryInfo
|
||||
import org.koitharu.kotatsu.details.ui.model.MangaBranch
|
||||
@@ -74,22 +64,19 @@ class DetailsViewModel @Inject constructor(
|
||||
private val bookmarksRepository: BookmarksRepository,
|
||||
private val settings: AppSettings,
|
||||
private val scrobblers: Set<@JvmSuppressWildcards Scrobbler>,
|
||||
private val imageGetter: Html.ImageGetter,
|
||||
@LocalStorageChanges private val localStorageChanges: SharedFlow<LocalManga?>,
|
||||
private val downloadScheduler: DownloadWorker.Scheduler,
|
||||
private val interactor: DetailsInteractor,
|
||||
savedStateHandle: SavedStateHandle,
|
||||
private val deleteLocalMangaUseCase: DeleteLocalMangaUseCase,
|
||||
private val doubleMangaLoadUseCase: DoubleMangaLoadUseCase,
|
||||
private val relatedMangaUseCase: RelatedMangaUseCase,
|
||||
private val extraProvider: ListExtraProvider,
|
||||
networkState: NetworkState,
|
||||
private val detailsLoadUseCase: DetailsLoadUseCase,
|
||||
private val progressUpdateUseCase: ProgressUpdateUseCase,
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val intent = MangaIntent(savedStateHandle)
|
||||
private val mangaId = intent.mangaId
|
||||
private val doubleManga: MutableStateFlow<DoubleManga?> =
|
||||
MutableStateFlow(intent.manga?.let { DoubleManga(it) })
|
||||
private var loadingJob: Job
|
||||
|
||||
val onShowToast = MutableEventFlow<Int>()
|
||||
@@ -97,8 +84,9 @@ class DetailsViewModel @Inject constructor(
|
||||
val onSelectChapter = MutableEventFlow<Long>()
|
||||
val onDownloadStarted = MutableEventFlow<Unit>()
|
||||
|
||||
val manga = doubleManga.map { it?.any }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, doubleManga.value?.any)
|
||||
val details = MutableStateFlow(intent.manga?.let { MangaDetails(it, null, null, false) })
|
||||
val manga = details.map { x -> x?.toManga() }
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null)
|
||||
|
||||
val history = historyRepository.observeOne(mangaId)
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null)
|
||||
@@ -106,8 +94,15 @@ class DetailsViewModel @Inject constructor(
|
||||
val favouriteCategories = interactor.observeIsFavourite(mangaId)
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, false)
|
||||
|
||||
val newChaptersCount = interactor.observeNewChapters(mangaId)
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0)
|
||||
val remoteManga = MutableStateFlow<Manga?>(null)
|
||||
|
||||
val newChaptersCount = details.flatMapLatest { d ->
|
||||
if (d?.isLocal == false) {
|
||||
interactor.observeNewChapters(mangaId)
|
||||
} else {
|
||||
flowOf(0)
|
||||
}
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0)
|
||||
|
||||
private val chaptersQuery = MutableStateFlow("")
|
||||
val selectedBranch = MutableStateFlow<String?>(null)
|
||||
@@ -135,28 +130,17 @@ class DetailsViewModel @Inject constructor(
|
||||
if (it != null) bookmarksRepository.observeBookmarks(it) else flowOf(emptyList())
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Lazily, emptyList())
|
||||
|
||||
val localSize = doubleManga
|
||||
.map {
|
||||
val local = it?.local
|
||||
if (local != null) {
|
||||
val file = local.url.toUri().toFileOrNull()
|
||||
file?.computeSize() ?: 0L
|
||||
} else {
|
||||
0L
|
||||
}
|
||||
val localSize = details
|
||||
.map { it?.local }
|
||||
.distinctUntilChanged()
|
||||
.map { local ->
|
||||
local?.file?.computeSize() ?: 0L
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.WhileSubscribed(), 0)
|
||||
|
||||
val description = manga
|
||||
.distinctUntilChangedBy { it?.description.orEmpty() }
|
||||
.transformLatest {
|
||||
val description = it?.description
|
||||
if (description.isNullOrEmpty()) {
|
||||
emit(null)
|
||||
} else {
|
||||
emit(description.parseAsHtml().filterSpans().sanitize())
|
||||
emit(description.parseAsHtml(imageGetter = imageGetter).filterSpans())
|
||||
}
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.WhileSubscribed(5000), null)
|
||||
@Deprecated("")
|
||||
val description = details
|
||||
.map { it?.description }
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Lazily, null)
|
||||
|
||||
val onMangaRemoved = MutableEventFlow<Manga>()
|
||||
val isScrobblingAvailable: Boolean
|
||||
@@ -165,53 +149,41 @@ class DetailsViewModel @Inject constructor(
|
||||
val scrobblingInfo: StateFlow<List<ScrobblingInfo>> = interactor.observeScrobblingInfo(mangaId)
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
val relatedManga: StateFlow<List<MangaItemModel>> = doubleManga.map {
|
||||
it?.remote
|
||||
}.distinctUntilChangedBy { it?.id }
|
||||
.mapLatest {
|
||||
if (it != null && settings.isRelatedMangaEnabled) {
|
||||
relatedMangaUseCase.invoke(it)?.toUi(ListMode.GRID, extraProvider).orEmpty()
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
val relatedManga: StateFlow<List<MangaItemModel>> = manga.mapLatest {
|
||||
if (it != null && settings.isRelatedMangaEnabled) {
|
||||
relatedMangaUseCase.invoke(it)?.toUi(ListMode.GRID, extraProvider).orEmpty()
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
|
||||
}.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
|
||||
|
||||
val branches: StateFlow<List<MangaBranch>> = combine(
|
||||
doubleManga,
|
||||
details,
|
||||
selectedBranch,
|
||||
) { m, b ->
|
||||
val chapters = m?.chapters
|
||||
if (chapters.isNullOrEmpty()) return@combine emptyList()
|
||||
chapters.groupBy { x -> x.branch }
|
||||
(m?.chapters ?: return@combine emptyList())
|
||||
.map { x -> MangaBranch(x.key, x.value.size, x.key == b) }
|
||||
.sortedWith(BranchComparator())
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
val isChaptersEmpty: StateFlow<Boolean> = combine(
|
||||
doubleManga,
|
||||
isLoading,
|
||||
) { manga, loading ->
|
||||
manga?.any != null && manga.chapters.isNullOrEmpty() && !loading
|
||||
val isChaptersEmpty: StateFlow<Boolean> = details.map {
|
||||
it != null && it.isLoaded && it.allChapters.isEmpty()
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), false)
|
||||
|
||||
val chapters = combine(
|
||||
combine(
|
||||
doubleManga,
|
||||
details,
|
||||
history,
|
||||
selectedBranch,
|
||||
newChaptersCount,
|
||||
bookmarks,
|
||||
networkState,
|
||||
) { manga, history, branch, news, bookmarks, isOnline ->
|
||||
mapChapters(
|
||||
manga?.remote?.takeIf { isOnline },
|
||||
manga?.local,
|
||||
) { manga, history, branch, news, bookmarks ->
|
||||
manga?.mapChapters(
|
||||
history,
|
||||
news,
|
||||
branch,
|
||||
bookmarks,
|
||||
)
|
||||
).orEmpty()
|
||||
},
|
||||
isChaptersReversed,
|
||||
chaptersQuery,
|
||||
@@ -234,6 +206,17 @@ class DetailsViewModel @Inject constructor(
|
||||
onShowTip.call(Unit)
|
||||
}
|
||||
}
|
||||
launchJob(Dispatchers.Default) {
|
||||
val manga = details.firstOrNull { !it?.chapters.isNullOrEmpty() } ?: return@launchJob
|
||||
val h = history.firstOrNull()
|
||||
if (h != null) {
|
||||
progressUpdateUseCase(manga.toManga())
|
||||
}
|
||||
}
|
||||
launchJob(Dispatchers.Default) {
|
||||
val manga = details.firstOrNull { it != null && it.isLocal } ?: return@launchJob
|
||||
remoteManga.value = interactor.findRemote(manga.toManga())
|
||||
}
|
||||
}
|
||||
|
||||
fun reload() {
|
||||
@@ -242,7 +225,7 @@ class DetailsViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun deleteLocal() {
|
||||
val m = doubleManga.value?.local
|
||||
val m = details.value?.local?.manga
|
||||
if (m == null) {
|
||||
onShowToast.call(R.string.file_not_found)
|
||||
return
|
||||
@@ -295,13 +278,13 @@ class DetailsViewModel @Inject constructor(
|
||||
|
||||
fun markChapterAsCurrent(chapterId: Long) {
|
||||
launchJob(Dispatchers.Default) {
|
||||
val manga = checkNotNull(doubleManga.value)
|
||||
val chapters = checkNotNull(manga.filterChapters(selectedBranchValue).chapters)
|
||||
val manga = checkNotNull(details.value)
|
||||
val chapters = checkNotNull(manga.chapters[selectedBranchValue])
|
||||
val chapterIndex = chapters.indexOfFirst { it.id == chapterId }
|
||||
check(chapterIndex in chapters.indices) { "Chapter not found" }
|
||||
val percent = chapterIndex / chapters.size.toFloat()
|
||||
historyRepository.addOrUpdate(
|
||||
manga = manga.requireAny(),
|
||||
manga = manga.toManga(),
|
||||
chapterId = chapterId,
|
||||
page = 0,
|
||||
scroll = 0,
|
||||
@@ -313,7 +296,7 @@ class DetailsViewModel @Inject constructor(
|
||||
fun download(chaptersIds: Set<Long>?) {
|
||||
launchJob(Dispatchers.Default) {
|
||||
downloadScheduler.schedule(
|
||||
doubleManga.requireValue().requireAny(),
|
||||
details.requireValue().toManga(),
|
||||
chaptersIds,
|
||||
)
|
||||
onDownloadStarted.call(Unit)
|
||||
@@ -333,14 +316,18 @@ class DetailsViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun doLoad() = launchLoadingJob(Dispatchers.Default) {
|
||||
doubleMangaLoadUseCase.invoke(intent)
|
||||
.onFirst {
|
||||
val manga = it.requireAny()
|
||||
detailsLoadUseCase.invoke(intent)
|
||||
.onEachWhile {
|
||||
if (it.allChapters.isEmpty()) {
|
||||
return@onEachWhile false
|
||||
}
|
||||
val manga = it.toManga()
|
||||
// find default branch
|
||||
val hist = historyRepository.getOne(manga)
|
||||
selectedBranch.value = manga.getPreferredBranch(hist)
|
||||
true
|
||||
}.collect {
|
||||
doubleManga.value = it
|
||||
details.value = it
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,21 +343,12 @@ class DetailsViewModel @Inject constructor(
|
||||
private suspend fun onDownloadComplete(downloadedManga: LocalManga?) {
|
||||
downloadedManga ?: return
|
||||
launchJob {
|
||||
doubleManga.update {
|
||||
details.update {
|
||||
interactor.updateLocal(it, downloadedManga)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
private fun getScrobbler(index: Int): Scrobbler? {
|
||||
val info = scrobblingInfo.value.getOrNull(index)
|
||||
val scrobbler = if (info != null) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.list.AdapterDelegateClickListenerAdapter
|
||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
import org.koitharu.kotatsu.core.util.ext.drawableEnd
|
||||
import org.koitharu.kotatsu.core.util.ext.drawableStart
|
||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||
import org.koitharu.kotatsu.core.util.ext.textAndVisible
|
||||
@@ -47,7 +48,6 @@ fun chapterListItemAD(
|
||||
}
|
||||
binding.imageViewBookmarked.isVisible = item.isBookmarked
|
||||
binding.imageViewDownloaded.isVisible = item.isDownloaded
|
||||
// binding.imageViewNew.isVisible = item.isNew
|
||||
binding.textViewTitle.drawableStart = if (item.isNew) {
|
||||
ContextCompat.getDrawable(context, R.drawable.ic_new)
|
||||
} else {
|
||||
|
||||
@@ -18,7 +18,7 @@ data class DownloadState(
|
||||
val currentPage: Int = 0,
|
||||
val eta: Long = -1L,
|
||||
val localManga: LocalManga? = null,
|
||||
val downloadedChapters: LongArray = LongArray(0),
|
||||
val downloadedChapters: Int = 0,
|
||||
val timestamp: Long = System.currentTimeMillis(),
|
||||
) {
|
||||
|
||||
@@ -41,61 +41,17 @@ data class DownloadState(
|
||||
.putLong(DATA_ETA, eta)
|
||||
.putLong(DATA_TIMESTAMP, timestamp)
|
||||
.putString(DATA_ERROR, error)
|
||||
.putLongArray(DATA_CHAPTERS, downloadedChapters)
|
||||
.putInt(DATA_CHAPTERS, downloadedChapters)
|
||||
.putBoolean(DATA_INDETERMINATE, isIndeterminate)
|
||||
.putBoolean(DATA_PAUSED, isPaused)
|
||||
.build()
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as DownloadState
|
||||
|
||||
if (manga != other.manga) return false
|
||||
if (isIndeterminate != other.isIndeterminate) return false
|
||||
if (isPaused != other.isPaused) return false
|
||||
if (isStopped != other.isStopped) return false
|
||||
if (error != other.error) return false
|
||||
if (totalChapters != other.totalChapters) return false
|
||||
if (currentChapter != other.currentChapter) return false
|
||||
if (totalPages != other.totalPages) return false
|
||||
if (currentPage != other.currentPage) return false
|
||||
if (eta != other.eta) return false
|
||||
if (localManga != other.localManga) return false
|
||||
if (!downloadedChapters.contentEquals(other.downloadedChapters)) return false
|
||||
if (timestamp != other.timestamp) return false
|
||||
if (max != other.max) return false
|
||||
if (progress != other.progress) return false
|
||||
return percent == other.percent
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = manga.hashCode()
|
||||
result = 31 * result + isIndeterminate.hashCode()
|
||||
result = 31 * result + isPaused.hashCode()
|
||||
result = 31 * result + isStopped.hashCode()
|
||||
result = 31 * result + (error?.hashCode() ?: 0)
|
||||
result = 31 * result + totalChapters
|
||||
result = 31 * result + currentChapter
|
||||
result = 31 * result + totalPages
|
||||
result = 31 * result + currentPage
|
||||
result = 31 * result + eta.hashCode()
|
||||
result = 31 * result + (localManga?.hashCode() ?: 0)
|
||||
result = 31 * result + downloadedChapters.contentHashCode()
|
||||
result = 31 * result + timestamp.hashCode()
|
||||
result = 31 * result + max
|
||||
result = 31 * result + progress
|
||||
result = 31 * result + percent.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val DATA_MANGA_ID = "manga_id"
|
||||
private const val DATA_MAX = "max"
|
||||
private const val DATA_PROGRESS = "progress"
|
||||
private const val DATA_CHAPTERS = "chapter"
|
||||
private const val DATA_CHAPTERS = "chapter_cnt"
|
||||
private const val DATA_ETA = "eta"
|
||||
private const val DATA_TIMESTAMP = "timestamp"
|
||||
private const val DATA_ERROR = "error"
|
||||
@@ -118,6 +74,6 @@ data class DownloadState(
|
||||
|
||||
fun getTimestamp(data: Data): Date = Date(data.getLong(DATA_TIMESTAMP, 0L))
|
||||
|
||||
fun getDownloadedChapters(data: Data): LongArray = data.getLongArray(DATA_CHAPTERS) ?: LongArray(0)
|
||||
fun getDownloadedChapters(data: Data): Int = data.getInt(DATA_CHAPTERS, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
package org.koitharu.kotatsu.download.ui.list
|
||||
|
||||
import android.transition.TransitionManager
|
||||
import android.view.View
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.work.WorkInfo
|
||||
import coil.ImageLoader
|
||||
import coil.request.SuccessResult
|
||||
import coil.util.CoilUtils
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.image.TrimTransformation
|
||||
import org.koitharu.kotatsu.core.util.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled
|
||||
import org.koitharu.kotatsu.core.util.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.core.util.ext.source
|
||||
import org.koitharu.kotatsu.core.util.ext.textAndVisible
|
||||
import org.koitharu.kotatsu.databinding.ItemDownloadBinding
|
||||
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.parsers.util.format
|
||||
|
||||
@@ -25,6 +30,7 @@ fun downloadItemAD(
|
||||
) {
|
||||
|
||||
val percentPattern = context.resources.getString(R.string.percent_string_pattern)
|
||||
// val expandIcon = ContextCompat.getDrawable(context, R.drawable.ic_expand_collapse)
|
||||
|
||||
val clickListener = object : View.OnClickListener, View.OnLongClickListener {
|
||||
override fun onClick(v: View) {
|
||||
@@ -47,16 +53,24 @@ fun downloadItemAD(
|
||||
itemView.setOnLongClickListener(clickListener)
|
||||
|
||||
bind { payloads ->
|
||||
binding.textViewTitle.text = item.manga.title
|
||||
binding.imageViewCover.newImageRequest(lifecycleOwner, item.manga.coverUrl)?.apply {
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_error_placeholder)
|
||||
allowRgb565(true)
|
||||
transformations(TrimTransformation())
|
||||
source(item.manga.source)
|
||||
enqueueWith(coil)
|
||||
if (ListModelDiffCallback.PAYLOAD_CHECKED_CHANGED in payloads && context.isAnimationsEnabled) {
|
||||
TransitionManager.beginDelayedTransition(binding.constraintLayout)
|
||||
}
|
||||
binding.textViewTitle.text = item.manga.title
|
||||
if ((CoilUtils.result(binding.imageViewCover) as? SuccessResult)?.memoryCacheKey != item.coverCacheKey) {
|
||||
binding.imageViewCover.newImageRequest(lifecycleOwner, item.manga.coverUrl)?.apply {
|
||||
placeholder(R.drawable.ic_placeholder)
|
||||
fallback(R.drawable.ic_placeholder)
|
||||
error(R.drawable.ic_error_placeholder)
|
||||
allowRgb565(true)
|
||||
transformations(TrimTransformation())
|
||||
memoryCacheKey(item.coverCacheKey)
|
||||
source(item.manga.source)
|
||||
enqueueWith(coil)
|
||||
}
|
||||
}
|
||||
// binding.textViewTitle.isChecked = item.isExpanded
|
||||
// binding.textViewTitle.drawableEnd = if (item.isExpandable) expandIcon else null
|
||||
when (item.workState) {
|
||||
WorkInfo.State.ENQUEUED,
|
||||
WorkInfo.State.BLOCKED -> {
|
||||
@@ -94,11 +108,11 @@ fun downloadItemAD(
|
||||
binding.progressBar.isVisible = false
|
||||
binding.progressBar.isEnabled = true
|
||||
binding.textViewPercent.isVisible = false
|
||||
if (item.totalChapters > 0) {
|
||||
if (item.chaptersDownloaded > 0) {
|
||||
binding.textViewDetails.text = context.resources.getQuantityString(
|
||||
R.plurals.chapters,
|
||||
item.totalChapters,
|
||||
item.totalChapters,
|
||||
item.chaptersDownloaded,
|
||||
item.chaptersDownloaded,
|
||||
)
|
||||
binding.textViewDetails.isVisible = true
|
||||
} else {
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.koitharu.kotatsu.download.ui.list
|
||||
|
||||
import android.text.format.DateUtils
|
||||
import androidx.work.WorkInfo
|
||||
import coil.memory.MemoryCache
|
||||
import org.koitharu.kotatsu.list.ui.ListModelDiffCallback
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import java.util.Date
|
||||
@@ -15,12 +17,15 @@ data class DownloadItemModel(
|
||||
val manga: Manga,
|
||||
val error: String?,
|
||||
val max: Int,
|
||||
val totalChapters: Int,
|
||||
val progress: Int,
|
||||
val eta: Long,
|
||||
val timestamp: Date,
|
||||
val chaptersDownloaded: Int,
|
||||
val isExpanded: Boolean,
|
||||
) : ListModel, Comparable<DownloadItemModel> {
|
||||
|
||||
val coverCacheKey = MemoryCache.Key(manga.coverUrl, mapOf("dl" to "1"))
|
||||
|
||||
val percent: Float
|
||||
get() = if (max > 0) progress / max.toFloat() else 0f
|
||||
|
||||
@@ -33,6 +38,9 @@ data class DownloadItemModel(
|
||||
val canResume: Boolean
|
||||
get() = workState == WorkInfo.State.RUNNING && isPaused
|
||||
|
||||
val isExpandable: Boolean
|
||||
get() = false // TODO
|
||||
|
||||
fun getEtaString(): CharSequence? = if (hasEta) {
|
||||
DateUtils.getRelativeTimeSpanString(
|
||||
eta,
|
||||
@@ -51,17 +59,10 @@ data class DownloadItemModel(
|
||||
return other is DownloadItemModel && other.id == id
|
||||
}
|
||||
|
||||
override fun getChangePayload(previousState: ListModel): Any? {
|
||||
return when (previousState) {
|
||||
is DownloadItemModel -> {
|
||||
if (workState == previousState.workState) {
|
||||
Unit
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
else -> super.getChangePayload(previousState)
|
||||
}
|
||||
override fun getChangePayload(previousState: ListModel): Any? = when {
|
||||
previousState !is DownloadItemModel -> super.getChangePayload(previousState)
|
||||
workState != previousState.workState -> null
|
||||
isExpanded != previousState.isExpanded -> ListModelDiffCallback.PAYLOAD_CHECKED_CHANGED
|
||||
else -> ListModelDiffCallback.PAYLOAD_ANYTHING_CHANGED
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.core.ui.list.ListSelectionController
|
||||
import org.koitharu.kotatsu.core.ui.list.RecyclerScrollKeeper
|
||||
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleActionObserver
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
@@ -53,6 +54,7 @@ class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>(),
|
||||
addItemDecoration(decoration)
|
||||
adapter = downloadsAdapter
|
||||
selectionController.attachToRecyclerView(this)
|
||||
RecyclerScrollKeeper(this).attach()
|
||||
}
|
||||
addMenuProvider(DownloadsMenuProvider(this, viewModel))
|
||||
viewModel.items.observe(this) {
|
||||
@@ -82,7 +84,11 @@ class DownloadsActivity : BaseActivity<ActivityDownloadsBinding>(),
|
||||
if (selectionController.onItemClick(item.id.mostSignificantBits)) {
|
||||
return
|
||||
}
|
||||
startActivity(DetailsActivity.newIntent(view.context, item.manga))
|
||||
if (item.isExpandable) {
|
||||
viewModel.expandCollapse(item)
|
||||
} else {
|
||||
startActivity(DetailsActivity.newIntent(view.context, item.manga))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onItemLongClick(item: DownloadItemModel, view: View): Boolean {
|
||||
|
||||
@@ -8,15 +8,19 @@ import androidx.work.Data
|
||||
import androidx.work.WorkInfo
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.plus
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
import org.koitharu.kotatsu.core.ui.BaseViewModel
|
||||
import org.koitharu.kotatsu.core.ui.model.DateTimeAgo
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleAction
|
||||
@@ -31,6 +35,7 @@ import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.list.ui.model.LoadingState
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
import java.util.Date
|
||||
import java.util.LinkedList
|
||||
import java.util.UUID
|
||||
@@ -41,13 +46,18 @@ import javax.inject.Inject
|
||||
class DownloadsViewModel @Inject constructor(
|
||||
private val workScheduler: DownloadWorker.Scheduler,
|
||||
private val mangaDataRepository: MangaDataRepository,
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val mangaCache = LongSparseArray<Manga>()
|
||||
private val cacheMutex = Mutex()
|
||||
private val works = workScheduler.observeWorks()
|
||||
.mapLatest { it.toDownloadsList() }
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null)
|
||||
private val expanded = MutableStateFlow(emptySet<UUID>())
|
||||
private val works = combine(
|
||||
workScheduler.observeWorks(),
|
||||
expanded,
|
||||
) { list, exp ->
|
||||
list.toDownloadsList(exp)
|
||||
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, null)
|
||||
|
||||
val onActionDone = MutableEventFlow<ReversibleAction>()
|
||||
|
||||
@@ -169,11 +179,21 @@ class DownloadsViewModel @Inject constructor(
|
||||
it.id.mostSignificantBits
|
||||
} ?: emptySet()
|
||||
|
||||
private suspend fun List<WorkInfo>.toDownloadsList(): List<DownloadItemModel> {
|
||||
fun expandCollapse(item: DownloadItemModel) {
|
||||
expanded.update {
|
||||
if (item.id in it) {
|
||||
it - item.id
|
||||
} else {
|
||||
it + item.id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun List<WorkInfo>.toDownloadsList(exp: Set<UUID>): List<DownloadItemModel> {
|
||||
if (isEmpty()) {
|
||||
return emptyList()
|
||||
}
|
||||
val list = mapNotNullTo(ArrayList(size)) { it.toUiModel() }
|
||||
val list = mapNotNullTo(ArrayList(size)) { it.toUiModel(it.id in exp) }
|
||||
list.sortByDescending { it.timestamp }
|
||||
return list
|
||||
}
|
||||
@@ -213,7 +233,7 @@ class DownloadsViewModel @Inject constructor(
|
||||
return destination
|
||||
}
|
||||
|
||||
private suspend fun WorkInfo.toUiModel(): DownloadItemModel? {
|
||||
private suspend fun WorkInfo.toUiModel(isExpanded: Boolean): DownloadItemModel? {
|
||||
val workData = if (outputData == Data.EMPTY) progress else outputData
|
||||
val mangaId = DownloadState.getMangaId(workData)
|
||||
if (mangaId == 0L) return null
|
||||
@@ -229,7 +249,8 @@ class DownloadsViewModel @Inject constructor(
|
||||
progress = DownloadState.getProgress(workData),
|
||||
eta = DownloadState.getEta(workData),
|
||||
timestamp = DownloadState.getTimestamp(workData),
|
||||
totalChapters = DownloadState.getDownloadedChapters(workData).size,
|
||||
chaptersDownloaded = DownloadState.getDownloadedChapters(workData),
|
||||
isExpanded = isExpanded,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -261,8 +282,16 @@ class DownloadsViewModel @Inject constructor(
|
||||
}
|
||||
return cacheMutex.withLock {
|
||||
mangaCache.getOrElse(mangaId) {
|
||||
mangaDataRepository.findMangaById(mangaId)?.also { mangaCache[mangaId] = it } ?: return null
|
||||
mangaDataRepository.findMangaById(mangaId)?.let {
|
||||
tryLoad(it) ?: it
|
||||
}?.also {
|
||||
mangaCache[mangaId] = it
|
||||
} ?: return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun tryLoad(manga: Manga) = runCatchingCancellable {
|
||||
(mangaRepositoryFactory.create(manga.source) as RemoteMangaRepository).peekDetails(manga)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.koitharu.kotatsu.download.ui.list.chapters
|
||||
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
|
||||
data class DownloadChapter(
|
||||
val number: Int,
|
||||
val name: String,
|
||||
val isDownloaded: Boolean,
|
||||
) : ListModel {
|
||||
|
||||
override fun areItemsTheSame(other: ListModel): Boolean {
|
||||
return other is DownloadChapter && other.name == name
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.koitharu.kotatsu.download.ui.list.chapters
|
||||
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.util.ext.drawableEnd
|
||||
import org.koitharu.kotatsu.databinding.ItemChapterDownloadBinding
|
||||
|
||||
fun downloadChapterAD() = adapterDelegateViewBinding<DownloadChapter, DownloadChapter, ItemChapterDownloadBinding>(
|
||||
{ layoutInflater, parent -> ItemChapterDownloadBinding.inflate(layoutInflater, parent, false) },
|
||||
) {
|
||||
|
||||
val iconDone = ContextCompat.getDrawable(context, R.drawable.ic_check)
|
||||
|
||||
bind {
|
||||
binding.textViewNumber.text = item.number.toString()
|
||||
binding.textViewTitle.text = item.name
|
||||
binding.textViewTitle.drawableEnd = if (item.isDownloaded) iconDone else null
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import okio.buffer
|
||||
import okio.sink
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions
|
||||
import org.koitharu.kotatsu.core.model.ids
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders
|
||||
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||
@@ -46,7 +47,6 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.util.Throttler
|
||||
import org.koitharu.kotatsu.core.util.ext.awaitFinishedWorkInfosByTag
|
||||
import org.koitharu.kotatsu.core.util.ext.awaitUpdateWork
|
||||
import org.koitharu.kotatsu.core.util.ext.awaitWorkInfoById
|
||||
import org.koitharu.kotatsu.core.util.ext.awaitWorkInfosByTag
|
||||
import org.koitharu.kotatsu.core.util.ext.deleteAwait
|
||||
import org.koitharu.kotatsu.core.util.ext.deleteWork
|
||||
@@ -105,11 +105,12 @@ class DownloadWorker @AssistedInject constructor(
|
||||
setForeground(getForegroundInfo())
|
||||
val mangaId = inputData.getLong(MANGA_ID, 0L)
|
||||
val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure()
|
||||
val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() }
|
||||
val downloadedIds = getDoneChapters()
|
||||
lastPublishedState = DownloadState(manga, isIndeterminate = true)
|
||||
publishState(DownloadState(manga, isIndeterminate = true))
|
||||
val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() }
|
||||
val downloadedIds = getDoneChapters(manga)
|
||||
return try {
|
||||
downloadMangaImpl(chaptersIds, downloadedIds)
|
||||
downloadMangaImpl(manga, chaptersIds, downloadedIds)
|
||||
Result.success(currentState.toWorkData())
|
||||
} catch (e: CancellationException) {
|
||||
withContext(NonCancellable) {
|
||||
@@ -147,10 +148,11 @@ class DownloadWorker @AssistedInject constructor(
|
||||
}
|
||||
|
||||
private suspend fun downloadMangaImpl(
|
||||
subject: Manga,
|
||||
includedIds: LongArray?,
|
||||
excludedIds: LongArray,
|
||||
excludedIds: Set<Long>,
|
||||
) {
|
||||
var manga = currentState.manga
|
||||
var manga = subject
|
||||
val chaptersToSkip = excludedIds.toMutableSet()
|
||||
withMangaLock(manga) {
|
||||
ContextCompat.registerReceiver(
|
||||
@@ -180,11 +182,7 @@ class DownloadWorker @AssistedInject constructor(
|
||||
val chapters = getChapters(mangaDetails, includedIds)
|
||||
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
||||
if (chaptersToSkip.remove(chapter.id)) {
|
||||
publishState(
|
||||
currentState.copy(
|
||||
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
||||
),
|
||||
)
|
||||
publishState(currentState.copy(downloadedChapters = currentState.downloadedChapters + 1))
|
||||
continue
|
||||
}
|
||||
val pages = runFailsafe(pausingHandle) {
|
||||
@@ -222,11 +220,7 @@ class DownloadWorker @AssistedInject constructor(
|
||||
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
||||
}.onFailure(Throwable::printStackTraceDebug)
|
||||
}
|
||||
publishState(
|
||||
currentState.copy(
|
||||
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
||||
),
|
||||
)
|
||||
publishState(currentState.copy(downloadedChapters = currentState.downloadedChapters + 1))
|
||||
}
|
||||
publishState(currentState.copy(isIndeterminate = true, eta = -1L))
|
||||
output.mergeWithExisting()
|
||||
@@ -333,11 +327,9 @@ class DownloadWorker @AssistedInject constructor(
|
||||
setProgress(state.toWorkData())
|
||||
}
|
||||
|
||||
private suspend fun getDoneChapters(): LongArray {
|
||||
val work = WorkManager.getInstance(applicationContext).awaitWorkInfoById(id)
|
||||
?: return LongArray(0)
|
||||
return DownloadState.getDownloadedChapters(work.progress)
|
||||
}
|
||||
private suspend fun getDoneChapters(manga: Manga) = runCatchingCancellable {
|
||||
localMangaRepository.getDetails(manga).chapters?.ids()
|
||||
}.getOrNull().orEmpty()
|
||||
|
||||
private fun getChapters(
|
||||
manga: Manga,
|
||||
|
||||
@@ -3,20 +3,22 @@ package org.koitharu.kotatsu.explore.data
|
||||
import androidx.room.withTransaction
|
||||
import dagger.Reusable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.dao.MangaSourcesDao
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity
|
||||
import org.koitharu.kotatsu.core.model.MangaSource
|
||||
import org.koitharu.kotatsu.core.model.isNsfw
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
||||
import org.koitharu.kotatsu.parsers.model.ContentType
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.move
|
||||
import java.util.Collections
|
||||
import java.util.EnumSet
|
||||
import javax.inject.Inject
|
||||
@@ -28,7 +30,7 @@ class MangaSourcesRepository @Inject constructor(
|
||||
) {
|
||||
|
||||
private val dao: MangaSourcesDao
|
||||
get() = db.sourcesDao
|
||||
get() = db.getSourcesDao()
|
||||
|
||||
private val remoteSources = EnumSet.allOf(MangaSource::class.java).apply {
|
||||
remove(MangaSource.LOCAL)
|
||||
@@ -76,14 +78,6 @@ class MangaSourcesRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setSourcesEnabled(sources: Iterable<MangaSource>, isEnabled: Boolean) {
|
||||
db.withTransaction {
|
||||
for (s in sources) {
|
||||
dao.setEnabled(s.name, isEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun disableAllSources() {
|
||||
db.withTransaction {
|
||||
assimilateNewSources()
|
||||
@@ -99,46 +93,26 @@ class MangaSourcesRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setPosition(source: MangaSource, index: Int) {
|
||||
db.withTransaction {
|
||||
val all = dao.findAll().toMutableList()
|
||||
val sourceIndex = all.indexOfFirst { x -> x.source == source.name }
|
||||
if (sourceIndex !in all.indices) {
|
||||
val entity = MangaSourceEntity(
|
||||
source = source.name,
|
||||
isEnabled = false,
|
||||
sortKey = index,
|
||||
)
|
||||
all.add(index, entity)
|
||||
dao.upsert(entity)
|
||||
} else {
|
||||
all.move(sourceIndex, index)
|
||||
}
|
||||
for ((i, e) in all.withIndex()) {
|
||||
if (e.sortKey != i) {
|
||||
dao.setSortKey(e.source, i)
|
||||
fun observeNewSources(): Flow<Set<MangaSource>> = observeIsNewSourcesEnabled().flatMapLatest {
|
||||
if (it) {
|
||||
combine(
|
||||
dao.observeAll(),
|
||||
observeIsNsfwDisabled(),
|
||||
) { entities, skipNsfw ->
|
||||
val result = EnumSet.copyOf(remoteSources)
|
||||
for (e in entities) {
|
||||
result.remove(MangaSource(e.source))
|
||||
}
|
||||
}
|
||||
if (skipNsfw) {
|
||||
result.removeAll { x -> x.isNsfw() }
|
||||
}
|
||||
result
|
||||
}.distinctUntilChanged()
|
||||
} else {
|
||||
flowOf(emptySet())
|
||||
}
|
||||
}
|
||||
|
||||
fun observeNewSources(): Flow<Set<MangaSource>> = dao.observeAll().map { entities ->
|
||||
val result = EnumSet.copyOf(remoteSources)
|
||||
for (e in entities) {
|
||||
result.remove(MangaSource(e.source))
|
||||
}
|
||||
result
|
||||
}.distinctUntilChanged()
|
||||
|
||||
suspend fun getNewSources(): Set<MangaSource> {
|
||||
val entities = dao.findAll()
|
||||
val result = EnumSet.copyOf(remoteSources)
|
||||
for (e in entities) {
|
||||
result.remove(MangaSource(e.source))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
suspend fun assimilateNewSources(): Set<MangaSource> {
|
||||
val new = getNewSources()
|
||||
if (new.isEmpty()) {
|
||||
@@ -153,6 +127,9 @@ class MangaSourcesRepository @Inject constructor(
|
||||
)
|
||||
}
|
||||
dao.insertIfAbsent(entities)
|
||||
if (settings.isNsfwContentDisabled) {
|
||||
new.removeAll { x -> x.isNsfw() }
|
||||
}
|
||||
return new
|
||||
}
|
||||
|
||||
@@ -160,6 +137,15 @@ class MangaSourcesRepository @Inject constructor(
|
||||
return dao.findAll().isEmpty()
|
||||
}
|
||||
|
||||
private suspend fun getNewSources(): MutableSet<MangaSource> {
|
||||
val entities = dao.findAll()
|
||||
val result = EnumSet.copyOf(remoteSources)
|
||||
for (e in entities) {
|
||||
result.remove(MangaSource(e.source))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun List<MangaSourceEntity>.toSources(skipNsfwSources: Boolean): List<MangaSource> {
|
||||
val result = ArrayList<MangaSource>(size)
|
||||
for (entity in this) {
|
||||
@@ -177,4 +163,8 @@ class MangaSourcesRepository @Inject constructor(
|
||||
private fun observeIsNsfwDisabled() = settings.observeAsFlow(AppSettings.KEY_DISABLE_NSFW) {
|
||||
isNsfwContentDisabled
|
||||
}
|
||||
|
||||
private fun observeIsNewSourcesEnabled() = settings.observeAsFlow(AppSettings.KEY_SOURCES_NEW) {
|
||||
isNewSourcesTipEnabled
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.fragment.app.viewModels
|
||||
@@ -15,9 +16,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import coil.ImageLoader
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.bookmarks.ui.BookmarksActivity
|
||||
import org.koitharu.kotatsu.core.exceptions.resolve.SnackbarErrorObserver
|
||||
import org.koitharu.kotatsu.core.os.AppShortcutManager
|
||||
import org.koitharu.kotatsu.core.ui.BaseFragment
|
||||
import org.koitharu.kotatsu.core.ui.dialog.TwoButtonsAlertDialog
|
||||
import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
|
||||
@@ -28,6 +31,7 @@ import org.koitharu.kotatsu.core.ui.widgets.TipView
|
||||
import org.koitharu.kotatsu.core.util.ext.addMenuProvider
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||
import org.koitharu.kotatsu.core.util.ext.viewLifecycleScope
|
||||
import org.koitharu.kotatsu.databinding.FragmentExploreBinding
|
||||
import org.koitharu.kotatsu.details.ui.DetailsActivity
|
||||
import org.koitharu.kotatsu.download.ui.list.DownloadsActivity
|
||||
@@ -55,6 +59,9 @@ class ExploreFragment :
|
||||
@Inject
|
||||
lateinit var coil: ImageLoader
|
||||
|
||||
@Inject
|
||||
lateinit var shortcutManager: AppShortcutManager
|
||||
|
||||
private val viewModel by viewModels<ExploreViewModel>()
|
||||
private var exploreAdapter: ExploreAdapter? = null
|
||||
|
||||
@@ -141,6 +148,8 @@ class ExploreFragment :
|
||||
override fun onItemLongClick(item: MangaSourceItem, view: View): Boolean {
|
||||
val menu = PopupMenu(view.context, view)
|
||||
menu.inflate(R.menu.popup_source)
|
||||
menu.menu.findItem(R.id.action_shortcut)
|
||||
?.isVisible = ShortcutManagerCompat.isRequestPinShortcutSupported(view.context)
|
||||
menu.setOnMenuItemClickListener(SourceMenuListener(item))
|
||||
menu.show()
|
||||
return true
|
||||
@@ -195,6 +204,12 @@ class ExploreFragment :
|
||||
viewModel.hideSource(sourceItem.source)
|
||||
}
|
||||
|
||||
R.id.action_shortcut -> {
|
||||
viewLifecycleScope.launch {
|
||||
shortcutManager.requestPinShortcut(sourceItem.source)
|
||||
}
|
||||
}
|
||||
|
||||
else -> return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.koitharu.kotatsu.core.util.ext.enqueueWith
|
||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||
import org.koitharu.kotatsu.core.util.ext.newImageRequest
|
||||
import org.koitharu.kotatsu.core.util.ext.resolveDp
|
||||
import org.koitharu.kotatsu.core.util.ext.setOnContextClickListenerCompat
|
||||
import org.koitharu.kotatsu.core.util.ext.source
|
||||
import org.koitharu.kotatsu.core.util.ext.textAndVisible
|
||||
import org.koitharu.kotatsu.databinding.ItemExploreButtonsBinding
|
||||
@@ -44,7 +45,12 @@ fun exploreButtonsAD(
|
||||
if (item.isRandomLoading) {
|
||||
val icon = CircularProgressDrawable(context)
|
||||
icon.strokeWidth = context.resources.resolveDp(2f)
|
||||
icon.setColorSchemeColors(context.getThemeColor(materialR.attr.colorPrimary, Color.DKGRAY))
|
||||
icon.setColorSchemeColors(
|
||||
context.getThemeColor(
|
||||
materialR.attr.colorPrimary,
|
||||
Color.DKGRAY
|
||||
)
|
||||
)
|
||||
binding.buttonRandom.icon = icon
|
||||
icon.start()
|
||||
} else {
|
||||
@@ -88,7 +94,13 @@ fun exploreSourceListItemAD(
|
||||
listener: OnListItemClickListener<MangaSourceItem>,
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
) = adapterDelegateViewBinding<MangaSourceItem, ListModel, ItemExploreSourceListBinding>(
|
||||
{ layoutInflater, parent -> ItemExploreSourceListBinding.inflate(layoutInflater, parent, false) },
|
||||
{ layoutInflater, parent ->
|
||||
ItemExploreSourceListBinding.inflate(
|
||||
layoutInflater,
|
||||
parent,
|
||||
false
|
||||
)
|
||||
},
|
||||
on = { item, _, _ -> item is MangaSourceItem && !item.isGrid },
|
||||
) {
|
||||
|
||||
@@ -96,6 +108,7 @@ fun exploreSourceListItemAD(
|
||||
|
||||
binding.root.setOnClickListener(eventListener)
|
||||
binding.root.setOnLongClickListener(eventListener)
|
||||
binding.root.setOnContextClickListenerCompat(eventListener)
|
||||
|
||||
bind {
|
||||
binding.textViewTitle.text = item.source.title
|
||||
@@ -115,7 +128,13 @@ fun exploreSourceGridItemAD(
|
||||
listener: OnListItemClickListener<MangaSourceItem>,
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
) = adapterDelegateViewBinding<MangaSourceItem, ListModel, ItemExploreSourceGridBinding>(
|
||||
{ layoutInflater, parent -> ItemExploreSourceGridBinding.inflate(layoutInflater, parent, false) },
|
||||
{ layoutInflater, parent ->
|
||||
ItemExploreSourceGridBinding.inflate(
|
||||
layoutInflater,
|
||||
parent,
|
||||
false
|
||||
)
|
||||
},
|
||||
on = { item, _, _ -> item is MangaSourceItem && item.isGrid },
|
||||
) {
|
||||
|
||||
@@ -123,6 +142,7 @@ fun exploreSourceGridItemAD(
|
||||
|
||||
binding.root.setOnClickListener(eventListener)
|
||||
binding.root.setOnLongClickListener(eventListener)
|
||||
binding.root.setOnContextClickListenerCompat(eventListener)
|
||||
|
||||
bind {
|
||||
binding.textViewTitle.text = item.source.title
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package org.koitharu.kotatsu.favourites.data
|
||||
|
||||
import org.koitharu.kotatsu.core.db.entity.SortOrder
|
||||
import org.koitharu.kotatsu.core.db.entity.toManga
|
||||
import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import java.util.Date
|
||||
|
||||
fun FavouriteCategoryEntity.toFavouriteCategory(id: Long = categoryId.toLong()) = FavouriteCategory(
|
||||
id = id,
|
||||
title = title,
|
||||
sortKey = sortKey,
|
||||
order = SortOrder(order, SortOrder.NEWEST),
|
||||
order = ListSortOrder(order, ListSortOrder.NEWEST),
|
||||
createdAt = Date(createdAt),
|
||||
isTrackingEnabled = track,
|
||||
isVisibleInLibrary = isVisibleInLibrary,
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package org.koitharu.kotatsu.favourites.data
|
||||
|
||||
import androidx.room.*
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.RawQuery
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Upsert
|
||||
import androidx.sqlite.db.SimpleSQLiteQuery
|
||||
import androidx.sqlite.db.SupportSQLiteQuery
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.favourites.domain.model.Cover
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
|
||||
@Dao
|
||||
abstract class FavouritesDao {
|
||||
@@ -22,7 +28,7 @@ abstract class FavouritesDao {
|
||||
@Query("SELECT * FROM favourites WHERE deleted_at = 0 GROUP BY manga_id ORDER BY created_at DESC LIMIT :limit")
|
||||
abstract suspend fun findLast(limit: Int): List<FavouriteManga>
|
||||
|
||||
fun observeAll(order: SortOrder): Flow<List<FavouriteManga>> {
|
||||
fun observeAll(order: ListSortOrder): Flow<List<FavouriteManga>> {
|
||||
val orderBy = getOrderBy(order)
|
||||
|
||||
@Language("RoomSql")
|
||||
@@ -47,7 +53,7 @@ abstract class FavouritesDao {
|
||||
)
|
||||
abstract suspend fun findAll(categoryId: Long): List<FavouriteManga>
|
||||
|
||||
fun observeAll(categoryId: Long, order: SortOrder): Flow<List<FavouriteManga>> {
|
||||
fun observeAll(categoryId: Long, order: ListSortOrder): Flow<List<FavouriteManga>> {
|
||||
val orderBy = getOrderBy(order)
|
||||
|
||||
@Language("RoomSql")
|
||||
@@ -72,13 +78,14 @@ abstract class FavouritesDao {
|
||||
)
|
||||
abstract suspend fun findAllManga(categoryId: Int): List<MangaEntity>
|
||||
|
||||
suspend fun findCovers(categoryId: Long, order: SortOrder): List<Cover> {
|
||||
suspend fun findCovers(categoryId: Long, order: ListSortOrder): List<Cover> {
|
||||
val orderBy = getOrderBy(order)
|
||||
|
||||
@Language("RoomSql")
|
||||
val query = SimpleSQLiteQuery(
|
||||
"SELECT m.cover_url AS url, m.source AS source FROM favourites AS f LEFT JOIN manga AS m ON f.manga_id = m.manga_id " +
|
||||
"WHERE f.category_id = ? AND deleted_at = 0 ORDER BY $orderBy",
|
||||
"SELECT manga.cover_url AS url, manga.source AS source FROM favourites " +
|
||||
"LEFT JOIN manga ON favourites.manga_id = manga.manga_id " +
|
||||
"WHERE favourites.category_id = ? AND deleted_at = 0 ORDER BY $orderBy",
|
||||
arrayOf<Any>(categoryId),
|
||||
)
|
||||
return findCoversImpl(query)
|
||||
@@ -157,13 +164,13 @@ abstract class FavouritesDao {
|
||||
@Query("UPDATE favourites SET deleted_at = :deletedAt WHERE category_id = :categoryId AND deleted_at = 0")
|
||||
protected abstract suspend fun setDeletedAtAll(categoryId: Long, deletedAt: Long)
|
||||
|
||||
private fun getOrderBy(sortOrder: SortOrder) = when (sortOrder) {
|
||||
SortOrder.RATING -> "rating DESC"
|
||||
SortOrder.NEWEST,
|
||||
SortOrder.UPDATED,
|
||||
-> "created_at DESC"
|
||||
|
||||
SortOrder.ALPHABETICAL -> "title ASC"
|
||||
private fun getOrderBy(sortOrder: ListSortOrder) = when (sortOrder) {
|
||||
ListSortOrder.RATING -> "manga.rating DESC"
|
||||
ListSortOrder.NEWEST -> "favourites.created_at DESC"
|
||||
ListSortOrder.ALPHABETIC -> "manga.title ASC"
|
||||
ListSortOrder.NEW_CHAPTERS -> "(SELECT chapters_new FROM tracks WHERE tracks.manga_id = manga.manga_id) DESC"
|
||||
ListSortOrder.UPDATED, // for legacy support
|
||||
ListSortOrder.PROGRESS -> "(SELECT percent FROM history WHERE history.manga_id = manga.manga_id) DESC"
|
||||
else -> throw IllegalArgumentException("Sort order $sortOrder is not supported")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||
import org.koitharu.kotatsu.core.db.entity.SortOrder
|
||||
import org.koitharu.kotatsu.core.db.entity.toEntities
|
||||
import org.koitharu.kotatsu.core.db.entity.toEntity
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
@@ -20,8 +19,8 @@ import org.koitharu.kotatsu.favourites.data.toFavouriteCategory
|
||||
import org.koitharu.kotatsu.favourites.data.toManga
|
||||
import org.koitharu.kotatsu.favourites.data.toMangaList
|
||||
import org.koitharu.kotatsu.favourites.domain.model.Cover
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.tracker.work.TrackerNotificationChannels
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -32,27 +31,27 @@ class FavouritesRepository @Inject constructor(
|
||||
) {
|
||||
|
||||
suspend fun getAllManga(): List<Manga> {
|
||||
val entities = db.favouritesDao.findAll()
|
||||
val entities = db.getFavouritesDao().findAll()
|
||||
return entities.toMangaList()
|
||||
}
|
||||
|
||||
suspend fun getLastManga(limit: Int): List<Manga> {
|
||||
val entities = db.favouritesDao.findLast(limit)
|
||||
val entities = db.getFavouritesDao().findLast(limit)
|
||||
return entities.toMangaList()
|
||||
}
|
||||
|
||||
fun observeAll(order: SortOrder): Flow<List<Manga>> {
|
||||
return db.favouritesDao.observeAll(order)
|
||||
fun observeAll(order: ListSortOrder): Flow<List<Manga>> {
|
||||
return db.getFavouritesDao().observeAll(order)
|
||||
.mapItems { it.toManga() }
|
||||
}
|
||||
|
||||
suspend fun getManga(categoryId: Long): List<Manga> {
|
||||
val entities = db.favouritesDao.findAll(categoryId)
|
||||
val entities = db.getFavouritesDao().findAll(categoryId)
|
||||
return entities.toMangaList()
|
||||
}
|
||||
|
||||
fun observeAll(categoryId: Long, order: SortOrder): Flow<List<Manga>> {
|
||||
return db.favouritesDao.observeAll(categoryId, order)
|
||||
fun observeAll(categoryId: Long, order: ListSortOrder): Flow<List<Manga>> {
|
||||
return db.getFavouritesDao().observeAll(categoryId, order)
|
||||
.mapItems { it.toManga() }
|
||||
}
|
||||
|
||||
@@ -62,25 +61,25 @@ class FavouritesRepository @Inject constructor(
|
||||
}
|
||||
|
||||
fun observeCategories(): Flow<List<FavouriteCategory>> {
|
||||
return db.favouriteCategoriesDao.observeAll().mapItems {
|
||||
return db.getFavouriteCategoriesDao().observeAll().mapItems {
|
||||
it.toFavouriteCategory()
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
||||
fun observeCategoriesForLibrary(): Flow<List<FavouriteCategory>> {
|
||||
return db.favouriteCategoriesDao.observeAllForLibrary().mapItems {
|
||||
return db.getFavouriteCategoriesDao().observeAllForLibrary().mapItems {
|
||||
it.toFavouriteCategory()
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
||||
fun observeCategoriesWithCovers(): Flow<Map<FavouriteCategory, List<Cover>>> {
|
||||
return db.favouriteCategoriesDao.observeAll()
|
||||
return db.getFavouriteCategoriesDao().observeAll()
|
||||
.map {
|
||||
db.withTransaction {
|
||||
val res = LinkedHashMap<FavouriteCategory, List<Cover>>()
|
||||
for (entity in it) {
|
||||
val cat = entity.toFavouriteCategory()
|
||||
res[cat] = db.favouritesDao.findCovers(
|
||||
res[cat] = db.getFavouritesDao().findCovers(
|
||||
categoryId = cat.id,
|
||||
order = cat.order,
|
||||
)
|
||||
@@ -91,35 +90,35 @@ class FavouritesRepository @Inject constructor(
|
||||
}
|
||||
|
||||
fun observeCategory(id: Long): Flow<FavouriteCategory?> {
|
||||
return db.favouriteCategoriesDao.observe(id)
|
||||
return db.getFavouriteCategoriesDao().observe(id)
|
||||
.map { it?.toFavouriteCategory() }
|
||||
}
|
||||
|
||||
fun observeCategoriesIds(mangaId: Long): Flow<Set<Long>> {
|
||||
return db.favouritesDao.observeIds(mangaId).map { it.toSet() }
|
||||
return db.getFavouritesDao().observeIds(mangaId).map { it.toSet() }
|
||||
}
|
||||
|
||||
suspend fun getCategory(id: Long): FavouriteCategory {
|
||||
return db.favouriteCategoriesDao.find(id.toInt()).toFavouriteCategory()
|
||||
return db.getFavouriteCategoriesDao().find(id.toInt()).toFavouriteCategory()
|
||||
}
|
||||
|
||||
suspend fun createCategory(
|
||||
title: String,
|
||||
sortOrder: SortOrder,
|
||||
sortOrder: ListSortOrder,
|
||||
isTrackerEnabled: Boolean,
|
||||
isVisibleOnShelf: Boolean,
|
||||
): FavouriteCategory {
|
||||
val entity = FavouriteCategoryEntity(
|
||||
title = title,
|
||||
createdAt = System.currentTimeMillis(),
|
||||
sortKey = db.favouriteCategoriesDao.getNextSortKey(),
|
||||
sortKey = db.getFavouriteCategoriesDao().getNextSortKey(),
|
||||
categoryId = 0,
|
||||
order = sortOrder.name,
|
||||
track = isTrackerEnabled,
|
||||
deletedAt = 0L,
|
||||
isVisibleInLibrary = isVisibleOnShelf,
|
||||
)
|
||||
val id = db.favouriteCategoriesDao.insert(entity)
|
||||
val id = db.getFavouriteCategoriesDao().insert(entity)
|
||||
val category = entity.toFavouriteCategory(id)
|
||||
channels.createChannel(category)
|
||||
return category
|
||||
@@ -128,26 +127,26 @@ class FavouritesRepository @Inject constructor(
|
||||
suspend fun updateCategory(
|
||||
id: Long,
|
||||
title: String,
|
||||
sortOrder: SortOrder,
|
||||
sortOrder: ListSortOrder,
|
||||
isTrackerEnabled: Boolean,
|
||||
isVisibleOnShelf: Boolean,
|
||||
) {
|
||||
db.favouriteCategoriesDao.update(id, title, sortOrder.name, isTrackerEnabled, isVisibleOnShelf)
|
||||
db.getFavouriteCategoriesDao().update(id, title, sortOrder.name, isTrackerEnabled, isVisibleOnShelf)
|
||||
}
|
||||
|
||||
suspend fun updateCategory(id: Long, isVisibleInLibrary: Boolean) {
|
||||
db.favouriteCategoriesDao.updateLibVisibility(id, isVisibleInLibrary)
|
||||
db.getFavouriteCategoriesDao().updateLibVisibility(id, isVisibleInLibrary)
|
||||
}
|
||||
|
||||
suspend fun updateCategoryTracking(id: Long, isTrackingEnabled: Boolean) {
|
||||
db.favouriteCategoriesDao.updateTracking(id, isTrackingEnabled)
|
||||
db.getFavouriteCategoriesDao().updateTracking(id, isTrackingEnabled)
|
||||
}
|
||||
|
||||
suspend fun removeCategories(ids: Collection<Long>) {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.favouritesDao.deleteAll(id)
|
||||
db.favouriteCategoriesDao.delete(id)
|
||||
db.getFavouritesDao().deleteAll(id)
|
||||
db.getFavouriteCategoriesDao().delete(id)
|
||||
}
|
||||
}
|
||||
// run after transaction success
|
||||
@@ -156,12 +155,12 @@ class FavouritesRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setCategoryOrder(id: Long, order: SortOrder) {
|
||||
db.favouriteCategoriesDao.updateOrder(id, order.name)
|
||||
suspend fun setCategoryOrder(id: Long, order: ListSortOrder) {
|
||||
db.getFavouriteCategoriesDao().updateOrder(id, order.name)
|
||||
}
|
||||
|
||||
suspend fun reorderCategories(orderedIds: List<Long>) {
|
||||
val dao = db.favouriteCategoriesDao
|
||||
val dao = db.getFavouriteCategoriesDao()
|
||||
db.withTransaction {
|
||||
for ((i, id) in orderedIds.withIndex()) {
|
||||
dao.updateSortKey(id, i)
|
||||
@@ -173,8 +172,8 @@ class FavouritesRepository @Inject constructor(
|
||||
db.withTransaction {
|
||||
for (manga in mangas) {
|
||||
val tags = manga.tags.toEntities()
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga.toEntity(), tags)
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga.toEntity(), tags)
|
||||
val entity = FavouriteEntity(
|
||||
mangaId = manga.id,
|
||||
categoryId = categoryId,
|
||||
@@ -182,7 +181,7 @@ class FavouritesRepository @Inject constructor(
|
||||
sortKey = 0,
|
||||
deletedAt = 0L,
|
||||
)
|
||||
db.favouritesDao.insert(entity)
|
||||
db.getFavouritesDao().insert(entity)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,7 +189,7 @@ class FavouritesRepository @Inject constructor(
|
||||
suspend fun removeFromFavourites(ids: Collection<Long>): ReversibleHandle {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.favouritesDao.delete(mangaId = id)
|
||||
db.getFavouritesDao().delete(mangaId = id)
|
||||
}
|
||||
}
|
||||
return ReversibleHandle { recoverToFavourites(ids) }
|
||||
@@ -199,23 +198,23 @@ class FavouritesRepository @Inject constructor(
|
||||
suspend fun removeFromCategory(categoryId: Long, ids: Collection<Long>): ReversibleHandle {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.favouritesDao.delete(categoryId = categoryId, mangaId = id)
|
||||
db.getFavouritesDao().delete(categoryId = categoryId, mangaId = id)
|
||||
}
|
||||
}
|
||||
return ReversibleHandle { recoverToCategory(categoryId, ids) }
|
||||
}
|
||||
|
||||
private fun observeOrder(categoryId: Long): Flow<SortOrder> {
|
||||
return db.favouriteCategoriesDao.observe(categoryId)
|
||||
private fun observeOrder(categoryId: Long): Flow<ListSortOrder> {
|
||||
return db.getFavouriteCategoriesDao().observe(categoryId)
|
||||
.filterNotNull()
|
||||
.map { x -> SortOrder(x.order, SortOrder.NEWEST) }
|
||||
.map { x -> ListSortOrder(x.order, ListSortOrder.NEWEST) }
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
|
||||
private suspend fun recoverToFavourites(ids: Collection<Long>) {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.favouritesDao.recover(mangaId = id)
|
||||
db.getFavouritesDao().recover(mangaId = id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,7 +222,7 @@ class FavouritesRepository @Inject constructor(
|
||||
private suspend fun recoverToCategory(categoryId: Long, ids: Collection<Long>) {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.favouritesDao.recover(mangaId = id, categoryId = categoryId)
|
||||
db.getFavouritesDao().recover(mangaId = id, categoryId = categoryId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.koitharu.kotatsu.favourites.ui.categories.edit.FavouritesCategoryEdit
|
||||
import org.koitharu.kotatsu.list.ui.adapter.ListStateHolderListener
|
||||
import org.koitharu.kotatsu.list.ui.adapter.TypedListSpacingDecoration
|
||||
import org.koitharu.kotatsu.list.ui.model.ListModel
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -176,12 +175,6 @@ class FavouriteCategoriesActivity :
|
||||
|
||||
companion object {
|
||||
|
||||
val SORT_ORDERS = arrayOf(
|
||||
SortOrder.ALPHABETICAL,
|
||||
SortOrder.NEWEST,
|
||||
SortOrder.RATING,
|
||||
)
|
||||
|
||||
fun newIntent(context: Context) = Intent(context, FavouriteCategoriesActivity::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,16 +18,15 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||
import org.koitharu.kotatsu.core.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.core.ui.model.titleRes
|
||||
import org.koitharu.kotatsu.core.ui.util.DefaultTextWatcher
|
||||
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
|
||||
import org.koitharu.kotatsu.core.util.ext.getSerializableCompat
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.observeEvent
|
||||
import org.koitharu.kotatsu.core.util.ext.setChecked
|
||||
import org.koitharu.kotatsu.core.util.ext.sortedByOrdinal
|
||||
import org.koitharu.kotatsu.databinding.ActivityCategoryEditBinding
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.FavouriteCategoriesActivity
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -38,7 +37,8 @@ class FavouritesCategoryEditActivity :
|
||||
DefaultTextWatcher {
|
||||
|
||||
private val viewModel by viewModels<FavouritesCategoryEditViewModel>()
|
||||
private var selectedSortOrder: SortOrder? = null
|
||||
private var selectedSortOrder: ListSortOrder? = null
|
||||
private val sortOrders = ListSortOrder.FAVORITES.sortedByOrdinal()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -68,7 +68,7 @@ class FavouritesCategoryEditActivity :
|
||||
|
||||
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
|
||||
super.onRestoreInstanceState(savedInstanceState)
|
||||
val order = savedInstanceState.getSerializableCompat<SortOrder>(KEY_SORT_ORDER)
|
||||
val order = savedInstanceState.getSerializableCompat<ListSortOrder>(KEY_SORT_ORDER)
|
||||
if (order != null) {
|
||||
selectedSortOrder = order
|
||||
}
|
||||
@@ -103,7 +103,7 @@ class FavouritesCategoryEditActivity :
|
||||
}
|
||||
|
||||
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedSortOrder = FavouriteCategoriesActivity.SORT_ORDERS.getOrNull(position)
|
||||
selectedSortOrder = sortOrders.getOrNull(position)
|
||||
}
|
||||
|
||||
private fun onCategoryChanged(category: FavouriteCategory?) {
|
||||
@@ -113,7 +113,7 @@ class FavouritesCategoryEditActivity :
|
||||
}
|
||||
viewBinding.editName.setText(category?.title)
|
||||
selectedSortOrder = category?.order
|
||||
val sortText = getString((category?.order ?: SortOrder.NEWEST).titleRes)
|
||||
val sortText = getString((category?.order ?: ListSortOrder.NEWEST).titleResId)
|
||||
viewBinding.editSort.setText(sortText, false)
|
||||
viewBinding.switchTracker.setChecked(category?.isTrackingEnabled ?: true, false)
|
||||
viewBinding.switchShelf.setChecked(category?.isVisibleInLibrary ?: true, false)
|
||||
@@ -135,17 +135,17 @@ class FavouritesCategoryEditActivity :
|
||||
}
|
||||
|
||||
private fun initSortSpinner() {
|
||||
val entries = FavouriteCategoriesActivity.SORT_ORDERS.map { getString(it.titleRes) }
|
||||
val entries = sortOrders.map { getString(it.titleResId) }
|
||||
val adapter = SortAdapter(this, entries)
|
||||
viewBinding.editSort.setAdapter(adapter)
|
||||
viewBinding.editSort.onItemClickListener = this
|
||||
}
|
||||
|
||||
private fun getSelectedSortOrder(): SortOrder {
|
||||
private fun getSelectedSortOrder(): ListSortOrder {
|
||||
selectedSortOrder?.let { return it }
|
||||
val entries = FavouriteCategoriesActivity.SORT_ORDERS.map { getString(it.titleRes) }
|
||||
val entries = sortOrders.map { getString(it.titleResId) }
|
||||
val index = entries.indexOf(viewBinding.editSort.text.toString())
|
||||
return FavouriteCategoriesActivity.SORT_ORDERS.getOrNull(index) ?: SortOrder.NEWEST
|
||||
return sortOrders.getOrNull(index) ?: ListSortOrder.NEWEST
|
||||
}
|
||||
|
||||
private class SortAdapter(
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.koitharu.kotatsu.core.util.ext.call
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.edit.FavouritesCategoryEditActivity.Companion.EXTRA_ID
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.edit.FavouritesCategoryEditActivity.Companion.NO_ID
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -48,7 +48,7 @@ class FavouritesCategoryEditViewModel @Inject constructor(
|
||||
|
||||
fun save(
|
||||
title: String,
|
||||
sortOrder: SortOrder,
|
||||
sortOrder: ListSortOrder,
|
||||
isTrackerEnabled: Boolean,
|
||||
isVisibleOnShelf: Boolean,
|
||||
) {
|
||||
|
||||
@@ -10,13 +10,11 @@ import androidx.fragment.app.viewModels
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.list.ListSelectionController
|
||||
import org.koitharu.kotatsu.core.ui.model.titleRes
|
||||
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
||||
import org.koitharu.kotatsu.core.util.ext.addMenuProvider
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.core.util.ext.sortedByOrdinal
|
||||
import org.koitharu.kotatsu.core.util.ext.withArgs
|
||||
import org.koitharu.kotatsu.databinding.FragmentListBinding
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.FavouriteCategoriesActivity
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.list.ui.MangaListFragment
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
|
||||
@@ -27,12 +25,14 @@ class FavouritesListFragment : MangaListFragment(), PopupMenu.OnMenuItemClickLis
|
||||
|
||||
override val isSwipeRefreshEnabled = false
|
||||
|
||||
val categoryId
|
||||
get() = viewModel.categoryId
|
||||
|
||||
override fun onViewBindingCreated(binding: FragmentListBinding, savedInstanceState: Bundle?) {
|
||||
super.onViewBindingCreated(binding, savedInstanceState)
|
||||
if (viewModel.categoryId != NO_ID) {
|
||||
addMenuProvider(FavouritesListMenuProvider(binding.root.context, viewModel))
|
||||
}
|
||||
viewModel.sortOrder.observe(viewLifecycleOwner, MenuInvalidator(requireActivity()))
|
||||
}
|
||||
|
||||
override fun onScrolledToEnd() = Unit
|
||||
@@ -40,14 +40,15 @@ class FavouritesListFragment : MangaListFragment(), PopupMenu.OnMenuItemClickLis
|
||||
override fun onFilterClick(view: View?) {
|
||||
val menu = PopupMenu(view?.context ?: return, view)
|
||||
menu.setOnMenuItemClickListener(this)
|
||||
for ((i, item) in FavouriteCategoriesActivity.SORT_ORDERS.withIndex()) {
|
||||
menu.menu.add(Menu.NONE, Menu.NONE, i, item.titleRes)
|
||||
val orders = ListSortOrder.FAVORITES.sortedByOrdinal()
|
||||
for ((i, item) in orders.withIndex()) {
|
||||
menu.menu.add(Menu.NONE, Menu.NONE, i, item.titleResId)
|
||||
}
|
||||
menu.show()
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
val order = FavouriteCategoriesActivity.SORT_ORDERS.getOrNull(item.order) ?: return false
|
||||
val order = ListSortOrder.FAVORITES.sortedByOrdinal().getOrNull(item.order) ?: return false
|
||||
viewModel.setSortOrder(order)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -5,12 +5,8 @@ import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.forEach
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.model.titleRes
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.FavouriteCategoriesActivity
|
||||
import org.koitharu.kotatsu.favourites.ui.categories.edit.FavouritesCategoryEditActivity
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
|
||||
class FavouritesListMenuProvider(
|
||||
private val context: Context,
|
||||
@@ -19,34 +15,12 @@ class FavouritesListMenuProvider(
|
||||
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
menuInflater.inflate(R.menu.opt_favourites, menu)
|
||||
val subMenu = menu.findItem(R.id.action_order)?.subMenu ?: return
|
||||
for (order in FavouriteCategoriesActivity.SORT_ORDERS) {
|
||||
subMenu.add(R.id.group_order, Menu.NONE, order.ordinal, order.titleRes)
|
||||
}
|
||||
subMenu.setGroupCheckable(R.id.group_order, true, true)
|
||||
}
|
||||
|
||||
override fun onPrepareMenu(menu: Menu) {
|
||||
super.onPrepareMenu(menu)
|
||||
val order = viewModel.sortOrder.value ?: return
|
||||
menu.findItem(R.id.action_order)?.subMenu?.forEach { item ->
|
||||
if (item.order == order.ordinal) {
|
||||
item.isChecked = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||
if (menuItem.groupId == R.id.group_order) {
|
||||
val order = SortOrder.entries[menuItem.order]
|
||||
viewModel.setSortOrder(order)
|
||||
return true
|
||||
}
|
||||
return when (menuItem.itemId) {
|
||||
R.id.action_edit -> {
|
||||
context.startActivity(
|
||||
FavouritesCategoryEditActivity.newIntent(context, viewModel.categoryId),
|
||||
)
|
||||
context.startActivity(FavouritesCategoryEditActivity.newIntent(context, viewModel.categoryId))
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.plus
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleAction
|
||||
import org.koitharu.kotatsu.core.util.ext.call
|
||||
import org.koitharu.kotatsu.download.ui.worker.DownloadWorker
|
||||
@@ -21,12 +22,12 @@ import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment.Companion.ARG_CATEGORY_ID
|
||||
import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment.Companion.NO_ID
|
||||
import org.koitharu.kotatsu.list.domain.ListExtraProvider
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.list.ui.MangaListViewModel
|
||||
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
||||
import org.koitharu.kotatsu.list.ui.model.LoadingState
|
||||
import org.koitharu.kotatsu.list.ui.model.toErrorState
|
||||
import org.koitharu.kotatsu.list.ui.model.toUi
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -40,7 +41,10 @@ class FavouritesListViewModel @Inject constructor(
|
||||
|
||||
val categoryId: Long = savedStateHandle[ARG_CATEGORY_ID] ?: NO_ID
|
||||
|
||||
val sortOrder: StateFlow<SortOrder?> = if (categoryId == NO_ID) {
|
||||
override val listMode = settings.observeAsFlow(AppSettings.KEY_LIST_MODE_FAVORITES) { favoritesListMode }
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, settings.favoritesListMode)
|
||||
|
||||
val sortOrder: StateFlow<ListSortOrder?> = if (categoryId == NO_ID) {
|
||||
MutableStateFlow(null)
|
||||
} else {
|
||||
repository.observeCategory(categoryId)
|
||||
@@ -50,7 +54,7 @@ class FavouritesListViewModel @Inject constructor(
|
||||
|
||||
override val content = combine(
|
||||
if (categoryId == NO_ID) {
|
||||
repository.observeAll(SortOrder.NEWEST)
|
||||
repository.observeAll(ListSortOrder.NEWEST)
|
||||
} else {
|
||||
repository.observeAll(categoryId)
|
||||
},
|
||||
@@ -94,7 +98,7 @@ class FavouritesListViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun setSortOrder(order: SortOrder) {
|
||||
fun setSortOrder(order: ListSortOrder) {
|
||||
if (categoryId == NO_ID) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.koitharu.kotatsu.core.ui.widgets.ChipsView
|
||||
import org.koitharu.kotatsu.core.util.ext.lifecycleScope
|
||||
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
||||
import org.koitharu.kotatsu.core.util.ext.require
|
||||
import org.koitharu.kotatsu.core.util.ext.sortedByOrdinal
|
||||
import org.koitharu.kotatsu.filter.ui.model.FilterHeaderModel
|
||||
import org.koitharu.kotatsu.filter.ui.model.FilterItem
|
||||
import org.koitharu.kotatsu.filter.ui.model.FilterState
|
||||
@@ -131,7 +132,7 @@ class FilterCoordinator @Inject constructor(
|
||||
observeState(),
|
||||
observeAvailableTags(),
|
||||
) { state, available ->
|
||||
val chips = createChipsList(state, available.orEmpty())
|
||||
val chips = createChipsList(state, available.orEmpty(), 8)
|
||||
FilterHeaderModel(chips, state.sortOrder, state.tags.isNotEmpty())
|
||||
}
|
||||
|
||||
@@ -157,11 +158,16 @@ class FilterCoordinator @Inject constructor(
|
||||
private suspend fun createChipsList(
|
||||
filterState: FilterState,
|
||||
availableTags: Set<MangaTag>,
|
||||
limit: Int,
|
||||
): List<ChipsView.ChipModel> {
|
||||
val selectedTags = filterState.tags.toMutableSet()
|
||||
var tags = searchRepository.getTagsSuggestion("", 6, repository.source)
|
||||
if (tags.isEmpty()) {
|
||||
tags = availableTags.take(6)
|
||||
var tags = if (selectedTags.isEmpty()) {
|
||||
searchRepository.getTagsSuggestion("", limit, repository.source)
|
||||
} else {
|
||||
searchRepository.getTagsSuggestion(selectedTags).take(limit)
|
||||
}
|
||||
if (tags.size < limit) {
|
||||
tags = tags + availableTags.take(limit - tags.size)
|
||||
}
|
||||
if (tags.isEmpty() && selectedTags.isEmpty()) {
|
||||
return emptyList()
|
||||
@@ -202,7 +208,7 @@ class FilterCoordinator @Inject constructor(
|
||||
state: FilterState,
|
||||
query: String,
|
||||
): List<ListModel> {
|
||||
val sortOrders = repository.sortOrders.sortedBy { it.ordinal }
|
||||
val sortOrders = repository.sortOrders.sortedByOrdinal()
|
||||
val tags = mergeTags(state.tags, allTags.tags).toList()
|
||||
val list = ArrayList<ListModel>(tags.size + sortOrders.size + 3)
|
||||
if (query.isEmpty()) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||
import org.koitharu.kotatsu.history.domain.model.HistoryOrder
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
|
||||
@Dao
|
||||
abstract class HistoryDao {
|
||||
@@ -23,7 +23,7 @@ abstract class HistoryDao {
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM history WHERE deleted_at = 0 AND manga_id IN (:ids)")
|
||||
abstract suspend fun findAll(ids: Collection<Long>): List<HistoryEntity?>
|
||||
abstract suspend fun findAll(ids: Collection<Long>): List<HistoryEntity>
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM history WHERE deleted_at = 0 ORDER BY updated_at DESC")
|
||||
@@ -33,12 +33,14 @@ abstract class HistoryDao {
|
||||
@Query("SELECT * FROM history WHERE deleted_at = 0 ORDER BY updated_at DESC LIMIT :limit")
|
||||
abstract fun observeAll(limit: Int): Flow<List<HistoryWithManga>>
|
||||
|
||||
fun observeAll(order: HistoryOrder): Flow<List<HistoryWithManga>> {
|
||||
fun observeAll(order: ListSortOrder): Flow<List<HistoryWithManga>> {
|
||||
val orderBy = when (order) {
|
||||
HistoryOrder.UPDATED -> "history.updated_at DESC"
|
||||
HistoryOrder.CREATED -> "history.created_at DESC"
|
||||
HistoryOrder.PROGRESS -> "history.percent DESC"
|
||||
HistoryOrder.ALPHABETIC -> "manga.title"
|
||||
ListSortOrder.UPDATED -> "history.updated_at DESC"
|
||||
ListSortOrder.NEWEST -> "history.created_at DESC"
|
||||
ListSortOrder.PROGRESS -> "history.percent DESC"
|
||||
ListSortOrder.ALPHABETIC -> "manga.title"
|
||||
ListSortOrder.NEW_CHAPTERS -> "(SELECT chapters_new FROM tracks WHERE tracks.manga_id = manga.manga_id) DESC"
|
||||
else -> throw IllegalArgumentException("Sort order $order is not supported")
|
||||
}
|
||||
|
||||
@Language("RoomSql")
|
||||
|
||||
@@ -15,11 +15,12 @@ import org.koitharu.kotatsu.core.db.entity.toMangaTag
|
||||
import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||
import org.koitharu.kotatsu.core.model.findById
|
||||
import org.koitharu.kotatsu.core.model.isLocal
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
||||
import org.koitharu.kotatsu.core.util.ext.mapItems
|
||||
import org.koitharu.kotatsu.history.domain.model.HistoryOrder
|
||||
import org.koitharu.kotatsu.history.domain.model.MangaWithHistory
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaTag
|
||||
import org.koitharu.kotatsu.scrobbling.common.domain.Scrobbler
|
||||
@@ -38,36 +39,36 @@ class HistoryRepository @Inject constructor(
|
||||
) {
|
||||
|
||||
suspend fun getList(offset: Int, limit: Int): List<Manga> {
|
||||
val entities = db.historyDao.findAll(offset, limit)
|
||||
val entities = db.getHistoryDao().findAll(offset, limit)
|
||||
return entities.map { it.manga.toManga(it.tags.toMangaTags()) }
|
||||
}
|
||||
|
||||
suspend fun getLastOrNull(): Manga? {
|
||||
val entity = db.historyDao.findAll(0, 1).firstOrNull() ?: return null
|
||||
val entity = db.getHistoryDao().findAll(0, 1).firstOrNull() ?: return null
|
||||
return entity.manga.toManga(entity.tags.toMangaTags())
|
||||
}
|
||||
|
||||
fun observeLast(): Flow<Manga?> {
|
||||
return db.historyDao.observeAll(1).map {
|
||||
return db.getHistoryDao().observeAll(1).map {
|
||||
val first = it.firstOrNull()
|
||||
first?.manga?.toManga(first.tags.toMangaTags())
|
||||
}
|
||||
}
|
||||
|
||||
fun observeAll(): Flow<List<Manga>> {
|
||||
return db.historyDao.observeAll().mapItems {
|
||||
return db.getHistoryDao().observeAll().mapItems {
|
||||
it.manga.toManga(it.tags.toMangaTags())
|
||||
}
|
||||
}
|
||||
|
||||
fun observeAll(limit: Int): Flow<List<Manga>> {
|
||||
return db.historyDao.observeAll(limit).mapItems {
|
||||
return db.getHistoryDao().observeAll(limit).mapItems {
|
||||
it.manga.toManga(it.tags.toMangaTags())
|
||||
}
|
||||
}
|
||||
|
||||
fun observeAllWithHistory(order: HistoryOrder): Flow<List<MangaWithHistory>> {
|
||||
return db.historyDao.observeAll(order).mapItems {
|
||||
fun observeAllWithHistory(order: ListSortOrder): Flow<List<MangaWithHistory>> {
|
||||
return db.getHistoryDao().observeAll(order).mapItems {
|
||||
MangaWithHistory(
|
||||
it.manga.toManga(it.tags.toMangaTags()),
|
||||
it.history.toMangaHistory(),
|
||||
@@ -76,13 +77,13 @@ class HistoryRepository @Inject constructor(
|
||||
}
|
||||
|
||||
fun observeOne(id: Long): Flow<MangaHistory?> {
|
||||
return db.historyDao.observe(id).map {
|
||||
return db.getHistoryDao().observe(id).map {
|
||||
it?.toMangaHistory()
|
||||
}
|
||||
}
|
||||
|
||||
fun observeHasItems(): Flow<Boolean> {
|
||||
return db.historyDao.observeCount()
|
||||
return db.getHistoryDao().observeCount()
|
||||
.map { it > 0 }
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
@@ -93,9 +94,12 @@ class HistoryRepository @Inject constructor(
|
||||
}
|
||||
val tags = manga.tags.toEntities()
|
||||
db.withTransaction {
|
||||
db.tagsDao.upsert(tags)
|
||||
db.mangaDao.upsert(manga.toEntity(), tags)
|
||||
db.historyDao.upsert(
|
||||
val existing = db.getMangaDao().find(manga.id)?.manga
|
||||
if (existing == null || existing.source == manga.source.name) {
|
||||
db.getTagsDao().upsert(tags)
|
||||
db.getMangaDao().upsert(manga.toEntity(), tags)
|
||||
}
|
||||
db.getHistoryDao().upsert(
|
||||
HistoryEntity(
|
||||
mangaId = manga.id,
|
||||
createdAt = System.currentTimeMillis(),
|
||||
@@ -116,29 +120,29 @@ class HistoryRepository @Inject constructor(
|
||||
}
|
||||
|
||||
suspend fun getOne(manga: Manga): MangaHistory? {
|
||||
return db.historyDao.find(manga.id)?.recoverIfNeeded(manga)?.toMangaHistory()
|
||||
return db.getHistoryDao().find(manga.id)?.recoverIfNeeded(manga)?.toMangaHistory()
|
||||
}
|
||||
|
||||
suspend fun getProgress(mangaId: Long): Float {
|
||||
return db.historyDao.findProgress(mangaId) ?: PROGRESS_NONE
|
||||
return db.getHistoryDao().findProgress(mangaId) ?: PROGRESS_NONE
|
||||
}
|
||||
|
||||
suspend fun clear() {
|
||||
db.historyDao.clear()
|
||||
db.getHistoryDao().clear()
|
||||
}
|
||||
|
||||
suspend fun delete(manga: Manga) {
|
||||
db.historyDao.delete(manga.id)
|
||||
db.getHistoryDao().delete(manga.id)
|
||||
}
|
||||
|
||||
suspend fun deleteAfter(minDate: Long) {
|
||||
db.historyDao.deleteAfter(minDate)
|
||||
db.getHistoryDao().deleteAfter(minDate)
|
||||
}
|
||||
|
||||
suspend fun delete(ids: Collection<Long>): ReversibleHandle {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.historyDao.delete(id)
|
||||
db.getHistoryDao().delete(id)
|
||||
}
|
||||
}
|
||||
return ReversibleHandle {
|
||||
@@ -151,13 +155,13 @@ class HistoryRepository @Inject constructor(
|
||||
* Useful for replacing saved manga on deleting it with remote source
|
||||
*/
|
||||
suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) {
|
||||
if (alternative == null || db.mangaDao.update(alternative.toEntity()) <= 0) {
|
||||
db.historyDao.delete(manga.id)
|
||||
if (alternative == null || db.getMangaDao().update(alternative.toEntity()) <= 0) {
|
||||
db.getHistoryDao().delete(manga.id)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getPopularTags(limit: Int): List<MangaTag> {
|
||||
return db.historyDao.findPopularTags(limit).map { x -> x.toMangaTag() }
|
||||
return db.getHistoryDao().findPopularTags(limit).map { x -> x.toMangaTag() }
|
||||
}
|
||||
|
||||
fun shouldSkip(manga: Manga): Boolean {
|
||||
@@ -175,21 +179,21 @@ class HistoryRepository @Inject constructor(
|
||||
private suspend fun recover(ids: Collection<Long>) {
|
||||
db.withTransaction {
|
||||
for (id in ids) {
|
||||
db.historyDao.recover(id)
|
||||
db.getHistoryDao().recover(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun HistoryEntity.recoverIfNeeded(manga: Manga): HistoryEntity {
|
||||
val chapters = manga.chapters
|
||||
if (chapters.isNullOrEmpty() || chapters.findById(chapterId) != null) {
|
||||
if (manga.isLocal || chapters.isNullOrEmpty() || chapters.findById(chapterId) != null) {
|
||||
return this
|
||||
}
|
||||
val newChapterId = chapters.getOrNull(
|
||||
(chapters.size * percent).toInt(),
|
||||
)?.id ?: return this
|
||||
val newEntity = copy(chapterId = newChapterId)
|
||||
db.historyDao.update(newEntity)
|
||||
db.getHistoryDao().update(newEntity)
|
||||
return newEntity
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.koitharu.kotatsu.history.domain.model
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import org.koitharu.kotatsu.R
|
||||
|
||||
enum class HistoryOrder(
|
||||
@StringRes val titleResId: Int,
|
||||
) {
|
||||
|
||||
UPDATED(R.string.updated),
|
||||
CREATED(R.string.order_added),
|
||||
PROGRESS(R.string.progress),
|
||||
ALPHABETIC(R.string.by_name);
|
||||
|
||||
fun isGroupingSupported() = this == UPDATED || this == CREATED || this == PROGRESS
|
||||
}
|
||||
@@ -9,9 +9,8 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.os.NetworkManageIntent
|
||||
import org.koitharu.kotatsu.core.ui.list.ListSelectionController
|
||||
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
||||
import org.koitharu.kotatsu.core.ui.list.RecyclerScrollKeeper
|
||||
import org.koitharu.kotatsu.core.util.ext.addMenuProvider
|
||||
import org.koitharu.kotatsu.core.util.ext.observe
|
||||
import org.koitharu.kotatsu.databinding.FragmentListBinding
|
||||
import org.koitharu.kotatsu.list.ui.MangaListFragment
|
||||
import org.koitharu.kotatsu.list.ui.size.DynamicItemSizeResolver
|
||||
@@ -25,10 +24,8 @@ class HistoryListFragment : MangaListFragment() {
|
||||
|
||||
override fun onViewBindingCreated(binding: FragmentListBinding, savedInstanceState: Bundle?) {
|
||||
super.onViewBindingCreated(binding, savedInstanceState)
|
||||
RecyclerScrollKeeper(binding.recyclerView).attach()
|
||||
addMenuProvider(HistoryListMenuProvider(binding.root.context, viewModel))
|
||||
val menuInvalidator = MenuInvalidator(requireActivity())
|
||||
viewModel.isGroupingEnabled.observe(viewLifecycleOwner, menuInvalidator)
|
||||
viewModel.sortOrder.observe(viewLifecycleOwner, menuInvalidator)
|
||||
}
|
||||
|
||||
override fun onScrolledToEnd() = Unit
|
||||
|
||||
@@ -5,12 +5,10 @@ import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.forEach
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.ui.dialog.RememberSelectionDialogListener
|
||||
import org.koitharu.kotatsu.core.util.ext.startOfDay
|
||||
import org.koitharu.kotatsu.history.domain.model.HistoryOrder
|
||||
import java.util.Date
|
||||
import java.util.concurrent.TimeUnit
|
||||
import com.google.android.material.R as materialR
|
||||
@@ -22,47 +20,19 @@ class HistoryListMenuProvider(
|
||||
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
menuInflater.inflate(R.menu.opt_history, menu)
|
||||
val subMenu = menu.findItem(R.id.action_order)?.subMenu ?: return
|
||||
for (order in HistoryOrder.entries) {
|
||||
subMenu.add(R.id.group_order, Menu.NONE, order.ordinal, order.titleResId)
|
||||
}
|
||||
subMenu.setGroupCheckable(R.id.group_order, true, true)
|
||||
}
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||
if (menuItem.groupId == R.id.group_order) {
|
||||
val order = HistoryOrder.entries[menuItem.order]
|
||||
viewModel.setSortOrder(order)
|
||||
return true
|
||||
}
|
||||
return when (menuItem.itemId) {
|
||||
R.id.action_clear_history -> {
|
||||
showClearHistoryDialog()
|
||||
true
|
||||
}
|
||||
|
||||
R.id.action_history_grouping -> {
|
||||
viewModel.setGrouping(!menuItem.isChecked)
|
||||
true
|
||||
}
|
||||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPrepareMenu(menu: Menu) {
|
||||
val order = viewModel.sortOrder.value
|
||||
menu.findItem(R.id.action_order)?.subMenu?.forEach { item ->
|
||||
if (item.order == order.ordinal) {
|
||||
item.isChecked = true
|
||||
}
|
||||
}
|
||||
menu.findItem(R.id.action_history_grouping)?.run {
|
||||
isChecked = viewModel.isGroupingEnabled.value == true
|
||||
isEnabled = order.isGroupingSupported()
|
||||
}
|
||||
}
|
||||
|
||||
private fun showClearHistoryDialog() {
|
||||
val selectionListener = RememberSelectionDialogListener(2)
|
||||
MaterialAlertDialogBuilder(context, materialR.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
|
||||
|
||||
@@ -26,9 +26,9 @@ import org.koitharu.kotatsu.core.util.ext.daysDiff
|
||||
import org.koitharu.kotatsu.core.util.ext.onFirst
|
||||
import org.koitharu.kotatsu.download.ui.worker.DownloadWorker
|
||||
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||
import org.koitharu.kotatsu.history.domain.model.HistoryOrder
|
||||
import org.koitharu.kotatsu.history.domain.model.MangaWithHistory
|
||||
import org.koitharu.kotatsu.list.domain.ListExtraProvider
|
||||
import org.koitharu.kotatsu.list.domain.ListSortOrder
|
||||
import org.koitharu.kotatsu.list.ui.MangaListViewModel
|
||||
import org.koitharu.kotatsu.list.ui.model.EmptyHint
|
||||
import org.koitharu.kotatsu.list.ui.model.EmptyState
|
||||
@@ -54,22 +54,21 @@ class HistoryListViewModel @Inject constructor(
|
||||
downloadScheduler: DownloadWorker.Scheduler,
|
||||
) : MangaListViewModel(settings, downloadScheduler) {
|
||||
|
||||
val sortOrder: StateFlow<HistoryOrder> = settings.observeAsStateFlow(
|
||||
private val sortOrder: StateFlow<ListSortOrder> = settings.observeAsStateFlow(
|
||||
scope = viewModelScope + Dispatchers.IO,
|
||||
key = AppSettings.KEY_HISTORY_ORDER,
|
||||
valueProducer = { historySortOrder },
|
||||
)
|
||||
|
||||
val isGroupingEnabled = settings.observeAsFlow(
|
||||
override val listMode = settings.observeAsFlow(AppSettings.KEY_LIST_MODE_HISTORY) { historyListMode }
|
||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, settings.historyListMode)
|
||||
|
||||
private val isGroupingEnabled = settings.observeAsFlow(
|
||||
key = AppSettings.KEY_HISTORY_GROUPING,
|
||||
valueProducer = { isHistoryGroupingEnabled },
|
||||
).combine(sortOrder) { g, s ->
|
||||
g && s.isGroupingSupported()
|
||||
}.stateIn(
|
||||
scope = viewModelScope + Dispatchers.Default,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = settings.isHistoryGroupingEnabled && sortOrder.value.isGroupingSupported(),
|
||||
)
|
||||
}
|
||||
|
||||
override val content = combine(
|
||||
sortOrder.flatMapLatest { repository.observeAllWithHistory(it) },
|
||||
@@ -101,10 +100,6 @@ class HistoryListViewModel @Inject constructor(
|
||||
|
||||
override fun onRetry() = Unit
|
||||
|
||||
fun setSortOrder(order: HistoryOrder) {
|
||||
settings.historySortOrder = order
|
||||
}
|
||||
|
||||
fun clearHistory(minDate: Long) {
|
||||
launchJob(Dispatchers.Default) {
|
||||
val stringRes = if (minDate <= 0) {
|
||||
@@ -128,10 +123,6 @@ class HistoryListViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun setGrouping(isGroupingEnabled: Boolean) {
|
||||
settings.isHistoryGroupingEnabled = isGroupingEnabled
|
||||
}
|
||||
|
||||
private suspend fun mapList(
|
||||
list: List<MangaWithHistory>,
|
||||
grouped: Boolean,
|
||||
@@ -173,10 +164,10 @@ class HistoryListViewModel @Inject constructor(
|
||||
return result
|
||||
}
|
||||
|
||||
private fun MangaHistory.header(order: HistoryOrder): ListHeader? = when (order) {
|
||||
HistoryOrder.UPDATED -> ListHeader(timeAgo(updatedAt))
|
||||
HistoryOrder.CREATED -> ListHeader(timeAgo(createdAt))
|
||||
HistoryOrder.PROGRESS -> ListHeader(
|
||||
private fun MangaHistory.header(order: ListSortOrder): ListHeader? = when (order) {
|
||||
ListSortOrder.UPDATED -> ListHeader(timeAgo(updatedAt))
|
||||
ListSortOrder.NEWEST -> ListHeader(timeAgo(createdAt))
|
||||
ListSortOrder.PROGRESS -> ListHeader(
|
||||
when (percent) {
|
||||
1f -> R.string.status_completed
|
||||
in 0f..0.01f -> R.string.status_planned
|
||||
@@ -185,7 +176,10 @@ class HistoryListViewModel @Inject constructor(
|
||||
},
|
||||
)
|
||||
|
||||
HistoryOrder.ALPHABETIC -> null
|
||||
ListSortOrder.ALPHABETIC,
|
||||
ListSortOrder.RELEVANCE,
|
||||
ListSortOrder.NEW_CHAPTERS,
|
||||
ListSortOrder.RATING -> null
|
||||
}
|
||||
|
||||
private fun timeAgo(date: Date): DateTimeAgo {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.koitharu.kotatsu.list.domain
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.parsers.util.find
|
||||
import java.util.EnumSet
|
||||
|
||||
enum class ListSortOrder(
|
||||
@StringRes val titleResId: Int,
|
||||
) {
|
||||
|
||||
UPDATED(R.string.updated),
|
||||
NEWEST(R.string.order_added),
|
||||
PROGRESS(R.string.progress),
|
||||
ALPHABETIC(R.string.by_name),
|
||||
RATING(R.string.by_rating),
|
||||
RELEVANCE(R.string.by_relevance),
|
||||
NEW_CHAPTERS(R.string.new_chapters),
|
||||
;
|
||||
|
||||
fun isGroupingSupported() = this == UPDATED || this == NEWEST || this == PROGRESS
|
||||
|
||||
companion object {
|
||||
|
||||
val HISTORY: Set<ListSortOrder> = EnumSet.of(UPDATED, NEWEST, PROGRESS, ALPHABETIC, NEW_CHAPTERS)
|
||||
val FAVORITES: Set<ListSortOrder> = EnumSet.of(ALPHABETIC, NEWEST, RATING, NEW_CHAPTERS, PROGRESS)
|
||||
val SUGGESTIONS: Set<ListSortOrder> = EnumSet.of(RELEVANCE)
|
||||
|
||||
operator fun invoke(value: String, fallback: ListSortOrder) = entries.find(value) ?: fallback
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package org.koitharu.kotatsu.list.ui
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import com.google.android.material.button.MaterialButtonToggleGroup
|
||||
import com.google.android.material.slider.Slider
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||
import org.koitharu.kotatsu.core.ui.sheet.BaseAdaptiveSheet
|
||||
import org.koitharu.kotatsu.core.util.ext.setValueRounded
|
||||
import org.koitharu.kotatsu.core.util.ext.showDistinct
|
||||
import org.koitharu.kotatsu.core.util.progress.IntPercentLabelFormatter
|
||||
import org.koitharu.kotatsu.databinding.DialogListModeBinding
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ListModeBottomSheet :
|
||||
BaseAdaptiveSheet<DialogListModeBinding>(),
|
||||
Slider.OnChangeListener,
|
||||
MaterialButtonToggleGroup.OnButtonCheckedListener {
|
||||
|
||||
@Inject
|
||||
lateinit var settings: AppSettings
|
||||
|
||||
override fun onCreateViewBinding(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
) = DialogListModeBinding.inflate(inflater, container, false)
|
||||
|
||||
override fun onViewBindingCreated(binding: DialogListModeBinding, savedInstanceState: Bundle?) {
|
||||
super.onViewBindingCreated(binding, savedInstanceState)
|
||||
val mode = settings.listMode
|
||||
binding.buttonList.isChecked = mode == ListMode.LIST
|
||||
binding.buttonListDetailed.isChecked = mode == ListMode.DETAILED_LIST
|
||||
binding.buttonGrid.isChecked = mode == ListMode.GRID
|
||||
binding.textViewGridTitle.isVisible = mode == ListMode.GRID
|
||||
binding.sliderGrid.isVisible = mode == ListMode.GRID
|
||||
|
||||
binding.sliderGrid.setLabelFormatter(IntPercentLabelFormatter(binding.root.context))
|
||||
binding.sliderGrid.setValueRounded(settings.gridSize.toFloat())
|
||||
binding.sliderGrid.addOnChangeListener(this)
|
||||
|
||||
binding.checkableGroup.addOnButtonCheckedListener(this)
|
||||
}
|
||||
|
||||
override fun onButtonChecked(group: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean) {
|
||||
if (!isChecked) {
|
||||
return
|
||||
}
|
||||
val mode = when (checkedId) {
|
||||
R.id.button_list -> ListMode.LIST
|
||||
R.id.button_list_detailed -> ListMode.DETAILED_LIST
|
||||
R.id.button_grid -> ListMode.GRID
|
||||
else -> return
|
||||
}
|
||||
requireViewBinding().textViewGridTitle.isVisible = mode == ListMode.GRID
|
||||
requireViewBinding().sliderGrid.isVisible = mode == ListMode.GRID
|
||||
settings.listMode = mode
|
||||
}
|
||||
|
||||
override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {
|
||||
if (fromUser) {
|
||||
settings.gridSize = value.toInt()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val TAG = "ListModeSelectDialog"
|
||||
|
||||
fun show(fm: FragmentManager) = ListModeBottomSheet().showDistinct(fm, TAG)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,11 @@ import android.view.MenuItem
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.fragment.app.Fragment
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment
|
||||
import org.koitharu.kotatsu.history.ui.HistoryListFragment
|
||||
import org.koitharu.kotatsu.list.ui.config.ListConfigBottomSheet
|
||||
import org.koitharu.kotatsu.list.ui.config.ListConfigSection
|
||||
import org.koitharu.kotatsu.suggestions.ui.SuggestionsFragment
|
||||
|
||||
class MangaListMenuProvider(
|
||||
private val fragment: Fragment,
|
||||
@@ -17,7 +22,13 @@ class MangaListMenuProvider(
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean = when (menuItem.itemId) {
|
||||
R.id.action_list_mode -> {
|
||||
ListModeBottomSheet.show(fragment.childFragmentManager)
|
||||
val section: ListConfigSection = when (fragment) {
|
||||
is HistoryListFragment -> ListConfigSection.History
|
||||
is SuggestionsFragment -> ListConfigSection.Suggestions
|
||||
is FavouritesListFragment -> ListConfigSection.Favorites(fragment.categoryId)
|
||||
else -> ListConfigSection.General
|
||||
}
|
||||
ListConfigBottomSheet.show(fragment.childFragmentManager, section)
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user