From 905d92d97a96a2359a7d88cc90822fef82eb7b44 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Mon, 1 Jun 2020 13:59:25 +0200 Subject: [PATCH 1/9] change minSdk to 21 --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index a34b7c1..bb463bb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,7 +34,7 @@ android { defaultConfig { applicationId "covid.trace.morocco" resValue "string", "build_config_package", "covid.trace.morocco" - minSdkVersion 22 + minSdkVersion 21 targetSdkVersion 28 versionCode 66 versionName "1.0.66" From cc41abbbbebd739fe97bdac58d2554a4e458e8a9 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Tue, 2 Jun 2020 07:33:50 +0200 Subject: [PATCH 2/9] Convert PreferencesHelper to Kotlin --- .../trace/morocco/PreferencesExtensions.kt | 28 ++++++++++ .../trace/morocco/PreferencesHelper.java | 52 ------------------- .../covid/trace/morocco/PreferencesHelper.kt | 46 ++++++++++++++++ 3 files changed, 74 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt delete mode 100644 app/src/main/java/covid/trace/morocco/PreferencesHelper.java create mode 100644 app/src/main/java/covid/trace/morocco/PreferencesHelper.kt diff --git a/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt b/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt new file mode 100644 index 0000000..2fd1279 --- /dev/null +++ b/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt @@ -0,0 +1,28 @@ +package covid.trace.morocco + +import android.content.SharedPreferences +import androidx.core.content.edit + +inline fun SharedPreferences.get(key: String?, defaultValue: T): T { + 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 SharedPreferences.put(key: String?, value: T) { + 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) + } + } +} diff --git a/app/src/main/java/covid/trace/morocco/PreferencesHelper.java b/app/src/main/java/covid/trace/morocco/PreferencesHelper.java deleted file mode 100644 index 7255463..0000000 --- a/app/src/main/java/covid/trace/morocco/PreferencesHelper.java +++ /dev/null @@ -1,52 +0,0 @@ -package covid.trace.morocco; - -import android.content.Context; -import android.content.SharedPreferences; - -public class PreferencesHelper { - - public static final String LANGUAGE_KEY = "language_key"; - public static final String ARABIC_LANGUAGE_CODE = "ar"; - public static final String FRENCH_LANGUAGE_CODE = "fr"; - public static final String AGE_RANGE = "AGE_RANGE"; - public static final String GENDER = "GENDER"; - public static final String REGION = "REGION"; - public static final String PROVINCE = "PROVINCE"; - public static final String INFOS_UPDATE = "INFOS_UPDATE"; - public static final String STATS_UPDATE = "STATS_UPDATE"; - private static PreferencesHelper sInstance = null; - private final SharedPreferences mPreference; - - private PreferencesHelper(Context context) { - mPreference = context.getSharedPreferences("covid_19", Context.MODE_PRIVATE); - } - - public static void init(Context context) { - if (sInstance == null) - sInstance = new PreferencesHelper(context); - } - - public static void setLanguage(String language) { - setPreference(LANGUAGE_KEY, language); - } - - public static String getCurrentLanguage() { - return getStringPreference(LANGUAGE_KEY, PreferencesHelper.ARABIC_LANGUAGE_CODE); - } - - public static void setPreference(String key, String value) { - sInstance.mPreference.edit().putString(key, value).apply(); - } - - public static void setPreference(String key, Boolean value) { - sInstance.mPreference.edit().putBoolean(key, value).apply(); - } - - public static String getStringPreference(String key, String defValue) { - return sInstance.mPreference.getString(key, defValue); - } - - public static boolean getBooleanPreference(String key, boolean defValue) { - return sInstance.mPreference.getBoolean(key, defValue); - } -} diff --git a/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt b/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt new file mode 100644 index 0000000..7f97e0a --- /dev/null +++ b/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt @@ -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 + +} \ No newline at end of file From 606544c231eda6526e83477f3e52724f0c1f24f3 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Wed, 3 Jun 2020 16:19:07 +0200 Subject: [PATCH 3/9] Move LocaleHelper to Kotlin --- .../covid/trace/morocco/LocaleHelper.java | 118 ------------------ .../java/covid/trace/morocco/LocaleHelper.kt | 92 ++++++++++++++ .../java/covid/trace/morocco/WiqaytnaApp.kt | 2 +- .../covid/trace/morocco/base/BaseActivity.kt | 4 +- .../morocco/base/BaseFragmentActivity.kt | 4 +- .../trace/morocco/onboarding/OTPFragment.kt | 2 +- .../onboarding/PersonalInfosActivity.kt | 2 +- .../onboarding/PreOnboardingActivity.kt | 2 +- .../onboarding/RegisterNumberFragment.kt | 2 +- .../trace/morocco/onboarding/SetupFragment.kt | 2 +- .../trace/morocco/onboarding/TouActivity.kt | 2 +- 11 files changed, 103 insertions(+), 129 deletions(-) delete mode 100644 app/src/main/java/covid/trace/morocco/LocaleHelper.java create mode 100644 app/src/main/java/covid/trace/morocco/LocaleHelper.kt diff --git a/app/src/main/java/covid/trace/morocco/LocaleHelper.java b/app/src/main/java/covid/trace/morocco/LocaleHelper.java deleted file mode 100644 index b617494..0000000 --- a/app/src/main/java/covid/trace/morocco/LocaleHelper.java +++ /dev/null @@ -1,118 +0,0 @@ -package covid.trace.morocco; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.os.Build; - -import androidx.annotation.RequiresApi; -import androidx.annotation.StringDef; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.Locale; - -import io.github.inflationx.calligraphy3.CalligraphyConfig; -import io.github.inflationx.calligraphy3.CalligraphyInterceptor; -import io.github.inflationx.viewpump.ViewPump; - - -public class LocaleHelper { - - private static final String LANGUAGE_PREF_KEY = "language_pref_key"; - public static final String FRENCH = "fr"; - public static final String ARABIC = "ar"; - - private static LocaleHelper instance; - - public static LocaleHelper getInstance() { - return instance; - } - - private SharedPreferences sharedPreferences; - private String currentLocale; - - public String getCurrentLocale() { - return currentLocale; - } - - private LocaleHelper() { - } - - public static void init(Context context) { - instance = new LocaleHelper(); - instance.sharedPreferences = context.getSharedPreferences("covid_19", Context.MODE_PRIVATE); - instance.currentLocale = instance.sharedPreferences.getString(PreferencesHelper.LANGUAGE_KEY, ARABIC); - } - - public Context setLocale(Context context) { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { - return updateLocale(context, currentLocale); - } - return updateLocaleLegacy(context, currentLocale); - } - - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) - private Context updateLocale(Context context, String language) { - Locale locale = new Locale(language); - Locale.setDefault(locale); - - Configuration config = context.getResources().getConfiguration(); - config.setLocale(locale); - - return context.createConfigurationContext(config); - } - - private Context updateLocaleLegacy(Context context, String language) { - Locale locale = new Locale(language); - Locale.setDefault(locale); - - Resources res = context.getResources(); - Configuration config = res.getConfiguration(); - config.locale = locale; - res.updateConfiguration(config, res.getDisplayMetrics()); - - return context; - } - - public boolean setLocaleIfNeeded(@Language String language) { - if (language.equals(currentLocale)) { - return false; - } - currentLocale = language; - sharedPreferences.edit() - .putString(PreferencesHelper.LANGUAGE_KEY, language) - .apply(); - return true; - } - - public void switchLocale() { - setLocaleIfNeeded(currentLocale.equals(FRENCH) ? ARABIC : FRENCH); - String font = currentLocale.equals(FRENCH) ? "fonts/Barlow-Regular.ttf" : "fonts/Almarai-Regular.ttf"; - setFont(font); - } - - public static void setFont(String fontPath) { - ViewPump.init( - ViewPump.builder() - .addInterceptor( - new CalligraphyInterceptor( - new CalligraphyConfig.Builder() - .setDefaultFontPath(fontPath) - .setFontAttrId(R.attr.fontPath) - .build() - ) - ) - .build() - ); - } - - @Retention(RetentionPolicy.SOURCE) - @StringDef({FRENCH, ARABIC}) - private @interface Language { - } -} diff --git a/app/src/main/java/covid/trace/morocco/LocaleHelper.kt b/app/src/main/java/covid/trace/morocco/LocaleHelper.kt new file mode 100644 index 0000000..2c54e34 --- /dev/null +++ b/app/src/main/java/covid/trace/morocco/LocaleHelper.kt @@ -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 + var currentLocale: String? = null + + fun init(context: Context?) { + 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 +} \ No newline at end of file diff --git a/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt b/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt index 6768a46..fb21156 100644 --- a/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt +++ b/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt @@ -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 { diff --git a/app/src/main/java/covid/trace/morocco/base/BaseActivity.kt b/app/src/main/java/covid/trace/morocco/base/BaseActivity.kt index ce1bdf1..d1992e4 100644 --- a/app/src/main/java/covid/trace/morocco/base/BaseActivity.kt +++ b/app/src/main/java/covid/trace/morocco/base/BaseActivity.kt @@ -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)!! ) ) } diff --git a/app/src/main/java/covid/trace/morocco/base/BaseFragmentActivity.kt b/app/src/main/java/covid/trace/morocco/base/BaseFragmentActivity.kt index 6ce2938..048b1e9 100644 --- a/app/src/main/java/covid/trace/morocco/base/BaseFragmentActivity.kt +++ b/app/src/main/java/covid/trace/morocco/base/BaseFragmentActivity.kt @@ -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)!! ) ) } diff --git a/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt b/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt index 369f999..61000a9 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt @@ -101,7 +101,7 @@ class OTPFragment : OnboardingFragmentInterface() { "21", "Onboarding third screen" ) - LocaleHelper.getInstance().switchLocale() + LocaleHelper.switchLocale() requireActivity().recreate() } diff --git a/app/src/main/java/covid/trace/morocco/onboarding/PersonalInfosActivity.kt b/app/src/main/java/covid/trace/morocco/onboarding/PersonalInfosActivity.kt index a65de31..eac3da2 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/PersonalInfosActivity.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/PersonalInfosActivity.kt @@ -315,7 +315,7 @@ class PersonalInfosActivity : BaseFragmentActivity(), Utils.OnRegionSelected, Ut private fun onLanguagePicked(lang: String) { PreferencesHelper.setLanguage(lang) - if (LocaleHelper.getInstance().setLocaleIfNeeded(lang)) { + if (LocaleHelper.setLocaleIfNeeded(lang)) { val intent = Intent(this, MainActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(intent) diff --git a/app/src/main/java/covid/trace/morocco/onboarding/PreOnboardingActivity.kt b/app/src/main/java/covid/trace/morocco/onboarding/PreOnboardingActivity.kt index 6410d1b..04656d0 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/PreOnboardingActivity.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/PreOnboardingActivity.kt @@ -21,7 +21,7 @@ class PreOnboardingActivity : BaseFragmentActivity() { language.setOnClickListener { Utils.firebaseAnalyticsEvent(baseContext, "first_screen_change_language", "19", "Onboarding third screen resend otp") - LocaleHelper.getInstance().switchLocale() + LocaleHelper.switchLocale() recreate() } diff --git a/app/src/main/java/covid/trace/morocco/onboarding/RegisterNumberFragment.kt b/app/src/main/java/covid/trace/morocco/onboarding/RegisterNumberFragment.kt index 7fd869b..c521741 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/RegisterNumberFragment.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/RegisterNumberFragment.kt @@ -124,7 +124,7 @@ class RegisterNumberFragment : OnboardingFragmentInterface() { "20", "Onboarding second screen" ) - LocaleHelper.getInstance().switchLocale() + LocaleHelper.switchLocale() requireActivity().recreate() } } diff --git a/app/src/main/java/covid/trace/morocco/onboarding/SetupFragment.kt b/app/src/main/java/covid/trace/morocco/onboarding/SetupFragment.kt index 16df631..5ac0bbe 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/SetupFragment.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/SetupFragment.kt @@ -42,7 +42,7 @@ class SetupFragment : OnboardingFragmentInterface() { "22", "Onboarding fourth screen" ) - LocaleHelper.getInstance().switchLocale() + LocaleHelper.switchLocale() requireActivity().recreate() } } diff --git a/app/src/main/java/covid/trace/morocco/onboarding/TouActivity.kt b/app/src/main/java/covid/trace/morocco/onboarding/TouActivity.kt index b879490..a7814d9 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/TouActivity.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/TouActivity.kt @@ -39,7 +39,7 @@ class TouActivity : BaseFragmentActivity() { "23", "Onboarding fourth screen" ) - LocaleHelper.getInstance().switchLocale() + LocaleHelper.switchLocale() recreate() } From 8de0117dc01b45aaf08e4103abb685dc52f98b4c Mon Sep 17 00:00:00 2001 From: Ali DOUIRI Date: Fri, 5 Jun 2020 11:17:37 +0200 Subject: [PATCH 4/9] remove bad indentation --- app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt b/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt index 02a9d1a..53ae35e 100644 --- a/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt +++ b/app/src/main/java/covid/trace/morocco/onboarding/OTPFragment.kt @@ -97,7 +97,6 @@ class OTPFragment : OnboardingFragmentInterface() { "21", "Onboarding third screen" ) - LocaleHelper.switchLocale() requireActivity().recreate() } From 741ff6499e2a3120239f13395a042dc1b4d07c4e Mon Sep 17 00:00:00 2001 From: alidouiri Date: Fri, 5 Jun 2020 11:47:01 +0200 Subject: [PATCH 5/9] Revert minSDKVersion to 22 --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index e4aa213..284cb3f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,7 +34,7 @@ android { defaultConfig { applicationId "covid.trace.morocco" resValue "string", "build_config_package", "covid.trace.morocco" - minSdkVersion 21 + minSdkVersion 22 targetSdkVersion 28 versionCode 70 versionName "1.0.70" From 60686154fe5168752ba419cfd2ebc5fd22227f3c Mon Sep 17 00:00:00 2001 From: alidouiri Date: Fri, 5 Jun 2020 12:08:50 +0200 Subject: [PATCH 6/9] Key can't be null --- .../java/covid/trace/morocco/PreferencesExtensions.kt | 4 ++-- .../main/java/covid/trace/morocco/PreferencesHelper.kt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt b/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt index 2fd1279..a78981d 100644 --- a/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt +++ b/app/src/main/java/covid/trace/morocco/PreferencesExtensions.kt @@ -3,7 +3,7 @@ package covid.trace.morocco import android.content.SharedPreferences import androidx.core.content.edit -inline fun SharedPreferences.get(key: String?, defaultValue: T): T { +inline fun SharedPreferences.get(key: String, defaultValue: T): T { 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 @@ -15,7 +15,7 @@ inline fun SharedPreferences.get(key: String?, defaultValue: T): T { return defaultValue } -inline fun SharedPreferences.put(key: String?, value: T) { +inline fun SharedPreferences.put(key: String, value: T) { edit { when (T::class) { Boolean::class -> putBoolean(key, value as Boolean) diff --git a/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt b/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt index 7f97e0a..64d5b52 100644 --- a/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt +++ b/app/src/main/java/covid/trace/morocco/PreferencesHelper.kt @@ -29,18 +29,18 @@ object PreferencesHelper { fun getCurrentLanguage(): String = getStringPreference(LANGUAGE_KEY, ARABIC_LANGUAGE_CODE) - fun setPreference(key: String?, value: String?) { + fun setPreference(key: String, value: String?) { mPreference?.put(key, value) } - fun setPreference(key: String?, value: Boolean?) { + fun setPreference(key: String, value: Boolean?) { mPreference?.put(key, value) } - fun getStringPreference(key: String?, defValue: String?): String = + fun getStringPreference(key: String, defValue: String?): String = mPreference?.get(key, defValue) as String - fun getBooleanPreference(key: String?, defValue: Boolean): Boolean = + fun getBooleanPreference(key: String, defValue: Boolean): Boolean = mPreference?.get(key, defValue) as Boolean } \ No newline at end of file From 4b4497d3ff9dd2cbf6f4b1264e5eb96814df5bc4 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Fri, 5 Jun 2020 12:22:53 +0200 Subject: [PATCH 7/9] Context can't be null --- app/src/main/java/covid/trace/morocco/LocaleHelper.kt | 4 ++-- app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/covid/trace/morocco/LocaleHelper.kt b/app/src/main/java/covid/trace/morocco/LocaleHelper.kt index 2c54e34..0a753cf 100644 --- a/app/src/main/java/covid/trace/morocco/LocaleHelper.kt +++ b/app/src/main/java/covid/trace/morocco/LocaleHelper.kt @@ -24,8 +24,8 @@ object LocaleHelper { private var sharedPreferences: SharedPreferences? = null var currentLocale: String? = null - fun init(context: Context?) { - sharedPreferences = context?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE) + fun init(context: Context) { + sharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE) currentLocale = sharedPreferences?.get(PreferencesHelper.LANGUAGE_KEY, ARABIC) } diff --git a/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt b/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt index fb21156..cb5477f 100644 --- a/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt +++ b/app/src/main/java/covid/trace/morocco/WiqaytnaApp.kt @@ -29,7 +29,7 @@ class WiqaytnaApp : Application() { LocaleHelper.setFont("fonts/Almarai-Regular.ttf") } - override fun attachBaseContext(base: Context?) { + override fun attachBaseContext(base: Context) { LocaleHelper.init(base) super.attachBaseContext(LocaleHelper.setLocale(base)) } From 998bb0430dbb58cf3446696ed3f46b5708bbe270 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Fri, 5 Jun 2020 18:30:09 +0200 Subject: [PATCH 8/9] Move StepProgressBar to Kotlin --- .../trace/morocco/view/StepProgressBar.java | 86 ------------------ .../trace/morocco/view/StepProgressBar.kt | 87 +++++++++++++++++++ 2 files changed, 87 insertions(+), 86 deletions(-) delete mode 100644 app/src/main/java/covid/trace/morocco/view/StepProgressBar.java create mode 100644 app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt diff --git a/app/src/main/java/covid/trace/morocco/view/StepProgressBar.java b/app/src/main/java/covid/trace/morocco/view/StepProgressBar.java deleted file mode 100644 index 60b8a9c..0000000 --- a/app/src/main/java/covid/trace/morocco/view/StepProgressBar.java +++ /dev/null @@ -1,86 +0,0 @@ -package covid.trace.morocco.view; - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.drawable.Drawable; -import android.util.AttributeSet; -import android.view.View; - -import covid.trace.morocco.R; - -/** - * I forked code from - * https://github.com/marcokstephen/StepProgressBar - */ -public class StepProgressBar extends View { - - private Drawable inactiveDrawable; - private Drawable activeDrawable; - - private float dashSpacing; - - private int maxNumDashes; - private int currentlyActiveDash; - private boolean cumulativeDashes; - - private static final int MIN_DOTS = -1; - private static final String OUT_OF_BOUNDS_ERROR = "Progress bar out of bounds!"; - - public StepProgressBar(Context context, AttributeSet attrs) { - super(context, attrs); - - TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.StepProgressBar, 0, 0); - try { - inactiveDrawable = ta.getDrawable(R.styleable.StepProgressBar_inactiveDotIcon); - activeDrawable = ta.getDrawable(R.styleable.StepProgressBar_activeDotIcon); - dashSpacing = ta.getDimensionPixelSize(R.styleable.StepProgressBar_spacing, 15); - maxNumDashes = ta.getInt(R.styleable.StepProgressBar_numberDots, 5); - currentlyActiveDash = ta.getInt(R.styleable.StepProgressBar_activeDotIndex, 0); - cumulativeDashes = ta.getBoolean(R.styleable.StepProgressBar_cumulativeDots, false); - } finally { - ta.recycle(); - } - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - int height = activeDrawable.getIntrinsicHeight() + getPaddingBottom() + getPaddingTop(); - setMeasuredDimension(widthMeasureSpec, height); - } - - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - - // Centering the dots in the middle of the canvas - float singleDotSize = dashSpacing + activeDrawable.getIntrinsicWidth(); - float combinedDotSize = singleDotSize * maxNumDashes - dashSpacing; - int startingX = (int) ((getWidth() - combinedDotSize) / 2); - - for (int i = 0; i < maxNumDashes; i++) { - int x = (int) (startingX + i * singleDotSize); - if ((cumulativeDashes && i < currentlyActiveDash) || i == currentlyActiveDash) { - activeDrawable.setBounds(x, 0, x + activeDrawable.getIntrinsicWidth(), activeDrawable.getIntrinsicHeight()); - activeDrawable.draw(canvas); - } else { - inactiveDrawable.setBounds(x, 0, x + inactiveDrawable.getIntrinsicWidth(), inactiveDrawable.getIntrinsicHeight()); - inactiveDrawable.draw(canvas); - } - } - } - - public void setCurrentProgressDot(int i) { - if (i >= maxNumDashes || i < MIN_DOTS) { - throw new IndexOutOfBoundsException(OUT_OF_BOUNDS_ERROR); - } - currentlyActiveDash = i; - invalidate(); - } - - public int getCurrentProgressDot() { - return currentlyActiveDash; - } - -} diff --git a/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt b/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt new file mode 100644 index 0000000..6538263 --- /dev/null +++ b/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt @@ -0,0 +1,87 @@ +package covid.trace.morocco.view + +import android.content.Context +import android.graphics.Canvas +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.View +import covid.trace.morocco.R + +/** + * I forked code from + * https://github.com/marcokstephen/StepProgressBar + */ +class StepProgressBar( + context: Context, + attrs: AttributeSet? +) : View(context, attrs) { + + companion object { + private const val MIN_DOTS = -1 + private const val OUT_OF_BOUNDS_ERROR = "Progress bar out of bounds!" + } + + private var inactiveDrawable: Drawable? = null + private var activeDrawable: Drawable? = null + private var dashSpacing = 0f + private var maxNumDashes = 0 + private var currentlyActiveDash = 0 + private var cumulativeDashes = false + + var currentProgressDot: Int + get() = currentlyActiveDash + set(i) { + if (i >= maxNumDashes || i < MIN_DOTS) { + throw IndexOutOfBoundsException(OUT_OF_BOUNDS_ERROR) + } + currentlyActiveDash = i + invalidate() + } + + init { + val ta = context.theme.obtainStyledAttributes(attrs, R.styleable.StepProgressBar, 0, 0) + try { + inactiveDrawable = ta.getDrawable(R.styleable.StepProgressBar_inactiveDotIcon) + activeDrawable = ta.getDrawable(R.styleable.StepProgressBar_activeDotIcon) + dashSpacing = ta.getDimensionPixelSize(R.styleable.StepProgressBar_spacing, 15).toFloat() + maxNumDashes = ta.getInt(R.styleable.StepProgressBar_numberDots, 5) + currentlyActiveDash = ta.getInt(R.styleable.StepProgressBar_activeDotIndex, 0) + cumulativeDashes = ta.getBoolean(R.styleable.StepProgressBar_cumulativeDots, false) + } finally { + ta.recycle() + } + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val height = activeDrawable!!.intrinsicHeight + paddingBottom + paddingTop + setMeasuredDimension(widthMeasureSpec, height) + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + // Centering the dots in the middle of the canvas + val singleDotSize = dashSpacing + activeDrawable!!.intrinsicWidth + val combinedDotSize = singleDotSize * maxNumDashes - dashSpacing + val startingX = ((width - combinedDotSize) / 2).toInt() + for (i in 0 until maxNumDashes) { + val x = (startingX + i * singleDotSize).toInt() + if (cumulativeDashes && i < currentlyActiveDash || i == currentlyActiveDash) { + activeDrawable!!.setBounds( + x, + 0, + x + activeDrawable!!.intrinsicWidth, + activeDrawable!!.intrinsicHeight + ) + activeDrawable!!.draw(canvas) + } else { + inactiveDrawable!!.setBounds( + x, + 0, + x + inactiveDrawable!!.intrinsicWidth, + inactiveDrawable!!.intrinsicHeight + ) + inactiveDrawable!!.draw(canvas) + } + } + } +} \ No newline at end of file From 4a50a6302df4a94ba5d92a2d41b16b280eaf17e6 Mon Sep 17 00:00:00 2001 From: alidouiri Date: Fri, 5 Jun 2020 18:53:18 +0200 Subject: [PATCH 9/9] Move StepProgressBar to Kotlin --- .../trace/morocco/view/StepProgressBar.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt b/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt index 6538263..825daf5 100644 --- a/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt +++ b/app/src/main/java/covid/trace/morocco/view/StepProgressBar.kt @@ -21,8 +21,8 @@ class StepProgressBar( private const val OUT_OF_BOUNDS_ERROR = "Progress bar out of bounds!" } - private var inactiveDrawable: Drawable? = null - private var activeDrawable: Drawable? = null + private var inactiveDrawable: Drawable + private var activeDrawable: Drawable private var dashSpacing = 0f private var maxNumDashes = 0 private var currentlyActiveDash = 0 @@ -53,34 +53,34 @@ class StepProgressBar( } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val height = activeDrawable!!.intrinsicHeight + paddingBottom + paddingTop + val height = activeDrawable.intrinsicHeight + paddingBottom + paddingTop setMeasuredDimension(widthMeasureSpec, height) } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // Centering the dots in the middle of the canvas - val singleDotSize = dashSpacing + activeDrawable!!.intrinsicWidth + val singleDotSize = dashSpacing + activeDrawable.intrinsicWidth val combinedDotSize = singleDotSize * maxNumDashes - dashSpacing val startingX = ((width - combinedDotSize) / 2).toInt() for (i in 0 until maxNumDashes) { val x = (startingX + i * singleDotSize).toInt() if (cumulativeDashes && i < currentlyActiveDash || i == currentlyActiveDash) { - activeDrawable!!.setBounds( + activeDrawable.setBounds( x, 0, - x + activeDrawable!!.intrinsicWidth, - activeDrawable!!.intrinsicHeight + x + activeDrawable.intrinsicWidth, + activeDrawable.intrinsicHeight ) - activeDrawable!!.draw(canvas) + activeDrawable.draw(canvas) } else { - inactiveDrawable!!.setBounds( + inactiveDrawable.setBounds( x, 0, - x + inactiveDrawable!!.intrinsicWidth, - inactiveDrawable!!.intrinsicHeight + x + inactiveDrawable.intrinsicWidth, + inactiveDrawable.intrinsicHeight ) - inactiveDrawable!!.draw(canvas) + inactiveDrawable.draw(canvas) } } }