Skip to content

Instantly share code, notes, and snippets.

View savvasdalkitsis's full-sized avatar

Savvas Dalkitsis savvasdalkitsis

View GitHub Profile
@savvasdalkitsis
savvasdalkitsis / setup.sh
Last active November 23, 2023 21:39
Usefull cli tools
# Commands installed
# tldr - better man
# scc - code counter
# duf - disk usage
# ctop - top for containers
# lazygit - git client
# fx - json viewer
# mc - midnight commander
# autojump (j) - jump to directories
LazyStaggeredGrid(columnCount = 2) {
(0..100).forEach { _ ->
item {
Box(modifier = Modifier
.fillMaxWidth()
.aspectRatio(ratio = random.nextDouble(0.2, 1.8).toFloat())
.background(Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)))
)
}
}
@savvasdalkitsis
savvasdalkitsis / blog_post_ask_for_photo.kt
Last active November 18, 2022 13:38
blog_post_ask_for_photo
// This requires NO permissions (camera, storage etc)
fun askForPhoto(activity: Activity, imageRequest: Int) {
val dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val picture = File(dir, UUID.randomUUID().toString())
// keep this. Will be used later in the onActivityResult method
photoUri = FileProvider.getUriForFile(this, "com.savvasdalkitsis.fileprovider", picture)
val photoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(photoIntent, imageRequest)
}
@savvasdalkitsis
savvasdalkitsis / blog_post_exif_data.kt
Last active November 18, 2022 13:38
blog_post_exif_data
fun getLocation(photoUri: Uri): Pair<Double, Double> {
contentResolver.openInputStream(photoUri).use { stream ->
val latLon = ExifInterface(stream).latLong
return Pair(latLon[0], latLon[1])
}
}
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_9.kt
Last active February 24, 2018 20:21
medium_post_fresco_9
val frescoOkHttpClient = OkHttpClient.Builder()
.connectTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(StaleIfErrorInterceptor())
.cache(newCache("fresco_cache", 100, ByteConstants.MB))
.build()
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_8.kt
Last active February 24, 2018 19:58
medium_post_fresco_8
class StaleIfErrorInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
return try {
chain.proceed(request.build())
} catch (e: IOException) {
Log.w(TAG, "Error on network call. Fallback to cache if present.", e)
chain.proceed(request
.cacheControl(CacheControl.FORCE_CACHE)
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_7.kt
Last active February 24, 2018 20:19
medium_post_fresco_7
val frescoOkHttpClient = OkHttpClient.Builder()
.connectTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.SECONDS)
.cache(newCache("fresco_cache", 100, ByteConstants.MB))
.build()
 
fun newCache(name: String, mb: Long, unit: Int): Cache =
Cache(File(applicationContext.cacheDir, name), mb * unit)
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_6.kt
Last active February 24, 2018 20:16
medium_post_fresco_6
val imagePipelineConfig = ImagePipelineConfig.newBuilder(context)
.setNetworkFetcher(OkHttpNetworkFetcherWithCache(frescoOkHttpClient))
// no way to disable the disk cache globally, setting everything 
// no to max 0 bytes
.setMainDiskCacheConfig(DiskCacheConfig.newBuilder(this)
.setMaxCacheSize(0)
.setMaxCacheSizeOnLowDiskSpace(0)
.setMaxCacheSizeOnVeryLowDiskSpace(0)
.build())
.build()
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_5.kt
Last active February 24, 2018 20:15
medium_post_fresco_5
/**
* The default [OkHttpNetworkFetcher] injects a ‘Cache-Control: no-store’ 
* header in the requests, preventing the response cache to work for images loaded 
* via Fresco. This is done because Fresco has its own disk cache which, 
* unfortunately, ignores the response cache headers. <br><br>
*
* We decided to disable that disk cache and rely on the http one, so we need to 
* remove the ‘no-store header from the request 
**/
class OkHttpNetworkFetcherWithCache : OkHttpNetworkFetcher {
@savvasdalkitsis
savvasdalkitsis / medium_post_fresco_4.kt
Last active February 25, 2018 14:56
medium_post_fresco_4
val imagePipelineConfig = OkHttpImagePipelineConfigFactory
.newBuilder(context, okHttpClient)
// no way to disable the disk cache globally, setting everything
// no to max 0 bytes
  .setMainDiskCacheConfig(DiskCacheConfig.newBuilder(context)
.setMaxCacheSize(0)
.setMaxCacheSizeOnLowDiskSpace(0)
.setMaxCacheSizeOnVeryLowDiskSpace(0)
.build())
.build()