Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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, _ ->
Copy link
Contributor Author

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.

Copy link
Member

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. :-)

(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
}
}
Loading