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
public Bitmap getScreenshotFromRecyclerView(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
@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 / AppExternalFileWriter.java
Last active February 25, 2023 06:45
File writer that helps you to write files in your own applocation directory in SD Card
package com.phtrivedi.opensource.externalfilewriter;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@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 / publish.gradle
Created August 27, 2015 07:24
Gradle file for copying release ready apk to desired directory.
def getReleasePath() {
//Define RELEASE_PATH anywhere(in properties file) and your apk + mapping file will be copied there.
return hasProperty('RELEASE_PATH') ? RELEASE_PATH : "${project.rootDir}\\Release"
}
android.applicationVariants.all { variant ->
//Mapping file for proguard path for each variant
def mappingFile = variant.variantData.mappingFile
@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 / 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 / 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 / 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