Skip to content

Instantly share code, notes, and snippets.

View albodelu's full-sized avatar

Al B. albodelu

View GitHub Profile
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.co.jakelee.updatelistener">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
object PostsNavigation : DynamicFeature {
override fun start(c: Context): Intent? =
loadClassOrNull<Activity>("com.sanogueralorenzo.posts.presentation.postlist.PostListActivity")
?.let { Intent(c, it) }
}
private val classMap = mutableMapOf<String, Class<*>>()
private inline fun <reified T : Any> Any.castOrNull() = this as? T
internal fun <T> loadClassOrNull(className: String): Class<out T>? {
return classMap.getOrPut(className) {
try {
Class.forName(className)
} catch (e: ClassNotFoundException) {
return null
@florina-muntenescu
florina-muntenescu / CustomTypefaceSpan.kt
Last active July 2, 2024 09:28
Style internationalized text using Annotations
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@JoseAlcerreca
JoseAlcerreca / EventObserver.kt
Created April 26, 2018 12:14
An Observer for Events, simplifying the pattern of checking if the Event's content has already been handled.
/**
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
* already been handled.
*
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
@JoseAlcerreca
JoseAlcerreca / Event.kt
Created April 26, 2018 10:25
An event wrapper for data that is exposed via a LiveData that represents an event.
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
@nekdenis
nekdenis / MockitoKotlin.kt
Last active September 27, 2018 02:05
My usage of Mockito with Kotlin
package com.sample
import org.mockito.Mockito
//wrappers:
inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)
inline fun <reified T : Any> mock(mocking: T.() -> Unit): T = Mockito.mock(T::class.java).apply { mocking() }
@pablisco
pablisco / gist:da25563d57559dd1d18f165272269b57
Last active April 15, 2022 03:23
ResourcesExceptions.kt
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.support.annotation.AnyRes
import android.support.v4.app.Fragment
import android.support.v4.content.res.ResourcesCompat.*
import android.view.View
val Context.animations
get() = ResourceMapper { resources.getAnimation(it) }
@vestrel00
vestrel00 / Dagger2SimpleExample.java
Last active October 7, 2021 19:59
A: Dagger.android 2.11 simple example with support for Singleton, PerActivity, PerFragment, and PerChildFragment scopes
// This is a super simplified example of how to use the new dagger.android framework
// introduced in Dagger 2.10. For a more complete, in-depth guide to dagger.android
// read https://proandroiddev.com/how-to-android-dagger-2-10-2-11-butterknife-mvp-part-1-eb0f6b970fd
// For a complete codebase using dagger.android 2.11-2.17, butterknife 8.7-8.8, and MVP,
// see https://github.com/vestrel00/android-dagger-butterknife-mvp
// This example works with Dagger 2.11-2.17. Starting with Dagger 2.11,
// @ContributesAndroidInjector was introduced removing the need to define @Subcomponent classes.
@Atternatt
Atternatt / NearestNeighbors.kt
Last active September 21, 2017 04:59
Recommendation algorithms
fun List<Number>.getNearestNeighbors(posibleNeighbors: List<List<Number>>, numberOfNeighbors: Int): List<List<Double>> {
return posibleNeighbors.mapIndexed { index, posibleNeighbor -> index to this.pearsonCorrelationWith(posibleNeighbor) }
.sortedBy { it.second }
.take(numberOfNeighbors)
.map { posibleNeighbors[it.first].map { it.toDouble() } }
}