1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Convert LicenseFragmentHelper methods to top-level declarations.

This commit is contained in:
Isira Seneviratne 2022-07-23 05:20:06 +05:30
parent 35a118a2a7
commit 013522c376
2 changed files with 75 additions and 77 deletions

View File

@ -8,7 +8,6 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.about.LicenseFragmentHelper.showLicense
import org.schabi.newpipe.databinding.FragmentLicensesBinding import org.schabi.newpipe.databinding.FragmentLicensesBinding
import org.schabi.newpipe.databinding.ItemSoftwareComponentBinding import org.schabi.newpipe.databinding.ItemSoftwareComponentBinding

View File

@ -14,14 +14,13 @@ import org.schabi.newpipe.util.ThemeHelper
import org.schabi.newpipe.util.external_communication.ShareUtils import org.schabi.newpipe.util.external_communication.ShareUtils
import java.io.IOException import java.io.IOException
object LicenseFragmentHelper { /**
/**
* @param context the context to use * @param context the context to use
* @param license the license * @param license the license
* @return String which contains a HTML formatted license page * @return String which contains a HTML formatted license page
* styled according to the context's theme * styled according to the context's theme
*/ */
private fun getFormattedLicense(context: Context, license: License): String { private fun getFormattedLicense(context: Context, license: License): String {
try { try {
return context.assets.open(license.filename).bufferedReader().use { it.readText() } return context.assets.open(license.filename).bufferedReader().use { it.readText() }
// split the HTML file and insert the stylesheet into the HEAD of the file // split the HTML file and insert the stylesheet into the HEAD of the file
@ -29,13 +28,13 @@ object LicenseFragmentHelper {
} catch (e: IOException) { } catch (e: IOException) {
throw IllegalArgumentException("Could not get license file: ${license.filename}", e) throw IllegalArgumentException("Could not get license file: ${license.filename}", e)
} }
} }
/** /**
* @param context the Android context * @param context the Android context
* @return String which is a CSS stylesheet according to the context's theme * @return String which is a CSS stylesheet according to the context's theme
*/ */
private fun getLicenseStylesheet(context: Context): String { private fun getLicenseStylesheet(context: Context): String {
val isLightTheme = ThemeHelper.isLightThemeSelected(context) val isLightTheme = ThemeHelper.isLightThemeSelected(context)
return ( return (
"body{padding:12px 15px;margin:0;" + "background:#" + getHexRGBColor( "body{padding:12px 15px;margin:0;" + "background:#" + getHexRGBColor(
@ -52,28 +51,28 @@ object LicenseFragmentHelper {
else R.color.dark_youtube_primary_color else R.color.dark_youtube_primary_color
) + "}" + "pre{white-space:pre-wrap}" ) + "}" + "pre{white-space:pre-wrap}"
) )
} }
/** /**
* Cast R.color to a hexadecimal color value. * Cast R.color to a hexadecimal color value.
* *
* @param context the context to use * @param context the context to use
* @param color the color number from R.color * @param color the color number from R.color
* @return a six characters long String with hexadecimal RGB values * @return a six characters long String with hexadecimal RGB values
*/ */
private fun getHexRGBColor(context: Context, color: Int): String { private fun getHexRGBColor(context: Context, color: Int): String {
return context.getString(color).substring(3) return context.getString(color).substring(3)
} }
fun showLicense(context: Context?, license: License): Disposable { fun showLicense(context: Context?, license: License): Disposable {
return showLicense(context, license) { alertDialog -> return showLicense(context, license) { alertDialog ->
alertDialog.setPositiveButton(R.string.ok) { dialog, _ -> alertDialog.setPositiveButton(R.string.ok) { dialog, _ ->
dialog.dismiss() dialog.dismiss()
} }
} }
} }
fun showLicense(context: Context?, component: SoftwareComponent): Disposable { fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
return showLicense(context, component.license) { alertDialog -> return showLicense(context, component.license) { alertDialog ->
alertDialog.setPositiveButton(R.string.dismiss) { dialog, _ -> alertDialog.setPositiveButton(R.string.dismiss) { dialog, _ ->
dialog.dismiss() dialog.dismiss()
@ -82,13 +81,13 @@ object LicenseFragmentHelper {
ShareUtils.openUrlInBrowser(context!!, component.link) ShareUtils.openUrlInBrowser(context!!, component.link)
} }
} }
} }
private fun showLicense( private fun showLicense(
context: Context?, context: Context?,
license: License, license: License,
block: (AlertDialog.Builder) -> Unit block: (AlertDialog.Builder) -> Unit
): Disposable { ): Disposable {
return if (context == null) { return if (context == null) {
Disposable.empty() Disposable.empty()
} else { } else {
@ -96,7 +95,8 @@ object LicenseFragmentHelper {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { formattedLicense -> .subscribe { formattedLicense ->
val webViewData = Base64.encodeToString(formattedLicense.toByteArray(), Base64.NO_PADDING) val webViewData =
Base64.encodeToString(formattedLicense.toByteArray(), 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")
@ -109,5 +109,4 @@ object LicenseFragmentHelper {
} }
} }
} }
}
} }