From 28c23d184a1bf0a0c84b5ef1da1776b1b09b78cf Mon Sep 17 00:00:00 2001 From: imkunet Date: Sun, 18 Feb 2024 06:19:25 -0500 Subject: [PATCH] Initial markdown render test --- .../java/eu/kanade/domain/DomainModule.kt | 2 + .../presentation/manga/MangaNotesScreen.kt | 89 +++++++++++++++++++ .../kanade/presentation/manga/MangaScreen.kt | 7 ++ .../manga/components/MangaToolbar.kt | 8 ++ .../kanade/tachiyomi/ui/manga/MangaScreen.kt | 2 + .../ui/manga/notes/MangaNotesScreen.kt | 42 +++++++++ .../ui/manga/notes/MangaNotesScreenModel.kt | 75 ++++++++++++++++ .../java/tachiyomi/data/manga/MangaMapper.kt | 4 + .../main/sqldelight/tachiyomi/data/mangas.sq | 3 +- .../domain/manga/interactor/SetMangaNotes.kt | 19 ++++ .../tachiyomi/domain/manga/model/Manga.kt | 2 + .../domain/manga/model/MangaUpdate.kt | 2 + .../moko-resources/base/strings.xml | 2 + 13 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eu/kanade/presentation/manga/MangaNotesScreen.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreen.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreenModel.kt create mode 100644 domain/src/main/java/tachiyomi/domain/manga/interactor/SetMangaNotes.kt diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index 08787e1f8..addd07f13 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -77,6 +77,7 @@ import tachiyomi.domain.manga.interactor.GetMangaWithChapters import tachiyomi.domain.manga.interactor.NetworkToLocalManga import tachiyomi.domain.manga.interactor.ResetViewerFlags import tachiyomi.domain.manga.interactor.SetMangaChapterFlags +import tachiyomi.domain.manga.interactor.SetMangaNotes import tachiyomi.domain.manga.repository.MangaRepository import tachiyomi.domain.release.interactor.GetApplicationRelease import tachiyomi.domain.release.service.ReleaseService @@ -122,6 +123,7 @@ class DomainModule : InjektModule { addFactory { GetUpcomingManga(get()) } addFactory { ResetViewerFlags(get()) } addFactory { SetMangaChapterFlags(get()) } + addFactory { SetMangaNotes(get()) } addFactory { FetchInterval(get()) } addFactory { SetMangaDefaultChapterFlags(get(), get(), get()) } addFactory { SetMangaViewerFlags(get()) } diff --git a/app/src/main/java/eu/kanade/presentation/manga/MangaNotesScreen.kt b/app/src/main/java/eu/kanade/presentation/manga/MangaNotesScreen.kt new file mode 100644 index 000000000..492a275f9 --- /dev/null +++ b/app/src/main/java/eu/kanade/presentation/manga/MangaNotesScreen.kt @@ -0,0 +1,89 @@ +package eu.kanade.presentation.manga + +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material.icons.filled.Edit +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.SpanStyle +import com.halilibo.richtext.markdown.Markdown +import com.halilibo.richtext.ui.RichTextStyle +import com.halilibo.richtext.ui.material3.RichText +import com.halilibo.richtext.ui.string.RichTextStringStyle +import eu.kanade.presentation.components.AppBar +import eu.kanade.presentation.components.AppBarTitle +import eu.kanade.tachiyomi.ui.manga.notes.MangaNotesScreenState +import tachiyomi.i18n.MR +import tachiyomi.presentation.core.components.material.ExtendedFloatingActionButton +import tachiyomi.presentation.core.components.material.Scaffold +import tachiyomi.presentation.core.components.material.padding +import tachiyomi.presentation.core.i18n.stringResource +import tachiyomi.presentation.core.screens.EmptyScreen + +@Composable +fun MangaNotesScreen( + state: MangaNotesScreenState.Success, + navigateUp: () -> Unit, + beginEditing: () -> Unit, + endEditing: () -> Unit, +) { + Scaffold( + topBar = { scrollBehavior -> + AppBar( + titleContent = { + AppBarTitle(title = stringResource(MR.strings.action_notes), subtitle = state.title) + }, + navigateUp = navigateUp, + scrollBehavior = scrollBehavior, + ) + }, + floatingActionButton = { + AnimatedVisibility( + true, + enter = fadeIn(), + exit = fadeOut(), + ) { + ExtendedFloatingActionButton( + text = { + Text(text = stringResource(if (state.editing) MR.strings.action_apply else MR.strings.action_edit)) + }, + icon = { Icon(imageVector = if (state.editing) Icons.Filled.Check else Icons.Filled.Edit, contentDescription = null) }, + onClick = { if (state.editing) endEditing() else beginEditing() }, + expanded = true, + ) + } + }, + ) { paddingValues -> + if (state.isEmpty || state.content == null) { + EmptyScreen( + stringRes = MR.strings.information_no_notes, + modifier = Modifier.padding(paddingValues), + ) + return@Scaffold + } + + RichText( + modifier = Modifier + .fillMaxWidth() + .padding( + horizontal = MaterialTheme.padding.small, + vertical = paddingValues.calculateTopPadding() + MaterialTheme.padding.small, + ), + style = RichTextStyle( + stringStyle = RichTextStringStyle( + linkStyle = SpanStyle(color = MaterialTheme.colorScheme.primary), + ), + ), + ) { + Markdown(content = state.content) + } + } +} diff --git a/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt b/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt index 3115d0457..99d086526 100644 --- a/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt @@ -112,6 +112,7 @@ fun MangaScreen( onEditCategoryClicked: (() -> Unit)?, onEditFetchIntervalClicked: (() -> Unit)?, onMigrateClicked: (() -> Unit)?, + onNotesClicked: () -> Unit, // For bottom action menu onMultiBookmarkClicked: (List, bookmarked: Boolean) -> Unit, @@ -160,6 +161,7 @@ fun MangaScreen( onEditCategoryClicked = onEditCategoryClicked, onEditIntervalClicked = onEditFetchIntervalClicked, onMigrateClicked = onMigrateClicked, + onClickNotes = onNotesClicked, onMultiBookmarkClicked = onMultiBookmarkClicked, onMultiMarkAsReadClicked = onMultiMarkAsReadClicked, onMarkPreviousAsReadClicked = onMarkPreviousAsReadClicked, @@ -195,6 +197,7 @@ fun MangaScreen( onEditCategoryClicked = onEditCategoryClicked, onEditIntervalClicked = onEditFetchIntervalClicked, onMigrateClicked = onMigrateClicked, + onClickNotes = onNotesClicked, onMultiBookmarkClicked = onMultiBookmarkClicked, onMultiMarkAsReadClicked = onMultiMarkAsReadClicked, onMarkPreviousAsReadClicked = onMarkPreviousAsReadClicked, @@ -240,6 +243,7 @@ private fun MangaScreenSmallImpl( onEditCategoryClicked: (() -> Unit)?, onEditIntervalClicked: (() -> Unit)?, onMigrateClicked: (() -> Unit)?, + onClickNotes: () -> Unit, // For bottom action menu onMultiBookmarkClicked: (List, bookmarked: Boolean) -> Unit, @@ -305,6 +309,7 @@ private fun MangaScreenSmallImpl( onClickEditCategory = onEditCategoryClicked, onClickRefresh = onRefresh, onClickMigrate = onMigrateClicked, + onClickNotes = onClickNotes, actionModeCounter = selectedChapterCount, onSelectAll = { onAllChapterSelected(true) }, onInvertSelection = { onInvertSelection() }, @@ -484,6 +489,7 @@ fun MangaScreenLargeImpl( onEditCategoryClicked: (() -> Unit)?, onEditIntervalClicked: (() -> Unit)?, onMigrateClicked: (() -> Unit)?, + onClickNotes: () -> Unit, // For bottom action menu onMultiBookmarkClicked: (List, bookmarked: Boolean) -> Unit, @@ -542,6 +548,7 @@ fun MangaScreenLargeImpl( onClickEditCategory = onEditCategoryClicked, onClickRefresh = onRefresh, onClickMigrate = onMigrateClicked, + onClickNotes = onClickNotes, actionModeCounter = selectedChapterCount, onSelectAll = { onAllChapterSelected(true) }, onInvertSelection = { onInvertSelection() }, diff --git a/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt b/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt index 4415bbf27..919e15957 100644 --- a/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt +++ b/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt @@ -44,6 +44,7 @@ fun MangaToolbar( onClickEditCategory: (() -> Unit)?, onClickRefresh: () -> Unit, onClickMigrate: (() -> Unit)?, + onClickNotes: () -> Unit, // For action mode actionModeCounter: Int, @@ -149,6 +150,13 @@ fun MangaToolbar( ), ) } + + add( + AppBar.OverflowAction( + title = stringResource(MR.strings.action_notes), + onClick = onClickNotes, + ) + ) } .build(), ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt index 9d71a8f51..6a6081967 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreen.kt @@ -50,6 +50,7 @@ import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceScreen import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchScreen import eu.kanade.tachiyomi.ui.category.CategoryScreen import eu.kanade.tachiyomi.ui.home.HomeScreen +import eu.kanade.tachiyomi.ui.manga.notes.MangaNotesScreen import eu.kanade.tachiyomi.ui.manga.track.TrackInfoDialogHomeScreen import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.ui.setting.SettingsScreen @@ -164,6 +165,7 @@ class MangaScreen( onMigrateClicked = { navigator.push(MigrateSearchScreen(successState.manga.id)) }.takeIf { successState.manga.favorite }, + onNotesClicked = { navigator.push(MangaNotesScreen(successState.manga.id)) }, onMultiBookmarkClicked = screenModel::bookmarkChapters, onMultiMarkAsReadClicked = screenModel::markChaptersRead, onMarkPreviousAsReadClicked = screenModel::markPreviousChapterRead, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreen.kt new file mode 100644 index 000000000..9896b8239 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreen.kt @@ -0,0 +1,42 @@ +package eu.kanade.tachiyomi.ui.manga.notes + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import cafe.adriel.voyager.core.model.rememberScreenModel +import cafe.adriel.voyager.navigator.LocalNavigator +import cafe.adriel.voyager.navigator.currentOrThrow +import eu.kanade.presentation.manga.MangaNotesScreen +import eu.kanade.presentation.util.Screen +import tachiyomi.presentation.core.screens.LoadingScreen + +class MangaNotesScreen(private val mangaId: Long) : Screen() { + @Composable + override fun Content() { + val navigator = LocalNavigator.currentOrThrow + + val screenModel = rememberScreenModel { MangaNotesScreenModel(mangaId = mangaId) } + val state by screenModel.state.collectAsState() + + DisposableEffect(Unit) { + onDispose { + + } + } + + if (state is MangaNotesScreenState.Loading) { + LoadingScreen() + return + } + + val successState = state as MangaNotesScreenState.Success + + MangaNotesScreen( + state = successState, + navigateUp = navigator::pop, + beginEditing = { screenModel.beginEditing() }, + endEditing = { screenModel.endEditing() }, + ) + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreenModel.kt new file mode 100644 index 000000000..a9e1b8e74 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/notes/MangaNotesScreenModel.kt @@ -0,0 +1,75 @@ +package eu.kanade.tachiyomi.ui.manga.notes + +import androidx.compose.runtime.Immutable +import cafe.adriel.voyager.core.model.StateScreenModel +import cafe.adriel.voyager.core.model.screenModelScope +import eu.kanade.tachiyomi.ui.category.CategoryEvent +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.receiveAsFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import tachiyomi.domain.manga.interactor.GetManga +import tachiyomi.domain.manga.interactor.SetMangaNotes +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +class MangaNotesScreenModel( + val mangaId: Long, + getManga: GetManga = Injekt.get(), + setMangaNotes: SetMangaNotes = Injekt.get(), +) : StateScreenModel(MangaNotesScreenState.Loading) { + + private val _events: Channel = Channel() + val events = _events.receiveAsFlow() + + init { + screenModelScope.launch { + getManga.subscribe(mangaId).collectLatest { manga -> + mutableState.update { + MangaNotesScreenState.Success( + content = manga.notes, + title = manga.title, + ) + } + } + } + } + + fun beginEditing() { + mutableState.update { + when (it) { + MangaNotesScreenState.Loading -> it + is MangaNotesScreenState.Success -> it.copy(editing = true) + } + } + } + + fun endEditing() { + mutableState.update { + when (it) { + MangaNotesScreenState.Loading -> it + is MangaNotesScreenState.Success -> it.copy(editing = false) + } + } + } +} + +sealed interface MangaNotesScreenState { + + @Immutable + data object Loading : MangaNotesScreenState + + @Immutable + data class Success( + val content: String?, + val title: String, + + val editing: Boolean = false, + ) : MangaNotesScreenState { + + val isEmpty: Boolean + get() = content.isNullOrEmpty() + } +} + diff --git a/data/src/main/java/tachiyomi/data/manga/MangaMapper.kt b/data/src/main/java/tachiyomi/data/manga/MangaMapper.kt index 53b45ad09..45c7e5dc9 100644 --- a/data/src/main/java/tachiyomi/data/manga/MangaMapper.kt +++ b/data/src/main/java/tachiyomi/data/manga/MangaMapper.kt @@ -31,6 +31,7 @@ object MangaMapper { version: Long, @Suppress("UNUSED_PARAMETER") isSyncing: Long, + notes: String?, ): Manga = Manga( id = id, source = source, @@ -55,6 +56,7 @@ object MangaMapper { lastModifiedAt = lastModifiedAt, favoriteModifiedAt = favoriteModifiedAt, version = version, + notes = notes, ) fun mapLibraryManga( @@ -82,6 +84,7 @@ object MangaMapper { favoriteModifiedAt: Long?, version: Long, isSyncing: Long, + notes: String?, totalCount: Long, readCount: Double, latestUpload: Long, @@ -115,6 +118,7 @@ object MangaMapper { favoriteModifiedAt, version, isSyncing, + notes, ), category = category, totalChapters = totalCount, diff --git a/data/src/main/sqldelight/tachiyomi/data/mangas.sq b/data/src/main/sqldelight/tachiyomi/data/mangas.sq index 3e21e5bc0..525bd71c2 100644 --- a/data/src/main/sqldelight/tachiyomi/data/mangas.sq +++ b/data/src/main/sqldelight/tachiyomi/data/mangas.sq @@ -27,7 +27,8 @@ CREATE TABLE mangas( last_modified_at INTEGER NOT NULL DEFAULT 0, favorite_modified_at INTEGER, version INTEGER NOT NULL DEFAULT 0, - is_syncing INTEGER NOT NULL DEFAULT 0 + is_syncing INTEGER NOT NULL DEFAULT 0, + notes TEXT ); CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1; diff --git a/domain/src/main/java/tachiyomi/domain/manga/interactor/SetMangaNotes.kt b/domain/src/main/java/tachiyomi/domain/manga/interactor/SetMangaNotes.kt new file mode 100644 index 000000000..763cc3a27 --- /dev/null +++ b/domain/src/main/java/tachiyomi/domain/manga/interactor/SetMangaNotes.kt @@ -0,0 +1,19 @@ +package tachiyomi.domain.manga.interactor + +import tachiyomi.domain.manga.model.Manga +import tachiyomi.domain.manga.model.MangaUpdate +import tachiyomi.domain.manga.repository.MangaRepository + +class SetMangaNotes( + private val mangaRepository: MangaRepository, +) { + + suspend fun awaitSetNotes(manga: Manga, notes: String): Boolean { + return mangaRepository.update( + MangaUpdate( + id = manga.id, + notes = notes, + ), + ) + } +} diff --git a/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt b/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt index fb634c1cd..f65537e49 100644 --- a/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt +++ b/domain/src/main/java/tachiyomi/domain/manga/model/Manga.kt @@ -32,6 +32,7 @@ data class Manga( val lastModifiedAt: Long, val favoriteModifiedAt: Long?, val version: Long, + val notes: String?, ) : Serializable { val expectedNextUpdate: Instant? @@ -126,6 +127,7 @@ data class Manga( lastModifiedAt = 0L, favoriteModifiedAt = null, version = 0L, + notes = null, ) } } diff --git a/domain/src/main/java/tachiyomi/domain/manga/model/MangaUpdate.kt b/domain/src/main/java/tachiyomi/domain/manga/model/MangaUpdate.kt index 7f1cc2f87..67882d848 100644 --- a/domain/src/main/java/tachiyomi/domain/manga/model/MangaUpdate.kt +++ b/domain/src/main/java/tachiyomi/domain/manga/model/MangaUpdate.kt @@ -24,6 +24,7 @@ data class MangaUpdate( val updateStrategy: UpdateStrategy? = null, val initialized: Boolean? = null, val version: Long? = null, + val notes: String? = null, ) fun Manga.toMangaUpdate(): MangaUpdate { @@ -49,5 +50,6 @@ fun Manga.toMangaUpdate(): MangaUpdate { updateStrategy = updateStrategy, initialized = initialized, version = version, + notes = notes, ) } diff --git a/i18n/src/commonMain/moko-resources/base/strings.xml b/i18n/src/commonMain/moko-resources/base/strings.xml index 2d202ccf6..a0b80a5fd 100644 --- a/i18n/src/commonMain/moko-resources/base/strings.xml +++ b/i18n/src/commonMain/moko-resources/base/strings.xml @@ -146,6 +146,7 @@ Move series to top Move to bottom Move series to bottom + Notes Install Share Save @@ -912,6 +913,7 @@ Failed to bypass Cloudflare Tap here for help with Cloudflare *required + There are no notes here yet! WebView is required for the app to function