Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 66bced40 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

chore: add a new utility class `CategoryStringFormatter` to handle category string formatting

parent 54988a4c
Loading
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.data.application.utils

import java.util.Locale

object CategoryStringFormatter {
    fun format(input: String): String {
        return when (input) {
            "unknown" -> "Unknown"
            else ->
                input.replace("_", " ").split(" ").joinToString(" ") { word ->
                    if (word.lowercase() == "and") word
                    else {
                        word.replaceFirstChar {
                            if (it.isLowerCase()) it.titlecase(Locale.getDefault())
                            else it.toString()
                        } // Example: books and reference -> Books and Reference
                    }
                }
        } // Capitalize each word except "and"
    }
}
+5 −4
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 * Copyright (C) 2021-2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
@@ -14,6 +13,7 @@
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.data.application.utils
@@ -26,7 +26,7 @@ import foundation.e.apps.data.enums.AppTag
object CategoryUtils {

    private const val CATEGORY_OPEN_GAMES_ID = "game_open_games"
    private const val CATEGORY_OPEN_GAMES_TITLE = "Open games"
    private const val CATEGORY_OPEN_GAMES_TITLE = "Open Games"
    private const val CATEGORY_TITLE_REPLACEABLE_CONJUNCTION = "&"
    private const val CATEGORY_TITLE_CONJUNCTION = "and"

@@ -127,7 +127,8 @@ object CategoryUtils {
        return if (category.contentEquals(CATEGORY_OPEN_GAMES_ID)) {
            CATEGORY_OPEN_GAMES_TITLE
        } else {
            categories.translations.getOrDefault(category, "")
            val value = categories.translations.getOrDefault(category, "")
            CategoryStringFormatter.format(value)
        }
    }

+9 −5
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import foundation.e.apps.MainActivity
import foundation.e.apps.R
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.application.data.shareUri
import foundation.e.apps.data.application.utils.CategoryStringFormatter
import foundation.e.apps.data.cleanapk.CleanApkRetrofit
import foundation.e.apps.data.enums.Origin
import foundation.e.apps.data.enums.ResultStatus
@@ -461,13 +462,16 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) {
    private fun updateCategoryTitle(app: Application) {
        binding.titleInclude.apply {
            var catText = app.category.ifBlank { args.category }
            when {
                catText.isBlank() -> categoryTitle.isVisible = false
                catText == "game_open_games" -> catText = getString(R.string.games) // F-droid games
                catText == "web_games" -> catText = getString(R.string.games) // PWA games

            catText = when (catText) {
                "game_open_games" -> getString(R.string.games) // F-droid games
                "web_games" -> getString(R.string.games) // PWA games
                else -> catText
            }

            catText = catText.replace("_", " ")
            catText = CategoryStringFormatter.format(catText)

            categoryTitle.isVisible = catText.isNotBlank()
            categoryTitle.text = catText
        }
    }
+83 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.category

import foundation.e.apps.data.application.utils.CategoryStringFormatter
import org.junit.Assert.assertEquals
import org.junit.Test

class CategoryStringFormatterTest {

    @Test
    fun testFormatString_withUnderscores() {
        val input = "health_and_fitness"
        val expected = "Health and Fitness"
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_withMultipleWords() {
        val input = "mental_health_and_fitness"
        val expected = "Mental Health and Fitness"
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_withLeadingAndTrailingUnderscores() {
        val input = "_health_and_fitness_"
        val expected = "Health and Fitness"
        val result =
            CategoryStringFormatter.format(input.trim('_')) // Trimming underscores for testing
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_withNoUnderscores() {
        val input = "health"
        val expected = "Health"
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_withOnlyAnd() {
        val input = "and"
        val expected = "and"
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_withEmptyString() {
        val input = ""
        val expected = ""
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }

    @Test
    fun testFormatString_unknown() {
        val input = "unknown"
        val expected = "Unknown"
        val result = CategoryStringFormatter.format(input)
        assertEquals(expected, result)
    }
}