Skip to content

Instantly share code, notes, and snippets.

View Zhuinden's full-sized avatar
🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
View GitHub Profile
@Zhuinden
Zhuinden / BasicTextFieldWithCursorAtEnd.kt
Last active November 11, 2024 20:40
A Compose TextField that initializes the cursor position at the end of the text, rather than the start, when focused without tapping the TextField manually.
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@Zhuinden
Zhuinden / MyFragment.java
Last active September 10, 2022 11:02
Nested child fragments for bottom navigation view in Java
public class MyFragment
extends Fragment {
public MyFragment() {
super(R.layout.my_fragment);
}
private int selectedIndex = 0;
private FirstFragment firstFragment;
@Zhuinden
Zhuinden / ByteArrayConverterFactory.java
Created January 22, 2022 16:56
ByteArrayConverterFactory
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.annotation.Nullable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
@Zhuinden
Zhuinden / MainActivity.kt
Last active December 30, 2022 09:40
Fragment multiple tabs without remove or replace
class MainActivity : AppCompatActivity() {
private lateinit var swipeFragment: SwipeFragment
private lateinit var favoritesFragment: FavoritesFragment
private lateinit var newsFragment: NewsFragment
private val fragments: Array<out Fragment> get() = arrayOf(swipeFragment, favoritesFragment, newsFragment)
private fun selectFragment(selectedFragment: Fragment) {
var transaction = supportFragmentManager.beginTransaction()
fragments.forEachIndexed { index, fragment ->
@Zhuinden
Zhuinden / FirestoreLiveData.kt
Created November 25, 2020 10:46
Firestore queries as LiveData
inline fun <reified T> Query.asLiveData(noinline onError: ((Throwable) -> Unit) = {}): FirestoreQueryLiveData<T> {
return FirestoreQueryLiveData(this, T::class.java, onError)
}
inline fun <reified T> DocumentReference.asLiveData(noinline onError: ((Throwable) -> Unit) = {}): FirestoreDocumentLiveData<T> {
return FirestoreDocumentLiveData(this, T::class.java, onError)
}
class FirestoreQueryLiveData<T>(
private val query: Query,
@Zhuinden
Zhuinden / CustomFragmentContainerView.java
Last active November 28, 2022 08:48
CustomFragmentContainerView
import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.widget.FrameLayout;
@Zhuinden
Zhuinden / NavigationDispatcher.kt
Last active June 19, 2020 00:59
Hilt: Navigation Dispatcher 2
@ActivityRetainedScoped
class NavigationDispatcher @Inject constructor() {
private val navigationEmitter: EventEmitter<NavigationCommand> = EventEmitter()
val navigationCommands: EventSource<NavigationCommand> = navigationEmitter
fun emit(navigationCommand: NavigationCommand) {
navigationEmitter.emit(navigationCommand)
}
}
@Zhuinden
Zhuinden / LoginViewModel.kt
Created June 19, 2020 00:51
Hilt LoginViewModel Login
fun onLoginClicked() {
val username = // ...
navigationDispatcher.emit { navController ->
navController.navigate(
LoggedOutGraphDirections.loggedOutToLoggedIn(username))
}
}
}
@Zhuinden
Zhuinden / LoginViewModel.kt
Created June 19, 2020 00:50
Hilt LoginViewModel
class LoginViewModel @ViewModelInject constructor(
private val navigationDispatcher: NavigationDispatcher,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
fun onRegisterClicked() {
navigationDispatcher.emit { navController ->
navController.navigate(R.id.logged_out_to_registration)
}
}
}
@Zhuinden
Zhuinden / MainActivity.kt
Last active June 21, 2020 17:44
Hilt MainActivity
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var navigationDispatcher: NavigationDispatcher
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigationDispatcher.navigationCommands.observe(this) { command ->