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

com.likethesalad.placeholder.tasks.actions.GatherRawStringsAction.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.models.StringsGatheredModel
import com.likethesalad.placeholder.models.raw.FlavorValuesRawFiles
import com.likethesalad.placeholder.models.raw.RawFiles
import com.likethesalad.placeholder.utils.AndroidXmlResDocument
import java.io.File

class GatherRawStringsAction(
    private val filesProvider: FilesProvider,
    private val resourcesHandler: ResourcesHandler
) {

    fun getInputFiles(): List {
        return filesProvider.getAllFoldersRawResourcesFiles().map {
            it.getRawFilesMetaList().flatten()
        }.flatten()
    }

    fun getOutputFile(): File {
        return filesProvider.getGatheredStringsFile()
    }

    fun gatherStrings() {
        for (folderRawFiles in filesProvider.getAllFoldersRawResourcesFiles()) {
            gatherStringsForFolder(folderRawFiles)
        }
    }

    private fun gatherStringsForFolder(rawFilesModel: RawFiles) {
        val mainStrings = getStringListFromFiles(rawFilesModel.mainValuesRawFiles)

        val complimentaryStrings: List> = when (rawFilesModel) {
            is FlavorValuesRawFiles -> listOf(getStringListFromFiles(rawFilesModel.complimentaryRawFiles))
            else -> listOf()
        }

        if (mainStrings.isNotEmpty() || complimentaryStrings.isNotEmpty()) {
            resourcesHandler.saveGatheredStrings(
                StringsGatheredModel(
                    rawFilesModel.suffix,
                    mainStrings,
                    complimentaryStrings
                )
            )
        }
    }

    private fun getStringListFromFiles(files: List): List {
        return files.map {
            AndroidXmlResDocument.readFromFile(it)
        }.flatMap { it.getStringResourceList() }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy