#12 Make three options to deactivate sidebars, tabs and reading - read from json - tidy file up

This commit is contained in:
Sean Mac Gillicuddy 2020-02-12 15:32:05 +00:00
parent 0153b4514d
commit 226e787f66
4 changed files with 56 additions and 196 deletions

View File

@ -18,32 +18,55 @@
package custom
import org.json.simple.JSONObject
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
const val dateFormat = "YYYY-MM"
data class CustomApp(
val name: String,
val url: String,
val enforcedLanguage: String,
val displayName: String,
val versionName: String = parseVersionNameFromUrlOrUsePattern(url, "YYYY-MM")
val versionName: String,
val disableSideBar: Boolean = false,
val disableTabs: Boolean = false,
val disableReadAloud: Boolean = false
) {
val versionCode: Int = formatDate("YYDDD0").toInt()
constructor(name: String, parsedJson: JSONObject) : this(
name,
parsedJson.getAndCast("zim_url"),
parsedJson.getAndCast("enforced_lang"),
parsedJson.getAndCast("app_name"),
readVersionOrInfer(parsedJson),
parsedJson.getAndCast("disable_sidebar") ?: false,
parsedJson.getAndCast("disable_tabs") ?: false,
parsedJson.getAndCast("disable_read_aloud") ?: false
)
val versionCode: Int = formatCurrentDate("YYDDD0").toInt()
companion object {
private fun readVersionOrInfer(parsedJson: JSONObject) =
(parsedJson.getAndCast("version_name")
?: versionNameFromUrl(parsedJson.getAndCast("zim_url")))
?: formatCurrentDate()
}
}
private fun parseVersionNameFromUrlOrUsePattern(url: String, pattern: String) =
private fun versionNameFromUrl(url: String) =
url.substringAfterLast("_")
.substringBeforeLast(".")
.takeIf {
try {
SimpleDateFormat(pattern, Locale.ROOT).parse(it) != null
SimpleDateFormat(dateFormat, Locale.ROOT).parse(it) != null
} catch (parseException: ParseException) {
false
}
}
?: formatDate(pattern)
private fun formatDate(pattern: String) =
private fun formatCurrentDate(pattern: String = dateFormat) =
Date().let(SimpleDateFormat(pattern, Locale.ROOT)::format)

View File

@ -24,202 +24,39 @@ import org.json.simple.JSONObject
import org.json.simple.parser.JSONParser
import java.io.File
typealias ProductFlavors = NamedDomainObjectContainer<ProductFlavor>
object CustomApps {
private val example = CustomApp(
name = "customexample",
url = "http://download.kiwix.org/zim/wikipedia_fr_test.zim",
enforcedLanguage = "en",
displayName = "Test Custom App"
)
private val phet = CustomApp(
name = "phet",
url = "http://download.kiwix.org/zim/phet/phet_mul_2018-09.zim",
enforcedLanguage = "en",
displayName = "PhET"
)
private val tunisie = CustomApp(
name = "tunisie",
url = "http://download.kiwix.org/zim/wikipedia_fr_tunisie_novid.zim",
enforcedLanguage = "fr",
versionName = "2018-07",
displayName = "Encyclopédie de la Tunisie"
)
private val venezuela = CustomApp(
name = "venezuela",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_es_venezuela_2018-07.zim",
enforcedLanguage = "es",
displayName = "Enciclopedia de Venezuela"
)
private val wikimed = CustomApp(
name = "wikimed",
url = "http://download.kiwix.org/zim/wikipedia_en_medicine_novid.zim",
enforcedLanguage = "en",
versionName = "2018-08",
displayName = "Medical Wikipedia"
)
private val wikimedar = CustomApp(
name = "wikimedar",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_ar_medicine_novid_2018-08.zim",
enforcedLanguage = "ar",
displayName = "وِيكيبيديا الطبية (بلا اتصال بالانترنت)"
)
private val wikimedde = CustomApp(
name = "wikimedde",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_de_medicine_novid_2018-10.zim",
enforcedLanguage = "de",
displayName = "Wikipedia Medizin (Offline)"
)
private val wikimedes = CustomApp(
name = "wikimedes",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_es_medicine_novid_2018-10.zim",
enforcedLanguage = "es",
displayName = "Wikipedia Médica (Offline)"
)
private val wikimedfa = CustomApp(
name = "wikimedfa",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_fa_medicine_novid_2018-07.zim",
enforcedLanguage = "fa",
displayName = "ویکی‌پدیای پزشکی (آفلاین)"
)
private val wikimedfr = CustomApp(
name = "wikimedfr",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_fr_medicine_novid_2018-07.zim",
enforcedLanguage = "fr",
displayName = "Wikipédia médicale"
)
private val wikimedja = CustomApp(
name = "wikimedja",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_ja_medicine_novid_2018-07.zim",
enforcedLanguage = "ja",
displayName = "医療ウィキペディア(オフライン)"
)
private val wikimedmini = CustomApp(
name = "wikimedmini",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_en_medicine_nodet_2018-07.zim",
enforcedLanguage = "en",
displayName = "Offline WikiMed mini"
)
private val wikimedor = CustomApp(
name = "wikimedor",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_or_medicine_novid_2018-07.zim",
enforcedLanguage = "or",
displayName = "ମେଡିକାଲ ଉଇକିପିଡିଆ (ଅଫଲାଇନ)"
)
private val wikimedpt = CustomApp(
name = "wikimedpt",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_pt_medicine_2018-10.zim",
enforcedLanguage = "pt",
displayName = "Wikipédia Médica (Offline)"
)
private val wikimedzh = CustomApp(
name = "wikimedzh",
url = "http://download.kiwix.org/zim/wikipedia/wikipedia_zh_medicine_novid_2018-07.zim",
enforcedLanguage = "zh",
displayName = "醫學維基百科(離線版)"
)
private val wikispecies = CustomApp(
name = "wikispecies",
url = "http://download.kiwix.org/zim/wikispecies/wikispecies_en_all_novid_2018-08.zim",
enforcedLanguage = "en",
displayName = "WikiSpecies"
)
private val wikivoyage = CustomApp(
name = "wikivoyage",
url = "http://download.kiwix.org/zim/wikivoyage/wikivoyage_en_all_novid_2018-10.zim",
enforcedLanguage = "en",
displayName = "Wikivoyage"
)
private val wikivoyageeurope = CustomApp(
name = "wikivoyageeurope",
url = "http://download.kiwix.org/zim/wikivoyage/wikivoyage_en_europe_novid_2018-10.zim",
enforcedLanguage = "en",
displayName = "Wikivoyage European Travels"
)
private val wikivoyagede = CustomApp(
name = "wikivoyagede",
url = "http://download.kiwix.org/zim/wikivoyage/wikivoyage_de_all_novid_2018-10.zim",
enforcedLanguage = "de",
displayName = "Wikivoyage auf Deutsch"
)
private val all = listOf(
example,
phet,
tunisie,
venezuela,
wikimed,
wikimedar,
wikimedde,
wikimedes,
wikimedfa,
wikimedfr,
wikimedja,
wikimedmini,
wikimedor,
wikimedpt,
wikimedzh,
wikispecies,
wikivoyage,
wikivoyageeurope,
wikivoyagede
)
fun createStatically(productFlavors: NamedDomainObjectContainer<ProductFlavor>) {
productFlavors.create(all)
fun createDynamically(srcFolder: File, productFlavors: ProductFlavors) {
productFlavors.create(customApps(srcFolder))
}
fun createDynamically(
srcFolder: File,
productFlavors: NamedDomainObjectContainer<ProductFlavor>
) {
productFlavors.create(
srcFolder.walk()
.filter { it.name == "info.json" }
.map {
val parsedJson = JSONParser().parse(it.readText()) as JSONObject
createCustomAppFromJson(
it.parentFile.name,
parsedJson.getAndCast("zim_url"),
parsedJson.getAndCast("enforced_lang"),
parsedJson.getAndCast("app_name"),
parsedJson.getAndCast("version_name")
)
}.toList()
)
private fun customApps(srcFolder: File) = srcFolder.walk()
.filter { it.name == "info.json" }
.map { CustomApp(it.parentFile.name, JSONParser().parse(it.readText()) as JSONObject) }
.toList()
}
fun ProductFlavors.create(customApps: List<CustomApp>) {
customApps.forEach { customApp ->
this.create(customApp.name) {
versionName = customApp.versionName
versionCode = customApp.versionCode
applicationIdSuffix = ".kiwixcustom${customApp.name}"
buildConfigField("String", "ZIM_URL", "\"${customApp.url}\"")
buildConfigField("String", "ENFORCED_LANG", "\"${customApp.enforcedLanguage}\"")
buildConfigField("Boolean", "DISABLE_SIDEBAR", "${customApp.disableSideBar}")
buildConfigField("Boolean", "DISABLE_TABS", "${customApp.disableTabs}")
buildConfigField("Boolean", "DISABLE_READ_ALOUD", "${customApp.disableReadAloud}")
configureStrings(customApp.displayName)
}
}
}
fun NamedDomainObjectContainer<ProductFlavor>.create(customApps: List<CustomApp>) {
customApps.forEach(this::create)
}
private fun createCustomAppFromJson(
name: String,
url: String,
enforcedLanguage: String,
displayName: String,
versionName: String?
) = if (versionName == null) CustomApp(name, url, enforcedLanguage, displayName)
else CustomApp(name, url, enforcedLanguage, displayName, versionName)
fun <T> JSONObject.getAndCast(columnName: String): T =
getOrDefault(columnName, null) as T
private fun NamedDomainObjectContainer<ProductFlavor>.create(
customApp: CustomApp
) {
create(customApp.name) {
versionName = customApp.versionName
versionCode = customApp.versionCode
applicationIdSuffix = ".kiwixcustom${customApp.name}"
buildConfigField("String", "ZIM_URL", "\"${customApp.url}\"")
buildConfigField("String", "ENFORCED_LANG", "\"${customApp.enforcedLanguage}\"")
buildConfigField("Boolean", "DISABLE_SIDEBAR", "false")
buildConfigField("Boolean", "DISABLE_READ_ALOUD", "false")
buildConfigField("Boolean", "DISABLE_TABS", "false")
configureStrings(customApp.displayName)
}
}
fun ProductFlavor.configureStrings(appName: String) {
resValue("string", "app_name", appName)
resValue("string", "app_search_string", "Search $appName")

View File

@ -21,9 +21,6 @@ android {
flavorDimensions("default")
productFlavors {
// Uncomment for static productFlavors
// CustomApps.createStatically(this)
CustomApps.createDynamically(project.file("src"), this)
all {
File("$projectDir/src", "$name/$name.zim").let {

View File

@ -1,6 +1,9 @@
{
"app_name": "Test Custom App",
"zim_url": "http://download.kiwix.org/zim/wikipedia_fr_test.zim",
"enforced_lang": "en"
"enforced_lang": "en",
"disable_sidebar": false,
"disable_tabs": false,
"disable_read_aloud": false
}