From e0dd45765d6de133c05fe8ebadafcec837f68f85 Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 20:30:16 +0430 Subject: [PATCH 01/12] New search fragment ui --- app/src/main/res/layout/fragment_search.xml | 240 +++++++++++++++++++- 1 file changed, 237 insertions(+), 3 deletions(-) diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index ff5368722..e43032be8 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -15,7 +15,8 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - android:layout_margin="5dp" + android:layout_margin="16dp" + app:cardCornerRadius="27dp" > + android:background="@android:color/transparent" + > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Date: Mon, 9 May 2022 21:10:51 +0430 Subject: [PATCH 02/12] Working on search history --- .../com/github/libretube/SearchFragment.kt | 39 +- .../adapters/SearchHistoryAdapter.kt | 4 + app/src/main/res/layout/fragment_search.xml | 440 +++++++++--------- app/src/main/res/values/strings.xml | 1 + 4 files changed, 273 insertions(+), 211 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index f05040dae..a0d117e7c 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -11,13 +11,15 @@ 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 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 kotlinx.coroutines.GlobalScope @@ -48,6 +50,21 @@ class SearchFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val recyclerView = view.findViewById(R.id.search_recycler) + + val historyRecycler = view.findViewById(R.id.history_recycler) + val history_tv = view.findViewById(R.id.tv_history) + + + //show search history + + recyclerView.visibility = GONE + historyRecycler.visibility = VISIBLE + history_tv.visibility = VISIBLE + + recyclerView.layoutManager = LinearLayoutManager(view.context) + recyclerView.adapter + + recyclerView.layoutManager = GridLayoutManager(view.context, 1) val autoTextView = view.findViewById(R.id.autoCompleteTextView) autoTextView.requestFocus() @@ -68,6 +85,7 @@ class SearchFragment : Fragment() { GlobalScope.launch { fetchSuggestions(s.toString(), autoTextView) delay(3000) + addtohistory(s.toString()) fetchSearch(s.toString(),recyclerView) } } @@ -143,4 +161,21 @@ class SearchFragment : Fragment() { super.onStop() hideKeyboard() } + + private fun addtohistory(query: String) { + var queryFromated = "|" + query + + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + + var history = sharedPreferences.getString("search_history", "") + + var splited_history = history!!.split("|") + queryFromated + + if (splited_history.size > 10) { + splited_history.drop(9) + } + + + sharedPreferences.edit().putString("search_history",splited_history.joinToString("|") ).apply() + } } 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..d57703137 --- /dev/null +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -0,0 +1,4 @@ +package com.github.libretube.adapters + +class SearchHistoryAdapter { +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index e43032be8..5cd485946 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -4,39 +4,37 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".SearchFragment" - > + tools:context=".SearchFragment"> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + app:hintEnabled="false"> @@ -44,239 +42,263 @@ - - - - - - - - - + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - + - + - - - - - - - - - - + + + + + + + + + + - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - + - + - + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - + - + - - - - - - - - - - + + + + + + + + + + - - - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - + - + - + + + + + + + Light Theme Dark Theme %1$s subscribers + History From a15707f247d61227b5f7fc5c6d78c5674e9cd3a8 Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 21:15:39 +0430 Subject: [PATCH 03/12] Working on search history --- app/src/main/res/drawable/ic_close2.xml | 14 +++++--- app/src/main/res/layout/searchhistory_row.xml | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/layout/searchhistory_row.xml 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/layout/searchhistory_row.xml b/app/src/main/res/layout/searchhistory_row.xml new file mode 100644 index 000000000..8c6b31e6a --- /dev/null +++ b/app/src/main/res/layout/searchhistory_row.xml @@ -0,0 +1,32 @@ + + + + + + + + \ No newline at end of file From 6dcd8fafc5b188493a9e7f50f52b7f24d7d8cbed Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 21:50:07 +0430 Subject: [PATCH 04/12] Saving history wokrs fine --- .../com/github/libretube/SearchFragment.kt | 32 +++++++++++++----- .../adapters/SearchHistoryAdapter.kt | 33 ++++++++++++++++++- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index a0d117e7c..6f79f2509 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -22,6 +22,7 @@ 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 @@ -62,7 +63,7 @@ class SearchFragment : Fragment() { history_tv.visibility = VISIBLE recyclerView.layoutManager = LinearLayoutManager(view.context) - recyclerView.adapter + recyclerView.adapter = SearchHistoryAdapter(getHistory()) recyclerView.layoutManager = GridLayoutManager(view.context, 1) @@ -163,19 +164,32 @@ class SearchFragment : Fragment() { } private fun addtohistory(query: String) { - var queryFromated = "|" + query - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - var history = sharedPreferences.getString("search_history", "") + var splited_history = getHistory() - var splited_history = history!!.split("|") + queryFromated - - if (splited_history.size > 10) { - splited_history.drop(9) + if (query == splited_history.get(splited_history.size - 1)) { + return + } else { + splited_history = splited_history + query } - sharedPreferences.edit().putString("search_history",splited_history.joinToString("|") ).apply() + if (splited_history.size > 10) { + splited_history = splited_history.takeLast(10) + } + + + sharedPreferences.edit().putString("search_history", splited_history.joinToString("|")) + .apply() + } + + private fun getHistory(): List { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + var history = sharedPreferences.getString("search_history", "") + var splited_history = history!!.split("|") + + return splited_history } } + diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index d57703137..c12d218e1 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -1,4 +1,35 @@ package com.github.libretube.adapters -class SearchHistoryAdapter { +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.R + +class SearchHistoryAdapter(private val historyList: List) : + 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( +// +// ) + } +} + +class SearchHistoryViewHolder(val v: View) : RecyclerView.ViewHolder(v) { + init { + } } \ No newline at end of file From 8149e69210942f8802c0bd0f9054f8a8fe2828e1 Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 22:20:36 +0430 Subject: [PATCH 05/12] Wokring on showing history --- .../com/github/libretube/SearchFragment.kt | 60 ++++++++++++------- .../adapters/SearchHistoryAdapter.kt | 20 +++++-- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 6f79f2509..7a7cec045 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -62,41 +62,53 @@ class SearchFragment : Fragment() { historyRecycler.visibility = VISIBLE history_tv.visibility = VISIBLE - recyclerView.layoutManager = LinearLayoutManager(view.context) - recyclerView.adapter = SearchHistoryAdapter(getHistory()) - + historyRecycler.layoutManager = LinearLayoutManager(view.context) + historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory()) recyclerView.layoutManager = GridLayoutManager(view.context, 1) val autoTextView = view.findViewById(R.id.autoCompleteTextView) 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!! != ""){ - 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!! != "") { + recyclerView.visibility = VISIBLE + historyRecycler.visibility = GONE + history_tv.visibility = GONE + recyclerView.adapter = null + + GlobalScope.launch { + fetchSuggestions(s.toString(), autoTextView) + delay(3000) + addtohistory(s.toString()) + fetchSearch(s.toString(), recyclerView) } - } - override fun afterTextChanged(s: Editable?) { } + } - }) + override fun afterTextChanged(s: Editable?) { + if (s!!.isEmpty()) { + recyclerView.visibility = GONE + historyRecycler.visibility = VISIBLE + history_tv.visibility = VISIBLE + historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory()) + } + } + + }) autoTextView.setOnEditorActionListener(OnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { hideKeyboard(); @@ -170,6 +182,8 @@ class SearchFragment : Fragment() { if (query == splited_history.get(splited_history.size - 1)) { return + } else if (query == "") { + return } else { splited_history = splited_history + query } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index c12d218e1..433f78588 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -1,13 +1,16 @@ package com.github.libretube.adapters +import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup 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 historyList: List) : +class SearchHistoryAdapter(private val context: Context, private val historyList: List) : RecyclerView.Adapter() { override fun getItemCount(): Int { return historyList.size @@ -23,9 +26,18 @@ class SearchHistoryAdapter(private val historyList: List) : 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( -// -// ) + + + holder.v.findViewById(R.id.delete_history).setOnClickListener { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + var splited_history = sharedPreferences.getString("search_history", "")!!.split("|") + + splited_history = splited_history - history + + sharedPreferences.edit().putString("search_history", splited_history.joinToString("|")) + .apply() + + } } } From 34b53d0b1716a332a513e5d97395b33e2a3061a8 Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 22:39:03 +0430 Subject: [PATCH 06/12] Search history almost works --- app/src/main/java/com/github/libretube/SearchFragment.kt | 3 ++- .../com/github/libretube/adapters/SearchHistoryAdapter.kt | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 7a7cec045..157d9f463 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -15,6 +15,7 @@ import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView import android.widget.TextView import android.widget.TextView.* +import android.widget.Toast import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager @@ -189,6 +190,7 @@ class SearchFragment : Fragment() { } + if (splited_history.size > 10) { splited_history = splited_history.takeLast(10) } @@ -202,7 +204,6 @@ class SearchFragment : Fragment() { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) var history = sharedPreferences.getString("search_history", "") var splited_history = history!!.split("|") - return splited_history } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 433f78588..d4593f464 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -1,6 +1,7 @@ package com.github.libretube.adapters import android.content.Context +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -10,10 +11,11 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.google.android.material.imageview.ShapeableImageView + class SearchHistoryAdapter(private val context: Context, private val historyList: List) : RecyclerView.Adapter() { override fun getItemCount(): Int { - return historyList.size + return historyList.size -1 } @@ -24,7 +26,7 @@ class SearchHistoryAdapter(private val context: Context, private val historyList } override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { - val history = historyList[position] + val history = historyList[position+1] holder.v.findViewById(R.id.history_text).text = history From 5b1f16fe977d73a12feeb1e7afdb1af2cc23ff57 Mon Sep 17 00:00:00 2001 From: archroid Date: Mon, 9 May 2022 22:52:13 +0430 Subject: [PATCH 07/12] Added search history action --- app/src/main/java/com/github/libretube/SearchFragment.kt | 7 ++++--- .../com/github/libretube/adapters/SearchHistoryAdapter.kt | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 157d9f463..797229a44 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -53,6 +53,8 @@ class SearchFragment : Fragment() { super.onViewCreated(view, savedInstanceState) val recyclerView = view.findViewById(R.id.search_recycler) + val autoTextView = view.findViewById(R.id.autoCompleteTextView) + val historyRecycler = view.findViewById(R.id.history_recycler) val history_tv = view.findViewById(R.id.tv_history) @@ -64,10 +66,9 @@ class SearchFragment : Fragment() { history_tv.visibility = VISIBLE historyRecycler.layoutManager = LinearLayoutManager(view.context) - historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory()) + historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView) recyclerView.layoutManager = GridLayoutManager(view.context, 1) - val autoTextView = view.findViewById(R.id.autoCompleteTextView) autoTextView.requestFocus() val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -105,7 +106,7 @@ class SearchFragment : Fragment() { recyclerView.visibility = GONE historyRecycler.visibility = VISIBLE history_tv.visibility = VISIBLE - historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory()) + historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView) } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index d4593f464..fb379b775 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -5,6 +5,7 @@ import android.util.Log 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 @@ -12,7 +13,7 @@ import com.github.libretube.R import com.google.android.material.imageview.ShapeableImageView -class SearchHistoryAdapter(private val context: Context, private val historyList: List) : +class SearchHistoryAdapter(private val context: Context, private val historyList: List , private val editText : AutoCompleteTextView) : RecyclerView.Adapter() { override fun getItemCount(): Int { return historyList.size -1 @@ -40,6 +41,10 @@ class SearchHistoryAdapter(private val context: Context, private val historyList .apply() } + + holder.v.setOnClickListener { + editText.setText(history) + } } } From 580e99329d9cf3c8547c9a17f3d65ad704bd5e72 Mon Sep 17 00:00:00 2001 From: archroid Date: Tue, 10 May 2022 20:12:17 +0430 Subject: [PATCH 08/12] Search history now saves as stringset --- .../com/github/libretube/SearchFragment.kt | 22 +++++++++++-------- .../adapters/SearchHistoryAdapter.kt | 4 ++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 797229a44..da9aa6727 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -180,32 +180,36 @@ class SearchFragment : Fragment() { private fun addtohistory(query: String) { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - var splited_history = getHistory() + var historyList = getHistory() - if (query == splited_history.get(splited_history.size - 1)) { + + if (historyList.size != 0 && query == historyList.get(historyList.size - 1)) { return } else if (query == "") { return } else { - splited_history = splited_history + query + historyList = historyList + query + } - if (splited_history.size > 10) { - splited_history = splited_history.takeLast(10) + if (historyList.size > 10) { + historyList = historyList.takeLast(10) } + var set: Set = HashSet(historyList) - sharedPreferences.edit().putString("search_history", splited_history.joinToString("|")) + sharedPreferences.edit().putStringSet("search_history", set) .apply() } private fun getHistory(): List { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - var history = sharedPreferences.getString("search_history", "") - var splited_history = history!!.split("|") - return splited_history + + val set: Set = sharedPreferences.getStringSet("search_history", null)!! + + return set.toList() } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index fb379b775..1bdfbe3a1 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -16,7 +16,7 @@ import com.google.android.material.imageview.ShapeableImageView class SearchHistoryAdapter(private val context: Context, private val historyList: List , private val editText : AutoCompleteTextView) : RecyclerView.Adapter() { override fun getItemCount(): Int { - return historyList.size -1 + return historyList.size } @@ -27,7 +27,7 @@ class SearchHistoryAdapter(private val context: Context, private val historyList } override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) { - val history = historyList[position+1] + val history = historyList[position] holder.v.findViewById(R.id.history_text).text = history From 8121e8534b6e63be70071be9b1f27c75ed007665 Mon Sep 17 00:00:00 2001 From: archroid Date: Tue, 10 May 2022 20:36:20 +0430 Subject: [PATCH 09/12] Search history works fine without any bugs --- .../java/com/github/libretube/SearchFragment.kt | 16 +++++++++++----- .../libretube/adapters/SearchHistoryAdapter.kt | 14 ++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index da9aa6727..87d88ecde 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -15,7 +15,6 @@ import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView import android.widget.TextView import android.widget.TextView.* -import android.widget.Toast import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager @@ -66,7 +65,12 @@ class SearchFragment : Fragment() { history_tv.visibility = VISIBLE historyRecycler.layoutManager = LinearLayoutManager(view.context) - historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView) + + var historylist = getHistory() + if (historylist.size != 0) { + historyRecycler.adapter = + SearchHistoryAdapter(requireContext(), historylist, autoTextView) + } recyclerView.layoutManager = GridLayoutManager(view.context, 1) autoTextView.requestFocus() @@ -106,7 +110,11 @@ class SearchFragment : Fragment() { recyclerView.visibility = GONE historyRecycler.visibility = VISIBLE history_tv.visibility = VISIBLE - historyRecycler.adapter = SearchHistoryAdapter(requireContext(),getHistory(),autoTextView) + var historylist = getHistory() + if (historylist.size != 0) { + historyRecycler.adapter = + SearchHistoryAdapter(requireContext(), historylist, autoTextView) + } } } @@ -206,9 +214,7 @@ class SearchFragment : Fragment() { private fun getHistory(): List { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - val set: Set = sharedPreferences.getStringSet("search_history", null)!! - return set.toList() } } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 1bdfbe3a1..6bfc96403 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -13,8 +13,10 @@ import com.github.libretube.R import com.google.android.material.imageview.ShapeableImageView -class SearchHistoryAdapter(private val context: Context, private val historyList: List , private val editText : AutoCompleteTextView) : +class SearchHistoryAdapter(private val context: Context, private var historyList: List , private val editText : AutoCompleteTextView) : RecyclerView.Adapter() { + + override fun getItemCount(): Int { return historyList.size } @@ -33,15 +35,19 @@ class SearchHistoryAdapter(private val context: Context, private val historyList holder.v.findViewById(R.id.delete_history).setOnClickListener { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - var splited_history = sharedPreferences.getString("search_history", "")!!.split("|") +// var historyList = sharedPreferences.getStringSet("search_history", null)!!.toList() - splited_history = splited_history - history + historyList = historyList - history - sharedPreferences.edit().putString("search_history", splited_history.joinToString("|")) + sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)) .apply() + Log.d("TAG", "onBindViewHolder: " + historyList.size) + + notifyDataSetChanged() } + holder.v.setOnClickListener { editText.setText(history) } From fcd10bb0f0a2c5d28b6d984cf2fa2c0a4b29b249 Mon Sep 17 00:00:00 2001 From: archroid Date: Tue, 10 May 2022 20:53:59 +0430 Subject: [PATCH 10/12] Added search history icon; Fixed a nullpointer crash --- .../java/com/github/libretube/SearchFragment.kt | 11 ++++++++--- app/src/main/res/drawable/ic_history.xml | 11 +++++++++++ app/src/main/res/layout/searchhistory_row.xml | 14 ++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/drawable/ic_history.xml diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 87d88ecde..ec77960fe 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -213,9 +213,14 @@ class SearchFragment : Fragment() { } private fun getHistory(): List { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - val set: Set = sharedPreferences.getStringSet("search_history", null)!! - return set.toList() + 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/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/searchhistory_row.xml b/app/src/main/res/layout/searchhistory_row.xml index 8c6b31e6a..e3282ec58 100644 --- a/app/src/main/res/layout/searchhistory_row.xml +++ b/app/src/main/res/layout/searchhistory_row.xml @@ -7,6 +7,16 @@ android:layout_marginBottom="16dp" android:background="?android:attr/selectableItemBackground"> + @@ -24,7 +34,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" - android:padding="8dp" + android:padding="5dp" android:src="@drawable/ic_close2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" From 9b608fabbf8791d84baf5968de4f64a002fc4b06 Mon Sep 17 00:00:00 2001 From: archroid Date: Tue, 10 May 2022 21:05:51 +0430 Subject: [PATCH 11/12] Code cleanup --- .../com/github/libretube/SearchFragment.kt | 5 - .../libretube/adapters/ChannelAdapter.kt | 4 +- .../libretube/adapters/PlaylistAdapter.kt | 2 +- .../libretube/adapters/PlaylistsAdapter.kt | 8 +- .../libretube/adapters/SearchAdapter.kt | 4 +- .../adapters/SearchHistoryAdapter.kt | 5 - .../libretube/adapters/SubscriptionAdapter.kt | 4 +- .../adapters/SubscriptionChannelAdapter.kt | 4 - .../libretube/adapters/TrendingAdapter.kt | 4 +- app/src/main/res/layout/fragment_player.xml | 251 ------------------ app/src/main/res/layout/fragment_search.xml | 22 +- 11 files changed, 25 insertions(+), 288 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index ec77960fe..6d1865770 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -13,7 +13,6 @@ import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputMethodManager import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView -import android.widget.TextView import android.widget.TextView.* import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope @@ -55,14 +54,12 @@ class SearchFragment : Fragment() { val autoTextView = view.findViewById(R.id.autoCompleteTextView) val historyRecycler = view.findViewById(R.id.history_recycler) - val history_tv = view.findViewById(R.id.tv_history) //show search history recyclerView.visibility = GONE historyRecycler.visibility = VISIBLE - history_tv.visibility = VISIBLE historyRecycler.layoutManager = LinearLayoutManager(view.context) @@ -91,7 +88,6 @@ class SearchFragment : Fragment() { if (s!! != "") { recyclerView.visibility = VISIBLE historyRecycler.visibility = GONE - history_tv.visibility = GONE recyclerView.adapter = null GlobalScope.launch { @@ -109,7 +105,6 @@ class SearchFragment : Fragment() { if (s!!.isEmpty()) { recyclerView.visibility = GONE historyRecycler.visibility = VISIBLE - history_tv.visibility = VISIBLE var historylist = getHistory() if (historylist.size != 0) { historyRecycler.adapter = 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 eb3a10903..994e6ceab 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -10,11 +10,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 index 6bfc96403..7d369f4cd 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -1,7 +1,6 @@ package com.github.libretube.adapters import android.content.Context -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -35,19 +34,15 @@ class SearchHistoryAdapter(private val context: Context, private var historyList holder.v.findViewById(R.id.delete_history).setOnClickListener { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) -// var historyList = sharedPreferences.getStringSet("search_history", null)!!.toList() historyList = historyList - history sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)) .apply() - Log.d("TAG", "onBindViewHolder: " + historyList.size) - notifyDataSetChanged() } - holder.v.setOnClickListener { editText.setText(history) } 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/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index d3057fba0..3509103bb 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -272,257 +272,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> - + + + + + + + + + + Date: Wed, 11 May 2022 13:11:19 +0430 Subject: [PATCH 12/12] Update strings.xml --- app/src/main/res/values/strings.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 397830202..48c6e8bc7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -63,5 +63,4 @@ Light Theme Dark Theme %1$s subscribers - History