diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c7d4f040..d377b664 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -115,6 +115,9 @@ + (ActivityGiftInboxBinding::inflate) { + private val viewModel: GiftInboxViewModel by viewModel() + private val inboxAdapter = GiftInboxAdapter( + onCardClick = { viewModel.onCardClick(it) }, + onInquiryClick = { viewModel.onInquiryClick() }, + onRetryClick = { viewModel.retry() } + ) + private val viewMode: GiftInboxViewMode by lazy { + GiftInboxViewMode.entries.firstOrNull { it.name == intent.getStringExtra(EXTRA_GIFT_INBOX_VIEW_MODE) } + ?: GiftInboxViewMode.ALL + } + + override fun setupView() { + binding.btnBack.setOnClickListener { finish() } + binding.retry.setOnClickListener { viewModel.retry() } + binding.refresh.setOnRefreshListener { viewModel.refresh() } + binding.refresh.setColorSchemeResources(R.color.soda_400) + val layoutManager = LinearLayoutManager(this) + binding.list.layoutManager = layoutManager + binding.list.adapter = inboxAdapter + binding.list.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + if (dy > 0 && layoutManager.findLastVisibleItemPosition() >= inboxAdapter.itemCount - 3) { + viewModel.loadNextPage() + } + } + }) + viewModel.state.observe(this, ::render) + viewModel.toastLiveData.observe(this) { event -> + if (event != null) { + viewModel.consumeToast()?.let { toast -> + showToast(toast.resId?.let(::getString) ?: toast.message.orEmpty()) + } + } + } + viewModel.inquiryLiveData.observe(this) { requested -> + if (requested) viewModel.consumeInquiryIntent()?.let(::startActivity) + } + viewModel.loadFirstPage() + } + + private fun render(state: GiftInboxUiState) { + val content = state as? GiftInboxUiState.Content + binding.progress.isVisible = state == GiftInboxUiState.Loading + binding.error.isVisible = state is GiftInboxUiState.Error + binding.refresh.isRefreshing = content?.isRefreshing == true + binding.refresh.isEnabled = state != GiftInboxUiState.Loading + binding.list.isVisible = state is GiftInboxUiState.Content || state == GiftInboxUiState.Empty + if (state is GiftInboxUiState.Error) { + binding.errorMessage.text = state.message?.takeIf { it.isNotBlank() } ?: getString(R.string.common_error_unknown) + } + if (binding.list.isVisible) { + inboxAdapter.submitList( + GiftInboxPresenter.toUiModels( + items = content?.items.orEmpty(), + viewMode = viewMode, + isCreator = SharedPreferenceManager.role == MemberRole.CREATOR.name, + isAppending = content?.isAppending == true, + canRetry = content?.appendError == true || content?.refreshError == true + ) + ) { + binding.list.post { + if (!binding.list.canScrollVertically(1)) viewModel.loadNextPage() + } + } + } + } + + companion object { + const val EXTRA_GIFT_INBOX_VIEW_MODE = "EXTRA_GIFT_INBOX_VIEW_MODE" + + fun newIntent(context: Context, viewMode: GiftInboxViewMode = GiftInboxViewMode.ALL): Intent = + Intent(context, GiftInboxActivity::class.java).putExtra(EXTRA_GIFT_INBOX_VIEW_MODE, viewMode.name) + } +} diff --git a/app/src/main/res/layout/activity_gift_inbox.xml b/app/src/main/res/layout/activity_gift_inbox.xml new file mode 100644 index 00000000..8ef14379 --- /dev/null +++ b/app/src/main/res/layout/activity_gift_inbox.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + +