Skip to content

Commit

Permalink
ui: remove xml style bookmark history context menu dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Nov 15, 2024
1 parent 3616b13 commit f64657e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
actionModeMenuViewModel.finish()
}
}

is ActionModeMenuState.ReadFromHere -> readFromThisSentence()

is Gpt -> {
Expand Down Expand Up @@ -1805,7 +1805,6 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
addAlbum(getString(R.string.app_name), "", incognito = true)
focusOnInput()
},
onHistoryChanged = { },
splitScreenAction = { url -> toggleSplitScreen(url) },
addEmptyTabAction = { newATab() }
)
Expand Down Expand Up @@ -2477,6 +2476,8 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
}
}
}

else -> Unit
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package info.plateaukao.einkbro.view.dialog.compose

import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.icons.outlined.Tab
import androidx.compose.material.icons.outlined.TabUnselected
import androidx.compose.material.icons.outlined.ViewStream
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.database.Bookmark
import info.plateaukao.einkbro.view.compose.MyTheme

class BookmarkContextMenuDlgFragment(
private val bookmark: Bookmark,
private val allowEdit: Boolean = true,
private val onClicked: (ContextMenuItemType) -> Unit,
) : ComposeDialogFragment() {
override fun setupComposeView() {
composeView.setContent {
MyTheme {
BookmarkContextMenuScreen(
title = bookmark.title,
allowEdit = allowEdit,
onClicked = { onClicked(it); dismiss() })
}
}
}
}

@Composable
fun BookmarkContextMenuScreen(
title: String,
allowEdit: Boolean = true,
onClicked: (ContextMenuItemType) -> Unit,
) {
Column(
modifier = Modifier
.wrapContentHeight()
.width(320.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = title,
Modifier.padding(4.dp),
color = MaterialTheme.colors.onBackground,
overflow = TextOverflow.Ellipsis,
maxLines = 1,
)
HorizontalSeparator()
Row(
modifier = Modifier
.width(IntrinsicSize.Max)
.horizontalScroll(rememberScrollState()),
) {
ContextMenuItem(R.string.main_menu_new_tabOpen, true, Icons.Outlined.Tab) {
onClicked(ContextMenuItemType.NewTabForeground)
}
ContextMenuItem(R.string.main_menu_new_tab, true, Icons.Outlined.TabUnselected) {
onClicked(ContextMenuItemType.NewTabBackground)
}
ContextMenuItem(R.string.split_screen, true, Icons.Outlined.ViewStream) {
onClicked(ContextMenuItemType.SplitScreen)
}
if (allowEdit) {
ContextMenuItem(R.string.menu_edit, true, Icons.Outlined.Edit) { onClicked(ContextMenuItemType.Edit) }
}
ContextMenuItem(R.string.menu_delete, true, Icons.Outlined.Delete) {
onClicked(ContextMenuItemType.Delete)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package info.plateaukao.einkbro.view.dialog.compose

import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
Expand Down Expand Up @@ -40,7 +38,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -50,14 +47,11 @@ import androidx.lifecycle.LifecycleCoroutineScope
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.database.Bookmark
import info.plateaukao.einkbro.database.BookmarkManager
import info.plateaukao.einkbro.databinding.DialogMenuContextListBinding
import info.plateaukao.einkbro.unit.ViewUnit
import info.plateaukao.einkbro.view.EBToast
import info.plateaukao.einkbro.view.compose.MyTheme
import info.plateaukao.einkbro.view.compose.NormalTextModifier
import info.plateaukao.einkbro.view.dialog.BookmarkEditDialog
import info.plateaukao.einkbro.view.dialog.DialogManager
import info.plateaukao.einkbro.view.dialog.dismissWithAction
import info.plateaukao.einkbro.viewmodel.BookmarkViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand All @@ -74,7 +68,6 @@ class BookmarksDialogFragment(
private val syncBookmarksAction: (Boolean) -> Unit,
private val linkBookmarksAction: () -> Unit,
) : ComposeDialogFragment(), KoinComponent {
private val dialogManager: DialogManager by lazy { DialogManager(requireActivity()) }

private lateinit var bookmarksUpdateJob: Job

Expand Down Expand Up @@ -155,58 +148,53 @@ class BookmarksDialogFragment(
}

private fun showBookmarkContextMenu(bookmark: Bookmark) {
val dialogView = DialogMenuContextListBinding.inflate(LayoutInflater.from(requireContext()))
val optionDialog = dialogManager.showOptionDialog(dialogView.root)
BookmarkContextMenuDlgFragment(
bookmark,
) {
when (it) {
ContextMenuItemType.NewTabForeground -> {
bookmarkIconClickAction(
bookmark.title,
bookmark.url,
true
)
dialog?.dismiss()
}

if (bookmark.isDirectory) {
dialogView.menuContextListNewTab.visibility = View.GONE
dialogView.menuContextListNewTabOpen.visibility = View.GONE
dialogView.menuContextListSplitScreen.visibility = View.GONE
}
ContextMenuItemType.NewTabBackground -> {
bookmarkIconClickAction(
bookmark.title,
bookmark.url,
false
)
dialog?.dismiss()
}

dialogView.menuContextListSplitScreen.setOnClickListener {
optionDialog.dismissWithAction { splitScreenAction(bookmark.url); dialog?.dismiss() }
}
ContextMenuItemType.SplitScreen -> {
splitScreenAction(bookmark.url)
dialog?.dismiss()
}

dialogView.menuTitle.text = bookmark.title
dialogView.menuContextListEdit.visibility = View.VISIBLE
dialogView.menuContextListNewTab.setOnClickListener {
optionDialog.dismissWithAction {
bookmarkIconClickAction(getString(R.string.app_name), bookmark.url, false)
dialog?.dismiss()
}
}
dialogView.menuContextListNewTabOpen.setOnClickListener {
optionDialog.dismissWithAction {
bookmarkIconClickAction(
bookmark.title,
bookmark.url,
true
)
dialog?.dismiss()
}
}
dialogView.menuContextListDelete.setOnClickListener {
lifecycleScope.launch {
bookmarkViewModel.deleteBookmark(bookmark)
syncBookmarksAction(true)
}
optionDialog.dismiss()
}
ContextMenuItemType.Edit -> BookmarkEditDialog(
requireActivity(),
bookmarkViewModel,
bookmark,
{
ViewUnit.hideKeyboard(requireActivity())
syncBookmarksAction(true)
},
{ ViewUnit.hideKeyboard(requireActivity()) }
).show()

dialogView.menuContextListEdit.setOnClickListener {
BookmarkEditDialog(
requireActivity(),
bookmarkViewModel,
bookmark,
{
ViewUnit.hideKeyboard(requireActivity())
ContextMenuItemType.Delete -> lifecycleScope.launch {
bookmarkViewModel.deleteBookmark(bookmark)
syncBookmarksAction(true)
},
{ ViewUnit.hideKeyboard(requireActivity()) }
).show()
optionDialog.dismiss()
}
}

else -> Unit
}
}.show(parentFragmentManager, "bookmark_context_menu")

}
}

Expand Down Expand Up @@ -243,7 +231,7 @@ fun DialogPanel(
Spacer(modifier = Modifier.size(36.dp))
}
Text(
if (folder.id == 0) stringResource(id = R.string.bookmarks) else folder.title,
if (folder.id == 0) "" else folder.title,
Modifier
.weight(1F)
.padding(horizontal = 5.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ enum class ContextMenuItemType {
NewTabForeground, NewTabBackground,
ShareLink, CopyLink, SelectText, OpenWith,
SaveBookmark, SaveAs,
SplitScreen, AdBlock, TranslateImage, Tts
SplitScreen, AdBlock, TranslateImage, Tts, Edit, Delete
}

@Preview(showBackground = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package info.plateaukao.einkbro.view.viewControllers

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.compose.runtime.MutableState
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.database.Bookmark
import info.plateaukao.einkbro.database.Record
import info.plateaukao.einkbro.database.RecordDb
import info.plateaukao.einkbro.database.RecordType
import info.plateaukao.einkbro.databinding.DialogMenuContextListBinding
import info.plateaukao.einkbro.preference.ConfigManager
import info.plateaukao.einkbro.unit.BrowserUnit
import info.plateaukao.einkbro.unit.IntentUnit
import info.plateaukao.einkbro.unit.ViewUnit
import info.plateaukao.einkbro.view.Album
import info.plateaukao.einkbro.view.compose.HistoryAndTabsView
import info.plateaukao.einkbro.view.dialog.DialogManager
import info.plateaukao.einkbro.view.dialog.compose.BookmarkContextMenuDlgFragment
import info.plateaukao.einkbro.view.dialog.compose.ContextMenuItemType
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -35,7 +35,6 @@ class OverviewDialogController(
private val gotoUrlAction: (String) -> Unit,
private val addTabAction: (String, String, Boolean) -> Unit,
private val addIncognitoTabAction: () -> Unit,
private val onHistoryChanged: () -> Unit,
private val splitScreenAction: (String) -> Unit,
private val addEmptyTabAction: () -> Unit,
) : KoinComponent {
Expand Down Expand Up @@ -80,10 +79,6 @@ class OverviewDialogController(
}
}

fun showAlbum(index: Int){
albumFocusIndex.value = index
}

private fun clickHistoryItem(record: Record) {
gotoUrlAction(record.url)
if (record.type == RecordType.Bookmark) {
Expand Down Expand Up @@ -139,53 +134,40 @@ class OverviewDialogController(
okAction = {
BrowserUnit.clearHistory(context)
hide()
onHistoryChanged()
})
}

private fun closeAllTabs() {
albumList.value.listIterator().forEach {
it.remove(true)
}

}

private fun showHistoryContextMenu(record: Record) {
val dialogView = DialogMenuContextListBinding.inflate(LayoutInflater.from(context))
val dialog = dialogManager.showOptionDialog(dialogView.root)

with(dialogView) {
menuTitle.text = record.title
menuContextListEdit.visibility = GONE
menuContextListNewTab.setOnClickListener {
dialog.dismissWithAction {
addTabAction(context.getString(R.string.app_name), record.url, false)
}
}
menuContextListSplitScreen.setOnClickListener {
dialog.dismissWithAction { splitScreenAction(record.url) }
hide()
}
menuContextListNewTabOpen.setOnClickListener {
dialog.dismissWithAction {
val parentFragmentManager = (context as FragmentActivity).supportFragmentManager
BookmarkContextMenuDlgFragment(
Bookmark(record.title ?: "no title", record.url),
allowEdit = false,
) {
when (it) {
ContextMenuItemType.Delete -> deleteHistory(record)

ContextMenuItemType.NewTabForeground ->
addTabAction(context.getString(R.string.app_name), record.url, true)
hide()
}
}
menuContextListDelete.setOnClickListener {
dialog.dismissWithAction { deleteHistory(record) }

ContextMenuItemType.NewTabBackground ->
addTabAction(context.getString(R.string.app_name), record.url, false)

ContextMenuItemType.SplitScreen -> splitScreenAction(record.url)

else -> Unit
}
}
hide()
}.show(parentFragmentManager, "history_context_menu")
}

private fun deleteHistory(record: Record) {
recordDb.deleteHistoryItem(record)
onHistoryChanged()
refreshHistoryList()
}
}

private fun Dialog.dismissWithAction(action: () -> Unit) {
dismiss()
action()
}
Loading

0 comments on commit f64657e

Please sign in to comment.