From 03b2e8e937beaf2a5fb954f513091f8f7b7c2f2b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 14 Aug 2022 17:23:20 +0200 Subject: [PATCH] styled snackbar --- .../libretube/activities/AboutActivity.kt | 30 +++++-------------- .../activities/NoInternetActivity.kt | 6 ++-- .../libretube/extensions/ShowSnackBar.kt | 26 ++++++++++++++++ .../libretube/preferences/MainSettings.kt | 30 ++++++++----------- app/src/main/res/drawable/snackbar_shape.xml | 6 ++++ 5 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/extensions/ShowSnackBar.kt create mode 100644 app/src/main/res/drawable/snackbar_shape.xml diff --git a/app/src/main/java/com/github/libretube/activities/AboutActivity.kt b/app/src/main/java/com/github/libretube/activities/AboutActivity.kt index a6a23a862..6504f5c3d 100644 --- a/app/src/main/java/com/github/libretube/activities/AboutActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/AboutActivity.kt @@ -13,8 +13,8 @@ import com.github.libretube.WEBLATE_URL import com.github.libretube.WEBSITE_URL import com.github.libretube.databinding.ActivityAboutBinding import com.github.libretube.extensions.BaseActivity +import com.github.libretube.extensions.showSnackBar import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.google.android.material.snackbar.Snackbar class AboutActivity : BaseActivity() { private lateinit var binding: ActivityAboutBinding @@ -29,8 +29,7 @@ class AboutActivity : BaseActivity() { openLinkFromHref(WEBSITE_URL) } binding.website.setOnLongClickListener { - val text = getString(R.string.website_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.website_summary) true } @@ -38,8 +37,7 @@ class AboutActivity : BaseActivity() { openLinkFromHref(PIPED_GITHUB_URL) } binding.piped.setOnLongClickListener { - val text = getString(R.string.piped_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.piped_summary) true } @@ -47,8 +45,7 @@ class AboutActivity : BaseActivity() { openLinkFromHref(WEBLATE_URL) } binding.translate.setOnLongClickListener { - val text = getString(R.string.translate_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.translate_summary) true } @@ -56,8 +53,7 @@ class AboutActivity : BaseActivity() { openLinkFromHref(DONATE_URL) } binding.donate.setOnLongClickListener { - val text = getString(R.string.donate_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.donate_summary) true } @@ -65,8 +61,7 @@ class AboutActivity : BaseActivity() { openLinkFromHref(GITHUB_URL) } binding.github.setOnLongClickListener { - val text = getString(R.string.contributing_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.contributing_summary) true } @@ -74,8 +69,7 @@ class AboutActivity : BaseActivity() { showLicense() } binding.license.setOnLongClickListener { - val text = getString(R.string.license_summary) - showSnackBar(text) + binding.root.showSnackBar(R.string.license_summary) true } } @@ -86,16 +80,6 @@ class AboutActivity : BaseActivity() { startActivity(intent) } - private fun showSnackBar(text: String) { - val snackBar = Snackbar - .make(binding.root, text, Snackbar.LENGTH_LONG) - - // prevent the text from being partially hidden - snackBar.setTextMaxLines(3) - - snackBar.show() - } - private fun showLicense() { val licenseString = assets ?.open("gpl3.html") diff --git a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt index e60ae1ee4..fa5f2cae7 100644 --- a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt @@ -5,9 +5,9 @@ import android.os.Bundle import com.github.libretube.R import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.extensions.BaseActivity +import com.github.libretube.extensions.showSnackBar import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ThemeHelper -import com.google.android.material.snackbar.Snackbar class NoInternetActivity : BaseActivity() { private lateinit var binding: ActivityNointernetBinding @@ -21,9 +21,7 @@ class NoInternetActivity : BaseActivity() { if (ConnectionHelper.isNetworkAvailable(this)) { ThemeHelper.restartMainActivity(this) } else { - val snackBar = Snackbar - .make(binding.root, R.string.turnInternetOn, Snackbar.LENGTH_LONG) - snackBar.show() + binding.root.showSnackBar(R.string.turnInternetOn) } } binding.noInternetSettingsImageView.setOnClickListener { diff --git a/app/src/main/java/com/github/libretube/extensions/ShowSnackBar.kt b/app/src/main/java/com/github/libretube/extensions/ShowSnackBar.kt new file mode 100644 index 000000000..1fa11b86c --- /dev/null +++ b/app/src/main/java/com/github/libretube/extensions/ShowSnackBar.kt @@ -0,0 +1,26 @@ +package com.github.libretube.extensions + +import android.view.View +import android.widget.FrameLayout +import com.github.libretube.R +import com.google.android.material.snackbar.Snackbar + +fun View.showSnackBar(text: Int) { + val snackBar = Snackbar.make(this, text, Snackbar.LENGTH_SHORT) + snackBar.setTextMaxLines(3) + snackBar.animationMode = Snackbar.ANIMATION_MODE_SLIDE + + val params = snackBar.view.layoutParams as FrameLayout.LayoutParams + val sideMargin = 70 + + params.setMargins( + sideMargin, + params.topMargin, + sideMargin, + sideMargin + 50 + ) + snackBar.view.layoutParams = params + + snackBar.view.background = resources.getDrawable(R.drawable.snackbar_shape, null) + snackBar.show() +} diff --git a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt index 4f115937e..b465a0652 100644 --- a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt @@ -7,9 +7,10 @@ import com.github.libretube.BuildConfig import com.github.libretube.R import com.github.libretube.activities.SettingsActivity import com.github.libretube.dialogs.UpdateDialog +import com.github.libretube.extensions.showSnackBar import com.github.libretube.update.UpdateChecker +import com.github.libretube.util.ConnectionHelper import com.github.libretube.views.MaterialPreferenceFragment -import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -85,32 +86,25 @@ class MainSettings : MaterialPreferenceFragment() { // checking for update: yes -> dialog, no -> snackBar update?.setOnPreferenceClickListener { CoroutineScope(Dispatchers.IO).launch { + if (!ConnectionHelper.isNetworkAvailable(requireContext())) { + (activity as SettingsActivity).binding.root.showSnackBar(R.string.unknown_error) + return@launch + } // check for update val updateInfo = UpdateChecker.getLatestReleaseInfo() if (updateInfo?.name == null) { // request failed - val settingsActivity = activity as SettingsActivity - val snackBar = Snackbar - .make( - settingsActivity.binding.root, - R.string.unknown_error, - Snackbar.LENGTH_SHORT - ) - snackBar.show() + (activity as SettingsActivity).binding.root.showSnackBar(R.string.unknown_error) } else if (BuildConfig.VERSION_NAME != updateInfo.name) { // show the UpdateAvailableDialog if there's an update available val updateAvailableDialog = UpdateDialog(updateInfo) - updateAvailableDialog.show(childFragmentManager, UpdateDialog::class.java.name) + updateAvailableDialog.show( + childFragmentManager, + UpdateDialog::class.java.name + ) } else { // otherwise show the no update available snackBar - val settingsActivity = activity as SettingsActivity - val snackBar = Snackbar - .make( - settingsActivity.binding.root, - R.string.app_uptodate, - Snackbar.LENGTH_SHORT - ) - snackBar.show() + (activity as SettingsActivity).binding.root.showSnackBar(R.string.app_uptodate) } } true diff --git a/app/src/main/res/drawable/snackbar_shape.xml b/app/src/main/res/drawable/snackbar_shape.xml new file mode 100644 index 000000000..ca4993a53 --- /dev/null +++ b/app/src/main/res/drawable/snackbar_shape.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file