-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1743173
commit 5d789bf
Showing
6 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
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
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
93 changes: 93 additions & 0 deletions
93
app/src/main/java/ani/dantotsu/media/SearchHistoryAdapter.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,93 @@ | ||
package ani.dantotsu.media | ||
|
||
import android.content.SharedPreferences | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import ani.dantotsu.R | ||
import ani.dantotsu.databinding.ItemSearchHistoryBinding | ||
import ani.dantotsu.others.SharedPreferenceStringSetLiveData | ||
import uy.kohesive.injekt.Injekt | ||
import uy.kohesive.injekt.api.get | ||
|
||
class SearchHistoryAdapter(private val type: String, private val searchClicked: (String) -> Unit) : ListAdapter<String, SearchHistoryAdapter.SearchHistoryViewHolder>( | ||
DIFF_CALLBACK_INSTALLED | ||
) { | ||
private var searchHistoryLiveData: SharedPreferenceStringSetLiveData? = null | ||
private var searchHistory: MutableSet<String>? = null | ||
private var sharedPreferences: SharedPreferences? = null | ||
|
||
init { | ||
sharedPreferences = Injekt.get<SharedPreferences>() | ||
searchHistoryLiveData = SharedPreferenceStringSetLiveData( | ||
sharedPreferences!!, | ||
"searchHistory_$type", | ||
mutableSetOf() | ||
) | ||
searchHistoryLiveData?.observeForever { | ||
searchHistory = it.toMutableSet() | ||
submitList(searchHistory?.reversed()) | ||
} | ||
} | ||
|
||
fun remove(item: String) { | ||
searchHistory?.remove(item) | ||
sharedPreferences?.edit()?.putStringSet("searchHistory_$type", searchHistory)?.apply() | ||
} | ||
|
||
fun add(item: String) { | ||
if (searchHistory?.contains(item) == true || item.isBlank()) return | ||
if (sharedPreferences?.getBoolean("incognito", false) == true) return | ||
searchHistory?.add(item) | ||
sharedPreferences?.edit()?.putStringSet("searchHistory_$type", searchHistory)?.apply() | ||
} | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): SearchHistoryAdapter.SearchHistoryViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(R.layout.item_search_history, parent, false) | ||
return SearchHistoryViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: SearchHistoryAdapter.SearchHistoryViewHolder, | ||
position: Int | ||
) { | ||
holder.binding.searchHistoryTextView.text = getItem(position) | ||
holder.binding.closeTextView.setOnClickListener { | ||
if (position >= itemCount || position < 0) return@setOnClickListener | ||
remove(getItem(position)) | ||
} | ||
holder.binding.searchHistoryTextView.setOnClickListener { | ||
if (position >= itemCount || position < 0) return@setOnClickListener | ||
searchClicked(getItem(position)) | ||
} | ||
} | ||
|
||
inner class SearchHistoryViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
val binding = ItemSearchHistoryBinding.bind(view) | ||
} | ||
|
||
companion object { | ||
val DIFF_CALLBACK_INSTALLED = object : DiffUtil.ItemCallback<String>() { | ||
override fun areItemsTheSame( | ||
oldItem: String, | ||
newItem: String | ||
): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: String, | ||
newItem: String | ||
): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} | ||
} |
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
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
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,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/extensionCardView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:orientation="horizontal" | ||
android:padding="10dp"> | ||
|
||
<LinearLayout | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_vertical" | ||
android:layout_weight="1" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/searchHistoryTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="HENTAI?????" | ||
android:textSize="15sp" | ||
android:fontFamily="@font/poppins_semi_bold"/> | ||
</LinearLayout> | ||
|
||
<ImageView | ||
android:id="@+id/closeTextView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_vertical" | ||
android:src="@drawable/ic_circle_cancel" | ||
android:textSize="14sp" | ||
app:tint="?attr/colorOnBackground"/> | ||
</LinearLayout> |