This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//region DSL | |
class ExecutionCancelationException(val failure: Any?) : RuntimeException() | |
interface ExecutionScope<in F> { | |
fun fail(f: F): Nothing = throw ExecutionCancelationException(f) | |
} | |
sealed interface Result<out F, out R> | |
data class Success<out R>(val result: R) : Result<Nothing, R> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Does this compile? | |
inline fun <A, B> A.andThen(block: (A) -> Unit) { | |
Runnable { | |
block(this) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override suspend fun execute(query: PostsQuery): Either<Failure, List<Post>> { | |
val operation = if (query.forceRefresh) MainSyncOperation else CacheSyncOperation | |
return either { | |
val posts = !repository.getAll(query = query, operation = operation) | |
posts | |
.filter { it.featuredImage != null } //we just want posts with featured images | |
.map { post -> | |
withContext(coroutineDispatcher) { | |
async { | |
val host: String? = try { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
@DisplayName("When Exporting only one file then rsync#sync should be called only once") | |
fun exportServerCallRsyncSync() { | |
collectorExporter.export(mockedFile, serverAddress, DirectExecutor, "").get() | |
verify(sync, times(1)).sync(absolutePathMock, serverAddress, "") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CollectorExporter constructor(context: Context, | |
private val executor: AppExecutor, | |
private val sync: Sync = SyncClient(context)) { | |
fun export(analyticsFolder: File, metadataFolder: File, collectorNetworkAddress: String, executor: ListeningExecutorService = this.executor, uploadFolder: String): | |
ListenableFuture<Unit> { | |
return executor.submit(Callable { | |
sync.sync(analyticsFolder.absolutePath, collectorNetworkAddress, uploadFolder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsageFragment : Fragment() { | |
private val numberWhatever by Argument("ARG_PROPERTY", 0) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
//same behaviour tham BundleParam but withArguments in fragments | |
print("$numberWhatever!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val day = today | |
val todayOnWeekInTheFuture = today + 1.week | |
//the power of infix | |
val isBefore = day isBefore todayOnWeekInTheFuture | |
day.formatedDateText("dd/MM/yyyy") | |
val oneJan2020at10Oclock = Dates.of(2020, 1, 1,10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun backgroundColorAnimator(): ReadWriteProperty<View, Int> = | |
object : ReadWriteProperty<View, Int> { | |
override fun getValue(thisRef: View, property: KProperty<*>): Int { | |
return (thisRef.background as? ColorDrawable)?.color ?: 0 | |
} | |
override fun setValue(thisRef: View, property: KProperty<*>, value: Int) { | |
ValueAnimator.ofObject(ArgbEvaluator(), getValue(thisRef, property), value).apply { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun alphaWithBounceAnimator(): ReadWriteProperty<ImageView, Int> = | |
object : ReadWriteProperty<ImageView, Int> { | |
override fun getValue(thisRef: ImageView, property: KProperty<*>): Int { | |
return thisRef.imageAlpha | |
} | |
override fun setValue(thisRef: ImageView, property: KProperty<*>, value: Int) { | |
val animX = ObjectAnimator.ofFloat(thisRef, "scaleX", 1f, 2.5f, 1f) |
NewerOlder