-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Migrate RecentSearch from java
to kt
#5993
Draft
neeldoshii
wants to merge
4
commits into
commons-app:main
Choose a base branch
from
neeldoshii:recent-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−147
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 0 additions & 147 deletions
147
app/src/main/java/fr/free/nrw/commons/explore/recentsearches/RecentSearchesFragment.java
This file was deleted.
Oops, something went wrong.
167 changes: 167 additions & 0 deletions
167
app/src/main/java/fr/free/nrw/commons/explore/recentsearches/RecentSearchesFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package fr.free.nrw.commons.explore.recentsearches | ||
|
||
import android.content.Context | ||
import android.content.DialogInterface | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AlertDialog | ||
import fr.free.nrw.commons.R | ||
import fr.free.nrw.commons.databinding.FragmentSearchHistoryBinding | ||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment | ||
import fr.free.nrw.commons.explore.SearchActivity | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Displays the recent searches screen. | ||
*/ | ||
class RecentSearchesFragment : CommonsDaggerSupportFragment() { | ||
|
||
@set:Inject | ||
var recentSearchesDao: RecentSearchesDao? = null | ||
private var _binding: FragmentSearchHistoryBinding? = null | ||
private val binding get() = _binding!! | ||
private var recentSearches: List<String> = emptyList() | ||
var adapter: ArrayAdapter<String>? = null | ||
|
||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
_binding = FragmentSearchHistoryBinding.inflate(inflater, container, false) | ||
|
||
val recentSearches = recentSearchesDao?.recentSearches(10) ?: emptyList() | ||
|
||
if (recentSearches.isEmpty()) { | ||
binding.recentSearchesDeleteButton.visibility = View.GONE | ||
binding.recentSearchesTextView.setText(R.string.no_recent_searches) | ||
} | ||
|
||
binding.recentSearchesDeleteButton.setOnClickListener { | ||
showDeleteRecentAlertDialog(requireContext()) | ||
} | ||
|
||
adapter = ArrayAdapter( | ||
requireContext(), R.layout.item_recent_searches, | ||
recentSearches | ||
) | ||
binding.recentSearchesList.adapter = adapter | ||
binding.recentSearchesList.setOnItemClickListener { _, _, position, _ -> | ||
(context as SearchActivity).updateText( | ||
recentSearches[position] | ||
) | ||
} | ||
binding.recentSearchesList.setOnItemLongClickListener { _, _, position, _ -> | ||
showDeleteAlertDialog(requireContext(), position) | ||
true | ||
} | ||
updateRecentSearches() | ||
|
||
return binding.root | ||
} | ||
|
||
private fun showDeleteRecentAlertDialog(context: Context) { | ||
AlertDialog.Builder(context) | ||
.setMessage(getString(R.string.delete_recent_searches_dialog)) | ||
.setPositiveButton( | ||
R.string.yes | ||
) { dialog: DialogInterface?, _ -> | ||
if (dialog != null) { | ||
setDeleteRecentPositiveButton( | ||
context, | ||
dialog | ||
) | ||
} | ||
} | ||
.setNegativeButton(R.string.no, null) | ||
.create() | ||
.show() | ||
} | ||
|
||
private fun setDeleteRecentPositiveButton( | ||
context: Context, | ||
dialog: DialogInterface | ||
) { | ||
recentSearchesDao!!.deleteAll() | ||
binding.recentSearchesDeleteButton.visibility = View.GONE | ||
binding.recentSearchesTextView.setText(R.string.no_recent_searches) | ||
Toast.makeText( | ||
getContext(), getString(R.string.search_history_deleted), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
recentSearches = recentSearchesDao!!.recentSearches(10) | ||
adapter = ArrayAdapter( | ||
context, R.layout.item_recent_searches, | ||
recentSearches | ||
) | ||
binding.recentSearchesList.adapter = adapter | ||
adapter?.notifyDataSetChanged() | ||
dialog.dismiss() | ||
} | ||
|
||
private fun showDeleteAlertDialog(context: Context, position: Int) { | ||
AlertDialog.Builder(context) | ||
.setMessage(R.string.delete_search_dialog) | ||
.setPositiveButton( | ||
getString(R.string.delete).uppercase(), | ||
(DialogInterface.OnClickListener { dialog: DialogInterface?, _: Int -> | ||
dialog?.let { | ||
setDeletePositiveButton( | ||
context, | ||
it, | ||
position | ||
) | ||
} | ||
}) | ||
) | ||
.setNegativeButton(android.R.string.cancel, null) | ||
.create() | ||
.show() | ||
} | ||
|
||
private fun setDeletePositiveButton( | ||
context: Context, | ||
dialog: DialogInterface, position: Int | ||
) { | ||
recentSearchesDao?.delete(recentSearchesDao?.find(recentSearches[position])) | ||
recentSearches = recentSearchesDao!!.recentSearches(10) | ||
adapter = ArrayAdapter( | ||
context, R.layout.item_recent_searches, | ||
recentSearches | ||
) | ||
binding.recentSearchesList.adapter = adapter | ||
adapter?.notifyDataSetChanged() | ||
dialog.dismiss() | ||
} | ||
|
||
/** | ||
* This method is called on back press of activity so we are updating the list from database to | ||
* refresh the recent searches list. | ||
*/ | ||
override fun onResume() { | ||
updateRecentSearches() | ||
super.onResume() | ||
} | ||
|
||
/** | ||
* This method is called when search query is null to update Recent Searches | ||
*/ | ||
fun updateRecentSearches() { | ||
recentSearches = recentSearchesDao!!.recentSearches(10) | ||
adapter?.notifyDataSetChanged() | ||
|
||
if (recentSearches.isNotEmpty()) { | ||
binding.recentSearchesDeleteButton.visibility = View.VISIBLE | ||
binding.recentSearchesTextView.setText(R.string.search_recent_header) | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
_binding = null | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Discussion:
Just a followup feature suggestion while using the app. What if we add swipe right, swipe left action on the item to delete particular item?
https://github.com/user-attachments/assets/e9138df6-0340-4330-989f-7a24a080a6af
~ Similar to like this deleting the item by performing swipe action.
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.
That would be a nice extra indeed.
Great to implement if it is short and maintainable. :-)