1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-10 12:00:03 +00:00

Merge pull request #8065 from TacoTheDank/aboutCleanup

Clean up the about package a bit
This commit is contained in:
litetex 2022-03-19 14:36:53 +01:00 committed by GitHub
commit 1f6fc0630d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 64 deletions

View File

@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import org.schabi.newpipe.BuildConfig import org.schabi.newpipe.BuildConfig
import org.schabi.newpipe.R import org.schabi.newpipe.R
@ -21,30 +20,28 @@ import org.schabi.newpipe.util.ThemeHelper
import org.schabi.newpipe.util.external_communication.ShareUtils import org.schabi.newpipe.util.external_communication.ShareUtils
class AboutActivity : AppCompatActivity() { class AboutActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Localization.assureCorrectAppLanguage(this) Localization.assureCorrectAppLanguage(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
ThemeHelper.setTheme(this) ThemeHelper.setTheme(this)
title = getString(R.string.title_activity_about) title = getString(R.string.title_activity_about)
val aboutBinding = ActivityAboutBinding.inflate(layoutInflater) val aboutBinding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(aboutBinding.root) setContentView(aboutBinding.root)
setSupportActionBar(aboutBinding.aboutToolbar) setSupportActionBar(aboutBinding.aboutToolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
// Create the adapter that will return a fragment for each of the three // Create the adapter that will return a fragment for each of the three
// primary sections of the activity. // primary sections of the activity.
val mAboutStateAdapter = AboutStateAdapter(this) val mAboutStateAdapter = AboutStateAdapter(this)
// Set up the ViewPager with the sections adapter. // Set up the ViewPager with the sections adapter.
aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter
TabLayoutMediator( TabLayoutMediator(
aboutBinding.aboutTabLayout, aboutBinding.aboutTabLayout,
aboutBinding.aboutViewPager2 aboutBinding.aboutViewPager2
) { tab: TabLayout.Tab, position: Int -> ) { tab, position ->
when (position) { tab.setText(mAboutStateAdapter.getPageTitle(position))
POS_ABOUT -> tab.setText(R.string.tab_about)
POS_LICENSE -> tab.setText(R.string.tab_licenses)
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
}
}.attach() }.attach()
} }
@ -75,13 +72,14 @@ class AboutActivity : AppCompatActivity() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val aboutBinding = FragmentAboutBinding.inflate(inflater, container, false) FragmentAboutBinding.inflate(inflater, container, false).apply {
aboutBinding.aboutAppVersion.text = BuildConfig.VERSION_NAME aboutAppVersion.text = BuildConfig.VERSION_NAME
aboutBinding.aboutGithubLink.openLink(R.string.github_url) aboutGithubLink.openLink(R.string.github_url)
aboutBinding.aboutDonationLink.openLink(R.string.donation_url) aboutDonationLink.openLink(R.string.donation_url)
aboutBinding.aboutWebsiteLink.openLink(R.string.website_url) aboutWebsiteLink.openLink(R.string.website_url)
aboutBinding.aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url) aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
return aboutBinding.root return root
}
} }
} }
@ -90,17 +88,29 @@ class AboutActivity : AppCompatActivity() {
* one of the sections/tabs/pages. * one of the sections/tabs/pages.
*/ */
private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) { 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 { override fun createFragment(position: Int): Fragment {
return when (position) { return when (position) {
POS_ABOUT -> AboutFragment() posAbout -> AboutFragment()
POS_LICENSE -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS) posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
else -> throw IllegalArgumentException("Unknown position for ViewPager2") else -> throw IllegalArgumentException("Unknown position for ViewPager2")
} }
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
// Show 2 total pages. // 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 "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
} }
} }

View File

@ -87,60 +87,50 @@ object LicenseFragmentHelper {
return context.getString(color).substring(3) return context.getString(color).substring(3)
} }
@JvmStatic
fun showLicense(context: Context?, license: License): Disposable { fun showLicense(context: Context?, license: License): Disposable {
return showLicense(context, license) { alertDialog ->
alertDialog.setPositiveButton(R.string.ok) { dialog, _ ->
dialog.dismiss()
}
}
}
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
return showLicense(context, component.license) { alertDialog ->
alertDialog.setPositiveButton(R.string.dismiss) { dialog, _ ->
dialog.dismiss()
}
alertDialog.setNeutralButton(R.string.open_website_license) { _, _ ->
ShareUtils.openUrlInBrowser(context!!, component.link)
}
}
}
private fun showLicense(
context: Context?,
license: License,
block: (AlertDialog.Builder) -> Unit
): Disposable {
return if (context == null) { return if (context == null) {
Disposable.empty() Disposable.empty()
} else { } else {
Observable.fromCallable { getFormattedLicense(context, license) } Observable.fromCallable { getFormattedLicense(context, license) }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { formattedLicense: String -> .subscribe { formattedLicense ->
val webViewData = Base64.encodeToString( val webViewData = Base64.encodeToString(
formattedLicense formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
.toByteArray(StandardCharsets.UTF_8),
Base64.NO_PADDING
) )
val webView = WebView(context) val webView = WebView(context)
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64") webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
val alert = AlertDialog.Builder(context)
alert.setTitle(license.name) AlertDialog.Builder(context).apply {
alert.setView(webView) setTitle(license.name)
Localization.assureCorrectAppLanguage(context) setView(webView)
alert.setNegativeButton( Localization.assureCorrectAppLanguage(context)
context.getString(R.string.ok) block(this)
) { dialog, _ -> dialog.dismiss() } show()
alert.show()
}
}
}
@JvmStatic
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
return if (context == null) {
Disposable.empty()
} else {
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)
} }
alert.show()
} }
} }
} }