Extensions.kt cleanup, dependencies update

This commit is contained in:
Arty Bishop committed 2021-11-28 12:17:00 +00:00
1 parent f694eb4f4d
commit 891721bf45
3 files changed
+32 -56

No files matched your search

@@ -18,7 +18,6 @@
package com.rtbishop.look4sat.presentation
import android.os.Bundle
import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
@@ -27,52 +26,40 @@ import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
import com.rtbishop.look4sat.R
import java.security.MessageDigest
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
fun NavController.navigateSafe(
@IdRes resId: Int,
args: Bundle? = null,
navOptions: NavOptions? = null,
navExtras: Navigator.Extras? = null
options: NavOptions? = null,
extras: Navigator.Extras? = null
) {
val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
if (action != null && currentDestination?.id != action.destinationId) {
navigate(resId, args, navOptions, navExtras)
navigate(resId, args, options, extras)
}
}
fun <T> Fragment.getNavResult(@IdRes id: Int, key: String, onResult: (result: T) -> Unit) {
val backStackEntry = findNavController().getBackStackEntry(id)
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME && backStackEntry.savedStateHandle.contains(key)) {
backStackEntry.savedStateHandle.get<T>(key)?.let(onResult)
backStackEntry.savedStateHandle.remove<T>(key)
}
}
backStackEntry.lifecycle.addObserver(observer)
viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_DESTROY) backStackEntry.lifecycle.removeObserver(observer)
})
}
fun <T> Fragment.setNavResult(key: String, value: T) {
findNavController().previousBackStackEntry?.savedStateHandle?.set(key, value)
}
fun <T> Fragment.getNavResult(@IdRes id: Int, key: String, onResult: (result: T) -> Unit) {
val navBackStackEntry = findNavController().getBackStackEntry(id)
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME
&& navBackStackEntry.savedStateHandle.contains(key)
) {
val result = navBackStackEntry.savedStateHandle.get<T>(key)
result?.let(onResult)
navBackStackEntry.savedStateHandle.remove<T>(key)
}
}
navBackStackEntry.lifecycle.addObserver(observer)
viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_DESTROY) {
navBackStackEntry.lifecycle.removeObserver(observer)
}
})
}
fun Int.toDate(): Date = Date(this.toLong() * 1000L)
fun Long.toTimerString(): String {
val format = "%02d:%02d:%02d"
val hours = TimeUnit.MILLISECONDS.toHours(this)
@@ -87,32 +74,20 @@ fun Double.round(decimals: Int): Double {
return kotlin.math.round(this * multiplier) / multiplier
}
fun Date.toString(format: String): String {
val dateFormatter = SimpleDateFormat(format, Locale.US)
return dateFormatter.format(this)
}
fun String.toDate(format: String): Date? {
return try {
return SimpleDateFormat(format, Locale.US).parse(this)
} catch (e: ParseException) {
null
fun String.getHash(type: String = "SHA-256"): String {
val hexChars = "0123456789ABCDEF"
val bytes = MessageDigest.getInstance(type).digest(this.toByteArray())
val result = StringBuilder(bytes.size * 2)
bytes.forEach {
val i = it.toInt()
result.append(hexChars[i shr 4 and 0x0f])
result.append(hexChars[i and 0x0f])
}
}
fun String.md5(): String {
val bytes = MessageDigest.getInstance("MD5").digest(this.toByteArray())
return bytes.joinToString("") { "%02x".format(it) }
}
fun String.sha1(): String {
val bytes = MessageDigest.getInstance("SHA-1").digest(this.toByteArray())
return bytes.joinToString("") { "%02x".format(it) }
return result.toString()
}
fun String.isEmailValid(): Boolean {
val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$"
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(this)
return matcher.matches()
return pattern.matcher(this).matches()
}
+3 -2
View File
@@ -1,20 +1,21 @@
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.Look4Sat.SplashScreen" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/greyDark</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_drawable</item>
<item name="postSplashScreenTheme">@style/Theme.Look4Sat.Main</item>
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
<style name="Theme.Look4Sat.Main" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/themeLight</item>
<item name="colorSecondary">@color/themeLight</item>
<item name="colorAccent">@color/themeLight</item>
<item name="android:textColorPrimary">@color/greyLight</item>
<item name="android:statusBarColor">@color/greyDark</item>
<item name="android:windowBackground">@color/greyDark</item>
<item name="android:navigationBarColor">@color/greyDark</item>
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
<style name="TopBar">
+2 -2
View File
@@ -9,12 +9,12 @@ buildscript {
navigation_version = '2.3.5'
preference_version = '1.1.1'
room_version = '2.3.0'
hilt_version = '2.40.1'
hilt_version = '2.40.2'
json_version = '20210307'
osmdroid_version = '6.1.11'
timber_version = '5.0.1'
junit_version = '4.13.2'
mockito_version = '4.0.0'
mockito_version = '4.1.0'
leak_canary_version = '2.7'
}
repositories {