Skip to content

Instantly share code, notes, and snippets.

View truedem's full-sized avatar

Pavel Kuznetsov truedem

View GitHub Profile
@cyb3rko
cyb3rko / CountryFlags.kt
Last active June 13, 2024 13:11
Get to flag unicode as String and use it anywhere. (Kotlin rewrite of https://gist.github.com/BurakDizlek/9eb54a0245a765dbdd312148f1c96d7f)
object CountryFlags {
private val A = getEmojiByUnicode(0x1F1E6)
private val B = getEmojiByUnicode(0x1F1E7)
private val C = getEmojiByUnicode(0x1F1E8)
private val D = getEmojiByUnicode(0x1F1E9)
private val E = getEmojiByUnicode(0x1F1EA)
private val F = getEmojiByUnicode(0x1F1EB)
private val G = getEmojiByUnicode(0x1F1EC)
private val H = getEmojiByUnicode(0x1F1ED)
private val I = getEmojiByUnicode(0x1F1EE)
@DJDarkByte
DJDarkByte / LifecycleRecyclerView.kt
Last active August 21, 2020 22:50
RecyclerView that cleans up it's mess instead of leaking
class LifecycleRecyclerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr), LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy() {
adapter = null
}
@truedem
truedem / ViewModelFactory.kt
Last active December 14, 2020 13:20
ViewModelFactory for Android Jetpack
class ViewModelFactory constructor(
private val baseRepository: BaseRepository, // your repository class to handle database, network, shared preferences, etc.
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
// usage in activity:
// val sharedVM by viewModels<MySharedViewModel> { ViewModelFactory(App.getRep(), this) }
// App.getRep() returns instance of BaseRepository, replace with your actual logic
@willyrh495
willyrh495 / EncryptionAES.kt
Created June 17, 2020 03:43
AES encryption using AndroidKeyStore
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import java.security.*
import javax.crypto.*
import javax.crypto.spec.GCMParameterSpec
import android.util.Base64
import androidx.annotation.VisibleForTesting
class EncryptionAES {
@ologe
ologe / PreferenceExtensions.kt
Last active November 13, 2024 21:10
Android shared preference observer using kotlin coroutines (1.3.0)
inline fun <reified T> SharedPreferences.observeKey(key: String, default: T): Flow<T> = channelFlow {
send(getItem(key, default))
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, k ->
if (key == k) {
trySend(getItem(key, default))
}
}
registerOnSharedPreferenceChangeListener(listener)
@iamnaran
iamnaran / activity_layout.xml
Last active July 30, 2024 12:55
Layout And RecyclerView Animation Android (Made Simple with XML only)
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout_animation"
android:orientation="vertical">
</ScrollView>
class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
@NestedScrollType
private var lastStartedType: Int = 0
private var offsetAnimator: ValueAnimator? = null
var isSnappingEnabled = false
// rest of the code
@paramsen
paramsen / build.groovy
Last active January 17, 2024 08:17
Generate Android version code from git count in Gradle (build.gradle)
// Generate a minor version code from git commit count (for prod builds)
static def generateVersionCode() {
def result = "git rev-list HEAD --count".execute().text.trim() //unix
if(result.empty) result = "PowerShell -Command git rev-list HEAD --count".execute().text.trim() //windows
if(result.empty) throw new RuntimeException("Could not generate versioncode on this platform? Cmd output: ${result.text}")
return result.toInteger()
}
def majorVersion = 1
@k-kagurazaka
k-kagurazaka / DebounceTest.kt
Created December 1, 2017 14:18
RxJava debounce like operator implementation for kotlin coroutine
launch(UI) {
editText.onTextChanged()
.debounce(1, TimeUnit.SECONDS)
.consumeEach {
Log.d("DebounceTest", "value: $it")
}
}
}
fun EditText.onTextChanged(): ReceiveChannel<String> =
@gfreivasc
gfreivasc / BackgroundService.java
Last active July 7, 2022 06:05
A BackgroundService class for Android
package com.gabrielfv.android.engine;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;