From bc4a0a575c9ff6abda22e3f70ab6d0aeea48553c Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Fri, 18 Mar 2022 13:18:23 -0400 Subject: [PATCH] Clean up the about package a bit --- .../org/schabi/newpipe/about/AboutActivity.kt | 51 ++++++++------ .../newpipe/about/LicenseFragmentHelper.kt | 69 +++++++++---------- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt index 32c460d0a..50a3984e3 100644 --- a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt +++ b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt @@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.viewpager2.adapter.FragmentStateAdapter -import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import org.schabi.newpipe.BuildConfig import org.schabi.newpipe.R @@ -21,30 +20,28 @@ import org.schabi.newpipe.util.ThemeHelper import org.schabi.newpipe.util.external_communication.ShareUtils class AboutActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { Localization.assureCorrectAppLanguage(this) super.onCreate(savedInstanceState) ThemeHelper.setTheme(this) title = getString(R.string.title_activity_about) + val aboutBinding = ActivityAboutBinding.inflate(layoutInflater) setContentView(aboutBinding.root) setSupportActionBar(aboutBinding.aboutToolbar) - supportActionBar!!.setDisplayHomeAsUpEnabled(true) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + // Create the adapter that will return a fragment for each of the three // primary sections of the activity. val mAboutStateAdapter = AboutStateAdapter(this) - // Set up the ViewPager with the sections adapter. aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter TabLayoutMediator( aboutBinding.aboutTabLayout, aboutBinding.aboutViewPager2 - ) { tab: TabLayout.Tab, position: Int -> - when (position) { - POS_ABOUT -> tab.setText(R.string.tab_about) - POS_LICENSE -> tab.setText(R.string.tab_licenses) - else -> throw IllegalArgumentException("Unknown position for ViewPager2") - } + ) { tab, position -> + tab.setText(mAboutStateAdapter.getPageTitle(position)) }.attach() } @@ -75,13 +72,14 @@ class AboutActivity : AppCompatActivity() { container: ViewGroup?, savedInstanceState: Bundle? ): View { - val aboutBinding = FragmentAboutBinding.inflate(inflater, container, false) - aboutBinding.aboutAppVersion.text = BuildConfig.VERSION_NAME - aboutBinding.aboutGithubLink.openLink(R.string.github_url) - aboutBinding.aboutDonationLink.openLink(R.string.donation_url) - aboutBinding.aboutWebsiteLink.openLink(R.string.website_url) - aboutBinding.aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url) - return aboutBinding.root + FragmentAboutBinding.inflate(inflater, container, false).apply { + aboutAppVersion.text = BuildConfig.VERSION_NAME + aboutGithubLink.openLink(R.string.github_url) + aboutDonationLink.openLink(R.string.donation_url) + aboutWebsiteLink.openLink(R.string.website_url) + aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url) + return root + } } } @@ -90,17 +88,29 @@ class AboutActivity : AppCompatActivity() { * one of the sections/tabs/pages. */ private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) { + private val posAbout = 0 + private val posLicense = 1 + private val totalCount = 2 + override fun createFragment(position: Int): Fragment { return when (position) { - POS_ABOUT -> AboutFragment() - POS_LICENSE -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS) + posAbout -> AboutFragment() + posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS) else -> throw IllegalArgumentException("Unknown position for ViewPager2") } } override fun getItemCount(): Int { // Show 2 total pages. - return TOTAL_COUNT + return totalCount + } + + fun getPageTitle(position: Int): Int { + return when (position) { + posAbout -> R.string.tab_about + posLicense -> R.string.tab_licenses + else -> throw IllegalArgumentException("Unknown position for ViewPager2") + } } } @@ -187,8 +197,5 @@ class AboutActivity : AppCompatActivity() { "https://github.com/ByteHamster/SearchPreference", StandardLicenses.MIT ), ) - private const val POS_ABOUT = 0 - private const val POS_LICENSE = 1 - private const val TOTAL_COUNT = 2 } } diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.kt b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.kt index a04de8abc..0a7fb502c 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.kt +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.kt @@ -2,6 +2,7 @@ package org.schabi.newpipe.about import android.content.Context import android.util.Base64 +import android.view.View import android.webkit.WebView import androidx.appcompat.app.AlertDialog import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers @@ -87,7 +88,6 @@ object LicenseFragmentHelper { return context.getString(color).substring(3) } - @JvmStatic fun showLicense(context: Context?, license: License): Disposable { return if (context == null) { Disposable.empty() @@ -95,26 +95,20 @@ object LicenseFragmentHelper { Observable.fromCallable { getFormattedLicense(context, license) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe { formattedLicense: String -> - val webViewData = Base64.encodeToString( - formattedLicense - .toByteArray(StandardCharsets.UTF_8), - Base64.NO_PADDING - ) - val webView = WebView(context) - webView.loadData(webViewData, "text/html; charset=UTF-8", "base64") - val alert = AlertDialog.Builder(context) - alert.setTitle(license.name) - alert.setView(webView) - Localization.assureCorrectAppLanguage(context) - alert.setNegativeButton( - context.getString(R.string.ok) - ) { dialog, _ -> dialog.dismiss() } - alert.show() + .subscribe { formattedLicense -> + AlertDialog.Builder(context).apply { + setTitle(license.name) + setView(loadLicense(context, formattedLicense)) + Localization.assureCorrectAppLanguage(context) + setPositiveButton(R.string.ok) { dialog, _ -> + dialog.dismiss() + } + show() + } } } } - @JvmStatic + fun showLicense(context: Context?, component: SoftwareComponent): Disposable { return if (context == null) { Disposable.empty() @@ -122,26 +116,29 @@ object LicenseFragmentHelper { Observable.fromCallable { getFormattedLicense(context, component.license) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe { formattedLicense: String -> - val webViewData = Base64.encodeToString( - formattedLicense - .toByteArray(StandardCharsets.UTF_8), - Base64.NO_PADDING - ) - val webView = WebView(context) - webView.loadData(webViewData, "text/html; charset=UTF-8", "base64") - val alert = AlertDialog.Builder(context) - alert.setTitle(component.license.name) - alert.setView(webView) - Localization.assureCorrectAppLanguage(context) - alert.setPositiveButton( - R.string.dismiss - ) { dialog, _ -> dialog.dismiss() } - alert.setNeutralButton(R.string.open_website_license) { _, _ -> - ShareUtils.openUrlInBrowser(context, component.link) + .subscribe { formattedLicense -> + AlertDialog.Builder(context).apply { + setTitle(component.license.name) + setView(loadLicense(context, formattedLicense)) + Localization.assureCorrectAppLanguage(context) + setPositiveButton(R.string.dismiss) { dialog, _ -> + dialog.dismiss() + } + setNeutralButton(R.string.open_website_license) { _, _ -> + ShareUtils.openUrlInBrowser(context, component.link) + } + show() } - alert.show() } } } + + private fun loadLicense(context: Context, formattedLicense: String): View { + val webViewData = Base64.encodeToString( + formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING + ) + return WebView(context).apply { + loadData(webViewData, "text/html; charset=UTF-8", "base64") + } + } }