Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to kotlin #12

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
defaultConfig {
applicationId "covid.trace.morocco"
resValue "string", "build_config_package", "covid.trace.morocco"
minSdkVersion 22
minSdkVersion 21
DouiriAli marked this conversation as resolved.
Show resolved Hide resolved
targetSdkVersion 28
versionCode 70
versionName "1.0.70"
Expand Down
118 changes: 0 additions & 118 deletions app/src/main/java/covid/trace/morocco/LocaleHelper.java

This file was deleted.

92 changes: 92 additions & 0 deletions app/src/main/java/covid/trace/morocco/LocaleHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package covid.trace.morocco

import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.annotation.StringDef
import io.github.inflationx.calligraphy3.CalligraphyConfig
import io.github.inflationx.calligraphy3.CalligraphyInterceptor
import io.github.inflationx.viewpump.ViewPump
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.util.*

object LocaleHelper {

private const val SHARED_PREF_NAME = "covid_19"
private const val PATH_FONT_FR = "fonts/Barlow-Regular.ttf"
private const val PATH_FONT_AR = "fonts/Almarai-Regular.ttf"
private const val LANGUAGE_PREF_KEY = "language_pref_key"
const val FRENCH = "fr"
const val ARABIC = "ar"

private var sharedPreferences: SharedPreferences? = null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a lateinit sharedPreferences should be more appropriate. don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sharedPreferences can be null if a class calls a method (for example the method setLocaleIfNeeded(@language language: String)) without initiating sharedPreferences via the method init(context: Context?)

Copy link

@mosadialiou mosadialiou Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see BUT calling init is mandatory. It's a prerequisite to use LocaleHelper. As you can see init is called only once here https://github.com/Wiqaytna-app/wiqaytna_android/pull/12/files#diff-72d398151f1a0977873d51d832bb38f2R33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about my previous comment?!

var currentLocale: String? = null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non optional currentLocale here

Copy link
Contributor Author

@DouiriAli DouiriAli Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentLocale can be null if a class does not call the method init(context: Context?), but it should be private


fun init(context: Context?) {
DouiriAli marked this conversation as resolved.
Show resolved Hide resolved
sharedPreferences = context?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
currentLocale = sharedPreferences?.get(PreferencesHelper.LANGUAGE_KEY, ARABIC)
}

fun setLocale(context: Context?): Context? {
return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
updateLocale(context, currentLocale)
} else updateLocaleLegacy(context, currentLocale)
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun updateLocale(context: Context?, language: String?): Context? {
val locale = Locale(language)
Locale.setDefault(locale)
val config = context?.resources?.configuration
config?.setLocale(locale)
return context?.createConfigurationContext(config)
}

private fun updateLocaleLegacy(context: Context?, language: String?): Context? {
val locale = Locale(language)
Locale.setDefault(locale)
val res = context?.resources
val config = res?.configuration
config?.locale = locale
res?.updateConfiguration(config, res.displayMetrics)
return context
}

fun setLocaleIfNeeded(@Language language: String): Boolean {
if (language == currentLocale) {
return false
}
currentLocale = language
sharedPreferences?.put(PreferencesHelper.LANGUAGE_KEY, language)
return true
}

fun switchLocale() {
setLocaleIfNeeded(if (currentLocale == FRENCH) ARABIC else FRENCH)
val font =
if (currentLocale == FRENCH) PATH_FONT_FR else PATH_FONT_AR
setFont(font)
}

fun setFont(fontPath: String?) {
ViewPump.init(
ViewPump.builder()
.addInterceptor(
CalligraphyInterceptor(
CalligraphyConfig.Builder()
.setDefaultFontPath(fontPath)
.setFontAttrId(R.attr.fontPath)
.build()
)
)
.build()
)
}

@kotlin.annotation.Retention(AnnotationRetention.SOURCE)
@StringDef(FRENCH, ARABIC)
private annotation class Language
}
28 changes: 28 additions & 0 deletions app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package covid.trace.morocco

import android.content.SharedPreferences
import androidx.core.content.edit

inline fun <reified T> SharedPreferences.get(key: String?, defaultValue: T): T {
DouiriAli marked this conversation as resolved.
Show resolved Hide resolved
when (T::class) {
Boolean::class -> return this.getBoolean(key, defaultValue as Boolean) as T
Float::class -> return this.getFloat(key, defaultValue as Float) as T
Int::class -> return this.getInt(key, defaultValue as Int) as T
Long::class -> return this.getLong(key, defaultValue as Long) as T
String::class -> return this.getString(key, defaultValue as String) as T
}

return defaultValue
}

inline fun <reified T> SharedPreferences.put(key: String?, value: T) {
DouiriAli marked this conversation as resolved.
Show resolved Hide resolved
edit {
when (T::class) {
Boolean::class -> putBoolean(key, value as Boolean)
Float::class -> putFloat(key, value as Float)
Int::class -> putInt(key, value as Int)
Long::class -> putLong(key, value as Long)
String::class -> putString(key, value as String)
}
}
}
52 changes: 0 additions & 52 deletions app/src/main/java/covid/trace/morocco/PreferencesHelper.java

This file was deleted.

46 changes: 46 additions & 0 deletions app/src/main/java/covid/trace/morocco/PreferencesHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package covid.trace.morocco

import android.content.Context
import android.content.SharedPreferences

object PreferencesHelper {

const val SHARED_PREF_NAME = "covid_19"
const val LANGUAGE_KEY = "language_key"
const val ARABIC_LANGUAGE_CODE = "ar"
const val FRENCH_LANGUAGE_CODE = "fr"
const val AGE_RANGE = "AGE_RANGE"
const val GENDER = "GENDER"
const val REGION = "REGION"
const val PROVINCE = "PROVINCE"
const val INFOS_UPDATE = "INFOS_UPDATE"
const val STATS_UPDATE = "STATS_UPDATE"

private var mPreference: SharedPreferences? = null

fun init(context: Context) {
mPreference = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
}

fun setLanguage(language: String?) {
setPreference(LANGUAGE_KEY, language)
}

fun getCurrentLanguage(): String =
getStringPreference(LANGUAGE_KEY, ARABIC_LANGUAGE_CODE)

fun setPreference(key: String?, value: String?) {
mPreference?.put(key, value)
}

fun setPreference(key: String?, value: Boolean?) {
mPreference?.put(key, value)
}

fun getStringPreference(key: String?, defValue: String?): String =
mPreference?.get(key, defValue) as String

fun getBooleanPreference(key: String?, defValue: Boolean): Boolean =
mPreference?.get(key, defValue) as Boolean

}
2 changes: 1 addition & 1 deletion app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WiqaytnaApp : Application() {

override fun attachBaseContext(base: Context?) {
LocaleHelper.init(base)
super.attachBaseContext(LocaleHelper.getInstance().setLocale(base))
super.attachBaseContext(LocaleHelper.setLocale(base))
}

companion object {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/covid/trace/morocco/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import io.github.inflationx.viewpump.ViewPumpContextWrapper
open class BaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
LocaleHelper.getInstance().setLocale(this)
LocaleHelper.setLocale(this)
super.onCreate(savedInstanceState)
}

override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(
ViewPumpContextWrapper.wrap(
LocaleHelper.getInstance().setLocale(newBase)
LocaleHelper.setLocale(newBase)!!
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import io.github.inflationx.viewpump.ViewPumpContextWrapper
open class BaseFragmentActivity : FragmentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
LocaleHelper.getInstance().setLocale(this)
LocaleHelper.setLocale(this)
super.onCreate(savedInstanceState)
}

override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(
ViewPumpContextWrapper.wrap(
LocaleHelper.getInstance().setLocale(newBase)
LocaleHelper.setLocale(newBase)!!
)
)
}
Expand Down
Loading