Skip to content

Instantly share code, notes, and snippets.

View G00fY2's full-sized avatar

Thomas Wirth G00fY2

View GitHub Profile
@G00fY2
G00fY2 / UsabillaControllerImpl.kt
Created November 16, 2021 14:21
UsabillaController example
import android.content.Context
import com.usabilla.sdk.ubform.Usabilla
import com.usabilla.sdk.ubform.UsabillaReadyCallback
import com.example.app.BuildConfig
import javax.inject.Inject
class UsabillaControllerImpl @Inject constructor() : UsabillaController, UsabillaReadyCallback {
@Inject
lateinit var context: Context
@G00fY2
G00fY2 / GetLinkPreviewDataInteractorImpl.kt
Last active March 21, 2022 18:02
Extract link preview data from given URL
class GetLinkPreviewDataInteractorImpl @Inject constructor(
private val okHttpClient: OkHttpClient
) : GetLinkPreviewDataInteractor {
override fun execute(url: String): Single<LinkPreviewData> {
return Single.fromCallable {
okHttpClient.newCall(Builder().url(url).build()).execute().use { response ->
val requestedUrl = response.request.url.toString()
Jsoup.parse(response.body!!.string()).let {
@G00fY2
G00fY2 / ProjectConfiguration.kt
Last active March 24, 2023 14:55
Check for unstable dependencies in Gradle configurations which where implicitly resolved
configurations.configureEach {
if (this.name.contains("Compile") && !this.name.contains("TestCompile")) {
this.incoming.afterResolve {
this.resolutionResult.allComponents {
if (this.selectionReason.isConflictResolution &&
this.moduleVersion?.version.let {
it != null && isNonStable(it)
}
) {
throw GradleException(
@G00fY2
G00fY2 / TasksRxExtensions.kt
Last active November 3, 2022 12:50
Extension function to convert Google Play Services Tasks result to RxJava Single
import com.google.android.gms.tasks.Task
import io.reactivex.rxjava3.core.Single
import java.util.concurrent.CancellationException
import java.util.concurrent.TimeUnit
fun <T : Any> Task<T>.toSingle(): Single<T> {
return if (isComplete) {
Single.fromCallable(::asRequired)
} else {
Single.create<T> { emitter ->
@G00fY2
G00fY2 / CustomRxJava3CallAdapterFactory.kt
Created November 3, 2022 13:08
CustomRxJava3CallAdapterFactory to transform HttpExceptions to custom error
import com.squareup.moshi.Moshi
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.Maybe
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import retrofit2.Call
import retrofit2.CallAdapter
@G00fY2
G00fY2 / FormatIBAN.kt
Created November 3, 2022 13:27
Extension function to format IBAN Strings
fun String.formatIBAN(): String {
val iban = replace("\\s+".toRegex(), "").uppercase(Locale.getDefault())
val maxLength = (iban.length * 1.25).roundToInt()
val result = StringBuilder(maxLength)
iban.forEachIndexed { index, c ->
when {
index > 0 && index % 4 == 0 -> result.append(" $c")
else -> result.append(c)
}
}
@Module
@InstallIn(SingletonComponent::class)
internal object DataStoreModule {
@Provides
@Singleton
fun provideMyCacheDataStore(
@ApplicationContext context: Context,
moshi: Moshi,
): DataStore<MyCachedDataClass> {