Skip to content

Instantly share code, notes, and snippets.

@shkschneider
shkschneider / Log
Last active Apr 29, 2019
Android Log helper (automatic class, method and line) replacing android.util.Log
View Log
package me.shkschneider.skeleton.helper;
import android.text.TextUtils;
import android.util.Log;
/**
* Improved android.util.Log.
*
* Logs class and method automatically:
* [MyApplication onCreate():34] Hello, world!
@shkschneider
shkschneider / Android.pro
Last active May 1, 2018
ProGuard: obfuscate private fields with Android bug 78377
View Android.pro
# <https://code.google.com/p/android/issues/detail?id=78377#c188>
# <https://stackoverflow.com/questions/30526173>
-keepattributes **
-keep class !android.support.v7.internal.view.menu.**, ** {
!private <fields>;
protected <fields>;
public <fields>;
<methods>;
}
#-dontpreverify
@shkschneider
shkschneider / Gson.getJsonElement
Last active Aug 29, 2015
GsonParsor.getJsonElement(JsonObject, String)
View Gson.getJsonElement
// Uses GSON library
// getJsonElement(jsonObject, "{errors}{email}[reasons:0]")
// Retrieves the first JsonElement of the JsonArray "reasons" of the JsonObject "email" of the JsonObject "errors"... in one line ;)
public static JsonElement getJsonElement(@NonNull final JsonObject jsonObject, final String string) {
// matches all
if (TextUtils.isEmpty(string)) {
return jsonObject;
}
// bad formats
View Code39Extended.java
public class Code39Extended {
protected Code39Extended() {
// Empty
}
private static final Character NULL = '\0';
// <https://en.wikipedia.org/wiki/Code_39#Full_ASCII_Code_39>
private static final HashMap<String, HashMap<String, Character>> EXTENDED = new HashMap<String, HashMap<String, Character>>() {
@shkschneider
shkschneider / brainwallet.py
Created Sep 18, 2017
Bitcoin brainwallet (mainnet)
View brainwallet.py
#!/usr/bin/env python
#
# <https://github.com/arzzen/python-simple-brainwallet>
# <https://github.com/jgilmour/brainwallet-check>
# <https://bitcointalk.org/index.php?topic=84238.0>
# JeromeS
# ShkSchneider <https://github.com/shkschneider>
import base58
import binascii
View MainViewModel.kt
class MainViewModel : ViewModel() {
private lateinit var users: MutableLiveData<List<User>>
fun getUsers(): LiveData<List<User>> {
if (!::users.isInitialized) {
users = MutableLiveData()
// TODO loading of the data (hopefully using coroutines)
users.postValue(arrayListOf(
User(login = "user.name1"),
View MainActivity_viewModel.kt
private val viewModel by lazy {
ViewModelProviders.of(this).get(MainViewModel::class.java)
}
// inside onCreate()
viewModel.getUsers().observe(this, Observer {
display(it)
})
View DataManager.kt
object DataManager {
fun dummy(context: Context) {
val db = MyDatabase.get(context)
db.users().insert(User(login = "user.name"))
db.projects().insert(Project(name = "project.name"))
db.notifications().insert(Notification(msg = "created"))
}
}
View MyDatabase.kt
@Database(entities = [
User::class,
Project::class,
Notification::class
], version = 1, exportSchema = false)
abstract class MyDatabase : RoomDatabase() {
abstract fun users(): Users
abstract fun projects(): Projects
abstract fun notifications(): Notifications
companion object {
View MainViewModel.kt
@UiThread
fun getUsers(context: Context): LiveData<List<User>> {
if (!::users.isInitialized) {
users = MutableLiveData()
users.postValue(MyDatabase.get(context).users().getAll())
GlobalScope.launch(Dispatchers.Main) {
val usersFromDb: List<User> = async(Dispatchers.IO) {
return@async MyDatabase.get(context).users().getAll()
}.await()
users.value = usersFromDb