Skip to content

Instantly share code, notes, and snippets.

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

Prasham Trivedi PrashamTrivedi

🏠
Working from home
View GitHub Profile
@PrashamTrivedi
PrashamTrivedi / React-Redux.md
Created July 3, 2018 06:10
My notes on React redux and it's usages

Redux

  • Redux is a library that manages states. Based on functional principals it solves a problem of data flow in an interesting way.
  • In an app your data should have a unidirectional flow. The data flows forward, it never comes back.
  • A data with changed properties is not same data (Autovalue), it can be a different copy object.
  • In redux, you pass an action, A reducer listens to action and changes store connects both acion and reducer
  • actoin: Must have a string, can optionaly have any type of payload.
  • reducer: Must return a state, it can never throw error or return undefined thing.
    • In any error case or un-recognized action, it should return previous state passed.
  • store: Allows to subscribe/unsubscribe updates, can read current state at any time and sends actions to reducers
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.gradle.internal.os.OperatingSystem;
task(debugSomething) {
doLast {
println OperatingSystem.properties
}
@PrashamTrivedi
PrashamTrivedi / DbSchema.kt
Last active March 26, 2019 12:28
Some extension functions with room: Requires Export schema Read this section https://medium.com/google-developers/testing-room-migrations-be93cdb0d975#6872
import com.squareup.moshi.Json
data class DbSchema(@field:Json(name = "formatVersion") val formatVersion: Int? = 0, @field:Json(
name = "database") val database: Database? = Database())
data class Database(@field:Json(name = "version") val version: Int = 0, @field:Json(name = "identityHash") val identityHash: String? = "", @field:Json(
name = "entities") val entities: List<Entity?>? = listOf(), @field:Json(name = "setupQueries") val setupQueries: List<String?>? = listOf())
@PrashamTrivedi
PrashamTrivedi / CursorDelegate.kt
Created January 23, 2018 08:04
Handle cursor by delegates
inline fun <T> Cursor.delegate(key: String? = null,
crossinline getter: Cursor.(Int) -> T): ReadOnlyProperty<Any?, T> {
return object : ReadOnlyProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
val s = key ?: property.name
return getter(getColumnIndex(s))
}
}
@PrashamTrivedi
PrashamTrivedi / GlideDsl.kt
Created January 10, 2018 14:53
A rough Idea for using Glide as DSL....
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.widget.ImageView
import com.bumptech.glide.DrawableTypeRequest
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
@PrashamTrivedi
PrashamTrivedi / DownloadRequest.kt
Last active February 25, 2023 12:43
Download File with progress indicator, written in Kotlin with Co-routines
suspend fun downloadFile(url: String,
downloadFile: File,
downloadProgressFun: (bytesRead: Long, contentLength: Long, isDone: Boolean) -> Unit) {
async(CommonPool) {
val request = with(Request.Builder()) {
url(url)
}.build()
val client = with(OkHttpClient.Builder()) {
addNetworkInterceptor { chain ->
@PrashamTrivedi
PrashamTrivedi / LoginActivity.kt
Last active December 21, 2017 12:29
Files to reproduce databinding error in 3.1.0-alpha06
class LoginActivity : AppCompatActivity() {
@Inject lateinit var loginPresenter: LoginPresenter
lateinit var binding: ActivityLoginBinding
val loginViewModel: LoginViewmodel by lazy {
LoginViewmodel()
}
@PrashamTrivedi
PrashamTrivedi / ApiResponse.kt
Last active February 21, 2018 14:06
Code for blogpost. Koroutines, MVP+VM
sealed class ApiResponse<T> {
class Loading<Unit> : ApiResponse<Unit>()
class Nothing<Unit> : ApiResponse<Unit>()
class NoInternet<Unit> : ApiResponse<Unit>()
class ConnectionError<Unit>(val throwable: Throwable, val error: String) : ApiResponse<Unit>()
data class Success<T>(val response: Response<T>,
val data: T? = response.body(),
val headers: Headers? = response.headers()) : ApiResponse<T>()
data class SuccessData<T>(val data: T?) : ApiResponse<T>()
@PrashamTrivedi
PrashamTrivedi / io17Modified.xml
Created November 29, 2017 07:33
Modified intellij theme for io17, with Font Ligatures, Logcat colors and changed size
<scheme name="io17" version="142" parent_scheme="Darcula">
<option name="FONT_SCALE" value="1.0" />
<metaInfo>
<property name="created">2017-11-13T20:44:16</property>
<property name="ide">AndroidStudio</property>
<property name="ideVersion">3.0.0.18</property>
<property name="modified">2017-11-29T13:01:35</property>
<property name="originalScheme">Darcula</property>
</metaInfo>
<option name="EDITOR_FONT_SIZE" value="16" />
@PrashamTrivedi
PrashamTrivedi / Spaniard.kt
Last active September 14, 2017 08:16
Utils for creating spans with minimum texts
package com.spaniard
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.CharacterStyle
class Spaniard(val originalString: String) {
var spannableStringBuilder = SpannableStringBuilder(originalString)