Skip to content

Commit

Permalink
feat: use web title as file name and book name
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Nov 1, 2024
1 parent 583a1a0 commit f908bc6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,12 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
}

private fun requestNotificationPermission() {
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
} else {
val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
} else {
}
}
}

// 發起權限請求
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
Expand Down Expand Up @@ -2648,7 +2649,7 @@ open class BrowserActivity : FragmentActivity(), BrowserController {

override fun showSaveEpubDialog() = dialogManager.showSaveEpubDialog { uri ->
if (uri == null) {
epubManager.showWriteEpubFilePicker(writeEpubFilePickerLauncher)
epubManager.showWriteEpubFilePicker(writeEpubFilePickerLauncher, ebWebView.title ?: "einkbro")
} else {
saveEpub(uri)
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/info/plateaukao/einkbro/epub/EpubManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EpubManager(private val context: Context) : KoinComponent {
val isNewFile =
(DocumentFile.fromSingleUri(activity, fileUri)?.length() ?: 0).toInt() == 0

val bookName = if (isNewFile) getBookName() else ""
val bookName = if (isNewFile) getBookName(ebWebView.title?.pruneWebTitle()) else ""
val chapterName = getChapterName(ebWebView.title?.pruneWebTitle())

if (bookName != null && chapterName != null) {
Expand Down Expand Up @@ -98,20 +98,23 @@ class EpubManager(private val context: Context) : KoinComponent {
).show()
}

private suspend fun getBookName(): String? {
private suspend fun getBookName(defaultBookName: String?): String? {
return TextInputDialog(
context,
context.getString(R.string.book_name),
context.getString(R.string.book_name_description),
"einkbro book"
defaultBookName ?: "einkbro book"
).show()
}

fun showWriteEpubFilePicker(activityResultLauncher: ActivityResultLauncher<Intent>) {
fun showWriteEpubFilePicker(
activityResultLauncher: ActivityResultLauncher<Intent>,
fileName: String,
) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = Constants.MIME_TYPE_EPUB
intent.putExtra(Intent.EXTRA_TITLE, "einkbro.epub")
intent.putExtra(Intent.EXTRA_TITLE, "$fileName.epub")
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activityResultLauncher.launch(intent)
Expand Down

0 comments on commit f908bc6

Please sign in to comment.