mirror of
https://github.com/dzeiocom/OpenHealth.git
synced 2025-06-07 07:19:54 +00:00
misc: Cleanup Utils/Units/Workers and Application/Settings files
This commit is contained in:
parent
eb2c6f7c4c
commit
86e127f0bd
@ -19,6 +19,7 @@ class Application : Application() {
|
|||||||
|
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
|
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
|
||||||
|
// setup the CrashHandler
|
||||||
CrashHandler.Builder()
|
CrashHandler.Builder()
|
||||||
.withActivity(ErrorActivity::class.java)
|
.withActivity(ErrorActivity::class.java)
|
||||||
.withPrefs(prefs)
|
.withPrefs(prefs)
|
||||||
@ -28,12 +29,15 @@ class Application : Application() {
|
|||||||
.build()
|
.build()
|
||||||
.setup(this)
|
.setup(this)
|
||||||
|
|
||||||
// Android Dynamics Colors
|
// setup for Android Dynamics Colors
|
||||||
DynamicColors.applyToActivitiesIfAvailable(this)
|
DynamicColors.applyToActivitiesIfAvailable(this)
|
||||||
|
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the language of the application if said in the settings
|
||||||
|
*/
|
||||||
override fun attachBaseContext(base: Context) {
|
override fun attachBaseContext(base: Context) {
|
||||||
super.attachBaseContext(LocaleUtils.onAttach(base))
|
super.attachBaseContext(LocaleUtils.onAttach(base))
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.dzeio.openhealth
|
package com.dzeio.openhealth
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object containing every keys for the different settings of the application
|
||||||
|
*/
|
||||||
object Settings {
|
object Settings {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
package com.dzeio.openhealth.units
|
|
||||||
|
|
||||||
object UnitFactory {
|
|
||||||
@Deprecated("Move to Units.Mass.find")
|
|
||||||
fun mass(unit: String): Units.Mass {
|
|
||||||
return when (unit.lowercase()) {
|
|
||||||
"kilogram", "kilograms", "kg" -> Units.Mass.KILOGRAM
|
|
||||||
"pound", "pounds", "lb" -> Units.Mass.POUND
|
|
||||||
else -> Units.Mass.KILOGRAM
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Move to Units.Volume.find")
|
|
||||||
fun volume(unit: String): Units.Volume {
|
|
||||||
return when (unit.lowercase()) {
|
|
||||||
"milliliter", "milliliters", "ml" -> Units.Volume.MILLILITER
|
|
||||||
"imperial ounce", "imperial ounces", "oz" -> Units.Volume.IMPERIAL_OUNCE
|
|
||||||
"us ounce", "us ounces" -> Units.Volume.US_OUNCE
|
|
||||||
else -> Units.Volume.MILLILITER
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,13 @@ package com.dzeio.openhealth.units
|
|||||||
|
|
||||||
import com.dzeio.openhealth.R
|
import com.dzeio.openhealth.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object containing the differents units and how they are converted
|
||||||
|
*/
|
||||||
object Units {
|
object Units {
|
||||||
|
/**
|
||||||
|
* the Mass Unit
|
||||||
|
*/
|
||||||
enum class Mass(
|
enum class Mass(
|
||||||
val id: String,
|
val id: String,
|
||||||
/**
|
/**
|
||||||
@ -41,11 +47,17 @@ object Units {
|
|||||||
return value * modifier
|
return value * modifier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format the value and let the hundred of grams to be outputed
|
||||||
|
*/
|
||||||
fun formatToString(value: Float): String {
|
fun formatToString(value: Float): String {
|
||||||
return String.format("%.1f", value * modifier)
|
return String.format("%.1f", value * modifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the Volume unit
|
||||||
|
*/
|
||||||
enum class Volume(
|
enum class Volume(
|
||||||
val id: String,
|
val id: String,
|
||||||
/**
|
/**
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
package com.dzeio.openhealth.units
|
|
||||||
|
|
||||||
@Deprecated("Move to Units.Volume")
|
|
||||||
enum class WaterUnit(
|
|
||||||
val unit: String,
|
|
||||||
val fromML: Float
|
|
||||||
) {
|
|
||||||
ML("ml", 1f),
|
|
||||||
US_OZ("oz", 0.03381413f),
|
|
||||||
IMP_OZ("oz", 0.03519503f);
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun fromSettings(value: String): WaterUnit {
|
|
||||||
return when (value.lowercase()) {
|
|
||||||
"milliliter" -> ML
|
|
||||||
"us ounce" -> US_OZ
|
|
||||||
"imperial ounce" -> IMP_OZ
|
|
||||||
else -> ML
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,7 +9,9 @@ import java.nio.channels.FileChannel
|
|||||||
object BitmapUtils {
|
object BitmapUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find source of function lol
|
* Stolen from StackOverflow but I don't remember where...
|
||||||
|
*
|
||||||
|
* Convert an immutable Bitmap to a mutable one if possible
|
||||||
*/
|
*/
|
||||||
fun convertToMutable(context: Context, imgIn: Bitmap): Bitmap? {
|
fun convertToMutable(context: Context, imgIn: Bitmap): Bitmap? {
|
||||||
val width = imgIn.width
|
val width = imgIn.width
|
||||||
@ -46,4 +48,4 @@ object BitmapUtils {
|
|||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,11 @@ import androidx.core.content.edit
|
|||||||
import com.dzeio.openhealth.Application
|
import com.dzeio.openhealth.Application
|
||||||
import com.dzeio.openhealth.core.Observable
|
import com.dzeio.openhealth.core.Observable
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that ease the SharedPreferences works
|
||||||
|
*
|
||||||
|
* It allow to use others types simpler and to export them as LiveData for realtime updates
|
||||||
|
*/
|
||||||
class Configuration(
|
class Configuration(
|
||||||
private val prefs: SharedPreferences
|
private val prefs: SharedPreferences
|
||||||
) : SharedPreferences.OnSharedPreferenceChangeListener {
|
) : SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
@ -7,6 +7,13 @@ import android.util.Log
|
|||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stolen from StackOverflow https://stackoverflow.com/a/10868126/7335674
|
||||||
|
*
|
||||||
|
* Allows to download an image asynchronously
|
||||||
|
*
|
||||||
|
* TODO: rework so it is not deprecated anymore
|
||||||
|
*/
|
||||||
class DownloadImageTask(var bmImage: ImageView) :
|
class DownloadImageTask(var bmImage: ImageView) :
|
||||||
AsyncTask<String?, Void?, Bitmap?>() {
|
AsyncTask<String?, Void?, Bitmap?>() {
|
||||||
@Deprecated("Deprecated in Java")
|
@Deprecated("Deprecated in Java")
|
||||||
|
@ -4,10 +4,13 @@ import android.graphics.Canvas
|
|||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.RectF
|
import android.graphics.RectF
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils class to draw more complexe elements on a canvas
|
||||||
|
*/
|
||||||
object DrawUtils {
|
object DrawUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fuck Graphics
|
* Draw an arc on a canvas or a the previous comment was "Fuck Graphics"
|
||||||
*/
|
*/
|
||||||
fun drawArc(canvas: Canvas, percent: Float, rect: RectF, pColor: Int, strokeWidth: Float = 1f) {
|
fun drawArc(canvas: Canvas, percent: Float, rect: RectF, pColor: Int, strokeWidth: Float = 1f) {
|
||||||
val r1 = RectF(
|
val r1 = RectF(
|
||||||
@ -26,25 +29,6 @@ object DrawUtils {
|
|||||||
canvas.drawArc(r1, 180f, 180 * percent / 100f, false, paint)
|
canvas.drawArc(r1, 180f, 180 * percent / 100f, false, paint)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fuck Graphics
|
|
||||||
*/
|
|
||||||
fun drawRect(canvas: Canvas, rect: RectF, pColor: Int, strokeWidth: Float = 1f) {
|
|
||||||
val r1 = RectF(
|
|
||||||
canvas.realSize(true, rect.left),
|
|
||||||
canvas.realSize(false, rect.top),
|
|
||||||
canvas.realSize(true, rect.right),
|
|
||||||
canvas.realSize(false, rect.bottom)
|
|
||||||
)
|
|
||||||
val paint = Paint().apply {
|
|
||||||
this.strokeWidth = canvas.realSize(true, strokeWidth)
|
|
||||||
style = Paint.Style.STROKE
|
|
||||||
color = pColor
|
|
||||||
isAntiAlias = true
|
|
||||||
}
|
|
||||||
canvas.drawRect(r1, paint)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Canvas.realSize(isWidth: Boolean, value: Float): Float {
|
private fun Canvas.realSize(isWidth: Boolean, value: Float): Float {
|
||||||
val it = if (isWidth) this.width else this.height
|
val it = if (isWidth) this.width else this.height
|
||||||
return it * value / 100
|
return it * value / 100
|
||||||
@ -55,4 +39,4 @@ object DrawUtils {
|
|||||||
return it * value / 100
|
return it * value / 100
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,11 @@ import java.text.SimpleDateFormat
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils object to create graphics using the mikephil charting library
|
||||||
|
*
|
||||||
|
* TODO: migrate to DzeioCharts once it is ready
|
||||||
|
*/
|
||||||
object GraphUtils {
|
object GraphUtils {
|
||||||
|
|
||||||
fun lineChartSetup(chart: LineChart, mainColor: Int, textColor: Int) {
|
fun lineChartSetup(chart: LineChart, mainColor: Int, textColor: Int) {
|
||||||
@ -96,16 +101,4 @@ object GraphUtils {
|
|||||||
// return super.getAxisLabel(value, axis)
|
// return super.getAxisLabel(value, axis)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DateTimeValueFormatter(
|
|
||||||
private val transformer: Int = 1
|
|
||||||
) : ValueFormatter() {
|
|
||||||
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
|
|
||||||
return SimpleDateFormat(
|
|
||||||
"yyyy-MM-dd hh",
|
|
||||||
Locale.getDefault()
|
|
||||||
).format(Date(value.toLong() * transformer))
|
|
||||||
// return super.getAxisLabel(value, axis)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import android.content.Context
|
|||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.LocaleList
|
import android.os.LocaleList
|
||||||
import android.util.Log
|
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.dzeio.openhealth.Settings
|
import com.dzeio.openhealth.Settings
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
@ -44,18 +43,19 @@ object LocaleUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getPersistedData(context: Context): String {
|
private fun getPersistedData(context: Context): String {
|
||||||
|
// TODO: use the config class
|
||||||
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
return preferences.getString(Settings.APP_LANGUAGE, Locale.getDefault().language)
|
return preferences.getString(Settings.APP_LANGUAGE, Locale.getDefault().language)
|
||||||
?: Locale.getDefault().language
|
?: Locale.getDefault().language
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun persist(context: Context, language: String?) {
|
private fun persist(context: Context, language: String?) {
|
||||||
|
// TODO: use the config class
|
||||||
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
preferences.edit().putString(Settings.APP_LANGUAGE, language).apply()
|
preferences.edit().putString(Settings.APP_LANGUAGE, language).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateResources(context: Context, language: String): Context {
|
private fun updateResources(context: Context, language: String): Context {
|
||||||
Log.d("LocaleUtils", language)
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
val locale = Locale(language)
|
val locale = Locale(language)
|
||||||
Locale.setDefault(locale)
|
Locale.setDefault(locale)
|
||||||
@ -73,4 +73,4 @@ object LocaleUtils {
|
|||||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,14 @@ import android.content.pm.PackageManager
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Utils class to check for permissions in multiple android environments
|
||||||
|
*/
|
||||||
object PermissionsManager {
|
object PermissionsManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow to check for one permission
|
||||||
|
*/
|
||||||
fun hasPermission(context: Context, permission: String): Boolean =
|
fun hasPermission(context: Context, permission: String): Boolean =
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
|
context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
|
||||||
@ -17,6 +23,9 @@ object PermissionsManager {
|
|||||||
) == PackageManager.PERMISSION_GRANTED
|
) == PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for multiple permissions at once
|
||||||
|
*/
|
||||||
fun hasPermission(context: Context, permissions: Array<String>): Boolean {
|
fun hasPermission(context: Context, permissions: Array<String>): Boolean {
|
||||||
for (permission in permissions) {
|
for (permission in permissions) {
|
||||||
val res = hasPermission(context, permission)
|
val res = hasPermission(context, permission)
|
||||||
|
@ -6,7 +6,16 @@ import android.content.Intent
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.dzeio.openhealth.ui.MainActivity
|
import com.dzeio.openhealth.ui.MainActivity
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils class for services
|
||||||
|
*/
|
||||||
object ServiceUtils {
|
object ServiceUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that start a service
|
||||||
|
*
|
||||||
|
* If I remember correctly I stole this from StackOverflow... TODO: get the original URL
|
||||||
|
*/
|
||||||
fun <T> startService(context: Context, service: Class<T>) {
|
fun <T> startService(context: Context, service: Class<T>) {
|
||||||
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||||
for (runninService in activityManager.getRunningServices(Integer.MAX_VALUE)) {
|
for (runninService in activityManager.getRunningServices(Integer.MAX_VALUE)) {
|
||||||
@ -18,4 +27,4 @@ object ServiceUtils {
|
|||||||
Log.i(MainActivity.TAG, "Starting service ${service.name}")
|
Log.i(MainActivity.TAG, "Starting service ${service.name}")
|
||||||
Intent(context, service).also { intent -> context.startService(intent) }
|
Intent(context, service).also { intent -> context.startService(intent) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,11 @@ import android.util.Log
|
|||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TOTALLY BASED on stolen code from stackoverflow that I don't remember where... f*ck
|
||||||
|
*
|
||||||
|
* Basically it allows to change the way `EditTextPreference` works to allow only numbers
|
||||||
|
*/
|
||||||
class IntEditTextPreference : EditTextPreference, EditTextPreference.OnBindEditTextListener {
|
class IntEditTextPreference : EditTextPreference, EditTextPreference.OnBindEditTextListener {
|
||||||
|
|
||||||
private var txt: String? = null
|
private var txt: String? = null
|
||||||
|
@ -17,6 +17,9 @@ import com.dzeio.openhealth.interfaces.NotificationIds
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The worker that remind the user to drink water hourly
|
||||||
|
*/
|
||||||
class WaterReminderWorker(
|
class WaterReminderWorker(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
params: WorkerParameters
|
params: WorkerParameters
|
||||||
@ -24,6 +27,10 @@ class WaterReminderWorker(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "${Application.TAG}/WaterWorker"
|
const val TAG = "${Application.TAG}/WaterWorker"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* functio nthat setup the worker
|
||||||
|
*/
|
||||||
fun setup(context: Context) {
|
fun setup(context: Context) {
|
||||||
schedule(
|
schedule(
|
||||||
TAG,
|
TAG,
|
||||||
@ -37,6 +44,8 @@ class WaterReminderWorker(
|
|||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
Log.d(TAG, "Ran! ${Date().toLocaleString()}")
|
Log.d(TAG, "Ran! ${Date().toLocaleString()}")
|
||||||
|
|
||||||
|
// send a new notification to the user
|
||||||
with(NotificationManagerCompat.from(context)) {
|
with(NotificationManagerCompat.from(context)) {
|
||||||
val flag =
|
val flag =
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else 0
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else 0
|
||||||
|
@ -58,11 +58,10 @@
|
|||||||
|
|
||||||
<PreferenceCategory android:title="Steps settings">
|
<PreferenceCategory android:title="Steps settings">
|
||||||
<com.dzeio.openhealth.utils.fields.IntEditTextPreference
|
<com.dzeio.openhealth.utils.fields.IntEditTextPreference
|
||||||
|
android:inputType="number"
|
||||||
android:key="com.dzeio.open-health.steps.goal-daily"
|
android:key="com.dzeio.open-health.steps.goal-daily"
|
||||||
android:title="Number of steps each steps"
|
|
||||||
android:selectAllOnFocus="true"
|
android:selectAllOnFocus="true"
|
||||||
android:singleLine="true"
|
android:singleLine="true"
|
||||||
android:inputType="number"
|
android:title="Number of steps each days" />
|
||||||
/>
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user