Skip to content

Commit cff221e

Browse files
Sources are "unknown" fixed (#88)
* fix: Sources are unknown. * refactor: simplify ContentSource.toMangaSource mapping * Add TODO to remove toContentPage mapper
1 parent a8f2591 commit cff221e

25 files changed

Lines changed: 162 additions & 47 deletions

app/src/main/kotlin/io/github/landwarderer/futon/backups/data/model/SourceBackup.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SourceBackup(
1212
@SerialName("added_in") val addedIn: Int,
1313
@SerialName("pinned") val isPinned: Boolean = false,
1414
@SerialName("enabled") val isEnabled: Boolean = true, // for compatibility purposes, should be only true
15+
@SerialName("title") val title: String? = null,
1516
) {
1617

1718
constructor(entity: MangaSourceEntity) : this(
@@ -21,6 +22,7 @@ class SourceBackup(
2122
addedIn = entity.addedIn,
2223
isPinned = entity.isPinned,
2324
isEnabled = entity.isEnabled,
25+
title = entity.title,
2426
)
2527

2628
fun toEntity() = MangaSourceEntity(
@@ -31,5 +33,6 @@ class SourceBackup(
3133
lastUsedAt = lastUsedAt,
3234
isPinned = isPinned,
3335
cfState = 0,
36+
title = title,
3437
)
3538
}

app/src/main/kotlin/io/github/landwarderer/futon/core/db/MangaDatabase.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import io.github.landwarderer.futon.core.db.migrations.Migration24To25
4242
import io.github.landwarderer.futon.core.db.migrations.Migration25To26
4343
import io.github.landwarderer.futon.core.db.migrations.Migration26To27
4444
import io.github.landwarderer.futon.core.db.migrations.Migration27To28
45+
import io.github.landwarderer.futon.core.db.migrations.Migration28To29
4546
import io.github.landwarderer.futon.core.db.migrations.Migration2To3
4647
import io.github.landwarderer.futon.core.db.migrations.Migration3To4
4748
import io.github.landwarderer.futon.core.db.migrations.Migration4To5
@@ -73,7 +74,7 @@ import kotlinx.coroutines.Dispatchers
7374
import kotlinx.coroutines.isActive
7475
import kotlinx.coroutines.launch
7576

76-
const val DATABASE_VERSION = 28
77+
const val DATABASE_VERSION = 29
7778

7879
@Database(
7980
entities = [
@@ -148,6 +149,7 @@ fun getDatabaseMigrations(context: Context): Array<Migration> = arrayOf(
148149
Migration25To26(),
149150
Migration26To27(),
150151
Migration27To28(),
152+
Migration28To29(),
151153
)
152154

153155
fun MangaDatabase(context: Context): MangaDatabase = Room

app/src/main/kotlin/io/github/landwarderer/futon/core/db/dao/MangaSourcesDao.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,28 @@ abstract class MangaSourcesDao {
5252
@Query("UPDATE sources SET used_at = :value WHERE source = :source")
5353
abstract suspend fun setLastUsed(source: String, value: Long)
5454

55+
@Query("UPDATE sources SET title = :title WHERE source = :source")
56+
abstract suspend fun setTitle(source: String, title: String?)
57+
58+
@Transaction
59+
open suspend fun setPinned(source: String, isPinned: Boolean) {
60+
if (updateIsPinned(source, isPinned) == 0) {
61+
val entity = MangaSourceEntity(
62+
source = source,
63+
isEnabled = false,
64+
sortKey = getMaxSortKey() + 1,
65+
addedIn = BuildConfig.VERSION_CODE,
66+
lastUsedAt = 0,
67+
isPinned = isPinned,
68+
cfState = CloudFlareHelper.PROTECTION_NOT_DETECTED,
69+
title = null,
70+
)
71+
upsert(entity)
72+
}
73+
}
74+
5575
@Query("UPDATE sources SET pinned = :isPinned WHERE source = :source")
56-
abstract suspend fun setPinned(source: String, isPinned: Boolean)
76+
protected abstract suspend fun updateIsPinned(source: String, isPinned: Boolean): Int
5777

5878
@Query("UPDATE sources SET cf_state = :state WHERE source = :source")
5979
abstract suspend fun setCfState(source: String, state: Int)
@@ -88,6 +108,7 @@ abstract class MangaSourcesDao {
88108
lastUsedAt = 0,
89109
isPinned = false,
90110
cfState = CloudFlareHelper.PROTECTION_NOT_DETECTED,
111+
title = null,
91112
)
92113
upsert(entity)
93114
}

app/src/main/kotlin/io/github/landwarderer/futon/core/db/entity/MangaSourceEntity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ data class MangaSourceEntity(
1818
@ColumnInfo(name = "used_at") val lastUsedAt: Long,
1919
@ColumnInfo(name = "pinned") val isPinned: Boolean,
2020
@ColumnInfo(name = "cf_state") val cfState: Int,
21+
@ColumnInfo(name = "title") val title: String?,
2122
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.landwarderer.futon.core.db.migrations
2+
3+
import androidx.room.migration.Migration
4+
import androidx.sqlite.db.SupportSQLiteDatabase
5+
6+
class Migration28To29 : Migration(28, 29) {
7+
override fun migrate(db: SupportSQLiteDatabase) {
8+
db.execSQL("ALTER TABLE sources ADD COLUMN title TEXT")
9+
}
10+
}

app/src/main/kotlin/io/github/landwarderer/futon/core/model/MangaSource.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import org.koitharu.kotatsu.parsers.model.MangaParserSource
2121
import org.koitharu.kotatsu.parsers.model.MangaSource
2222
import org.koitharu.kotatsu.parsers.util.splitTwoParts
2323
import java.util.Locale
24+
import java.util.concurrent.ConcurrentHashMap
25+
26+
private val MIHON_TITLES = ConcurrentHashMap<String, String>()
27+
28+
fun updateMihonTitle(name: String, title: String) {
29+
MIHON_TITLES[name] = title
30+
}
2431

2532
data object LocalMangaSource : MangaSource {
2633
override val name = "LOCAL"
@@ -34,7 +41,7 @@ data object TestMangaSource : MangaSource {
3441
override val name = "TEST"
3542
}
3643

37-
fun MangaSource(name: String?): MangaSource {
44+
fun MangaSource(name: String?, title: String? = null): MangaSource {
3845
when (name ?: return UnknownMangaSource) {
3946
UnknownMangaSource.name -> return UnknownMangaSource
4047
LocalMangaSource.name -> return LocalMangaSource
@@ -45,17 +52,20 @@ fun MangaSource(name: String?): MangaSource {
4552
return ExternalMangaSource(packageName = parts.first, authority = parts.second)
4653
}
4754
if (name.startsWith("mihon:") || name.startsWith("MIHON_")) {
48-
return AnonymousMangaSource(name)
55+
return AnonymousMangaSource(name, title)
4956
}
5057
MangaParserSource.entries.forEach {
5158
if (it.name == name) return it
5259
}
5360
return UnknownMangaSource
5461
}
5562

56-
private data class AnonymousMangaSource(override val name: String) : MangaSource
63+
data class AnonymousMangaSource(
64+
override val name: String,
65+
val title: String? = null
66+
) : MangaSource
5767

58-
fun Collection<String>.toMangaSources() = map(::MangaSource)
68+
fun Collection<String>.toMangaSources() = map { MangaSource(it) }
5969

6070
fun MangaSource.isNsfw(): Boolean = when (val source = unwrap()) {
6171
is MangaSourceInfo -> source.mangaSource.isNsfw()
@@ -127,7 +137,8 @@ fun MangaSource.getTitle(context: Context): String = when (val source = unwrap()
127137
LocalMangaSource -> context.getString(R.string.local_storage)
128138
TestMangaSource -> context.getString(R.string.test_parser)
129139
is ExternalMangaSource -> source.resolveName(context)
130-
is MihonMangaSource -> source.displayName
140+
is MihonMangaSource -> source.displayName.also { updateMihonTitle(source.name, it) }
141+
is AnonymousMangaSource -> MIHON_TITLES[source.name] ?: source.title ?: context.getString(R.string.unknown)
131142
else -> context.getString(R.string.unknown)
132143
}
133144

app/src/main/kotlin/io/github/landwarderer/futon/core/nav/AppRouter.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class AppRouter private constructor(
125125

126126
/** Activities **/
127127

128-
fun openList(source: MangaSource, filter: MangaListFilter?, sortOrder: SortOrder?) {
129-
startActivity(listIntent(contextOrNull() ?: return, source, filter, sortOrder))
128+
fun openList(source: MangaSource, filter: MangaListFilter?, sortOrder: SortOrder?, sourceTitle: String? = null) {
129+
startActivity(listIntent(contextOrNull() ?: return, source, filter, sortOrder, sourceTitle))
130130
}
131131

132132
fun openList(tag: MangaTag) = openList(tag.source, MangaListFilter(tags = setOf(tag)), null)
@@ -141,8 +141,8 @@ class AppRouter private constructor(
141141

142142
fun openSearch(source: MangaSource, query: String) = openList(source, MangaListFilter(query = query), null)
143143

144-
fun openDetails(manga: Manga) {
145-
startActivity(detailsIntent(contextOrNull() ?: return, manga))
144+
fun openDetails(manga: Manga, sourceTitle: String? = null) {
145+
startActivity(detailsIntent(contextOrNull() ?: return, manga, sourceTitle))
146146
}
147147

148148
fun openDetails(mangaId: Long) {
@@ -694,18 +694,20 @@ class AppRouter private constructor(
694694
(view.context.findActivity() as? FragmentActivity)?.let(::AppRouter)
695695
}
696696

697-
fun detailsIntent(context: Context, manga: Manga) = Intent(context, DetailsActivity::class.java)
697+
fun detailsIntent(context: Context, manga: Manga, sourceTitle: String? = null) = Intent(context, DetailsActivity::class.java)
698698
.putExtra(KEY_MANGA, ParcelableManga(manga))
699+
.putExtra(KEY_SOURCE_TITLE, sourceTitle)
699700
.setData(shortMangaUrl(manga.id))
700701

701702
fun detailsIntent(context: Context, mangaId: Long) = Intent(context, DetailsActivity::class.java)
702703
.putExtra(KEY_ID, mangaId)
703704
.setData(shortMangaUrl(mangaId))
704705

705-
fun listIntent(context: Context, source: MangaSource, filter: MangaListFilter?, sortOrder: SortOrder?): Intent =
706+
fun listIntent(context: Context, source: MangaSource, filter: MangaListFilter?, sortOrder: SortOrder?, sourceTitle: String? = null): Intent =
706707
Intent(context, MangaListActivity::class.java)
707708
.setAction(ACTION_MANGA_EXPLORE)
708709
.putExtra(KEY_SOURCE, source.name)
710+
.putExtra(KEY_SOURCE_TITLE, sourceTitle)
709711
.apply {
710712
if (!filter.isNullOrEmpty()) {
711713
putExtra(KEY_FILTER, ParcelableMangaListFilter(filter))
@@ -830,6 +832,7 @@ class AppRouter private constructor(
830832
const val KEY_READER_MODE = "reader_mode"
831833
const val KEY_SORT_ORDER = "sort_order"
832834
const val KEY_SOURCE = "source"
835+
const val KEY_SOURCE_TITLE = "source_title"
833836
const val KEY_TAB = "tab"
834837
const val KEY_TITLE = "title"
835838
const val KEY_URL = "url"

app/src/main/kotlin/io/github/landwarderer/futon/core/nav/MangaIntent.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.lifecycle.SavedStateHandle
77
import io.github.landwarderer.futon.core.model.parcelable.ParcelableManga
88
import io.github.landwarderer.futon.core.nav.AppRouter.Companion.KEY_ID
99
import io.github.landwarderer.futon.core.nav.AppRouter.Companion.KEY_MANGA
10+
import io.github.landwarderer.futon.core.nav.AppRouter.Companion.KEY_SOURCE_TITLE
1011
import io.github.landwarderer.futon.core.util.ext.getParcelableCompat
1112
import io.github.landwarderer.futon.core.util.ext.getParcelableExtraCompat
1213
import org.koitharu.kotatsu.parsers.model.Manga
@@ -15,24 +16,28 @@ class MangaIntent private constructor(
1516
@JvmField val manga: Manga?,
1617
@JvmField val id: Long,
1718
@JvmField val uri: Uri?,
19+
@JvmField val sourceTitle: String?,
1820
) {
1921

2022
constructor(intent: Intent?) : this(
2123
manga = intent?.getParcelableExtraCompat<ParcelableManga>(KEY_MANGA)?.manga,
2224
id = intent?.getLongExtra(KEY_ID, ID_NONE) ?: ID_NONE,
2325
uri = intent?.data,
26+
sourceTitle = intent?.getStringExtra(KEY_SOURCE_TITLE),
2427
)
2528

2629
constructor(savedStateHandle: SavedStateHandle) : this(
2730
manga = savedStateHandle.get<ParcelableManga>(KEY_MANGA)?.manga,
2831
id = savedStateHandle[KEY_ID] ?: ID_NONE,
2932
uri = savedStateHandle[AppRouter.KEY_DATA],
33+
sourceTitle = savedStateHandle[KEY_SOURCE_TITLE],
3034
)
3135

3236
constructor(args: Bundle?) : this(
3337
manga = args?.getParcelableCompat<ParcelableManga>(KEY_MANGA)?.manga,
3438
id = args?.getLong(KEY_ID, ID_NONE) ?: ID_NONE,
3539
uri = null,
40+
sourceTitle = args?.getString(KEY_SOURCE_TITLE),
3641
)
3742

3843
val mangaId: Long
@@ -42,6 +47,6 @@ class MangaIntent private constructor(
4247

4348
const val ID_NONE = 0L
4449

45-
fun of(manga: Manga) = MangaIntent(manga, manga.id, null)
50+
fun of(manga: Manga) = MangaIntent(manga, manga.id, null, null)
4651
}
4752
}

app/src/main/kotlin/io/github/landwarderer/futon/details/ui/DetailsActivity.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ class DetailsActivity :
394394
ListItemType.MANGA_GRID,
395395
mangaGridItemAD(
396396
sizeResolver = StaticItemSizeResolver(resources.getDimensionPixelSize(R.dimen.smaller_grid_width)),
397-
) { item, view ->
397+
) { item, _ ->
398398
router.openDetails(item.toMangaWithOverride())
399399
},
400400
).also { rv.adapter = it }
@@ -460,7 +460,12 @@ class DetailsActivity :
460460
textViewSource.isVisible = false
461461
textViewSourceLabel.isVisible = false
462462
} else {
463-
textViewSource.textAndVisible = manga.source.getTitle(this@DetailsActivity)
463+
val sourceTitle = manga.source.getTitle(this@DetailsActivity)
464+
textViewSource.textAndVisible = if (sourceTitle == getString(R.string.unknown)) {
465+
viewModel.sourceTitle ?: sourceTitle
466+
} else {
467+
sourceTitle
468+
}
464469
textViewSource.setTooltipCompat(manga.source.getSummary(this@DetailsActivity))
465470
textViewSourceLabel.isVisible = textViewSource.isVisible == true
466471
}

app/src/main/kotlin/io/github/landwarderer/futon/details/ui/DetailsViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class DetailsViewModel @Inject constructor(
8787
private val intent = MangaIntent(savedStateHandle)
8888
private var loadingJob: Job
8989
val mangaId = intent.mangaId
90+
val sourceTitle = intent.sourceTitle
9091
private val scrobblers: Set<@JvmSuppressWildcards Scrobbler> by lazy { scrobblersProvider.get() }
9192

9293
init {

0 commit comments

Comments
 (0)