Skip to content

Instantly share code, notes, and snippets.

View necatisozer's full-sized avatar
👨‍💻
You don't know me, you're about to...

Necati Sözer necatisozer

👨‍💻
You don't know me, you're about to...
View GitHub Profile
@ZakTaccardi
ZakTaccardi / NoInitialStateFlow.kt
Created May 9, 2020 01:26
NoInitialStateFlow.kt
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
/**
* A variant of [StateFlow] that allows an initial state
*/
interface NoInitialStateFlow<T> : Flow<T> {
/**
@LouisCAD
LouisCAD / Companion object Powers example.kt
Last active January 21, 2024 07:45
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
package i_love_companion_objects
suspend fun main() {
// Discoverability is easy with the companion object
// (although autocomplete could use some performance improvements...)
doStuff(SomeInterface.createWithDefaults())
val someInstance: SomeInterface = SomeInterface.create(
parameterOne = 0,
parameterTwo = 1
)
#your package name
import android.content.Intent
import android.util.SparseArray
import androidx.core.util.forEach
import androidx.core.util.set
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LiveData
sealed class Try<T> {
companion object {
operator fun <T> invoke(func: () -> T): Try<T> =
try {
Success(func())
} catch (error: Exception) {
Failure(error)
}
ext {
compileSDKVersionValue = 29
minSDKVersionValue = 22
targetSDKVersionValue = 29
libraries = [
cardView : 'androidx.cardview:cardview:1.0.0',
androidXLegacySupport : 'androidx.legacy:legacy-support-v4:1.0.0',
androidXAppCompat : 'androidx.appcompat:appcompat:1.1.0',
@marcellogalhardo
marcellogalhardo / ActivityScope.kt
Last active January 20, 2021 10:11
Scoping Dagger Components with ViewModels
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@sockeqwe
sockeqwe / AdapterDelegateDslExample.kt
Created July 22, 2019 08:19
AdapterDelegates Kotlin DSL
/**
* Let's say we want to display a list of Animals in a RecyclerView.
* The list can contain items of type Dog, Cat and Snake.
*/
// AdapterDelegate for Dog.
fun dogAdapterDelegate(picasso : Picasso) = adapterDelegate<Dog, Animal>(R.layout.item_dog){ // Generics Types means this AdapterDelegate is used if item is instanceof Dog (whole list is List<Anmial>)
// this block is run once in onCreateViewHolder. Think of it as an intializer for a ViewHolder.
val name = findViewById(R.id.name)
val image = findViewById(R.id.image)
open class BaseRepository{
suspend fun <T : Any> safeApiCall(call: suspend () -> Response<T>, errorMessage: String): T? {
val result : Result<T> = safeApiResult(call,errorMessage)
var data : T? = null
when(result) {
is Result.Success ->
data = result.data
sealed class Result<out T: Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}
@naturalwarren
naturalwarren / CoinbaseRxJava2CallAdapterFactory.kt
Last active March 16, 2022 04:58
A Kotlin-esque API for Retrofit.
/**
* Copyright 2019 Coinbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,