mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-03 11:17:25 +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
|
||||
|
||||
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()
|
||||
}
|
||||
Reference in new issue
Block a user