PixelDroid-App-Android/app/src/main/java/com/h/pixeldroid/utils/Utils.kt

48 lines
1.5 KiB
Kotlin
Raw Normal View History

package com.h.pixeldroid.utils
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.net.ConnectivityManager
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
2020-09-10 20:20:23 +02:00
import com.h.pixeldroid.R
fun hasInternet(context: Context): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return cm.activeNetwork != null
}
fun normalizeDomain(domain: String): String {
return "https://" + domain
.replace("http://", "")
.replace("https://", "")
.trim(Char::isWhitespace)
}
/**
* @brief Updates the application's theme depending on the given preferences and resources
*/
fun setThemeFromPreferences(preferences: SharedPreferences, resources : Resources) {
val themes = resources.getStringArray(R.array.theme_values)
//Set the theme
when(preferences.getString("theme", "")) {
//Light
themes[1] -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
//Dark
themes[2] -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
else -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
2020-09-10 20:20:23 +02:00
}
}
}
}