Merge pull request #8682 from Isira-Seneviratne/Refactor_LicenseFragmentHelper
Refactor LicenseFragmentHelper.
This commit is contained in:
commit
5640365fbd
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,6 @@ 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
|
||||||
|
@ -37,21 +36,17 @@ object LicenseFragmentHelper {
|
||||||
*/
|
*/
|
||||||
private fun getLicenseStylesheet(context: Context): String {
|
private fun getLicenseStylesheet(context: Context): String {
|
||||||
val isLightTheme = ThemeHelper.isLightThemeSelected(context)
|
val isLightTheme = ThemeHelper.isLightThemeSelected(context)
|
||||||
return (
|
val licenseBackgroundColor = getHexRGBColor(
|
||||||
"body{padding:12px 15px;margin:0;" + "background:#" + getHexRGBColor(
|
context, if (isLightTheme) R.color.light_license_background_color else R.color.dark_license_background_color
|
||||||
context,
|
|
||||||
if (isLightTheme) R.color.light_license_background_color
|
|
||||||
else R.color.dark_license_background_color
|
|
||||||
) + ";" + "color:#" + getHexRGBColor(
|
|
||||||
context,
|
|
||||||
if (isLightTheme) R.color.light_license_text_color
|
|
||||||
else R.color.dark_license_text_color
|
|
||||||
) + "}" + "a[href]{color:#" + getHexRGBColor(
|
|
||||||
context,
|
|
||||||
if (isLightTheme) R.color.light_youtube_primary_color
|
|
||||||
else R.color.dark_youtube_primary_color
|
|
||||||
) + "}" + "pre{white-space:pre-wrap}"
|
|
||||||
)
|
)
|
||||||
|
val licenseTextColor = getHexRGBColor(
|
||||||
|
context, if (isLightTheme) R.color.light_license_text_color else R.color.dark_license_text_color
|
||||||
|
)
|
||||||
|
val youtubePrimaryColor = getHexRGBColor(
|
||||||
|
context, if (isLightTheme) R.color.light_youtube_primary_color else R.color.dark_youtube_primary_color
|
||||||
|
)
|
||||||
|
return "body{padding:12px 15px;margin:0;background:#$licenseBackgroundColor;color:#$licenseTextColor}" +
|
||||||
|
"a[href]{color:#$youtubePrimaryColor}pre{white-space:pre-wrap}"
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,29 +60,25 @@ object LicenseFragmentHelper {
|
||||||
return context.getString(color).substring(3)
|
return context.getString(color).substring(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
|
||||||
return showLicense(context, component.license) { alertDialog ->
|
return showLicense(context, component.license) {
|
||||||
alertDialog.setPositiveButton(R.string.dismiss) { dialog, _ ->
|
setPositiveButton(R.string.dismiss) { dialog, _ ->
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
alertDialog.setNeutralButton(R.string.open_website_license) { _, _ ->
|
setNeutralButton(R.string.open_website_license) { _, _ ->
|
||||||
ShareUtils.openUrlInBrowser(context!!, component.link)
|
ShareUtils.openUrlInBrowser(context!!, component.link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun showLicense(context: Context?, license: License) = showLicense(context, license) {
|
||||||
|
setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() }
|
||||||
|
}
|
||||||
|
|
||||||
private fun showLicense(
|
private fun showLicense(
|
||||||
context: Context?,
|
context: Context?,
|
||||||
license: License,
|
license: License,
|
||||||
block: (AlertDialog.Builder) -> Unit
|
block: AlertDialog.Builder.() -> AlertDialog.Builder
|
||||||
): Disposable {
|
): Disposable {
|
||||||
return if (context == null) {
|
return if (context == null) {
|
||||||
Disposable.empty()
|
Disposable.empty()
|
||||||
|
@ -96,18 +87,17 @@ 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")
|
||||||
|
|
||||||
AlertDialog.Builder(context).apply {
|
|
||||||
setTitle(license.name)
|
|
||||||
setView(webView)
|
|
||||||
Localization.assureCorrectAppLanguage(context)
|
Localization.assureCorrectAppLanguage(context)
|
||||||
block(this)
|
AlertDialog.Builder(context)
|
||||||
show()
|
.setTitle(license.name)
|
||||||
}
|
.setView(webView)
|
||||||
}
|
.block()
|
||||||
|
.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue