All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.likethesalad.placeholder.data.resources.AndroidResourcesHandler.kt Maven / Gradle / Ivy

Go to download

This is a Gradle plugin for Android applications which resolves XML string references in other XML strings.

There is a newer version: 1.3.0
Show newest version
package com.likethesalad.placeholder.data.resources

import com.google.gson.Gson
import com.likethesalad.placeholder.data.storage.FilesProvider
import com.likethesalad.placeholder.models.StringResourceModel
import com.likethesalad.placeholder.models.StringsGatheredModel
import com.likethesalad.placeholder.models.StringsTemplatesModel
import com.likethesalad.placeholder.utils.AndroidXmlResDocument
import java.io.File


class AndroidResourcesHandler(private val filesProvider: FilesProvider) : ResourcesHandler {

    private val gson = Gson()

    override fun getGatheredStringsFromFile(stringFile: File): StringsGatheredModel {
        return gson.fromJson(stringFile.readText(), StringsGatheredModel::class.java)
    }

    override fun saveGatheredStrings(stringsGathered: StringsGatheredModel) {
        val jsonStrings = gson.toJson(stringsGathered)
        filesProvider.getGatheredStringsFile(stringsGathered.suffix).writeText(jsonStrings)
    }

    override fun saveResolvedStringList(
        resolvedStrings: List,
        suffix: String
    ) {
        val resDocument = AndroidXmlResDocument()
        resDocument.appendAllStringResources(resolvedStrings)
        resDocument.saveToFile(filesProvider.getResolvedFile(suffix))
    }

    override fun removeResolvedStringFileIfExists(suffix: String) {
        val file = filesProvider.getResolvedFile(suffix)
        if (file.exists()) {
            file.delete()
        }
    }

    override fun getTemplatesFromFile(templateFile: File): StringsTemplatesModel {
        return if (templateFile.exists()) {
            gson.fromJson(templateFile.readText(), StringsTemplatesModel::class.java)
        } else {
            StringsTemplatesModel("", listOf(), mapOf())
        }
    }

    override fun saveTemplatesToFile(templates: StringsTemplatesModel, templateFile: File) {
        val jsonTemplates = gson.toJson(templates)
        templateFile.writeText(jsonTemplates)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy