Skip to content

Instantly share code, notes, and snippets.

@neilsmithdesign
neilsmithdesign / swift-ui-protocol-view-models.swift
Last active April 27, 2024 17:13
SwiftUI views with protocol interfaces to view models.
import SwiftUI
/// View model protocol
protocol ViewModel: ObservableObject {
var count: Int { get }
func increase()
}
/// Concrete implementation
class MyViewModel: ViewModel {
@dellisd
dellisd / Resource.kt
Last active December 28, 2023 11:33
Kotlin Multiplatform test resources
// Common
const val RESOURCE_PATH = "./src/commonTest/resources"
expect class Resource(name: String) {
val name: String
fun exists(): Boolean
fun readText(): String
}
@RBusarow
RBusarow / TestCoroutineExtension.kt
Last active November 20, 2023 19:03
A JUnit 4 Rule and JUnit 5 Extension for utilizing TestCoroutineDispatcher and TestCoroutineScope from kotlinx.coroutines-test
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.ExtensionContext
@SabagRonen
SabagRonen / FakeFragmentInjector.kt
Last active August 31, 2019 15:59
Fragment espresso with Dagger
inline fun <reified F : Fragment> createFakeFragmentInjector(crossinline block : F.() -> Unit)
: DispatchingAndroidInjector<Any> {
val myApp = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as MyApp
val originalDispatchingActivityInjector = myApp.dispatchingActivityInjector
var originalFragmentInjector: AndroidInjector<Any>? = null
val fragmentInjector = AndroidInjector<Fragment> { fragment ->
originalFragmentInjector?.inject(fragment)
if (fragment is F) {
fragment.block()
}
@milhomem
milhomem / clientCert.android.java
Last active April 18, 2024 21:18
How to connect using Client Certificate in Android with OkHttp
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream clientCertificateContent = new FileInputStream("/path/to/publicAndPrivateKey.p12");
keyStore.load(clientCertificateContent, "private key password".toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "private key password".toCharArray());
FileInputStream myTrustedCAFileContent = new FileInputStream("/path/to/embedded/CA-Chain.pem");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate myCAPublicKey = (X509Certificate) certificateFactory.generateCertificate(myTrustedCAFileContent);
@BenziAhamed
BenziAhamed / XML.swift
Created October 15, 2015 19:42
Example XML processing
//: Playground - noun: a place where people can play
import UIKit
let xml = "<coord2 count=\"3\">"
+ "<markers>"
+ "<marker>"
+ "<item>marker1</item>"
+ "</marker>"
@Tetr4
Tetr4 / ForceCacheOrNetwork.java
Last active February 1, 2020 05:17
How to force a cached or network response on Android with Retrofit + OkHttp
OkHttpClient okHttpClient = new OkHttpClient();
try {
int cacheSize = 10 * 1024 * 1024 // 10 MiB
Cache cache = new Cache(getCacheDir(), cacheSize);
okHttpClient.setCache(cache);
} catch (IOException e) {
Log.e(TAG, "Could not set cache", e);
}
// Forces cache. Used for cache connection