Skip to content

Instantly share code, notes, and snippets.

View mohanmanu484's full-sized avatar

Mohan mohanmanu484

  • Banglore
View GitHub Profile
@mohanmanu484
mohanmanu484 / CheckboxStateSaver.kt
Created January 2, 2024 09:37
Checkbox custom state saver
val CheckBoxStateSaver = listSaver<CheckBoxState, Any>(
save = {
listOf<Any>(it.isSelected,it.name)
},
restore = { data->
CheckBoxState(isSelected = data[0] as Boolean,data[1] as String)
}
)
@mohanmanu484
mohanmanu484 / ListSaver.kt
Created December 7, 2023 07:48
Lazy List saver
val Saver: Saver<LazyListState, *> = listSaver(
save = { listOf(it.firstVisibleItemIndex, it.firstVisibleItemScrollOffset) },
restore = {
LazyListState(
firstVisibleItemIndex = it[0],
firstVisibleItemScrollOffset = it[1]
)
}
)
@mohanmanu484
mohanmanu484 / CheckBoxStateSaver.kt
Created December 7, 2023 07:41
CheckBoxStateSaver
val CheckBoxStateSaver = Saver<CheckBoxState, Boolean>(
save = {
it.isSelected
},
restore = { isSelected->
CheckBoxState(isSelected = isSelected)
}
)
@mohanmanu484
mohanmanu484 / UserItem.kt
Last active December 7, 2023 07:36
UserItem
@Parcelize
data class CheckBoxState constructor(
val isSelected: Boolean = false,
val name: String = "Checkbox State"
) : Parcelable
@Composable
fun UserItem() {
var checkBoxState by rememberSaveable {
fun <T> autoSaver(): Saver<T, Any> =
@Suppress("UNCHECKED_CAST")
(AutoSaver as Saver<T, Any>)
private val AutoSaver = Saver<Any?, Any>(
save = { it },
restore = { it }
)
@mohanmanu484
mohanmanu484 / EmailFieldSaveable.kt
Created December 6, 2023 11:04
EmailField which uses rememberSaveable
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EmailField(name: String, modifier: Modifier = Modifier) {
var email: String by rememberSaveable {
mutableStateOf("")
}
TextField(value = email, modifier = modifier, onValueChange = {
email = it
})
}
@mohanmanu484
mohanmanu484 / EmailFieldRemember.kt
Created December 6, 2023 11:02
EmailField which uses remeber
@Composable
fun EmailField(name: String, modifier: Modifier = Modifier) {
var email: String by remember {
mutableStateOf("")
}
TextField(value = email, modifier = modifier, onValueChange = {
email = it
})
}
package calendar
import java.text.SimpleDateFormat
import java.util.*
import java.text.DateFormat
class KCalendar {
private val holidays = listOf(SpecificDayHoliday(23, 6),
@mohanmanu484
mohanmanu484 / GenericAdapter.kt
Created December 13, 2017 07:41
Generic adapter with kotlin
abstract class GenericAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHolder> {
var listItems: List<T>
constructor(listItems: List<T>) {
this.listItems = listItems
}
constructor() {
@mohanmanu484
mohanmanu484 / ApiManager.java
Created May 26, 2017 05:31 — forked from marcelpinto/ApiManager.java
Cache RxJava observables on Android approach to reuse and avoid multiple calls.
private ObservableManager obsManager = new ObservableManager(EventBus.getDefault());
@Override
public Subscription getStores() {
// Get the observable if exists and is not too old
Observable<StoresList> observable = obsManager.get(ObservableManager.Types.STORES);
if (observable == null) {
// If is null create it and us cache to keep it in memeroy
observable = api.getStoresList()
.compose(applySchedulers(api.getStoresList()))