Skip to content

Commit

Permalink
SEARCH HISTORY OR SMTH IDK ANY MORE
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Jan 28, 2024
1 parent 1743173 commit 5d789bf
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/ani/dantotsu/media/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SearchActivity : AppCompatActivity() {
private lateinit var mediaAdaptor: MediaAdaptor
private lateinit var progressAdapter: ProgressAdapter
private lateinit var concatAdapter: ConcatAdapter
private lateinit var headerAdaptor: SearchAdapter

lateinit var result: SearchResults
lateinit var updateChips: (() -> Unit)
Expand Down Expand Up @@ -76,7 +77,7 @@ class SearchActivity : AppCompatActivity() {

progressAdapter = ProgressAdapter(searched = model.searched)
mediaAdaptor = MediaAdaptor(style, model.searchResults.results, this, matchParent = true)
val headerAdaptor = SearchAdapter(this)
headerAdaptor = SearchAdapter(this, model.searchResults.type)

val gridSize = (screenWidth / 120f).toInt()
val gridLayoutManager = GridLayoutManager(this, gridSize)
Expand Down Expand Up @@ -154,9 +155,15 @@ class SearchActivity : AppCompatActivity() {
}
}

fun emptyMediaAdapter() {
mediaAdaptor.notifyItemRangeRemoved(0, model.searchResults.results.size)
model.searchResults.results.clear()
}

private var searchTimer = Timer()
private var loading = false
fun search() {
headerAdaptor.setHistoryVisibility(false)
val size = model.searchResults.results.size
model.searchResults.results.clear()
binding.searchRecyclerView.post {
Expand Down Expand Up @@ -188,6 +195,7 @@ class SearchActivity : AppCompatActivity() {

var state: Parcelable? = null
override fun onPause() {
headerAdaptor.addHistory()
super.onPause()
state = binding.searchRecyclerView.layoutManager?.onSaveInstanceState()
}
Expand Down
67 changes: 64 additions & 3 deletions app/src/main/java/ani/dantotsu/media/SearchAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package ani.dantotsu.media

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -22,16 +25,26 @@ import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.currContext
import ani.dantotsu.databinding.ItemChipBinding
import ani.dantotsu.databinding.ItemSearchHeaderBinding
import ani.dantotsu.logger
import ani.dantotsu.others.SharedPreferenceStringSetLiveData
import ani.dantotsu.saveData
import com.google.android.material.checkbox.MaterialCheckBox.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get


class SearchAdapter(private val activity: SearchActivity) :
class SearchAdapter(private val activity: SearchActivity, private val type: String) :
RecyclerView.Adapter<SearchAdapter.SearchHeaderViewHolder>() {
private val itemViewType = 6969
var search: Runnable? = null
var requestFocus: Runnable? = null
private var textWatcher: TextWatcher? = null
private lateinit var searchHistoryAdapter: SearchHistoryAdapter
private lateinit var binding: ItemSearchHeaderBinding

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchHeaderViewHolder {
val binding =
Expand All @@ -41,8 +54,11 @@ class SearchAdapter(private val activity: SearchActivity) :

@SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(holder: SearchHeaderViewHolder, position: Int) {
val binding = holder.binding
binding = holder.binding

searchHistoryAdapter = SearchHistoryAdapter(type) { s -> logger(s) }
binding.searchHistoryList.layoutManager = LinearLayoutManager(binding.root.context)
binding.searchHistoryList.adapter = searchHistoryAdapter

val imm: InputMethodManager =
activity.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
Expand Down Expand Up @@ -104,7 +120,18 @@ class SearchAdapter(private val activity: SearchActivity) :
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
searchTitle()
if (s.toString().isBlank()) {
activity.emptyMediaAdapter()
CoroutineScope(Dispatchers.IO).launch {
delay(200)
activity.runOnUiThread {
setHistoryVisibility(true)
}
}
} else {
setHistoryVisibility(false)
searchTitle()
}
}
}
binding.searchBarText.addTextChangedListener(textWatcher)
Expand Down Expand Up @@ -177,6 +204,40 @@ class SearchAdapter(private val activity: SearchActivity) :
requestFocus = Runnable { binding.searchBarText.requestFocus() }
}

fun setHistoryVisibility(visible: Boolean) {
if (visible) {
binding.searchResultLayout.startAnimation(fadeOutAnimation())
binding.searchHistoryList.startAnimation(fadeInAnimation())
binding.searchResultLayout.visibility = View.GONE
binding.searchHistoryList.visibility = View.VISIBLE
} else {
if (binding.searchResultLayout.visibility != View.VISIBLE) {
binding.searchResultLayout.startAnimation(fadeInAnimation())
binding.searchHistoryList.startAnimation(fadeOutAnimation())
}
binding.searchResultLayout.visibility = View.VISIBLE
binding.searchHistoryList.visibility = View.GONE
}
}

private fun fadeInAnimation(): Animation {
return AlphaAnimation(0f, 1f).apply {
duration = 150
fillAfter = true
}
}

private fun fadeOutAnimation(): Animation {
return AlphaAnimation(1f, 0f).apply {
duration = 150
fillAfter = true
}
}


fun addHistory() {
searchHistoryAdapter.add(binding.searchBarText.text.toString())
}

override fun getItemCount(): Int = 1

Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/ani/dantotsu/media/SearchHistoryAdapter.kt
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
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
android:layout_margin="16dp"
android:translationZ="7dp"
app:cardBackgroundColor="@color/bg_opp"
android:visibility="gone"
app:cardCornerRadius="16dp">

<androidx.constraintlayout.utils.widget.ImageFilterView
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/res/layout/item_search_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@


<LinearLayout
android:id="@+id/searchResultLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:visibility="gone"
android:orientation="horizontal">

<TextView
Expand Down Expand Up @@ -175,7 +177,17 @@
app:srcCompat="@drawable/ic_round_grid_view_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription,ImageContrastCheck" />

</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/searchHistoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:dividerHeight="0dp"
tools:listitem="@layout/item_search_history" />

</LinearLayout>
34 changes: 34 additions & 0 deletions app/src/main/res/layout/item_search_history.xml
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>

0 comments on commit 5d789bf

Please sign in to comment.