Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,25 @@ object EhDB {
db.filterDao().update(filter)
}

fun exportDB(context: Context, file: Path) {
context(Context)
fun exportDB(file: Path) {
db.query("PRAGMA wal_checkpoint(FULL)", null).use { it.moveToNext() }
val dbFile = context.getDatabasePath(DB_NAME)
val dbFile = getDatabasePath(DB_NAME)
dbFile.toOkioPath() sendTo file
}

suspend fun importDB(context: Context, uri: Uri) {
context(Context)
suspend fun importDB(uri: Uri) {
val tempDBName = "tmp.db"
resource {
context.deleteDatabase(tempDBName)
deleteDatabase(tempDBName)
roomDb<EhDatabase>(tempDBName) {
createFromInputStream { context.contentResolver.openInputStream(uri) }
createFromInputStream { contentResolver.openInputStream(uri) }
addMigrations(Schema17to18())
}
} release {
it.close()
context.deleteDatabase(tempDBName)
deleteDatabase(tempDBName)
} use { oldDB ->
db.galleryDao().insertOrIgnore(oldDB.galleryDao().list())

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/hippo/ehviewer/client/EhUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.hippo.ehviewer.client

import android.app.DownloadManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.os.Environment
import androidx.compose.material3.MaterialTheme
Expand All @@ -32,6 +31,7 @@ import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.parser.Archive
import com.hippo.ehviewer.spider.SpiderDen
import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.FileUtils
import com.hippo.ehviewer.util.addTextToClipboard
Expand Down Expand Up @@ -193,7 +193,7 @@ object EhUtils {
return title.substringBeforeLast('|').trim().ifEmpty { null }
}

context(Context)
context(MainActivity)
suspend fun downloadArchive(galleryDetail: GalleryDetail, archive: Archive) {
val gid = galleryDetail.gid
EhEngine.downloadArchive(gid, galleryDetail.token, archive.res, archive.isHAtH)?.let {
Expand Down
80 changes: 34 additions & 46 deletions app/src/main/kotlin/com/hippo/ehviewer/ui/CommonOperations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.download.tempDownloadDir
import com.hippo.ehviewer.ui.destinations.ReaderScreenDestination
import com.hippo.ehviewer.ui.reader.ReaderScreenArgs
import com.hippo.ehviewer.ui.screen.implicit
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.ui.tools.LabeledCheckbox
import com.hippo.ehviewer.util.FavouriteStatusRouter
import com.hippo.ehviewer.util.bgWork
import com.hippo.ehviewer.util.findActivity
import com.hippo.ehviewer.util.isAtLeastT
import com.hippo.ehviewer.util.mapToLongArray
import com.hippo.ehviewer.util.requestPermission
Expand Down Expand Up @@ -108,11 +108,9 @@ suspend fun keepNoMediaFileStatus(downloadDir: Path = downloadLocation) {

fun getFavoriteIcon(favorited: Boolean) = if (favorited) Icons.Default.Favorite else Icons.Default.FavoriteBorder

suspend fun DialogState.startDownload(
context: Context,
forceDefault: Boolean,
vararg galleryInfos: BaseGalleryInfo,
) = with(context) {
context(MainActivity)
suspend fun DialogState.startDownload(forceDefault: Boolean, vararg galleryInfos: BaseGalleryInfo) {
val context = implicit<Context>()
if (isAtLeastT) {
requestPermission(Manifest.permission.POST_NOTIFICATIONS)
}
Expand All @@ -125,9 +123,7 @@ suspend fun DialogState.startDownload(
ContextCompat.startForegroundService(context, intent)
}
if (toAdd.isEmpty()) {
return with(findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list)
}
return showTip(R.string.added_to_download_list)
}
var justStart = forceDefault
var label: String? = null
Expand All @@ -151,9 +147,7 @@ suspend fun DialogState.startDownload(
ContextCompat.startForegroundService(context, intent)
}
// Notify
with(findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list)
}
showTip(R.string.added_to_download_list)
} else {
// Let use chose label
val list = DownloadManager.labelList
Expand Down Expand Up @@ -184,9 +178,7 @@ suspend fun DialogState.startDownload(
} else {
Settings.hasDefaultDownloadLabel = false
}
with(context.findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list)
}
showTip(R.string.added_to_download_list)
}
}

Expand Down Expand Up @@ -286,7 +278,7 @@ fun DestinationsNavigator.navToReader(uri: Uri) = navToReader(ReaderScreenArgs.A

private fun DestinationsNavigator.navToReader(args: ReaderScreenArgs) = navigate(ReaderScreenDestination(args)) { launchSingleTop = true }

context(DialogState, Context, DestinationsNavigator)
context(DialogState, MainActivity, DestinationsNavigator)
suspend fun doGalleryInfoAction(info: BaseGalleryInfo) {
val downloaded = DownloadManager.getDownloadState(info.gid) != DownloadInfo.STATE_INVALID
val favorited = info.favoriteSlot != NOT_FAVORITED
Expand All @@ -308,41 +300,37 @@ suspend fun doGalleryInfoAction(info: BaseGalleryInfo) {
add(Icons.AutoMirrored.Default.DriveFileMove to R.string.download_move_dialog_title)
}
}
val selected = awaitSelectItemWithIcon(items, EhUtils.getSuitableTitle(info))
with(findActivity<MainActivity>()) {
when (selected) {
0 -> {
EhDB.putHistoryInfo(info)
navToReader(info)
}

1 -> withUIContext {
if (downloaded) {
confirmRemoveDownload(info)
} else {
startDownload(this@with, false, info)
}
}
when (awaitSelectItemWithIcon(items, EhUtils.getSuitableTitle(info))) {
0 -> {
EhDB.putHistoryInfo(info)
navToReader(info)
}

2 -> if (favorited) {
runSuspendCatching {
removeFromFavorites(info)
showTip(R.string.remove_from_favorite_success)
}.onFailure {
showTip(R.string.remove_from_favorite_failure)
}
1 -> withUIContext {
if (downloaded) {
confirmRemoveDownload(info)
} else {
runSuspendCatching {
modifyFavorites(info)
showTip(R.string.add_to_favorite_success)
}.onFailure {
showTip(R.string.add_to_favorite_failure)
}
startDownload(false, info)
}
}

3 -> showMoveDownloadLabel(info)
2 -> if (favorited) {
runSuspendCatching {
removeFromFavorites(info)
showTip(R.string.remove_from_favorite_success)
}.onFailure {
showTip(R.string.remove_from_favorite_failure)
}
} else {
runSuspendCatching {
modifyFavorites(info)
showTip(R.string.add_to_favorite_success)
}.onFailure {
showTip(R.string.add_to_favorite_failure)
}
}
true

3 -> showMoveDownloadLabel(info)
}
}

Expand Down
9 changes: 5 additions & 4 deletions app/src/main/kotlin/com/hippo/ehviewer/ui/ComposeDefault.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.hippo.ehviewer.ui

import androidx.activity.compose.LocalActivity
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import com.hippo.ehviewer.ui.screen.implicit
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.ui.tools.LocalDialogState
import com.hippo.ehviewer.ui.tools.NoopTransitionsVisibilityScope
import com.hippo.ehviewer.ui.tools.TransitionsVisibilityScope
import com.hippo.ehviewer.ui.tools.togetherWith
import com.hippo.ehviewer.util.findActivity
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.CoroutineScope

Expand All @@ -25,7 +23,7 @@ inline fun <R> AnimatedVisibilityScope.composing(
) = with(NoopTransitionsVisibilityScope) {
togetherWith(implicit<AnimatedVisibilityScope>()) {
block(
with(LocalContext.current) { remember { findActivity() } },
activity(),
LocalSnackBarHostState.current,
LocalDialogState.current,
LocalSharedTransitionScope.current,
Expand All @@ -35,3 +33,6 @@ inline fun <R> AnimatedVisibilityScope.composing(
)
}
}

@Composable
fun activity() = LocalActivity.current as MainActivity
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hippo.ehviewer.ui

import android.content.Context
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -62,7 +61,7 @@ private fun GalleryDetail.generateContent() = arrayOf(
R.string.favorite_name to favoriteName,
)

context(Context, DestinationsNavigator)
context(MainActivity, DestinationsNavigator)
@Composable
fun GalleryInfoBottomSheet(detail: GalleryDetail) {
Column(modifier = Modifier.fillMaxWidth()) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class MainActivity : EhActivity() {
runSuspendCatching {
withIOContext {
AppUpdater.checkForUpdate()?.let {
dialogState.showNewVersion(this@MainActivity, it)
dialogState.showNewVersion(it)
}
}
}.onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.gallery.Page
import com.hippo.ehviewer.gallery.PageLoader
import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.FileUtils
import com.hippo.ehviewer.util.awaitActivityResult
Expand Down Expand Up @@ -82,7 +83,7 @@ suspend fun copy(page: Page) {
}
}

context(SnackbarHostState, Context, PageLoader)
context(SnackbarHostState, MainActivity, PageLoader)
suspend fun save(page: Page) {
val granted = isAtLeastQ || requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
val cannotSave = getString(R.string.error_cant_save_image)
Expand Down Expand Up @@ -136,7 +137,7 @@ suspend fun save(page: Page) {
}
}

context(SnackbarHostState, Context, PageLoader)
context(SnackbarHostState, MainActivity, PageLoader)
suspend fun saveTo(page: Page) {
val filename = getImageFilename(page.index)
if (filename == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ fun AnimatedVisibilityScope.FavouritesScreen(navigator: DestinationsNavigator) =
onClick(Icons.Default.Download) {
val info = checkedInfoMap.takeAndClear()
runSwallowingWithUI {
startDownload(implicit<Context>(), false, *info.toTypedArray())
startDownload(false, *info.toTypedArray())
}
}
onClick(Icons.Default.Delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fun GalleryDetailContent(
fun onDownloadButtonClick() {
galleryDetail ?: return
if (DownloadManager.getDownloadState(galleryDetail.gid) == DownloadInfo.STATE_INVALID) {
launchUI { startDownload(implicit<MainActivity>(), false, galleryDetail.galleryInfo) }
launchUI { startDownload(false, galleryDetail.galleryInfo) }
} else {
launch { confirmRemoveDownload(galleryDetail) }
}
Expand Down Expand Up @@ -376,7 +376,7 @@ fun GalleryDetailContent(
}
}

context(Context, CoroutineScope, DestinationsNavigator, DialogState, SnackbarHostState)
context(MainActivity, CoroutineScope, DestinationsNavigator, DialogState, SnackbarHostState)
@Composable
fun BelowHeader(galleryDetail: GalleryDetail) {
@Composable
Expand Down
Loading