mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Extensions.kt cleanup, dependencies update
This commit is contained in:
1 parent
f694eb4f4d
commit
891721bf45
3 files changed
+32
-56
No files matched your search
@@ -18,7 +18,6 @@
|
|||||||
package com.rtbishop.look4sat.presentation
|
package com.rtbishop.look4sat.presentation
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
|
||||||
import androidx.annotation.IdRes
|
import androidx.annotation.IdRes
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
@@ -27,52 +26,40 @@ import androidx.navigation.NavController
|
|||||||
import androidx.navigation.NavOptions
|
import androidx.navigation.NavOptions
|
||||||
import androidx.navigation.Navigator
|
import androidx.navigation.Navigator
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import com.google.android.material.snackbar.Snackbar
|
|
||||||
import com.rtbishop.look4sat.R
|
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
import java.text.ParseException
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.*
|
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
fun NavController.navigateSafe(
|
fun NavController.navigateSafe(
|
||||||
@IdRes resId: Int,
|
@IdRes resId: Int,
|
||||||
args: Bundle? = null,
|
args: Bundle? = null,
|
||||||
navOptions: NavOptions? = null,
|
options: NavOptions? = null,
|
||||||
navExtras: Navigator.Extras? = null
|
extras: Navigator.Extras? = null
|
||||||
) {
|
) {
|
||||||
val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
|
val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
|
||||||
if (action != null && currentDestination?.id != action.destinationId) {
|
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) {
|
fun <T> Fragment.setNavResult(key: String, value: T) {
|
||||||
findNavController().previousBackStackEntry?.savedStateHandle?.set(key, value)
|
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 {
|
fun Long.toTimerString(): String {
|
||||||
val format = "%02d:%02d:%02d"
|
val format = "%02d:%02d:%02d"
|
||||||
val hours = TimeUnit.MILLISECONDS.toHours(this)
|
val hours = TimeUnit.MILLISECONDS.toHours(this)
|
||||||
@@ -87,32 +74,20 @@ fun Double.round(decimals: Int): Double {
|
|||||||
return kotlin.math.round(this * multiplier) / multiplier
|
return kotlin.math.round(this * multiplier) / multiplier
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Date.toString(format: String): String {
|
fun String.getHash(type: String = "SHA-256"): String {
|
||||||
val dateFormatter = SimpleDateFormat(format, Locale.US)
|
val hexChars = "0123456789ABCDEF"
|
||||||
return dateFormatter.format(this)
|
val bytes = MessageDigest.getInstance(type).digest(this.toByteArray())
|
||||||
}
|
val result = StringBuilder(bytes.size * 2)
|
||||||
|
bytes.forEach {
|
||||||
fun String.toDate(format: String): Date? {
|
val i = it.toInt()
|
||||||
return try {
|
result.append(hexChars[i shr 4 and 0x0f])
|
||||||
return SimpleDateFormat(format, Locale.US).parse(this)
|
result.append(hexChars[i and 0x0f])
|
||||||
} catch (e: ParseException) {
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
}
|
return result.toString()
|
||||||
|
|
||||||
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) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun String.isEmailValid(): Boolean {
|
fun String.isEmailValid(): Boolean {
|
||||||
val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$"
|
val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$"
|
||||||
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
|
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
|
||||||
val matcher = pattern.matcher(this)
|
return pattern.matcher(this).matches()
|
||||||
return matcher.matches()
|
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,21 @@
|
|||||||
<resources>
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<style name="Theme.Look4Sat.SplashScreen" parent="Theme.SplashScreen">
|
<style name="Theme.Look4Sat.SplashScreen" parent="Theme.SplashScreen">
|
||||||
<item name="windowSplashScreenBackground">@color/greyDark</item>
|
<item name="windowSplashScreenBackground">@color/greyDark</item>
|
||||||
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_drawable</item>
|
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_drawable</item>
|
||||||
<item name="postSplashScreenTheme">@style/Theme.Look4Sat.Main</item>
|
<item name="postSplashScreenTheme">@style/Theme.Look4Sat.Main</item>
|
||||||
|
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="Theme.Look4Sat.Main" parent="Theme.AppCompat.NoActionBar">
|
<style name="Theme.Look4Sat.Main" parent="Theme.AppCompat.NoActionBar">
|
||||||
<item name="colorPrimary">@color/themeLight</item>
|
<item name="colorPrimary">@color/themeLight</item>
|
||||||
<item name="colorSecondary">@color/themeLight</item>
|
<item name="colorSecondary">@color/themeLight</item>
|
||||||
<item name="colorAccent">@color/themeLight</item>
|
<item name="colorAccent">@color/themeLight</item>
|
||||||
|
|
||||||
<item name="android:textColorPrimary">@color/greyLight</item>
|
<item name="android:textColorPrimary">@color/greyLight</item>
|
||||||
<item name="android:statusBarColor">@color/greyDark</item>
|
<item name="android:statusBarColor">@color/greyDark</item>
|
||||||
<item name="android:windowBackground">@color/greyDark</item>
|
<item name="android:windowBackground">@color/greyDark</item>
|
||||||
<item name="android:navigationBarColor">@color/greyDark</item>
|
<item name="android:navigationBarColor">@color/greyDark</item>
|
||||||
|
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="TopBar">
|
<style name="TopBar">
|
||||||
|
|||||||
+2
-2
@@ -9,12 +9,12 @@ buildscript {
|
|||||||
navigation_version = '2.3.5'
|
navigation_version = '2.3.5'
|
||||||
preference_version = '1.1.1'
|
preference_version = '1.1.1'
|
||||||
room_version = '2.3.0'
|
room_version = '2.3.0'
|
||||||
hilt_version = '2.40.1'
|
hilt_version = '2.40.2'
|
||||||
json_version = '20210307'
|
json_version = '20210307'
|
||||||
osmdroid_version = '6.1.11'
|
osmdroid_version = '6.1.11'
|
||||||
timber_version = '5.0.1'
|
timber_version = '5.0.1'
|
||||||
junit_version = '4.13.2'
|
junit_version = '4.13.2'
|
||||||
mockito_version = '4.0.0'
|
mockito_version = '4.1.0'
|
||||||
leak_canary_version = '2.7'
|
leak_canary_version = '2.7'
|
||||||
}
|
}
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
Reference in new issue
Block a user