Skip to content

Instantly share code, notes, and snippets.

View kobeumut's full-sized avatar
🏠
Working from home

Umut ADALI kobeumut

🏠
Working from home
View GitHub Profile
@kobeumut
kobeumut / ObserveOnceExtension.kt
Created April 21, 2019 08:24
Android Livedata Observe Once Only (Kotlin)
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> {
@kobeumut
kobeumut / Crypt.java
Created March 20, 2019 06:16
AES128 CBC mode in Java, Kotlin and Ruby
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"));
@kobeumut
kobeumut / check_data_size_on_list.dart
Last active October 7, 2021 12:30
This extension is used when the list is unknown. So if the list is empty or the calling position is not available, shows an error which is Index out of range or null point exception. This extension provides easy usage to get data.
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
@kobeumut
kobeumut / ApplicationExecutors.java
Created August 1, 2021 11:26
It can be used instead of coroutines in small projects
public class ApplicationExecutors {
private final Executor background;
private final Executor mainThread;
public Executor getBackground() {
return background;
}
public Executor getMainThread() {
@kobeumut
kobeumut / checkbox-radio-button-extension.swift
Created March 29, 2018 05:27
Swift Checkbox and Radio Buttons Extension
//Checkbox
import UIKit
class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "ic_check_box")! as UIImage
let uncheckedImage = UIImage(named: "ic_check_box_outline_blank")! as UIImage
// Bool property
var isChecked: Bool = false {
@kobeumut
kobeumut / ViewExt.kt
Created November 22, 2020 08:33
Find View with tag recursively in Kotlin
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)
//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
@kobeumut
kobeumut / DateExtension.kt
Created August 12, 2020 20:58 — forked from maiconhellmann/DateExtension.kt
Date extensions wrote in Kotlin
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)
}
@kobeumut
kobeumut / Extension.kt
Created June 3, 2020 19:50
Save file extension with retrofit and coroutine
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()
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++]
}