Skip to content

Instantly share code, notes, and snippets.

View devrath's full-sized avatar
💭
I'm grateful when for one drop in glass. Since I knw exactly what to do with it

Devrath devrath

💭
I'm grateful when for one drop in glass. Since I knw exactly what to do with it
View GitHub Profile
@devrath
devrath / AbstractSealedClass.kt
Created August 9, 2022 10:50
Defining the sealed class
sealed class Animal {
object Cat : Animal() // We can define a Object classes like this
data class Dog(val name: String) : Animal() // We can define data classes like this
}
@devrath
devrath / FakeUser.kt
Created August 3, 2022 15:01
Adding a fake user
data class FakeUser(
override val id: String = "",
override val firstName: String = "",
override val lastName: String = "",
override val email: String = "",
override val image: String = "",
override val mobile: Long = 0,
override val gender: String = "",
override val profileCompleted: Int = 0
) : UserAbstract(id,firstName,lastName,email,image,mobile,gender,profileCompleted)
@devrath
devrath / AbstractClass.kt
Created August 3, 2022 14:59
user class extending abstract class
/**
* A data model class for User with required fields.
*/
data class User(
override val id: String = "",
override val firstName: String = "",
override val lastName: String = "",
override val email: String = "",
override val image: String = "",
override val mobile: Long = 0,
@devrath
devrath / UserData.kt
Created August 3, 2022 14:51
Sample user class
data class User(
val id: String = "",
val firstName: String = "",
val lastName: String = "",
val email: String = "",
val image: String = "",
val mobile: Long = 0,
val gender: String = "",
val profileCompleted: Int = 0
)
@devrath
devrath / callBackFlowBuilder.kt
Created July 27, 2022 03:40
CallbackFlow builder
// https://medium.com/swlh/callback-flows-in-android-d2c6ed5bc488
fun TextView.textWatcherFlow(): Flow<CharSequence?> = callbackFlow<CharSequence?> {
val textWatcher = object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
sendBlocking(s)
}
// Other callback mehthods
}
addTextChangedListener(textWatcher)
awaitClose { removeTextChangedListener(textWatcher) }
@devrath
devrath / Definition.kt
Last active July 24, 2022 15:02
Here we have taken a example where firebase is taken as a service we are expecting a callback. In place of firebase it can be a normal API call or any other third party API call
// DEFINITION: -> Creates a CompletableDeferred in an active state. It is optionally a child of a parent job.
@Suppress("FunctionName")
public fun <T> CompletableDeferred(parent: Job? = null): CompletableDeferred<T> = CompletableDeferredImpl(parent)
@devrath
devrath / completeCallback.kt
Created July 24, 2022 12:37
Sample for callback returning from flow
fun readMessage(dialog: QBChatDialog): Flow<ArrayList<QBChatMessage>> {
val historyDeferred = CompletableDeferred<ArrayList<QBChatMessage>>()
chatHelper.ReadChatHistory(dialog, object : QBEntityCallback<ArrayList<QBChatMessage>> {
override fun onSuccess(listmessage: ArrayList<QBChatMessage>?, p1: Bundle?) {
historyDeferred.complete(listmessage ?: arrayListOf())
}
override fun onError(p0: QBResponseException?) {
historyDeferred.completeExceptionally(p0 ?: CancellationException())
}
@devrath
devrath / ConvertingFlow.kt
Created July 23, 2022 05:24
Gist for converting flow
fun getAllPosts() = flow<State<List<Post>>> {
// Emit loading state
emit(State.loading())
val snapshot = mPostsCollection.get().await()
val posts = snapshot.toObjects(Post::class.java)
// Emit success state with data
emit(State.success(posts))
public interface Element {
Boolean accept(Visitor visitor);
}
@devrath
devrath / Android Studio as Diff and Merge Tool
Created May 8, 2022 06:17
Snippet of how to use android studio as diff tool and merge tool
[merge]
tool = studio
[mergetool "studio"]
prompt = false
cmd = /Applications/Android\\ Studio.app/Contents/MacOS/studio merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
trustExitCode = true
[diff]
tool = studio
[difftool "studio"]
prompt = false