From 3c20cd32b475db8c15c78492bb565c2c3181c71e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 11 Nov 2022 19:29:01 +0100 Subject: [PATCH] fix search bar issues --- .../libretube/ui/activities/MainActivity.kt | 53 ++---------------- .../libretube/ui/fragments/SearchFragment.kt | 21 +------ .../libretube/ui/tools/BreakReminder.kt | 55 +++++++++++++++++++ 3 files changed, 61 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/tools/BreakReminder.kt diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index d6f79a951..28a2d331d 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -14,7 +14,6 @@ import android.view.View import android.view.WindowInsets import android.view.WindowInsetsController import android.view.WindowManager -import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView import androidx.core.os.bundleOf @@ -35,12 +34,12 @@ import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.models.SearchViewModel import com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.sheets.PlayingQueueSheet +import com.github.libretube.ui.tools.BreakReminder import com.github.libretube.util.NavBarHelper import com.github.libretube.util.NetworkHelper import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.ThemeHelper -import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.elevation.SurfaceColors class MainActivity : BaseActivity() { @@ -49,15 +48,14 @@ class MainActivity : BaseActivity() { lateinit var navController: NavController private var startFragmentId = R.id.homeFragment - var autoRotationEnabled = false + + val autoRotationEnabled = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_ROTATION, false) lateinit var searchView: SearchView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - autoRotationEnabled = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_ROTATION, false) - // enable auto rotation if turned on requestOrientationChange() @@ -129,7 +127,7 @@ class MainActivity : BaseActivity() { val log = PreferenceHelper.getErrorLog() if (log != "") ErrorDialog().show(supportFragmentManager, null) - setupBreakReminder() + BreakReminder.setupBreakReminder(applicationContext) setupSubscriptionsBadge() @@ -167,49 +165,6 @@ class MainActivity : BaseActivity() { } } - /** - * Show a break reminder when watched too long - */ - private fun setupBreakReminder() { - if (!PreferenceHelper.getBoolean( - PreferenceKeys.BREAK_REMINDER_TOGGLE, - false - ) - ) { - return - } - val breakReminderPref = PreferenceHelper.getString( - PreferenceKeys.BREAK_REMINDER, - "0" - ) - if (!breakReminderPref.all { Character.isDigit(it) } || - breakReminderPref == "" || breakReminderPref == "0" - ) { - return - } - Handler(Looper.getMainLooper()).postDelayed( - { - try { - MaterialAlertDialogBuilder(this) - .setTitle(getString(R.string.take_a_break)) - .setMessage( - getString( - R.string.already_spent_time, - breakReminderPref - ) - ) - .setPositiveButton(R.string.okay, null) - .show() - } catch (e: Exception) { - kotlin.runCatching { - Toast.makeText(this, R.string.take_a_break, Toast.LENGTH_LONG).show() - } - } - }, - breakReminderPref.toLong() * 60 * 1000 - ) - } - override fun onPrepareOptionsMenu(menu: Menu?): Boolean { menu?.findItem(R.id.action_queue)?.isVisible = PlayingQueue.isNotEmpty() return super.onPrepareOptionsMenu(menu) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt index 3a706dca0..a4fe2a8ce 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt @@ -7,9 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope -import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager -import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.databinding.FragmentSearchBinding import com.github.libretube.db.DatabaseHolder.Companion.Database @@ -20,8 +18,6 @@ import com.github.libretube.ui.adapters.SearchHistoryAdapter import com.github.libretube.ui.adapters.SearchSuggestionsAdapter import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.models.SearchViewModel -import retrofit2.HttpException -import java.io.IOException class SearchFragment : BaseFragment() { private lateinit var binding: FragmentSearchBinding @@ -72,12 +68,9 @@ class SearchFragment : BaseFragment() { lifecycleScope.launchWhenCreated { val response = try { RetrofitInstance.api.getSuggestions(query) - } catch (e: IOException) { + } catch (e: Exception) { println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launchWhenCreated - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") + Log.e(TAG(), e.toString()) return@launchWhenCreated } // only load the suggestions if the input field didn't get cleared yet @@ -109,14 +102,4 @@ class SearchFragment : BaseFragment() { binding.historyEmpty.visibility = View.VISIBLE } } - - override fun onStop() { - if (findNavController().currentDestination?.id != R.id.searchResultFragment) { - // remove the search focus - (activity as MainActivity) - .binding.toolbar.menu - .findItem(R.id.action_search).collapseActionView() - } - super.onStop() - } } diff --git a/app/src/main/java/com/github/libretube/ui/tools/BreakReminder.kt b/app/src/main/java/com/github/libretube/ui/tools/BreakReminder.kt new file mode 100644 index 000000000..7e38e059f --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/tools/BreakReminder.kt @@ -0,0 +1,55 @@ +package com.github.libretube.ui.tools + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.widget.Toast +import com.github.libretube.R +import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.util.PreferenceHelper +import com.google.android.material.dialog.MaterialAlertDialogBuilder + +object BreakReminder { + /** + * Show a break reminder when watched too long + */ + fun setupBreakReminder(context: Context) { + if (!PreferenceHelper.getBoolean( + PreferenceKeys.BREAK_REMINDER_TOGGLE, + false + ) + ) { + return + } + val breakReminderPref = PreferenceHelper.getString( + PreferenceKeys.BREAK_REMINDER, + "0" + ) + if (!breakReminderPref.all { Character.isDigit(it) } || + breakReminderPref == "" || breakReminderPref == "0" + ) { + return + } + Handler(Looper.getMainLooper()).postDelayed( + { + try { + MaterialAlertDialogBuilder(context) + .setTitle(R.string.take_a_break) + .setMessage( + context.getString( + R.string.already_spent_time, + breakReminderPref + ) + ) + .setPositiveButton(R.string.okay, null) + .show() + } catch (e: Exception) { + runCatching { + Toast.makeText(context, R.string.take_a_break, Toast.LENGTH_LONG).show() + } + } + }, + breakReminderPref.toLong() * 60 * 1000 + ) + } +}