Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
View GitHub Profile
@Zhuinden
Zhuinden / MainActivity.kt
Last active May 10, 2021
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,
View CustomFragmentContainerView.java
public class CustomFragmentContainerView extends FrameLayout {
private ArrayList<View> mAppearingFragmentChildren = new ArrayList<>();
private ArrayList<View> mDisappearingFragmentChildren = new ArrayList<>();
private ArrayList<View> mTransitioningFragmentViews = new ArrayList<>();
private boolean mDrawAppearingViewsFirst = false;
private boolean mDrawDisappearingViewsFirst = false;
public CustomFragmentContainerView(@NonNull Context context) {
@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)
}
}
View NavigationCommand.kt
typealias NavigationCommand = (NavController) -> Unit
@Zhuinden
Zhuinden / HiltUtils.kt
Last active Mar 19, 2021
Hilt NavGraphViewModels utils
View HiltUtils.kt
inline fun <reified T : ViewModel> Fragment.hiltNavGraphViewModels(@IdRes navGraphIdRes: Int) =
viewModels<T>(
ownerProducer = { findNavController().getBackStackEntry(navGraphIdRes) },
factoryProducer = { defaultViewModelProviderFactory } // TODO: FIX THIS. THIS ISN'T SUFFICIENT!!!
)