Skip to content

Commit

Permalink
change SearchRecyclerViewAdapter to work with SearchResult
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Jan 28, 2024
1 parent 358777c commit 9d8aa44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,30 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.amaze.filemanager.R
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult
import com.amaze.filemanager.ui.activities.MainActivity
import com.amaze.filemanager.ui.colors.ColorPreference
import java.util.Random

class SearchRecyclerViewAdapter :
ListAdapter<HybridFileParcelable, SearchRecyclerViewAdapter.ViewHolder>(
ListAdapter<SearchResult, SearchRecyclerViewAdapter.ViewHolder>(

object : DiffUtil.ItemCallback<HybridFileParcelable>() {
object : DiffUtil.ItemCallback<SearchResult>() {
override fun areItemsTheSame(
oldItem: HybridFileParcelable,
newItem: HybridFileParcelable
oldItem: SearchResult,
newItem: SearchResult
): Boolean {
return oldItem.path == newItem.path && oldItem.name == newItem.name
return oldItem.file.path == newItem.file.path &&
oldItem.file.name == newItem.file.name
}

override fun areContentsTheSame(
oldItem: HybridFileParcelable,
newItem: HybridFileParcelable
oldItem: SearchResult,
newItem: SearchResult
): Boolean {
return oldItem.path == newItem.path && oldItem.name == newItem.name
return oldItem.file.path == newItem.file.path &&
oldItem.file.name == newItem.file.name &&
oldItem.matchRange == newItem.matchRange
}
}
) {
Expand All @@ -62,17 +65,17 @@ class SearchRecyclerViewAdapter :
}

override fun onBindViewHolder(holder: SearchRecyclerViewAdapter.ViewHolder, position: Int) {
val item = getItem(position)
val (file, matchResult) = getItem(position)

holder.fileNameTV.text = item.name
holder.filePathTV.text = item.path.substring(0, item.path.lastIndexOf("/"))
holder.fileNameTV.text = file.name
holder.filePathTV.text = file.path.substring(0, file.path.lastIndexOf("/"))

holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context))

val colorPreference =
(AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference

if (item.isDirectory) {
if (file.isDirectory) {
holder.colorView.setBackgroundColor(colorPreference.primaryFirstTab)
} else {
holder.colorView.setBackgroundColor(colorPreference.accent)
Expand All @@ -93,16 +96,16 @@ class SearchRecyclerViewAdapter :

view.setOnClickListener {

val item = getItem(adapterPosition)
val (file, _) = getItem(adapterPosition)

if (!item.isDirectory) {
item.openFile(
if (!file.isDirectory) {
file.openFile(
AppConfig.getInstance().mainActivityContext as MainActivity?,
false
)
} else {
(AppConfig.getInstance().mainActivityContext as MainActivity?)
?.goToMain(item.path)
?.goToMain(file.path)
}

(AppConfig.getInstance().mainActivityContext as MainActivity?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.amaze.filemanager.adapters.SearchRecyclerViewAdapter;
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult;
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResultListSorter;
import com.amaze.filemanager.filesystem.HybridFileParcelable;
import com.amaze.filemanager.filesystem.files.sort.DirSortBy;
import com.amaze.filemanager.filesystem.files.sort.SortBy;
import com.amaze.filemanager.filesystem.files.sort.SortOrder;
Expand Down Expand Up @@ -376,11 +375,7 @@ private void updateResultList(List<SearchResult> newResults, String searchTerm)
ArrayList<SearchResult> items = new ArrayList<>(newResults);
Collections.sort(
items, new SearchResultListSorter(DirSortBy.NONE_ON_TOP, sortType, searchTerm));
ArrayList<HybridFileParcelable> files = new ArrayList<>();
for (SearchResult searchResult : items) {
files.add(searchResult.getFile());
}
searchRecyclerViewAdapter.submitList(files);
searchRecyclerViewAdapter.submitList(items);
searchRecyclerViewAdapter.notifyDataSetChanged();
}

Expand Down

0 comments on commit 9d8aa44

Please sign in to comment.