Skip to content

Commit

Permalink
Merge pull request #9187 from wordpress-mobile/issue/9157-site-creati…
Browse files Browse the repository at this point in the history
…on-back-pressed

Issue/9157 site creation back pressed
  • Loading branch information
oguzkocer authored Feb 13, 2019
2 parents f3ce309 + b2baf64 commit 943c64c
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import org.wordpress.android.R
*/
class BasicFragmentDialog : AppCompatDialogFragment() {
private lateinit var mTag: String
private lateinit var mTitle: String
private lateinit var mMessage: String
private lateinit var mPositiveButtonLabel: String
private var mTitle: String? = null
private var mNegativeButtonLabel: String? = null
private var mCancelButtonLabel: String? = null
private var dismissedByPositiveButton = false
Expand All @@ -37,7 +37,7 @@ class BasicFragmentDialog : AppCompatDialogFragment() {

fun initialize(
tag: String,
title: String,
title: String?,
message: String,
positiveButtonLabel: String,
negativeButtonLabel: String? = null,
Expand Down Expand Up @@ -79,8 +79,7 @@ class BasicFragmentDialog : AppCompatDialogFragment() {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = Builder(ContextThemeWrapper(activity, R.style.Calypso_Dialog_Alert))
builder.setTitle(mTitle)
.setMessage(mMessage)
builder.setMessage(mMessage)
.setPositiveButton(mPositiveButtonLabel) { _, _ ->
dismissedByPositiveButton = true
val activity = activity
Expand All @@ -89,6 +88,10 @@ class BasicFragmentDialog : AppCompatDialogFragment() {
}
}.setCancelable(true)

mTitle?.let {
builder.setTitle(mTitle)
}

mNegativeButtonLabel?.let {
builder.setNegativeButton(mNegativeButtonLabel) { _, _ ->
dismissedByNegativeButton = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ import org.wordpress.android.WordPress
import org.wordpress.android.ui.ActivityLauncher
import org.wordpress.android.ui.accounts.HelpActivity.Origin
import org.wordpress.android.ui.main.SitePickerActivity
import org.wordpress.android.ui.posts.BasicFragmentDialog.BasicDialogNegativeClickInterface
import org.wordpress.android.ui.posts.BasicFragmentDialog.BasicDialogPositiveClickInterface
import org.wordpress.android.ui.sitecreation.NewSiteCreationMainVM.NewSiteCreationScreenTitle.ScreenTitleEmpty
import org.wordpress.android.ui.sitecreation.NewSiteCreationMainVM.NewSiteCreationScreenTitle.ScreenTitleGeneral
import org.wordpress.android.ui.sitecreation.NewSiteCreationMainVM.NewSiteCreationScreenTitle.ScreenTitleStepCount
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteCreationCompleted
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteNotCreated
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteNotInLocalDb
import org.wordpress.android.ui.sitecreation.SiteCreationStep.DOMAINS
import org.wordpress.android.ui.sitecreation.SiteCreationStep.SEGMENTS
import org.wordpress.android.ui.sitecreation.SiteCreationStep.SITE_INFO
import org.wordpress.android.ui.sitecreation.SiteCreationStep.SITE_PREVIEW
import org.wordpress.android.ui.sitecreation.SiteCreationStep.VERTICALS
import org.wordpress.android.ui.sitecreation.previews.NewSiteCreationPreviewFragment
import org.wordpress.android.ui.sitecreation.previews.SitePreviewScreenListener
import org.wordpress.android.ui.sitecreation.domains.DomainsScreenListener
import org.wordpress.android.ui.sitecreation.domains.NewSiteCreationDomainsFragment
import org.wordpress.android.ui.sitecreation.misc.OnHelpClickedListener
import org.wordpress.android.ui.sitecreation.misc.OnSkipClickedListener
import org.wordpress.android.ui.sitecreation.previews.NewSiteCreationPreviewFragment
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteCreationCompleted
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteNotCreated
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState.SiteNotInLocalDb
import org.wordpress.android.ui.sitecreation.previews.SitePreviewScreenListener
import org.wordpress.android.ui.sitecreation.segments.NewSiteCreationSegmentsFragment
import org.wordpress.android.ui.sitecreation.segments.SegmentsScreenListener
import org.wordpress.android.ui.sitecreation.siteinfo.NewSiteCreationSiteInfoFragment
import org.wordpress.android.ui.sitecreation.siteinfo.SiteInfoScreenListener
import org.wordpress.android.ui.sitecreation.verticals.NewSiteCreationVerticalsFragment
import org.wordpress.android.ui.sitecreation.verticals.VerticalsScreenListener
import org.wordpress.android.ui.utils.UiHelpers
import org.wordpress.android.util.wizard.WizardNavigationTarget
import javax.inject.Inject

Expand All @@ -48,8 +51,11 @@ class NewSiteCreationActivity : AppCompatActivity(),
SiteInfoScreenListener,
SitePreviewScreenListener,
OnSkipClickedListener,
OnHelpClickedListener {
OnHelpClickedListener,
BasicDialogPositiveClickInterface,
BasicDialogNegativeClickInterface {
@Inject internal lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject internal lateinit var uiHelpers: UiHelpers
private lateinit var mainViewModel: NewSiteCreationMainVM

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -89,6 +95,21 @@ class NewSiteCreationActivity : AppCompatActivity(),
finish()
}
})
mainViewModel.dialogActionObservable.observe(this, Observer { dialogHolder ->
dialogHolder?.let {
val supportFragmentManager = requireNotNull(supportFragmentManager) {
"FragmentManager can't be null at this point"
}
dialogHolder.show(this, supportFragmentManager, uiHelpers)
}
})
mainViewModel.exitFlowObservable.observe(this, Observer {
setResult(Activity.RESULT_CANCELED)
finish()
})
mainViewModel.onBackPressedObservable.observe(this, Observer {
super.onBackPressed()
})
}

override fun onSegmentSelected(segmentId: Long) {
Expand All @@ -103,6 +124,10 @@ class NewSiteCreationActivity : AppCompatActivity(),
mainViewModel.onDomainsScreenFinished(domain)
}

override fun onSiteCreationCompleted() {
mainViewModel.onSiteCreationCompleted()
}

override fun onSitePreviewScreenDismissed(createSiteState: CreateSiteState) {
mainViewModel.onSitePreviewScreenFinished(createSiteState)
}
Expand Down Expand Up @@ -162,6 +187,14 @@ class NewSiteCreationActivity : AppCompatActivity(),
fragmentTransaction.commit()
}

override fun onPositiveClicked(instanceTag: String) {
mainViewModel.onPositiveDialogButtonClicked(instanceTag)
}

override fun onNegativeClicked(instanceTag: String) {
mainViewModel.onNegativeDialogButtonClicked(instanceTag)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
onBackPressed()
Expand All @@ -171,9 +204,6 @@ class NewSiteCreationActivity : AppCompatActivity(),
}

override fun onBackPressed() {
if (!mainViewModel.shouldSuppressBackPress()) {
mainViewModel.onBackPressed()
super.onBackPressed()
}
mainViewModel.onBackPressed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import org.wordpress.android.ui.sitecreation.NewSiteCreationMainVM.NewSiteCreati
import org.wordpress.android.ui.sitecreation.NewSiteCreationMainVM.NewSiteCreationScreenTitle.ScreenTitleStepCount
import org.wordpress.android.ui.sitecreation.misc.NewSiteCreationTracker
import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.CreateSiteState
import org.wordpress.android.ui.utils.UiString.UiStringRes
import org.wordpress.android.util.wizard.WizardManager
import org.wordpress.android.util.wizard.WizardNavigationTarget
import org.wordpress.android.util.wizard.WizardState
import org.wordpress.android.viewmodel.SingleEventObservable
import org.wordpress.android.viewmodel.SingleLiveEvent
import org.wordpress.android.viewmodel.helpers.DialogHolder
import javax.inject.Inject

const val TAG_WARNING_DIALOG = "back_pressed_warning_dialog"
const val KEY_CURRENT_STEP = "key_current_step"
const val KEY_SITE_CREATION_STATE = "key_site_creation_state"

Expand All @@ -41,6 +44,7 @@ class NewSiteCreationMainVM @Inject constructor(
private val wizardManager: WizardManager<SiteCreationStep>
) : ViewModel() {
private var isStarted = false
private var siteCreationCompleted = false

private lateinit var siteCreationState: SiteCreationState

Expand All @@ -52,9 +56,18 @@ class NewSiteCreationMainVM @Inject constructor(
)
}

private val _dialogAction = SingleLiveEvent<DialogHolder>()
val dialogActionObservable: LiveData<DialogHolder> = _dialogAction

private val _wizardFinishedObservable = SingleLiveEvent<CreateSiteState>()
val wizardFinishedObservable: LiveData<CreateSiteState> = _wizardFinishedObservable

private val _exitFlowObservable = SingleLiveEvent<Unit>()
val exitFlowObservable: LiveData<Unit> = _exitFlowObservable

private val _onBackPressedObservable = SingleLiveEvent<Unit>()
val onBackPressedObservable: LiveData<Unit> = _onBackPressedObservable

fun start(savedInstanceState: Bundle?) {
if (isStarted) return
if (savedInstanceState == null) {
Expand Down Expand Up @@ -82,10 +95,23 @@ class NewSiteCreationMainVM @Inject constructor(
wizardManager.showNextStep()
}

fun shouldSuppressBackPress(): Boolean = wizardManager.isLastStep()

fun onBackPressed() {
wizardManager.onBackPressed()
return if (wizardManager.isLastStep()) {
if (siteCreationCompleted) {
exitFlow(false)
} else {
_dialogAction.value = DialogHolder(
tag = TAG_WARNING_DIALOG,
title = null,
message = UiStringRes(R.string.new_site_creation_preview_back_pressed_warning),
positiveButton = UiStringRes(R.string.exit),
negativeButton = UiStringRes(R.string.cancel)
)
}
} else {
wizardManager.onBackPressed()
_onBackPressedObservable.call()
}
}

fun onVerticalsScreenFinished(verticalId: String) {
Expand Down Expand Up @@ -124,10 +150,42 @@ class NewSiteCreationMainVM @Inject constructor(
}
}

fun onSiteCreationCompleted() {
siteCreationCompleted = true
}

/**
* Exits the flow and tracks an event when the user force-exits the "site creation in progress" before it completes.
*/
private fun exitFlow(forceExit: Boolean) {
if (forceExit) {
tracker.trackFlowExited()
}
_exitFlowObservable.call()
}

fun onSitePreviewScreenFinished(createSiteState: CreateSiteState) {
_wizardFinishedObservable.value = createSiteState
}

fun onPositiveDialogButtonClicked(instanceTag: String) {
when (instanceTag) {
TAG_WARNING_DIALOG -> {
exitFlow(true)
}
else -> NotImplementedError("Unknown dialog tag: $instanceTag")
}
}

fun onNegativeDialogButtonClicked(instanceTag: String) {
when (instanceTag) {
TAG_WARNING_DIALOG -> {
// do nothing
}
else -> NotImplementedError("Unknown dialog tag: $instanceTag")
}
}

sealed class NewSiteCreationScreenTitle {
data class ScreenTitleStepCount(@StringRes val resId: Int, val stepsCount: Int, val stepPosition: Int) :
NewSiteCreationScreenTitle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class NewSiteCreationTracker @Inject constructor(val tracker: AnalyticsTrackerWr
tracker.track(AnalyticsTracker.Stat.ENHANCED_SITE_CREATION_COMPLETED)
}

fun trackFlowExited() {
tracker.track(AnalyticsTracker.Stat.ENHANCED_SITE_CREATION_EXITED)
}

fun trackErrorShown(errorContext: String, errorType: NewSiteCreationErrorType, errorDescription: String? = null) {
trackErrorShown(errorContext, errorType.toString().toLowerCase(), errorDescription)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class NewSiteCreationPreviewFragment : NewSiteCreationBaseFormFragment(),
viewModel.onHelpClicked.observe(this, Observer {
helpClickedListener.onHelpClicked(HelpActivity.Origin.SITE_CREATION_CREATING)
})
viewModel.onSiteCreationCompleted.observe(this, Observer {
sitePreviewScreenListener.onSiteCreationCompleted()
})
viewModel.onOkButtonClicked.observe(this, Observer { createSiteState ->
createSiteState?.let {
sitePreviewScreenListener.onSitePreviewScreenDismissed(createSiteState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class NewSitePreviewViewModel @Inject constructor(
private val _onOkButtonClicked = SingleLiveEvent<CreateSiteState>()
val onOkButtonClicked: LiveData<CreateSiteState> = _onOkButtonClicked

private val _onSiteCreationCompleted = SingleLiveEvent<CreateSiteState>()
val onSiteCreationCompleted: LiveData<CreateSiteState> = _onSiteCreationCompleted

init {
dispatcher.register(fetchWpComSiteUseCase)
}
Expand Down Expand Up @@ -176,6 +179,7 @@ class NewSitePreviewViewModel @Inject constructor(
val remoteSiteId = event.payload as Long
createSiteState = SiteNotInLocalDb(remoteSiteId)
fetchNewlyCreatedSiteModel(remoteSiteId)
_onSiteCreationCompleted.asyncCall()
}
FAILURE -> {
serviceStateForRetry = event.payload as NewSiteCreationServiceState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import org.wordpress.android.ui.sitecreation.previews.NewSitePreviewViewModel.Cr

interface SitePreviewScreenListener {
fun onSitePreviewScreenDismissed(createSiteState: CreateSiteState)
fun onSiteCreationCompleted()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.wordpress.android.ui.utils.UiString

class DialogHolder(
private val tag: String,
private val title: UiString,
private val title: UiString?,
private val message: UiString,
private val positiveButton: UiString,
private val negativeButton: UiString
Expand All @@ -17,7 +17,7 @@ class DialogHolder(
val dialog = BasicFragmentDialog()
dialog.initialize(
tag = tag,
title = uiHelpers.getTextOfUiString(context, title),
title = title?.let { uiHelpers.getTextOfUiString(context, title) },
message = uiHelpers.getTextOfUiString(context, message),
positiveButtonLabel = uiHelpers.getTextOfUiString(context, positiveButton),
negativeButtonLabel = uiHelpers.getTextOfUiString(context, negativeButton),
Expand Down
2 changes: 2 additions & 0 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<string name="preview_count">Preview %d</string>
<string name="write_post">Write Post</string>
<string name="dismiss">dismiss</string>
<string name="exit">exit</string>

<string name="button_not_now">Not now</string>

Expand Down Expand Up @@ -2506,4 +2507,5 @@
<string name="site_created_but_not_fetched_snackbar_message">It seems like you\'re on a slow connection. If you don\'t see your new site in the list, try refreshing.</string>
<string name="new_site_creation_clear_all_content_description">Clear</string>
<string name="new_site_creation_site_preview_content_description">Showing site preview</string>
<string name="new_site_creation_preview_back_pressed_warning">You may lose your progress. Are you sure you want to exit?</string>
</resources>
Loading

0 comments on commit 943c64c

Please sign in to comment.