Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
View GitHub Profile
@Zhuinden
Zhuinden / MyFragment.java
Last active Sep 10, 2022
Nested child fragments for bottom navigation view in Java
View MyFragment.java
public class MyFragment
extends Fragment {
public MyFragment() {
super(R.layout.my_fragment);
}
private int selectedIndex = 0;
private FirstFragment firstFragment;
View ByteArrayConverterFactory.java
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 Jul 23, 2022
Fragment multiple tabs without remove or replace
View MainActivity.kt
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 Nov 25, 2020
Firestore queries as LiveData
View FirestoreLiveData.kt
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 Nov 28, 2022
CustomFragmentContainerView
View CustomFragmentContainerView.java
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 Jun 19, 2020
Hilt: Navigation Dispatcher 2
View NavigationDispatcher.kt
@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 Jun 19, 2020
Hilt LoginViewModel Login
View LoginViewModel.kt
fun onLoginClicked() {
val username = // ...
navigationDispatcher.emit { navController ->
navController.navigate(
LoggedOutGraphDirections.loggedOutToLoggedIn(username))
}
}
}
@Zhuinden
Zhuinden / LoginViewModel.kt
Created Jun 19, 2020
Hilt LoginViewModel
View LoginViewModel.kt
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 Jun 21, 2020
Hilt MainActivity
View MainActivity.kt
@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 ->
@Zhuinden
Zhuinden / NavigationDispatcher.kt
Created Jun 19, 2020
Hilt NavigationDispatcher
View NavigationDispatcher.kt
@ActivityRetainedScoped
class NavigationDispatcher @Inject constructor() {
private val navigationEmitter: EventEmitter<NavigationCommand> = EventEmitter()
val navigationCommands: EventSource<NavigationCommand> = navigationEmitter
fun emit(navigationCommand: NavigationCommand) {
navigationEmitter.emit(navigationCommand)
}
}