Skip to content

Instantly share code, notes, and snippets.

View lgawin's full-sized avatar

Łukasz Gawin lgawin

  • FoQuS
  • Warsaw, Poland
View GitHub Profile
@lgawin
lgawin / TimeSuffixesTest.kt
Created March 7, 2024 03:51
AM/PM translations
import org.junit.Assert.assertEquals
import org.junit.Test
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.Locale
class TimeSuffixesTest {
@Test
fun getAmPm() {
@lgawin
lgawin / RoleObserver.kt
Last active January 6, 2024 00:46
Helper for requesting roles with example usage
import android.app.role.RoleManager
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import java.util.UUID
@lgawin
lgawin / MockkAndroidLog.kt
Last active March 30, 2022 16:08
JUnit 4 @rule and JUnit5 test extension to mockk `android.util.Log`
// JUnit5 test extension
//
// usage:
//
// @ExtendWith(MockkAndroidLog::class)
// class TestClass {
// ...
// }
import android.util.Log
import io.mockk.clearStaticMockk
package com.orderbird.testing
import android.app.Activity
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.orderbird.testing.AnimationScaleSettings.disableAnimations
import com.orderbird.testing.AnimationScaleSettings.restoreAnimations
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
@lgawin
lgawin / Brewfile
Last active January 6, 2021 14:24
Initialize MacOsX
cask_args appdir: "/Applications"
# Apps
cask "google-chrome"
package com.example.android.testing.blueprint
import android.content.res.AssetManager
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
@lgawin
lgawin / FindViewKtx.kt
Created May 6, 2020 08:28
Delegate for finding view by id
import android.app.Activity
import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
fun <T : View> Activity.findView(@IdRes res : Int) : Lazy<T> {
@Suppress("UNCHECKED_CAST")
return lazy(LazyThreadSafetyMode.NONE){ findViewById<T>(res) }
}
@lgawin
lgawin / DiffUtilKtx.kt
Last active January 24, 2020 13:32
Some helper methods for RecyclerView's `DiffUtil`s
import androidx.recyclerview.widget.DiffUtil
import kotlin.reflect.KProperty1
@Suppress("NOTHING_TO_INLINE")
inline fun <T : Any, R: Any> byProperty(kProperty: KProperty1<T, R>): (T, T) -> Boolean =
{ old, new -> kProperty.get(old) == kProperty.get(new) }
@Suppress("FunctionName")
inline fun <T : Any> DiffItemCallback(
crossinline areItemsTheSame: (T, T) -> Boolean,
@lgawin
lgawin / DiffObservableListKtx.kt
Last active September 26, 2019 21:48
ObservableList extensions for `me.tatarka.bindingcollectionadapter2.collections`
import androidx.databinding.ObservableList
import androidx.recyclerview.widget.DiffUtil
import me.tatarka.bindingcollectionadapter2.collections.AsyncDiffObservableList
import me.tatarka.bindingcollectionadapter2.collections.DiffObservableList
@Suppress("NOTHING_TO_INLINE", "FunctionName")
inline fun <T : Any> DiffObservableList(
itemCallback: DiffUtil.ItemCallback<T>,
async: Boolean = true
): DiffList<T> =
@lgawin
lgawin / DatePickerBindingAdapter.kt
Created April 11, 2019 11:11
Two-way adapter for `DatePicker` when using ThreeTenAbp
@BindingAdapter("date")
fun setDate(datePicker: DatePicker, value: LocalDate) {
if (value != LocalDate.of(datePicker.year, datePicker.month + 1, datePicker.dayOfMonth)) {
datePicker.updateDate(value.year, value.monthValue - 1, value.dayOfMonth)
}
}
@BindingAdapter(value = ["dateAttrChanged", "onDateChanged"], requireAll = false)
fun setDateChangeListener(
datePicker: DatePicker,