Skip to content

Instantly share code, notes, and snippets.

View reuniware's full-sized avatar
🎯
Focusing

Quant & Fintech Opensource Projects reuniware

🎯
Focusing
View GitHub Profile
@reuniware
reuniware / LocaleManager.java
Created January 29, 2019 17:49 — forked from Jasonlhy/LocaleManager.java
LocaleManager for handling android language switch at runtime
public class LocaleManager {
private static final String LANGUAGE_KEY = "CHOOSE_LANGUAGE";
public static Context setLocale(Context c) {
String savedLanguage = getLanguage(c);
return savedLanguage == null ? c : updateResources(c, savedLanguage);
}
public static Context setNewLocale(Context c, String language) {
persistLanguage(c, language);
@reuniware
reuniware / SwipeController.kt
Last active March 13, 2019 13:31
Implements a basic swipe on recyclerview item
package com.util
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper.Callback
import android.support.v7.widget.helper.ItemTouchHelper.*
import android.view.MotionEvent
import android.support.v7.widget.helper.ItemTouchHelper.ACTION_STATE_SWIPE
@reuniware
reuniware / gist:3f981835775c4f7f354501e5c0a45a59
Created March 14, 2019 16:45
Save a recyclerview adapter in a file in order to get it back (in kotlin)
// Sauvegarde de l'adapter fin de pouvoir appeler notifyDataSetChanged après suppression d'un élément depuis l'adapter, en vue refresh liste
lateinit var adapter: RecyclerView.Adapter<*>
fun saveAdapter(adapterToSave: RecyclerView.Adapter<*>) {
adapter = adapterToSave
}
fun getSavedkAdapter(): RecyclerView.Adapter<*>{
return adapter
@reuniware
reuniware / extension.kt
Last active March 25, 2019 10:56
Android kotlin encrypt decrypt example
lateinit var secretKey : SecretKey
lateinit var iv : ByteArray
fun encrypt(context:Context, strToEncrypt: String): ByteArray {
val plainText = strToEncrypt.toByteArray(Charsets.UTF_8)
val keygen = KeyGenerator.getInstance("AES")
keygen.init(256)
val key = keygen.generateKey()
secretKey = key
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, key)
@reuniware
reuniware / ViewPager.kt
Created April 9, 2019 16:23
Android Kotlin StackOverflowError
val handleScroll = View.OnScrollChangeListener { v, x, y, ox, oy ->
viewPager.scrollTo(ox, oy)
return@OnScrollChangeListener
}
viewPager.setOnScrollChangeListener(handleScroll)
@reuniware
reuniware / remove.kt
Created May 23, 2019 16:29
Android Kotlin AccountManager remove accounts
val accountManager = this.applicationContext.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager
val account = Account("syncaccount", "com.datasync.syncserviceaccount")
val accounts = accountManager.accounts
accounts.forEach {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
accountManager.removeAccount(account, null, null)
} else {
accountManager.removeAccountExplicitly(account)
}
}
@reuniware
reuniware / LogError.kt
Created May 23, 2019 16:30
SyncAdapter Log and Timber
package com.datasync
import android.accounts.Account
import android.content.*
import android.os.Bundle
import android.util.Log
import timber.log.Timber
class SyncAdapter @JvmOverloads constructor(
context: Context,
@reuniware
reuniware / alertdialog.kt
Last active July 5, 2019 11:57
Alertdialog Kotlin in Fragment (with same theme as the default theme when showing from normal Activity)
var alertDialogBuilder = AlertDialog.Builder(context, R.style.AlertDialogThemeCustom)
alertDialogBuilder.setTitle("Deletion")
alertDialogBuilder.setMessage("Do you confirm the deletion ?")
alertDialogBuilder.setCancelable(false)
alertDialogBuilder.setPositiveButton("Yes, delete", DialogInterface.OnClickListener { dialog, which ->
// ...
dialog.cancel()
} )
alertDialogBuilder.setNegativeButton("No, cancel", DialogInterface.OnClickListener { dialog, which ->
// Do nothing
@reuniware
reuniware / show_date_picker.kt
Created July 9, 2019 13:38
Show Date Picker dialog in Android
/*
https://ntic974.blogspot.com
*/
// Définir la date de début de stats par défaut au 1er janvier de l'année en cours
val cal = Calendar.getInstance()
cal.set(Calendar.MONTH, 0)
cal.set(Calendar.DAY_OF_MONTH, 1)
val year_start = cal.get(Calendar.YEAR)
val month_start = cal.get(Calendar.MONTH) // Mois de 0 à 11