Skip to content

Instantly share code, notes, and snippets.

public interface OnClickListener {
void onClick(View v);
}
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//handle click
}
});
protocol Grinder {
func grind(_ coffee: Coffee)
}
class GrinderMock: Grinder {
private(set) var timesUsed: Int = 0
func grind(_ coffee: Coffee) {
timesUsed += 1
}
}
class CoffeeMakerTest: XCTestCase {
protocol Shakeable {
func shake()
}
extension Shakeable where Self: UIView {
func shake() {
UIView.animate(...
}
}
interface ToastShowable {
fun showToast(context: Context, text: String) = ...
}
class MainActivity : ComponentActivity(), ToastShowable {
override fun onCreate(savedInstanceState: Bundle?) {
//...
showToast(this, "Le toast")
}
}
interface Message {
val timestamp: DateTime
}
class TextMessage(
val text: String,
override val timestamp: DateTime
) : Message
class ImgMessage(
@micHar
micHar / annotatedStringResource.kt
Last active May 12, 2023 10:33
Helper function loading annotated strings from strings.xml (might require further optimization)
import android.text.Annotation
import android.text.SpannedString
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.core.text.getSpans
@Composable
/**
* Sets the result to be obtained by [NavResultCallback] attached to [targetBackStackEntry]
*/
abstract class NavResultPusher<T>(
private val targetBackStackEntry: NavBackStackEntry,
private val key: String
) {
open fun setResult(result: T) = targetBackStackEntry.savedStateHandle.set(key, result)
}
/*
* Copyright 2020 Google LLC
*
* 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
private data class Border(val strokeWidth: Dp, val cornerRadius: Dp, val color: Color)
fun Modifier.bookCardBackground(
itemIndex: Int,
allItemsSize: Int
): Modifier {
val border = Border(
strokeWidth = 1.dp,
cornerRadius = 16.dp,
createPublisher(wrapper: observeUsersUseCase.observeUsers(usersCount: 5))
.zip(createPublisher(wrapper: observeUsersUseCase.observeUsers(usersCount: 5)))
.sink(
receiveCompletion: { _ in },
receiveValue: { user in
let user1 = user.0!.name
let user2 = user.1!.name
print("Date between \(user1) and \(user2)")
}
)