Skip to content

Commit 4dafe42

Browse files
committed
De-duplicate showLicense methods
1 parent bc4a0a5 commit 4dafe42

1 file changed

Lines changed: 29 additions & 36 deletions

File tree

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

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.schabi.newpipe.about
22

33
import android.content.Context
44
import android.util.Base64
5-
import android.view.View
65
import android.webkit.WebView
76
import androidx.appcompat.app.AlertDialog
87
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
@@ -89,56 +88,50 @@ object LicenseFragmentHelper {
8988
}
9089

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 ->
99-
AlertDialog.Builder(context).apply {
100-
setTitle(license.name)
101-
setView(loadLicense(context, formattedLicense))
102-
Localization.assureCorrectAppLanguage(context)
103-
setPositiveButton(R.string.ok) { dialog, _ ->
104-
dialog.dismiss()
105-
}
106-
show()
107-
}
108-
}
91+
return showLicense(context, license) { alertDialog ->
92+
alertDialog.setPositiveButton(R.string.ok) { dialog, _ ->
93+
dialog.dismiss()
94+
}
10995
}
11096
}
11197

11298
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 {
113114
return if (context == null) {
114115
Disposable.empty()
115116
} else {
116-
Observable.fromCallable { getFormattedLicense(context, component.license) }
117+
Observable.fromCallable { getFormattedLicense(context, license) }
117118
.subscribeOn(Schedulers.io())
118119
.observeOn(AndroidSchedulers.mainThread())
119120
.subscribe { formattedLicense ->
121+
val webViewData = Base64.encodeToString(
122+
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
123+
)
124+
val webView = WebView(context)
125+
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
126+
120127
AlertDialog.Builder(context).apply {
121-
setTitle(component.license.name)
122-
setView(loadLicense(context, formattedLicense))
128+
setTitle(license.name)
129+
setView(webView)
123130
Localization.assureCorrectAppLanguage(context)
124-
setPositiveButton(R.string.dismiss) { dialog, _ ->
125-
dialog.dismiss()
126-
}
127-
setNeutralButton(R.string.open_website_license) { _, _ ->
128-
ShareUtils.openUrlInBrowser(context, component.link)
129-
}
131+
block(this)
130132
show()
131133
}
132134
}
133135
}
134136
}
135-
136-
private fun loadLicense(context: Context, formattedLicense: String): View {
137-
val webViewData = Base64.encodeToString(
138-
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
139-
)
140-
return WebView(context).apply {
141-
loadData(webViewData, "text/html; charset=UTF-8", "base64")
142-
}
143-
}
144137
}

0 commit comments

Comments
 (0)