Skip to content

Commit

Permalink
refactor: toSentenceCase
Browse files Browse the repository at this point in the history
1. Replace Activity with Context (activities are also Context)
2. Add method that accepts `Resources`
3. Remove repeated code
  • Loading branch information
BrayanDSO committed Dec 7, 2024
1 parent 02355df commit b034183
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.ichi2.anki.ui.internationalization

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment

Expand All @@ -29,16 +30,15 @@ import androidx.fragment.app.Fragment
* "Toggle Suspend".toSentenceCase(R.string.sentence_toggle_suspend) // "Toggle suspend"
* ```
*/
fun String.toSentenceCase(activity: Activity, @StringRes resId: Int): String {
val resString = activity.getString(resId)
fun String.toSentenceCase(resources: Resources, @StringRes resId: Int): String {
val resString = resources.getString(resId)
// lowercase both for the comparison: sentence case doesn't mean all words are lowercase
if (this.lowercase() == resString.lowercase()) return resString
return this
}

fun String.toSentenceCase(fragment: Fragment, @StringRes resId: Int): String {
val resString = fragment.getString(resId)
// lowercase both for the comparison: sentence case doesn't mean all words are lowercase
if (this.lowercase() == resString.lowercase()) return resString
return this
}
fun String.toSentenceCase(context: Context, @StringRes resId: Int): String =
toSentenceCase(context.resources, resId)

fun String.toSentenceCase(fragment: Fragment, @StringRes resId: Int): String =
toSentenceCase(fragment.resources, resId)

0 comments on commit b034183

Please sign in to comment.