Skip to content

Instantly share code, notes, and snippets.

🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
Block or report user

Report or block Zhuinden

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View UserRepositoryUseCase.kt
// this is an anti-pattern (technically code smell)
class UserRepository(
private val getUsersUseCase: GetUsersUseCase,
private val deleteUserByIdUseCase: DeleteUserByIdUseCase
) {
fun getAllUsersWithChanges(): LiveData<List<User>> =
getUsersUseCase.getUsers()
fun deleteUserById(userId: String) {
deleteUserByIdUseCase.deleteUserById(userId)
View UserRepositoryDaoDelegate.kt
// useless repository
class UserRepository(
private val userDao: UserDao
): UserDao by userDao {}
@Zhuinden
Zhuinden / UserRepository.kt
Last active Nov 27, 2019
Anemic Repository 6
View UserRepository.kt
// this is an anti-pattern (technically code smell)
class UserRepository(
private val userDao: UserDao
) {
fun getAllUsersWithChanges(): LiveData<List<User>> =
userDao.getAllUserWithChanges()
fun getAllUsers(): List<User> =
userDao.getAllUsers()
@Zhuinden
Zhuinden / MyViewWithListener.kt
Last active Nov 27, 2019
Anemic Repository 5
View MyViewWithListener.kt
class MyView(...): ... {
interface Listener {
fun onTextChanged(text: String)
fun onButtonPressed()
fun onCheckBoxTicked()
}
var listener: Listener? = null
// can make this a list if you want
@Zhuinden
Zhuinden / MyViewWithEvents.kt
Last active Nov 27, 2019
Anemic Repository 4
View MyViewWithEvents.kt
class MyView(...): ... {
sealed class Events {
class OnTextChanged(val text: String): Events()
class OnButtonPressed(): Events()
class OnCheckBoxTicked(): Events()
}
lateinit var events: Observable<Events>
private set
View MyViewModelSavedState.kt
class MyViewModel(
savedStateHandle: SavedStateHandle,
private val userDao: UserDao
): ViewModel() {
private val selectedUserId: MutableLiveData<String> =
savedStateHandle.getLiveData("selectedUserId")
val selectedUser: LiveData<User> = selectedUserId.switchMap { id ->
userDao.getUserByIdWithChanges(id)
}
@Zhuinden
Zhuinden / MyFragmentWithRx.kt
Last active Nov 27, 2019
Anemic Repository 2
View MyFragmentWithRx.kt
class MyFragment: Fragment() {
private val compositeDisposable = CompositeDisposable()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
compositeDisposable += myView.events.subscribeBy { event ->
when(event) {
OnButtonPressed -> { ... }
OnTextChanged -> { text -> ... }
View MyFragmentWithListener.kt
class MyFragment: Fragment(), MyView.Listener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
myView.listener = this
}
override fun onDestroyView() {
super.onDestroyView()
myView.listener = null
}
@Zhuinden
Zhuinden / MyFragmentPagerAdapter.kt
Last active Nov 14, 2019
implementing FragmentPagerAdapter in a Fragment
View MyFragmentPagerAdapter.kt
class MyFragmentPagerAdapter(
private val context: Context,
fragmentManager: FragmentManager
) : FragmentPagerAdapter(fragmentManager) {
override fun getCount() = 2
override fun getItem(position: Int) = when(position) {
0 -> FirstFragment()
1 -> SecondFragment()
else -> throw IllegalStateException("Unexpected position $position")
View InvertViewLayout.kt
package com.davidmedenjak.overlappinginvertview
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
class InvertViewLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) {
You can’t perform that action at this time.