Library menu
parent
5d54298a22
commit
c4b03d1316
@ -0,0 +1,13 @@
|
|||||||
|
package org.koitharu.kotatsu.base.ui.dialog
|
||||||
|
|
||||||
|
import android.content.DialogInterface
|
||||||
|
|
||||||
|
class RememberSelectionDialogListener(initialValue: Int) : DialogInterface.OnClickListener {
|
||||||
|
|
||||||
|
var selection: Int = initialValue
|
||||||
|
private set
|
||||||
|
|
||||||
|
override fun onClick(dialog: DialogInterface?, which: Int) {
|
||||||
|
selection = which
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package org.koitharu.kotatsu.base.ui.util
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import org.koitharu.kotatsu.base.domain.ReversibleHandle
|
||||||
|
|
||||||
|
class ReversibleAction(
|
||||||
|
@StringRes val stringResId: Int,
|
||||||
|
val handle: ReversibleHandle?,
|
||||||
|
)
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
package org.koitharu.kotatsu.favourites.ui
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.core.graphics.Insets
|
||||||
|
import androidx.core.view.updatePadding
|
||||||
|
import androidx.fragment.app.commit
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||||
|
import org.koitharu.kotatsu.core.model.FavouriteCategory
|
||||||
|
import org.koitharu.kotatsu.databinding.ActivityContainerBinding
|
||||||
|
import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment
|
||||||
|
import org.koitharu.kotatsu.favourites.ui.list.FavouritesListFragment.Companion.NO_ID
|
||||||
|
|
||||||
|
class FavouritesActivity : BaseActivity<ActivityContainerBinding>() {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setContentView(ActivityContainerBinding.inflate(layoutInflater))
|
||||||
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
|
val categoryTitle = intent.getStringExtra(EXTRA_TITLE)
|
||||||
|
if (categoryTitle != null) {
|
||||||
|
title = categoryTitle
|
||||||
|
}
|
||||||
|
val fm = supportFragmentManager
|
||||||
|
if (fm.findFragmentById(R.id.container) == null) {
|
||||||
|
fm.commit {
|
||||||
|
val fragment = FavouritesListFragment.newInstance(intent.getLongExtra(EXTRA_CATEGORY_ID, NO_ID))
|
||||||
|
replace(R.id.container, fragment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onWindowInsetsChanged(insets: Insets) {
|
||||||
|
binding.toolbar.updatePadding(
|
||||||
|
left = insets.left,
|
||||||
|
right = insets.right,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val EXTRA_CATEGORY_ID = "cat_id"
|
||||||
|
private const val EXTRA_TITLE = "title"
|
||||||
|
|
||||||
|
fun newIntent(context: Context) = Intent(context, FavouritesActivity::class.java)
|
||||||
|
|
||||||
|
fun newIntent(context: Context, category: FavouriteCategory) = Intent(context, FavouritesActivity::class.java)
|
||||||
|
.putExtra(EXTRA_CATEGORY_ID, category.id)
|
||||||
|
.putExtra(EXTRA_TITLE, category.title)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package org.koitharu.kotatsu.library.ui
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuInflater
|
||||||
|
import android.view.MenuItem
|
||||||
|
import androidx.core.view.MenuProvider
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.ui.dialog.RememberSelectionDialogListener
|
||||||
|
import org.koitharu.kotatsu.favourites.ui.categories.CategoriesActivity
|
||||||
|
import org.koitharu.kotatsu.utils.ext.startOfDay
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
|
class LibraryMenuProvider(
|
||||||
|
private val context: Context,
|
||||||
|
private val viewModel: LibraryViewModel,
|
||||||
|
) : MenuProvider {
|
||||||
|
|
||||||
|
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||||
|
menuInflater.inflate(R.menu.opt_library, menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||||
|
return when (menuItem.itemId) {
|
||||||
|
R.id.action_clear_history -> {
|
||||||
|
showClearHistoryDialog()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.action_categories -> {
|
||||||
|
context.startActivity(CategoriesActivity.newIntent(context))
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showClearHistoryDialog() {
|
||||||
|
val selectionListener = RememberSelectionDialogListener(-1)
|
||||||
|
MaterialAlertDialogBuilder(context, materialR.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered)
|
||||||
|
.setTitle(R.string.clear_history)
|
||||||
|
.setSingleChoiceItems(
|
||||||
|
arrayOf(
|
||||||
|
context.getString(R.string.last_2_hours),
|
||||||
|
context.getString(R.string.today),
|
||||||
|
context.getString(R.string.clear_all_history),
|
||||||
|
),
|
||||||
|
selectionListener.selection,
|
||||||
|
selectionListener,
|
||||||
|
)
|
||||||
|
.setIcon(R.drawable.ic_delete)
|
||||||
|
.setNegativeButton(android.R.string.cancel, null)
|
||||||
|
.setPositiveButton(R.string.clear) { _, _ ->
|
||||||
|
val minDate = when (selectionListener.selection) {
|
||||||
|
0 -> System.currentTimeMillis() - TimeUnit.HOURS.toMillis(2)
|
||||||
|
1 -> Date().startOfDay()
|
||||||
|
2 -> 0L
|
||||||
|
else -> return@setPositiveButton
|
||||||
|
}
|
||||||
|
viewModel.clearHistory(minDate)
|
||||||
|
}.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:alpha="0.7" android:color="?attr/colorSecondaryContainer" />
|
||||||
|
</selector>
|
||||||
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<item android:alpha="0.7" android:color="?attr/colorSecondaryContainer" />
|
<item android:alpha="0.7" android:color="@color/kotatsu_secondaryContainer" />
|
||||||
</selector>
|
</selector>
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
|
||||||
|
<corners android:radius="6dp" />
|
||||||
|
<solid
|
||||||
|
android:color="?attr/colorAccent" />
|
||||||
|
<size android:height="32dp" />
|
||||||
|
|
||||||
|
</shape>
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:color="?attr/colorAccent"
|
||||||
|
android:drawable="@drawable/tab_rounded_rectangle"
|
||||||
|
android:padding="1dp" />
|
||||||
|
|
||||||
|
</selector>
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_categories"
|
||||||
|
android:orderInCategory="50"
|
||||||
|
android:title="@string/categories_"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_clear_history"
|
||||||
|
android:orderInCategory="50"
|
||||||
|
android:title="@string/clear_history"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
</menu>
|
||||||
Loading…
Reference in New Issue