Skip to content

Instantly share code, notes, and snippets.

View addeeandra's full-sized avatar
🧭
I am, therefore I am.

Aditya Chandra addeeandra

🧭
I am, therefore I am.
View GitHub Profile
@addeeandra
addeeandra / Event.kt
Last active January 31, 2021 15:43
ViewModel SingleLiveEvent - Wraps the MutableLiveData<ActionEvent<T>>
class Event<out T>(private val content: T) {
var hasBeenUsed = false
private set
fun getContentIfNotUsed(): T? {
return if (hasBeenUsed) {
null
} else {
hasBeenUsed = true
@addeeandra
addeeandra / build.gradle
Last active January 13, 2020 08:32
Default libs
buildScript {
ext {
def appcompat_version = '1.1.0'
def constraint_layout_version = '1.1.3'
def coroutines_android_version = '1.3.2'
def reflect_android_version = '1.3.61'
def lifecycle_version = '2.1.0'
def livedata_version = '2.2.0-rc02'
def material_version = '1.0.0'
def security_version = '1.0.0-alpha02'
@addeeandra
addeeandra / SecurePreferences.kt
Created January 13, 2020 08:48
SharedPreferences wrapper in secure way with encryption
import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import com.example.app.BuildConfig
class SecurePreferences(context: Context) {
private val _encryptedSharedPreferences: SharedPreferences
@addeeandra
addeeandra / SimpleTextWatcher.kt
Created February 18, 2020 07:18
A simple TextWatcher, cause you don't need all of its functions
import android.text.Editable
import android.text.TextWatcher
abstract class SimpleTextWatcher : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
@addeeandra
addeeandra / DatePickerBindingAdapter.kt
Last active October 21, 2022 09:16
Binding Adapter of Spinner, DatePicker, View, TextView, RecyclerView and SwipeRefreshLayout
import android.widget.DatePicker
import android.widget.Spinner
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import java.util.*
@BindingAdapter(value = ["selectedDate", "selectedDateAttrChanged"], requireAll = false)
fun DatePicker.selectedDate(
newSelectedDate: Date?,
@addeeandra
addeeandra / covid19map.html
Created April 13, 2020 14:14
Experiment with covid19api + Google Map
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Covid19 Cases in Countries</title>
</head>
<body style="padding: 0; margin: 0;">
<!-- Peta -->
@addeeandra
addeeandra / Validator.kt
Last active April 23, 2020 15:33
Fields Validator (idea)
class Validator {
companion object {
val EMAIL_REGEX =
Regex("(?:[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
val NUMBER_REGEX = Regex("^[0-9]+$")
}
enum class Field {
@addeeandra
addeeandra / APIManager.java
Created April 22, 2020 13:36 — forked from sideffect0/APIManager.java
Retrofit Client with Persistent Cookie
class APIManager {
// client with persistent Cookie
public static OkHttpClient getRetrofitClient(final Context context) {
OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager(new PersistentCookieStore(context), CookiePolicy.ACCEPT_ALL);
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
return client;
}
@addeeandra
addeeandra / AppPagedRecyclerViewAdapter.kt
Last active June 7, 2021 05:43
Simple (Paged) RecyclerViewAdapter
abstract class AppPagedRecyclerViewAdapter<BIND : ViewDataBinding, M : Any>(
diffUtil: DiffUtil.ItemCallback<M>
) : PagedListAdapter<M, AppPagedRecyclerViewAdapter.ViewHolder<BIND>>(diffUtil) {
abstract fun onCreateViewBindingHolder(inflater: LayoutInflater, parent: ViewGroup): BIND
abstract fun onPrepareBindViewHolder(binding: BIND, model: M)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<BIND> {
val inflater = LayoutInflater.from(parent.context)
@addeeandra
addeeandra / CombinedTransformation.kt
Created May 14, 2020 14:19
Combined Transformation of LiveData(s) using MediatorLiveData
class CombinedTransformation<T>(
vararg liveData: LiveData<*>,
private val onAllChanged: (data: List<Any?>) -> T
) : MediatorLiveData<T>() {
private val mLiveDataList: MutableList<Any?> = MutableList(liveData.size) { null }
init {
liveData.forEachIndexed { index, liveDatum ->
super.addSource(liveDatum) { datum ->