Skip to content

Commit

Permalink
[MBL-1478][MBL-1482] Add Primary Action button actions for Take Surve…
Browse files Browse the repository at this point in the history
…y and Confirm Address (#2084)

* add primary button actions for take survey and confirm address

* update test to reflect updated screen calls

* remove unused import

* lint

* add error handling for response for PPO query
  • Loading branch information
mtgriego authored Jul 31, 2024
1 parent f54f905 commit e76dce3
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.kickstarter.databinding.ActivityBackingDetailsBinding
import com.kickstarter.libs.utils.extensions.getEnvironment
import com.kickstarter.ui.IntentKey
import com.kickstarter.ui.extensions.finishWithAnimation
import com.kickstarter.viewmodels.BackingDetailsViewModel
import kotlinx.coroutines.launch

const val REFRESH = "refresh"

class BackingDetailsActivity : AppCompatActivity() {

private lateinit var binding: ActivityBackingDetailsBinding
Expand All @@ -39,7 +42,7 @@ class BackingDetailsActivity : AppCompatActivity() {
}

this.onBackPressedDispatcher.addCallback {
finishWithAnimation()
finishWithAnimation(withResult = REFRESH, intentKey = IntentKey.REFRESH_PPO_LIST)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ class PledgedProjectsOverviewActivity : AppCompatActivity() {
private lateinit var viewModelFactory: PledgedProjectsOverviewViewModel.Factory
private val viewModel: PledgedProjectsOverviewViewModel by viewModels { viewModelFactory }
private var theme = AppThemes.MATCH_SYSTEM.ordinal
private var startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data?.getBooleanExtra(IntentKey.FIX_PAYMENT_SUCCESS, false)
data?.let {
if (it.isTrue()) {
private var startForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data?.getBooleanExtra(IntentKey.FIX_PAYMENT_SUCCESS, false)
data?.let {
if (it.isTrue()) {
viewModel.getPledgedProjects()
}
}
val refresh = result.data?.getStringExtra(IntentKey.REFRESH_PPO_LIST)
if (!refresh.isNullOrEmpty()) {
viewModel.getPledgedProjects()
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -71,9 +76,11 @@ class PledgedProjectsOverviewActivity : AppCompatActivity() {

val ppoCardPagingSource = viewModel.ppoCardsState.collectAsLazyPagingItems()

val isLoading = ppoUIState.isLoading || ppoCardPagingSource.loadState.append is LoadState.Loading || ppoCardPagingSource.loadState.refresh is LoadState.Loading
val isLoading =
ppoUIState.isLoading || ppoCardPagingSource.loadState.append is LoadState.Loading || ppoCardPagingSource.loadState.refresh is LoadState.Loading
val isErrored = ppoUIState.isErrored || ppoCardPagingSource.loadState.hasError
val showEmptyState = ppoCardPagingSource.loadState.refresh is LoadState.NotLoading && ppoCardPagingSource.itemCount == 0
val showEmptyState =
ppoCardPagingSource.loadState.refresh is LoadState.NotLoading && ppoCardPagingSource.itemCount == 0

KickstarterApp(
useDarkTheme = isDarkModeEnabled(env = env)
Expand All @@ -86,29 +93,72 @@ class PledgedProjectsOverviewActivity : AppCompatActivity() {
ppoCards = ppoCardPagingSource,
totalAlerts = totalAlerts,
onAddressConfirmed = { viewModel.showSnackbarAndRefreshCardsList() },
onProjectPledgeSummaryClick = { url -> openBackingDetailsWebView(url) },
onSendMessageClick = { projectName -> viewModel.onMessageCreatorClicked(projectName) },
onProjectPledgeSummaryClick = { url ->
openBackingDetailsWebView(
url = url,
resultLauncher = null
)
},
onSendMessageClick = { projectName ->
viewModel.onMessageCreatorClicked(
projectName
)
},
isLoading = isLoading,
isErrored = isErrored,
showEmptyState = showEmptyState,
onSeeAllBackedProjectsClick = { startProfileActivity() },
pullRefreshCallback = {
// TODO call viewmodel.getPledgedProjects() here
viewModel.getPledgedProjects()
},
onPrimaryActionButtonClicked = { PPOCard ->
when (PPOCard.viewType()) {
PPOCardViewType.FIX_PAYMENT -> {
openManagePledge(
PPOCard.projectSlug ?: "",
resultLauncher = startForResult
)
}

PPOCardViewType.TAKE_SURVEY -> {
openBackingDetailsWebView(
url = PPOCard.backingDetailsUrl ?: "",
resultLauncher = startForResult
)
}

PPOCardViewType.CONFIRM_ADDRESS -> {
openBackingDetailsWebView(
url = PPOCard.backingDetailsUrl ?: "",
resultLauncher = startForResult
)
}

else -> {
}
}
},
onSecondaryActionButtonClicked = { PPOCard ->
},
onFixPaymentClick = { projectSlug -> openManagePledge(projectSlug, resultLauncher = startForResult) }
)
}

LaunchedEffect(Unit) {
viewModel.projectFlow
.collect {
startCreatorMessageActivity(project = it, previousScreen = MessagePreviousScreenType.PLEDGED_PROJECTS_OVERVIEW)
startCreatorMessageActivity(
project = it,
previousScreen = MessagePreviousScreenType.PLEDGED_PROJECTS_OVERVIEW
)
}
}

viewModel.provideSnackbarMessage { stringId, type ->
lifecycleScope.launch {
snackbarHostState.showSnackbar(message = getString(stringId), actionLabel = type)
snackbarHostState.showSnackbar(
message = getString(stringId),
actionLabel = type
)
}
}

Expand All @@ -122,13 +172,24 @@ class PledgedProjectsOverviewActivity : AppCompatActivity() {
}
}

private fun openBackingDetailsWebView(url: String) {
val intent = Intent(this, BackingDetailsActivity::class.java)
.putExtra(IntentKey.URL, url)
startActivity(intent)
private fun openBackingDetailsWebView(
url: String,
resultLauncher: ActivityResultLauncher<Intent>?
) {
resultLauncher?.launch(
Intent(this, BackingDetailsActivity::class.java)
.putExtra(IntentKey.URL, url)
) ?: {
val intent = Intent(this, BackingDetailsActivity::class.java)
.putExtra(IntentKey.URL, url)
startActivity(intent)
}
this.let {
TransitionUtils.transition(it, TransitionUtils.slideInFromRight())
}
}

fun startProfileActivity() {
private fun startProfileActivity() {
startActivity(
Intent(this, ProfileActivity::class.java)
)
Expand All @@ -137,7 +198,10 @@ class PledgedProjectsOverviewActivity : AppCompatActivity() {
}
}

private fun openManagePledge(projectSlug: String, resultLauncher: ActivityResultLauncher<Intent>) {
private fun openManagePledge(
projectSlug: String,
resultLauncher: ActivityResultLauncher<Intent>
) {
resultLauncher.launch(
Intent().getProjectIntent(this)
.putExtra(IntentKey.PROJECT_PARAM, projectSlug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ private fun PledgedProjectsOverviewScreenPreview() {
totalAlerts = 10,
onBackPressed = {},
onAddressConfirmed = {},
onPrimaryActionButtonClicked = {},
onSecondaryActionButtonClicked = {},
onProjectPledgeSummaryClick = {},
onSendMessageClick = {},
onSeeAllBackedProjectsClick = {},
errorSnackBarHostState = SnackbarHostState(),
onFixPaymentClick = {}
)
}
}
Expand All @@ -104,12 +105,13 @@ private fun PledgedProjectsOverviewScreenErrorPreview() {
totalAlerts = 10,
onBackPressed = {},
onAddressConfirmed = {},
onPrimaryActionButtonClicked = {},
onSecondaryActionButtonClicked = {},
onProjectPledgeSummaryClick = {},
onSendMessageClick = {},
onSeeAllBackedProjectsClick = {},
isErrored = true,
errorSnackBarHostState = SnackbarHostState(),
onFixPaymentClick = {}
)
}
}
Expand All @@ -131,10 +133,11 @@ private fun PledgedProjectsOverviewScreenEmptyPreview() {
totalAlerts = 0,
onBackPressed = {},
onAddressConfirmed = {},
onPrimaryActionButtonClicked = {},
onSecondaryActionButtonClicked = {},
onProjectPledgeSummaryClick = {},
onSendMessageClick = {},
errorSnackBarHostState = SnackbarHostState(),
onFixPaymentClick = {},
onSeeAllBackedProjectsClick = {},
)
}
Expand All @@ -154,11 +157,12 @@ fun PledgedProjectsOverviewScreen(
onProjectPledgeSummaryClick: (backingDetailsUrl: String) -> Unit,
onSendMessageClick: (projectName: String) -> Unit,
onSeeAllBackedProjectsClick: () -> Unit,
onPrimaryActionButtonClicked: (PPOCard) -> Unit,
onSecondaryActionButtonClicked: (PPOCard) -> Unit,
isLoading: Boolean = false,
isErrored: Boolean = false,
showEmptyState: Boolean = false,
pullRefreshCallback: () -> Unit = {},
onFixPaymentClick: (projectSlug: String) -> Unit,
) {
val openConfirmAddressAlertDialog = remember { mutableStateOf(false) }
var confirmedAddress by remember { mutableStateOf("") } // TODO: This is either the original shipping address or the user-edited address
Expand Down Expand Up @@ -244,20 +248,17 @@ fun PledgedProjectsOverviewScreen(
shippingAddress = it.address() ?: "", // TODO replace with formatted address from PPO response
showBadge = it.showBadge(),
onActionButtonClicked = {
when (it.viewType()) {
PPOCardViewType.FIX_PAYMENT -> {
onFixPaymentClick(it.projectSlug() ?: "")
}
else -> {}
}
onPrimaryActionButtonClicked(it)
},
onSecondaryActionButtonClicked = {
when (it.viewType()) {
PPOCardViewType.CONFIRM_ADDRESS -> {
confirmedAddress = it.address() ?: ""
openConfirmAddressAlertDialog.value = true
}
else -> {}
else -> {
onSecondaryActionButtonClicked(it)
}
}
},
timeNumberForAction = it.timeNumberForAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,10 @@ class KSApolloClientV2(val service: ApolloClient, val gson: Gson) : ApolloClient
}

override fun onResponse(response: Response<PledgedProjectsOverviewQuery.Data>) {
if (response.hasErrors()) {
ps.onError(Exception(response.errors?.first()?.message ?: ""))
}

response.data?.let { data ->
Observable.just(data.pledgeProjectsOverview())
.filter { it.pledges() != null }
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/kickstarter/ui/IntentKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ object IntentKey {
const val PREVIOUS_SCREEN = "com.kickstarter.kickstarter.previous_screen"
const val OAUTH_REDIRECT_URL = "com.kickstarter.kickstarter.oauth_redirect_url"
const val FIX_PAYMENT_SUCCESS = "com.kickstarter.kickstarter.fix_payment_success"
const val REFRESH_PPO_LIST = "com.kickstarter.kickstarter.refresh_ppo_list"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.kickstarter.R
import com.kickstarter.libs.featureflag.FlagKey
import com.kickstarter.libs.utils.ApplicationUtils
import com.kickstarter.libs.utils.extensions.getEnvironment
import com.kickstarter.ui.IntentKey
import com.kickstarter.ui.SharedPreferenceKey
import com.kickstarter.ui.activities.compose.FormularyScreen
import com.kickstarter.ui.activities.compose.ReportProjectCategoryScreen
Expand Down Expand Up @@ -102,7 +103,7 @@ class ReportProjectActivity : ComponentActivity() {
outputs = viewModel.outputs,
inputs = viewModel.inputs,
callback = {
finishWithAnimation(finishResult)
finishWithAnimation(finishResult, IntentKey.FLAGGINGKIND)
}
)
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/kickstarter/ui/extensions/ActivityExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ fun Activity.startProjectUpdatesActivity(projectAndData: ProjectData) {
overridePendingTransition(R.anim.slide_in_right, R.anim.fade_out_slide_out_left)
}

fun Activity.finishWithAnimation(withResult: String? = null) {
withResult?.let {
val intent = Intent().putExtra(IntentKey.FLAGGINGKIND, withResult)
setResult(Activity.RESULT_OK, intent)
fun Activity.finishWithAnimation(withResult: String? = null, intentKey: String? = null) {
intentKey?.let { key ->
withResult?.let { result ->
val intent = Intent().putExtra(key, result)
setResult(Activity.RESULT_OK, intent)
}
}
finish()
TransitionUtils.transition(this, TransitionUtils.slideInFromLeft())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ PledgedProjectsOverviewScreenTest : KSRobolectricTestCase() {
onAddressConfirmed = {},
onProjectPledgeSummaryClick = {},
onSeeAllBackedProjectsClick = {},
onFixPaymentClick = {}
onPrimaryActionButtonClicked = {},
onSecondaryActionButtonClicked = {}
)
}
}
Expand Down

0 comments on commit e76dce3

Please sign in to comment.