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

com.likethesalad.placeholder.tasks.actions.ResolvePlaceholdersAction.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.tasks.actions

import com.likethesalad.placeholder.data.resources.ResourcesHandler
import com.likethesalad.placeholder.data.storage.FilesProvider
import com.likethesalad.placeholder.models.StringResourceModel
import com.likethesalad.placeholder.resolver.TemplateResolver
import java.io.File

class ResolvePlaceholdersAction(
    private val filesProvider: FilesProvider,
    private val resourcesHandler: ResourcesHandler,
    private val templateResolver: TemplateResolver
) {

    fun getTemplatesFiles(): List {
        return filesProvider.getAllTemplatesFiles()
    }

    fun getResolvedFiles(): List {
        return filesProvider.getAllExpectedResolvedFiles()
    }

    fun resolve() {
        for (templateFile in filesProvider.getAllTemplatesFiles()) {
            val templatesModel = resourcesHandler.getTemplatesFromFile(templateFile)
            val resolvedTemplates = templateResolver.resolveTemplates(templatesModel)
            val curatedTemplates =
                filterNonTranslatableStringsForLanguage(templatesModel.suffix, resolvedTemplates)
            if (curatedTemplates.isNotEmpty()) {
                resourcesHandler.saveResolvedStringList(
                    curatedTemplates,
                    templatesModel.suffix
                )
            } else {
                // Clean up
                resourcesHandler.removeResolvedStringFileIfExists(templatesModel.suffix)
            }
        }
    }

    private fun filterNonTranslatableStringsForLanguage(
        suffix: String,
        resolvedStrings: List
    ): List {
        if (suffix.isNotEmpty()) {
            // It is a language specific file
            return resolvedStrings.filter { it.translatable }
        }
        return resolvedStrings
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy