-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GSoC'24] Add Right-Click Support to Long Click Listeners #16712
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*************************************************************************************** | ||
* * | ||
* Copyright (c) 2024 Sanjay Sargam <sargamsanjaykumar@gmail.com> * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify it under * | ||
* the terms of the GNU General Public License as published by the Free Software * | ||
* Foundation; either version 3 of the License, or (at your option) any later * | ||
* version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along with * | ||
* this program. If not, see <http://www.gnu.org/licenses/>. * | ||
****************************************************************************************/ | ||
|
||
package com.ichi2.anki | ||
|
||
import android.view.View | ||
import timber.log.Timber | ||
|
||
/** | ||
* A listener that has the same action for both "context click" (i.e., mostly right-click) and "long click" (i.e., holding the finger on the view). | ||
david-allison marked this conversation as resolved.
Show resolved
Hide resolved
david-allison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* * Note: In some contexts, a long press (long click) is expected to be informational, whereas a right-click (context click) is expected to be functional. | ||
* * Ensure that using the same action for both is appropriate for your use case. | ||
*/ | ||
fun interface OnContextAndLongClickListener : View.OnContextClickListener, View.OnLongClickListener { | ||
/** | ||
* The action to do for both contextClick and long click | ||
* @returns whether the operation was successful | ||
*/ | ||
fun onAction(v: View): Boolean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As usual, I'd like documentation. It can be very short, such as
|
||
|
||
override fun onContextClick(v: View): Boolean { | ||
Timber.i("${this.javaClass}: user context clicked") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, when I state "something such as", or similar requirement, you don't have to copy-paste it as-is. I don't guarantee that it's working properly.
Which to be honest I only realized because I could not guess what would be the class of I admit that I have no great idea about how to solve that. I would have loved to have an extra parameter which is a string value, but that would make the code far less readable, since we could not use only the declaration as a single function. As I've no better idea, I guess we can leave it as is. Unless someone wiser find a good solution that don't require too much work. |
||
return onAction(v) | ||
} | ||
|
||
override fun onLongClick(v: View): Boolean { | ||
Timber.i("${this.javaClass}: user long clicked") | ||
return onAction(v) | ||
} | ||
|
||
companion object { | ||
/** | ||
* Ensures [this] gets both a long click and a context click listener. | ||
* @see View.setOnLongClickListener | ||
* @see View.setOnContextClickListener | ||
*/ | ||
fun View.setOnContextAndLongClickListener(listener: OnContextAndLongClickListener?) { | ||
setOnLongClickListener(listener) | ||
setOnContextClickListener(listener) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ import androidx.recyclerview.widget.RecyclerView | |
import anki.decks.DeckTreeNode | ||
import com.ichi2.anki.CollectionManager.withCol | ||
import com.ichi2.anki.DeckSpinnerSelection | ||
import com.ichi2.anki.OnContextAndLongClickListener.Companion.setOnContextAndLongClickListener | ||
import com.ichi2.anki.R | ||
import com.ichi2.anki.analytics.AnalyticsDialogFragment | ||
import com.ichi2.anki.dialogs.DeckSelectionDialog.DecksArrayAdapter.DecksFilter | ||
|
@@ -179,7 +180,20 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() { | |
} | ||
} | ||
|
||
private fun showSubDeckDialog(parentDeckPath: String) { | ||
/** | ||
* Displays a dialog to create a subdeck under the specified parent deck. | ||
* | ||
* If the `deckID` is equal to `DeckSpinnerSelection.ALL_DECKS_ID`, a toast message is shown | ||
* indicating that a subdeck cannot be created for "All Decks," and the dialog is not displayed. | ||
* | ||
* @param parentDeckPath The path of the parent deck under which the subdeck will be created. | ||
* @param deckID The ID of the deck where the subdeck should be created. | ||
*/ | ||
private fun showSubDeckDialog(parentDeckPath: String, deckID: DeckId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some javadoc. |
||
if (deckID == DeckSpinnerSelection.ALL_DECKS_ID) { | ||
context?.let { showThemedToast(it, R.string.cannot_create_subdeck_for_all_decks, true) } | ||
return | ||
} | ||
launchCatchingTask { | ||
val parentId = withCol { decks.id(parentDeckPath) } | ||
val createDeckDialog = CreateDeckDialog(requireActivity(), R.string.create_subdeck, CreateDeckDialog.DeckDialogType.SUB_DECK, parentId) | ||
|
@@ -265,12 +279,9 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() { | |
expander.setOnClickListener { | ||
toggleExpansion(deckID) | ||
} | ||
deckHolder.setOnLongClickListener { // creating sub deck with parent deck path | ||
if (deckID == DeckSpinnerSelection.ALL_DECKS_ID) { | ||
context?.let { showThemedToast(it, R.string.cannot_create_subdeck_for_all_decks, true) } | ||
} else { | ||
showSubDeckDialog(deckName) | ||
} | ||
deckHolder.setOnContextAndLongClickListener { | ||
// creating sub deck with parent deck path | ||
showSubDeckDialog(deckName, deckID) | ||
true | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with having a function that is just a function. But in this case, I think you should not have the value deckContextAndLongClickListener and directly move its content at the only place where it's used