Skip to content

Instantly share code, notes, and snippets.

View Eldhopj's full-sized avatar
🏠
Working from home

Eldho P James Eldhopj

🏠
Working from home
View GitHub Profile
@Eldhopj
Eldhopj / PrefManager.kt
Last active August 17, 2020 18:21
Prefrence manager Kotlin
import android.annotation.SuppressLint
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
private const val COUNTRY_CODE = "COUNTRY_CODE"
private const val FIRST_TIME = "FIRST_TIME"
private const val THEME = "theme"
private const val PREVIOUS_USER = "PREVIOUS_USER"
class PrefManager(val context: Context, private val preferenceName: String = "eldhopj") {
@Eldhopj
Eldhopj / A.kt
Last active March 6, 2022 05:48
ActivityResult : Suppose ActivityResult & B are activities the navigation is from A -> B We need the result back, A <- B
// calling the Activity B
resultLauncher.launch(Intent(requireContext(), B::class.java))
// we get data in here from B
private var resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) {
Activity.RESULT_OK -> {
result.data?.getStringExtra("VALUE")?.let {
// data received here
@Eldhopj
Eldhopj / Callback.kt
Created August 11, 2022 04:41
suspendCoroutine { Convert callbacks into suspend functions }
private suspend fun singleValueEvent((ref: DatabaseReference ,onCancellation: CancellationCallback = {}),onCancellation: CancellationCallback = {}): DataResponse<DataSnapshot> {
return suspendCancellableCoroutine { continuation -> // can use suspendCoroutine not worry about cancellation
val valueEventListener = object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
continuation.resume(DataResponse.Error(error.toException()), onCancellation) // setting data
}
override fun onDataChange(snapshot: DataSnapshot) {
continuation.resume(DataResponse.Changed(snapshot), onCancellation)
}
@Eldhopj
Eldhopj / ConnectivityObserver.kt
Last active November 21, 2022 03:37
Internet Connectivity observer
interface ConnectivityObserver {
fun observe(): Flow<Status>
enum class Status {
Available, Unavailable, Losing, Lost
}
}