This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const val ARGUMENT_USER_ID = "ARGUMENT_USER_ID" | |
class UserDetailActivity : AppCompatActivity() { | |
// Pass Intent Argument to ViewModel | |
private val userDetailViewModel by lazy { | |
injector.userDetailViewModelFactory.create( | |
userId = intent.getIntExtra(ARGUMENT_USER_ID, 0) | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApplication : Application() { | |
// ... | |
val component: AppComponent by lazy { | |
DaggerAppComponent.builder() | |
.application(this) | |
.build() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = [AssistedInjectModule::class]) | |
@Singleton | |
interface ApplicationComponent { | |
// ... | |
@Component.Builder | |
interface Builder { | |
@BindsInstance | |
fun application(application: Application): Builder | |
fun build(): AppComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserDetailViewModel @AssistedInject constructor( | |
private val userRepository: UserRepository, | |
@Assisted private val userId: Int | |
) : ViewModel() { | |
private val reloadTrigger = MutableLiveData<Boolean>() | |
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) { | |
userRepository.getUserDetail(userId.toString(), reloadTrigger.value!!) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// module level gradle file | |
dependencies { | |
/* Dagger 2 - Assisted Inject */ | |
compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.3.3' | |
kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.3.3' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const val ARGUMENT_USER_ID = "ARGUMENT_USER_ID" | |
class UserDetailActivity : AppCompatActivity() { | |
@Inject | |
internal lateinit var viewModelFactory: ViewModelProvider.Factory | |
private lateinit var userDetailViewModel: UserDetailViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserDetailViewModel @Inject constructor( | |
private val userRepository: UserRepository | |
) : ViewModel() { | |
private lateinit var userId: String | |
private val reloadTrigger = MutableLiveData<Boolean>() | |
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) { | |
userRepository.getUserDetail(userId, reloadTrigger.value!!) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserListViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() { | |
init { | |
} | |
// DON'T DO THIS | |
fun getUsers(): LiveData<List<UserModel>> { | |
return userRepository.getUsers() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DON'T DO THIS | |
class UserListActivity : AppCompatActivity() { | |
@Inject internal lateinit var viewModelFactory: ViewModelProvider.Factory | |
private lateinit var userListViewModel: UserListViewModel | |
private lateinit var swipeRefreshLayout: SwipeRefreshLayout | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserListViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() { | |
private val reloadTrigger = MutableLiveData<Boolean>() | |
private val users: LiveData<List<UserModel>> = Transformations.switchMap(reloadTrigger) { | |
userRepository.getUsers() | |
} | |
init { | |
refreshUsers() | |
} |
NewerOlder