Skip to content

Commit 1f6fc06

Browse files
authored
Merge pull request #8065 from TacoTheDank/aboutCleanup
Clean up the about package a bit
2 parents e29aaaf + 4dafe42 commit 1f6fc06

2 files changed

Lines changed: 59 additions & 62 deletions

File tree

app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity
1010
import androidx.fragment.app.Fragment
1111
import androidx.fragment.app.FragmentActivity
1212
import androidx.viewpager2.adapter.FragmentStateAdapter
13-
import com.google.android.material.tabs.TabLayout
1413
import com.google.android.material.tabs.TabLayoutMediator
1514
import org.schabi.newpipe.BuildConfig
1615
import org.schabi.newpipe.R
@@ -21,30 +20,28 @@ import org.schabi.newpipe.util.ThemeHelper
2120
import org.schabi.newpipe.util.external_communication.ShareUtils
2221

2322
class AboutActivity : AppCompatActivity() {
23+
2424
override fun onCreate(savedInstanceState: Bundle?) {
2525
Localization.assureCorrectAppLanguage(this)
2626
super.onCreate(savedInstanceState)
2727
ThemeHelper.setTheme(this)
2828
title = getString(R.string.title_activity_about)
29+
2930
val aboutBinding = ActivityAboutBinding.inflate(layoutInflater)
3031
setContentView(aboutBinding.root)
3132
setSupportActionBar(aboutBinding.aboutToolbar)
32-
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
33+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
34+
3335
// Create the adapter that will return a fragment for each of the three
3436
// primary sections of the activity.
3537
val mAboutStateAdapter = AboutStateAdapter(this)
36-
3738
// Set up the ViewPager with the sections adapter.
3839
aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter
3940
TabLayoutMediator(
4041
aboutBinding.aboutTabLayout,
4142
aboutBinding.aboutViewPager2
42-
) { tab: TabLayout.Tab, position: Int ->
43-
when (position) {
44-
POS_ABOUT -> tab.setText(R.string.tab_about)
45-
POS_LICENSE -> tab.setText(R.string.tab_licenses)
46-
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
47-
}
43+
) { tab, position ->
44+
tab.setText(mAboutStateAdapter.getPageTitle(position))
4845
}.attach()
4946
}
5047

@@ -75,13 +72,14 @@ class AboutActivity : AppCompatActivity() {
7572
container: ViewGroup?,
7673
savedInstanceState: Bundle?
7774
): View {
78-
val aboutBinding = FragmentAboutBinding.inflate(inflater, container, false)
79-
aboutBinding.aboutAppVersion.text = BuildConfig.VERSION_NAME
80-
aboutBinding.aboutGithubLink.openLink(R.string.github_url)
81-
aboutBinding.aboutDonationLink.openLink(R.string.donation_url)
82-
aboutBinding.aboutWebsiteLink.openLink(R.string.website_url)
83-
aboutBinding.aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
84-
return aboutBinding.root
75+
FragmentAboutBinding.inflate(inflater, container, false).apply {
76+
aboutAppVersion.text = BuildConfig.VERSION_NAME
77+
aboutGithubLink.openLink(R.string.github_url)
78+
aboutDonationLink.openLink(R.string.donation_url)
79+
aboutWebsiteLink.openLink(R.string.website_url)
80+
aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
81+
return root
82+
}
8583
}
8684
}
8785

@@ -90,17 +88,29 @@ class AboutActivity : AppCompatActivity() {
9088
* one of the sections/tabs/pages.
9189
*/
9290
private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
91+
private val posAbout = 0
92+
private val posLicense = 1
93+
private val totalCount = 2
94+
9395
override fun createFragment(position: Int): Fragment {
9496
return when (position) {
95-
POS_ABOUT -> AboutFragment()
96-
POS_LICENSE -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
97+
posAbout -> AboutFragment()
98+
posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
9799
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
98100
}
99101
}
100102

101103
override fun getItemCount(): Int {
102104
// Show 2 total pages.
103-
return TOTAL_COUNT
105+
return totalCount
106+
}
107+
108+
fun getPageTitle(position: Int): Int {
109+
return when (position) {
110+
posAbout -> R.string.tab_about
111+
posLicense -> R.string.tab_licenses
112+
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
113+
}
104114
}
105115
}
106116

@@ -187,8 +197,5 @@ class AboutActivity : AppCompatActivity() {
187197
"https://github.com/ByteHamster/SearchPreference", StandardLicenses.MIT
188198
),
189199
)
190-
private const val POS_ABOUT = 0
191-
private const val POS_LICENSE = 1
192-
private const val TOTAL_COUNT = 2
193200
}
194201
}

app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.kt

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,60 +87,50 @@ object LicenseFragmentHelper {
8787
return context.getString(color).substring(3)
8888
}
8989

90-
@JvmStatic
9190
fun showLicense(context: Context?, license: License): Disposable {
92-
return if (context == null) {
93-
Disposable.empty()
94-
} else {
95-
Observable.fromCallable { getFormattedLicense(context, license) }
96-
.subscribeOn(Schedulers.io())
97-
.observeOn(AndroidSchedulers.mainThread())
98-
.subscribe { formattedLicense: String ->
99-
val webViewData = Base64.encodeToString(
100-
formattedLicense
101-
.toByteArray(StandardCharsets.UTF_8),
102-
Base64.NO_PADDING
103-
)
104-
val webView = WebView(context)
105-
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
106-
val alert = AlertDialog.Builder(context)
107-
alert.setTitle(license.name)
108-
alert.setView(webView)
109-
Localization.assureCorrectAppLanguage(context)
110-
alert.setNegativeButton(
111-
context.getString(R.string.ok)
112-
) { dialog, _ -> dialog.dismiss() }
113-
alert.show()
114-
}
91+
return showLicense(context, license) { alertDialog ->
92+
alertDialog.setPositiveButton(R.string.ok) { dialog, _ ->
93+
dialog.dismiss()
94+
}
11595
}
11696
}
117-
@JvmStatic
97+
11898
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
99+
return showLicense(context, component.license) { alertDialog ->
100+
alertDialog.setPositiveButton(R.string.dismiss) { dialog, _ ->
101+
dialog.dismiss()
102+
}
103+
alertDialog.setNeutralButton(R.string.open_website_license) { _, _ ->
104+
ShareUtils.openUrlInBrowser(context!!, component.link)
105+
}
106+
}
107+
}
108+
109+
private fun showLicense(
110+
context: Context?,
111+
license: License,
112+
block: (AlertDialog.Builder) -> Unit
113+
): Disposable {
119114
return if (context == null) {
120115
Disposable.empty()
121116
} else {
122-
Observable.fromCallable { getFormattedLicense(context, component.license) }
117+
Observable.fromCallable { getFormattedLicense(context, license) }
123118
.subscribeOn(Schedulers.io())
124119
.observeOn(AndroidSchedulers.mainThread())
125-
.subscribe { formattedLicense: String ->
120+
.subscribe { formattedLicense ->
126121
val webViewData = Base64.encodeToString(
127-
formattedLicense
128-
.toByteArray(StandardCharsets.UTF_8),
129-
Base64.NO_PADDING
122+
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
130123
)
131124
val webView = WebView(context)
132125
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
133-
val alert = AlertDialog.Builder(context)
134-
alert.setTitle(component.license.name)
135-
alert.setView(webView)
136-
Localization.assureCorrectAppLanguage(context)
137-
alert.setPositiveButton(
138-
R.string.dismiss
139-
) { dialog, _ -> dialog.dismiss() }
140-
alert.setNeutralButton(R.string.open_website_license) { _, _ ->
141-
ShareUtils.openUrlInBrowser(context, component.link)
126+
127+
AlertDialog.Builder(context).apply {
128+
setTitle(license.name)
129+
setView(webView)
130+
Localization.assureCorrectAppLanguage(context)
131+
block(this)
132+
show()
142133
}
143-
alert.show()
144134
}
145135
}
146136
}

0 commit comments

Comments
 (0)