Skip to content

Instantly share code, notes, and snippets.

View pablobaxter's full-sized avatar

Pablo Baxter pablobaxter

View GitHub Profile
@pablobaxter
pablobaxter / Timeout.kt
Created March 17, 2021 05:59
Flow timeout on the producer
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import java.util.concurrent.TimeoutException
fun <T> Flow<T>.withTimeout(timeoutMillis: Long): Flow<T> {
return flow { // New flow to wrap the old one
coroutineScope {
@pablobaxter
pablobaxter / HarmonyUsage.java
Created February 19, 2021 00:19
Harmony Example Usage Java
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = Harmony.getSharedPreferences(context, "my_prefs");
}
}
@pablobaxter
pablobaxter / HarmonyUsage.kt
Created February 19, 2021 00:16
Harmony Example Usage
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val prefs: SharedPreferences = context.getHarmonySharedPreferences("my_prefs")
}
}
@pablobaxter
pablobaxter / SimpleService.kt
Last active February 28, 2021 21:06
Other process SharedPreferences
// Assume this service has the 'android:process' tag in the AndroidManifest.xml
class SimpleService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val sharedPrefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
sharedPrefs.edit { putInt("my_counter", sharedPrefs.getInt("my_counter", 0) + 1) } // Simple counter increment
return super.onStartCommand(intent, flags, startId)
}
}
@pablobaxter
pablobaxter / MyActivity.kt
Last active February 18, 2021 06:47
Simple SharedPrefs Example
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharedPrefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
sharedPrefs.edit { putInt("my_counter", sharedPrefs.getInt("my_counter", 0) + 1) } // Simple counter increment
}
}