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
dependencies { | |
testImplementation "junit:junit:$junitVersion" | |
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion" | |
} |
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 MyActivity: AppCompatActivity() { | |
private val viewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceBundle: Bundle?) { | |
/*..*/ | |
lifecycleScope.launch { | |
repeatOnLifecycle(Lifecycle.State.STARTED) { | |
viewModel.uiState.collect { state -> | |
if (state.isAddedSuccessful == true) { |
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
data class MyUiState( | |
val info: MyInfoModel, | |
val isLoading: Boolean = false, | |
val isAddedSuccessful: Boolean? = null | |
) | |
class MyViewModel(...): ViewModel() { | |
private val _uiState = MutableStateFlow<MyUiState>(...) | |
val uiState: StateFlow<MyUiState> get() = _uiState.asStateFlow() |
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 MyActivity: AppCompatActivity() { | |
private val viewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
/*..*/ | |
lifecycleScope.launch { | |
repeatOnLifecycle(Lifecycle.State.STARTED) { | |
viewModel.navigateToAddedScreen.collect { isSuccessful -> | |
val intent = Intent(this, AddedActivity::class.java) |
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 MyViewModel(...): ViewModel() { | |
private val _uiState: MutableStateFlow<MyUiState> = /*..*/ | |
val uiState: StateFlow<MyUiState> get() = _uiState | |
// 이렇게 사용하지 마세요 | |
private val _navigateToAddedScreen: Channel<Boolean> = Channel<Boolean>() | |
val navigateToAddedScreen: Flow<Boolean> = _navigateToAddedScreen.receiveAsFlow() | |
private var makeAddJob: Job? = null |
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 MyActivity: AppCompatActivity() { | |
private lateinit var binding: ActivityMyBinding | |
private val viewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
/*...*/ | |
binding.refreshButton.setOnClickListener { | |
viewModel.refresh() | |
} |
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 MyActivity: AppCompatActivity() { | |
private lateinit var binding: ActivityMyBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
/*...*/ | |
with(binding) { | |
expandButton.setOnClickListener { | |
expandedView.visibility = View.VISIBLE |
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
ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == HeaderViewHolder.TYPE) { | |
return new HeaderViewHolder(parent); | |
} | |
return new MyViewHolder(parent); | |
} |
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
ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == HeaderViewHolder.TYPE) { | |
if (mHeaderViewHolder == null) { | |
mHeaderViewHolder = new HeaderViewHolder(parent); | |
} | |
return mHeaderViewHolder; | |
} | |
return new MyViewHolder(parent); | |
} |
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
public void onBindViewHolder(MyViewHolder holder, int position, List payloads) { | |
if (payloads.isEmpty()) { | |
// 전체 리-바인드 | |
onBindViewHolder(holder, position); | |
} else if (payloads.contains(LIKE_UPDATE)) { | |
// 부분적인 리-바인드 | |
Item item = mItems.get(position); | |
holder.likeButton.setEnabled(!item.isLiked()); | |
} | |
} |