From 35f4a31a760c2cf4cde6c27e0ac449eece01b30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 11 Mar 2020 18:25:28 +0100 Subject: [PATCH] [Home] Implement dismissing, adding and removing cards. Remove debug card. --- .../home/CardItemTouchHelperCallback.kt | 12 ++- .../ui/modules/home/HomeConfigDialog.kt | 80 +++++++++++++++++++ .../edziennik/ui/modules/home/HomeFragment.kt | 28 +++++-- app/src/main/res/layout/fragment_home.xml | 63 +++++++++++++-- app/src/main/res/values/strings.xml | 6 ++ 5 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeConfigDialog.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt index 0103e934..f3b916e8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt @@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.ui.modules.home import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView import com.google.android.material.card.MaterialCardView +import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragment.Companion.removeCard import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragment.Companion.swapCards import pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoIndicator @@ -14,7 +15,7 @@ class CardItemTouchHelperCallback(private val cardAdapter: HomeCardAdapter, priv companion object { private const val TAG = "CardItemTouchHelperCallback" private const val DRAG_FLAGS = ItemTouchHelper.UP or ItemTouchHelper.DOWN - private const val SWIPE_FLAGS = 0 + private const val SWIPE_FLAGS = ItemTouchHelper.RIGHT } private var dragCardView: MaterialCardView? = null @@ -27,11 +28,14 @@ class CardItemTouchHelperCallback(private val cardAdapter: HomeCardAdapter, priv val fromPosition = viewHolder.adapterPosition val toPosition = target.adapterPosition - swapCards(fromPosition, toPosition, cardAdapter) - return true + return swapCards(fromPosition, toPosition, cardAdapter) } - override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) = Unit + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { + removeCard(viewHolder.adapterPosition) + cardAdapter.items.removeAt(viewHolder.adapterPosition) + cardAdapter.notifyItemRemoved(viewHolder.adapterPosition) + } override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { super.onSelectedChanged(viewHolder, actionState) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeConfigDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeConfigDialog.kt new file mode 100644 index 00000000..268bf211 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeConfigDialog.kt @@ -0,0 +1,80 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2020-3-11. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCard.Companion.CARD_EVENTS +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCard.Companion.CARD_GRADES +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCard.Companion.CARD_LUCKY_NUMBER +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCard.Companion.CARD_TIMETABLE +import kotlin.collections.set + +class HomeConfigDialog( + val activity: AppCompatActivity, + private val reloadOnDismiss: Boolean = true, + val onShowListener: ((tag: String) -> Unit)? = null, + val onDismissListener: ((tag: String) -> Unit)? = null +) { + companion object { + const val TAG = "HomeConfigDialog" + } + + private val app by lazy { activity.application as App } + private val profileConfig by lazy { app.config.getFor(app.profileId).ui } + + private lateinit var dialog: AlertDialog + + init { run { + if (activity.isFinishing) + return@run + onShowListener?.invoke(TAG) + + val ids = listOf( + CARD_LUCKY_NUMBER, + CARD_TIMETABLE, + CARD_GRADES, + CARD_EVENTS + ) + val items = listOf( + app.getString(R.string.card_type_lucky_number), + app.getString(R.string.card_type_timetable), + app.getString(R.string.card_type_grades), + app.getString(R.string.card_type_events) + ) + val checkedItems = ids.map { it to false }.toMap().toMutableMap() + + val profileId = App.profileId + val homeCards = profileConfig.homeCards + .filter { it.profileId == profileId } + .toMutableList() + + homeCards.forEach { + checkedItems[it.cardId] = true + } + + dialog = MaterialAlertDialogBuilder(activity) + .setTitle(R.string.home_configure_add_remove) + .setMultiChoiceItems(items.toTypedArray(), checkedItems.values.toBooleanArray()) { _, which, isChecked -> + if (isChecked) { + homeCards += HomeCardModel(profileId, ids[which]) + } + else { + homeCards.removeAll { it.profileId == profileId && it.cardId == ids[which] } + } + } + .setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() } + .setOnDismissListener { + profileConfig.homeCards = homeCards + onDismissListener?.invoke(TAG) + if (reloadOnDismiss) (activity as? MainActivity)?.reloadTarget() + } + .show() + }} +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.kt index 9c0f7799..29c98d4d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.kt @@ -25,8 +25,12 @@ import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.databinding.FragmentHomeBinding +import pl.szczodrzynski.edziennik.onClick import pl.szczodrzynski.edziennik.ui.dialogs.home.StudentNumberDialog -import pl.szczodrzynski.edziennik.ui.modules.home.cards.* +import pl.szczodrzynski.edziennik.ui.modules.home.cards.HomeEventsCard +import pl.szczodrzynski.edziennik.ui.modules.home.cards.HomeGradesCard +import pl.szczodrzynski.edziennik.ui.modules.home.cards.HomeLuckyNumberCard +import pl.szczodrzynski.edziennik.ui.modules.home.cards.HomeTimetableCard import pl.szczodrzynski.edziennik.utils.Themes import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetPrimaryItem import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetSeparatorItem @@ -36,12 +40,12 @@ class HomeFragment : Fragment(), CoroutineScope { companion object { private const val TAG = "HomeFragment" - fun swapCards(fromPosition: Int, toPosition: Int, cardAdapter: HomeCardAdapter) { + fun swapCards(fromPosition: Int, toPosition: Int, cardAdapter: HomeCardAdapter): Boolean { val fromCard = cardAdapter.items[fromPosition] val toCard = cardAdapter.items[toPosition] if (fromCard.id == 100 || toCard.id == 100) { // debug card is not swappable - return + return false } cardAdapter.items[fromPosition] = cardAdapter.items[toPosition] cardAdapter.items[toPosition] = fromCard @@ -52,6 +56,15 @@ class HomeFragment : Fragment(), CoroutineScope { homeCards[fromPosition] = homeCards[toPosition] homeCards[toPosition] = fromPair App.config.forProfile().ui.homeCards = homeCards + return true + } + + fun removeCard(position: Int) { + val homeCards = App.config.forProfile().ui.homeCards.toMutableList() + if (position >= homeCards.size) + return + homeCards.removeAt(position) + App.config.forProfile().ui.homeCards = homeCards } } @@ -76,7 +89,7 @@ class HomeFragment : Fragment(), CoroutineScope { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { // TODO check if app, activity, b can be null - if (app.profile == null || !isAdded) + if (!isAdded) return activity.bottomSheet.prependItems( @@ -106,6 +119,9 @@ class HomeFragment : Fragment(), CoroutineScope { Toast.makeText(activity, R.string.main_menu_mark_as_read_success, Toast.LENGTH_SHORT).show() }) ) + b.configureCards.onClick { + HomeConfigDialog(activity, reloadOnDismiss = true) + } val showUnified = false @@ -130,8 +146,8 @@ class HomeFragment : Fragment(), CoroutineScope { else -> null } } - if (App.devMode) - items += HomeDebugCard(100, app, activity, this, app.profile) + //if (App.devMode) + // items += HomeDebugCard(100, app, activity, this, app.profile) val adapter = HomeCardAdapter(items) val itemTouchHelper = ItemTouchHelper(CardItemTouchHelperCallback(adapter, b.refreshLayout)) diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 63612c70..ab4c5a83 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -3,18 +3,67 @@ ~ Copyright (c) Kuba Szczodrzyński 2019-11-23. --> - + + - + android:layout_height="match_parent"> + + + + + + + + + + + +