This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { | |
observe(lifecycleOwner, object : Observer<T> { | |
override fun onChanged(t: T?) { | |
observer.onChanged(t) | |
removeObserver(this) | |
} | |
}) | |
} | |
//Using | |
liveData.observeOnce(this, Observer<Password> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.util.Base64; | |
public class Encryptor { | |
public static String encrypt(String key, String initVector, String value) { | |
try { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CheckData on List? { | |
T getItem<T>(int position,{dynamic errorReturnValue = "."}) => (this?.length ?? 0) > position ? this![position] : errorReturnValue; | |
} | |
//USAGE | |
// var list = [1,2,5,7]; | |
// print("${list[4]}"); this shows index out of range error so now you can use like below | |
// list.getItem(4) it's return dot because of the default errorReturnValue is . | |
// | |
// if you want specify the error value you can enter errorReturnValue parameter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationExecutors { | |
private final Executor background; | |
private final Executor mainThread; | |
public Executor getBackground() { | |
return background; | |
} | |
public Executor getMainThread() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ViewGroup.findViewWithTagRecursively(tag: Any): List<View>? { | |
val allViews: MutableList<View> = ArrayList<View>() | |
val childCount: Int = this.childCount | |
for (i in 0 until childCount) { | |
val childView: View = this.getChildAt(i) | |
if (childView is ViewGroup) { | |
childView.findViewWithTagRecursively(tag)?.let { allViews.addAll(it) } | |
} else { | |
val tagView: Any? = childView.tag | |
if (tagView != null && tagView == tag) allViews.add(childView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Xib class | |
import UIKit | |
protocol CustomViewDelegate: class { // make this class protocol so you can create `weak` reference | |
func goToNextScene() | |
} | |
class CustomView: UIView { | |
weak var delegate: CustomViewDelegate? // make this `weak` to avoid strong reference cycle b/w view controller and its views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat | |
import java.util.* | |
/** | |
* Pattern: yyyy-MM-dd HH:mm:ss | |
*/ | |
fun Date.formatToServerDateTimeDefaults(): String{ | |
val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) | |
return sdf.format(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Call<ResponseBody>.saveFile(fileNameWithExt: String, action: (file: File?) -> Unit) { | |
GlobalScope.launch { | |
try { | |
cacheDir.listFiles { _, name -> name.contains("${fileType}.pdf") }?.lastOrNull { | |
action(it) | |
true | |
}?: | |
withContext(Dispatchers.IO) { | |
val response = this@saveFile.execute() | |
val buffer = response.body()?.byteStream() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun alternateMerge(arr1: IntArray, arr2: IntArray, | |
n1: Int, n2: Int, arr3: IntArray) { | |
var i = 0 | |
var j = 0 | |
var k = 0 | |
// Traverse both array | |
while (i < n1 && j < n2) { | |
arr3[k++] = arr1[i++] | |
arr3[k++] = arr2[j++] | |
} |
NewerOlder