Skip to content

Instantly share code, notes, and snippets.

@mutkuensert
mutkuensert / CommandLine.groovy
Created January 13, 2025 11:59
Command line helper for gradle prints process outputs
class CommandLine {
static execute(String command) {
command.execute().waitForProcessOutput(System.out, System.err)
}
}
@mutkuensert
mutkuensert / HexToBase64.kt
Last active December 27, 2024 06:54
HexCodeUtils
object HexToBase64 {
fun encode(hex: String): String {
return Base64.getUrlEncoder().encodeToString(hex.decodeHex())
}
private fun String.decodeHex(): ByteArray {
check(length % 2 == 0) { "Must have an even length" }
return chunked(2)
@mutkuensert
mutkuensert / KSerializerExample.kt
Created October 22, 2024 11:13
SerialDescriptor example
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.decodeStructure
@mutkuensert
mutkuensert / ChangeLog.groovy
Last active March 22, 2024 16:09
Change log methods
ext.getChangeLog = { lastCommitsCount = 10 ->
return formatGitLog(getGitLogUntilCommitNumber(lastCommitsCount))
}
ext.getReleaseChangeLog = {
def lastTag = getLastTag()
return formatGitLog(getGitLogUntilTag(lastTag))
}
@mutkuensert
mutkuensert / Memory.kt
Created March 20, 2024 13:17
Getting total memory of Android device
val activityManager = (context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager)
val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
val totalMemory = memoryInfo.totalMem
@mutkuensert
mutkuensert / NavArgEncoder.kt
Created March 6, 2024 15:28
A function to convert objects to string json.
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
inline fun <reified T> encodeToStringNavArg(arg: T): String {
@OptIn(ExperimentalSerializationApi::class)
val json = Json {
ignoreUnknownKeys = true
@mutkuensert
mutkuensert / GenericRemoteMediator.kt
Created September 19, 2023 13:02
An helper class to create remote mediator easily.
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.get
import com.github.michaelbull.result.onFailure
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@mutkuensert
mutkuensert / DoubleExt.kt
Last active September 5, 2023 14:21
Digit remover extension for Double.
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
fun Double.withDecimalDigits(number: Int = 1): Double {
var diesisForDecimals = ""
repeat(number) {
diesisForDecimals = "#"
}
@mutkuensert
mutkuensert / SequentialRequestInterceptor.kt
Created August 8, 2023 20:38
Throws error if same request is done in 10 seconds.
private const val PREFS_SEQUENTIAL_REQUEST = "sequential-request-preferences"
private const val KEY_LAST_URL = "lastUrl"
private const val KEY_LAST_REQUEST_TIME = "lastRequestTime"
class SequentialRequestInterceptor(context: Context) : Interceptor {
private val preferences =
context.getSharedPreferences(PREFS_SEQUENTIAL_REQUEST, Context.MODE_PRIVATE)
override fun intercept(chain: Interceptor.Chain): Response {
val baseRequest = chain.request()
@mutkuensert
mutkuensert / ConvertToDoubleExt.kt
Last active July 20, 2023 15:27
String to double converter extension function
fun String.convertToDouble(): Double {
val newValue: String = this.replace(",", ".")
var withoutExtraDots = ""
var indexOfFirstDot: Int? = null
var index = 0
for (c in newValue) {
if (index == 0 && c == '.') return 0.0
if (indexOfFirstDot == null && c == '.') indexOfFirstDot = index