diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index f05040dae..6d1865770 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -11,15 +11,17 @@ import android.view.ViewGroup import android.view.WindowManager import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputMethodManager -import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView -import android.widget.TextView.OnEditorActionListener +import android.widget.TextView.* import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.adapters.SearchAdapter +import com.github.libretube.adapters.SearchHistoryAdapter import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -48,36 +50,70 @@ class SearchFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val recyclerView = view.findViewById(R.id.search_recycler) - recyclerView.layoutManager = GridLayoutManager(view.context, 1) + val autoTextView = view.findViewById(R.id.autoCompleteTextView) + + val historyRecycler = view.findViewById(R.id.history_recycler) + + + //show search history + + recyclerView.visibility = GONE + historyRecycler.visibility = VISIBLE + + historyRecycler.layoutManager = LinearLayoutManager(view.context) + + var historylist = getHistory() + if (historylist.size != 0) { + historyRecycler.adapter = + SearchHistoryAdapter(requireContext(), historylist, autoTextView) + } + + recyclerView.layoutManager = GridLayoutManager(view.context, 1) autoTextView.requestFocus() - val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val imm = + requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm!!.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT) - autoTextView.addTextChangedListener(object : TextWatcher { - override fun beforeTextChanged( - s: CharSequence?, - start: Int, - count: Int, - after: Int - ) { + autoTextView.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged( + s: CharSequence?, + start: Int, + count: Int, + after: Int + ) { + + } + + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { + if (s!! != "") { + recyclerView.visibility = VISIBLE + historyRecycler.visibility = GONE + recyclerView.adapter = null + + GlobalScope.launch { + fetchSuggestions(s.toString(), autoTextView) + delay(3000) + addtohistory(s.toString()) + fetchSearch(s.toString(), recyclerView) + } + } + } - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - if(s!! != ""){ - GlobalScope.launch { - fetchSuggestions(s.toString(), autoTextView) - delay(3000) - fetchSearch(s.toString(),recyclerView) - } + override fun afterTextChanged(s: Editable?) { + if (s!!.isEmpty()) { + recyclerView.visibility = GONE + historyRecycler.visibility = VISIBLE + var historylist = getHistory() + if (historylist.size != 0) { + historyRecycler.adapter = + SearchHistoryAdapter(requireContext(), historylist, autoTextView) } } + } - override fun afterTextChanged(s: Editable?) { - - } - - }) + }) autoTextView.setOnEditorActionListener(OnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { hideKeyboard(); @@ -143,4 +179,43 @@ class SearchFragment : Fragment() { super.onStop() hideKeyboard() } + + private fun addtohistory(query: String) { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + + var historyList = getHistory() + + + if (historyList.size != 0 && query == historyList.get(historyList.size - 1)) { + return + } else if (query == "") { + return + } else { + historyList = historyList + query + + } + + + + if (historyList.size > 10) { + historyList = historyList.takeLast(10) + } + + var set: Set = HashSet(historyList) + + sharedPreferences.edit().putStringSet("search_history", set) + .apply() + } + + private fun getHistory(): List { + try { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + val set: Set = sharedPreferences.getStringSet("search_history", HashSet())!! + return set.toList() + } catch (e: Exception) { + return emptyList() + } + + } } + diff --git a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt index 980780565..ee198e9c4 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -9,11 +9,11 @@ import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView -import com.squareup.picasso.Picasso import com.github.libretube.PlayerFragment import com.github.libretube.R -import com.github.libretube.obj.StreamItem import com.github.libretube.formatShort +import com.github.libretube.obj.StreamItem +import com.squareup.picasso.Picasso class ChannelAdapter(private val videoFeed: MutableList): RecyclerView.Adapter() { override fun getItemCount(): Int { diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt index 6eb7ef85e..8d8765caa 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -12,12 +12,12 @@ import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView -import com.squareup.picasso.Picasso import com.github.libretube.PlayerFragment import com.github.libretube.R import com.github.libretube.RetrofitInstance import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.StreamItem +import com.squareup.picasso.Picasso import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import retrofit2.HttpException diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt index b36450881..6e68c117e 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -2,16 +2,18 @@ package com.github.libretube.adapters import android.app.Activity import android.content.Context -import android.os.Handler import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.* +import android.widget.ImageView +import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView -import com.github.libretube.* +import com.github.libretube.MainActivity +import com.github.libretube.R +import com.github.libretube.RetrofitInstance import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.Playlists import com.squareup.picasso.Picasso diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index a501369c3..9f93cecd4 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -11,11 +11,11 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.MainActivity -import com.squareup.picasso.Picasso import com.github.libretube.PlayerFragment import com.github.libretube.R -import com.github.libretube.obj.SearchItem import com.github.libretube.formatShort +import com.github.libretube.obj.SearchItem +import com.squareup.picasso.Picasso class SearchAdapter(private val searchItems: List): RecyclerView.Adapter() { diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt new file mode 100644 index 000000000..7d369f4cd --- /dev/null +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -0,0 +1,55 @@ +package com.github.libretube.adapters + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AutoCompleteTextView +import android.widget.TextView +import androidx.preference.PreferenceManager +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.R +import com.google.android.material.imageview.ShapeableImageView + + +class SearchHistoryAdapter(private val context: Context, private var historyList: List , private val editText : AutoCompleteTextView) : + RecyclerView.Adapter() { + + + override fun getItemCount(): Int { + return historyList.size + } + + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchHistoryViewHolder { + val layoutInflater = LayoutInflater.from(parent.context) + val cell = layoutInflater.inflate(R.layout.searchhistory_row, parent, false) + return SearchHistoryViewHolder(cell) + } + + override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { + val history = historyList[position] + holder.v.findViewById(R.id.history_text).text = history + + + holder.v.findViewById(R.id.delete_history).setOnClickListener { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + + historyList = historyList - history + + sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)) + .apply() + + notifyDataSetChanged() + } + + holder.v.setOnClickListener { + editText.setText(history) + } + } +} + +class SearchHistoryViewHolder(val v: View) : RecyclerView.ViewHolder(v) { + init { + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt index 5e7326396..bdb2b6411 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt @@ -12,11 +12,11 @@ import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.MainActivity -import com.squareup.picasso.Picasso import com.github.libretube.PlayerFragment import com.github.libretube.R -import com.github.libretube.obj.StreamItem import com.github.libretube.formatShort +import com.github.libretube.obj.StreamItem +import com.squareup.picasso.Picasso class SubscriptionAdapter(private val videoFeed: List): RecyclerView.Adapter() { //private var limitedVideoFeed: MutableList = [""].toMutableList() diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index 6c357fc3c..db3baa725 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -1,20 +1,16 @@ package com.github.libretube.adapters -import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView -import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.MainActivity -import com.github.libretube.PlayerFragment import com.github.libretube.R import com.github.libretube.obj.Subscription import com.squareup.picasso.Picasso -import org.w3c.dom.Text class SubscriptionChannelAdapter(private val subscriptions: MutableList): RecyclerView.Adapter() { override fun getItemCount(): Int { diff --git a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt index 5cfdb6316..4c7d7666e 100644 --- a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt @@ -12,11 +12,11 @@ import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.MainActivity -import com.squareup.picasso.Picasso import com.github.libretube.PlayerFragment import com.github.libretube.R -import com.github.libretube.obj.StreamItem import com.github.libretube.formatShort +import com.github.libretube.obj.StreamItem +import com.squareup.picasso.Picasso class TrendingAdapter(private val videoFeed: List): RecyclerView.Adapter() { override fun getItemCount(): Int { diff --git a/app/src/main/res/drawable/ic_close2.xml b/app/src/main/res/drawable/ic_close2.xml index 70db409b3..3d79675c2 100644 --- a/app/src/main/res/drawable/ic_close2.xml +++ b/app/src/main/res/drawable/ic_close2.xml @@ -1,5 +1,11 @@ - - + + diff --git a/app/src/main/res/drawable/ic_history.xml b/app/src/main/res/drawable/ic_history.xml new file mode 100644 index 000000000..61ded0dec --- /dev/null +++ b/app/src/main/res/drawable/ic_history.xml @@ -0,0 +1,11 @@ + + + + diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index e92513b53..e078c8d40 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -305,257 +305,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + tools:context=".SearchFragment"> + app:layout_constraintTop_toTopOf="parent"> + + android:layout_marginRight="20dp" + android:background="@android:color/transparent" + app:hintEnabled="false"> @@ -43,6 +42,263 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file