Skip to content

Commit

Permalink
NTV-153:Checkout – Risk messaging (#1409)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadia committed Sep 27, 2021
1 parent 7a1b8e4 commit ea897c1
Show file tree
Hide file tree
Showing 25 changed files with 911 additions and 30 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,35 @@
package com.kickstarter.screenshoot.testing.ui.components

import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.test.platform.app.InstrumentationRegistry
import com.karumi.shot.ScreenshotTest
import com.kickstarter.ApplicationComponent
import com.kickstarter.R
import com.kickstarter.screenshoot.testing.InstrumentedApp
import org.junit.Before
import org.junit.Test

class RiskMessageShotTest : ScreenshotTest {

lateinit var component: ApplicationComponent

@Before
fun setup() {
// - Test Application
val app = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as InstrumentedApp
// - Test Dagger component for injecting on environment Mock Objects
component = app.component()
}

@Test
fun layoutInitializationByDefaultTest() {
val layout = (
LayoutInflater.from(InstrumentationRegistry.getInstrumentation().targetContext).inflate(
R.layout.fragment_checkout_risk_message, null
) as ConstraintLayout
).findViewById(R.id.risk_message_cl) as ConstraintLayout

compareScreenshot(layout)
}
}
41 changes: 38 additions & 3 deletions app/src/main/assets/json/server-config.json

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion app/src/main/java/com/kickstarter/libs/AnalyticEvents.kt
Expand Up @@ -33,6 +33,7 @@ import com.kickstarter.libs.utils.EventContextValues.CtaContextName.DISCOVER_SOR
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_INITIATE
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_OR_SIGN_UP
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_SUBMIT
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.PLEDGE_CONFIRM
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.PLEDGE_INITIATE
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.PLEDGE_SUBMIT
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.REWARD_CONTINUE
Expand Down Expand Up @@ -243,7 +244,7 @@ class AnalyticEvents(trackingClients: List<TrackingClientType?>) {
*/
fun trackDiscoveryPageViewed(discoveryParams: DiscoveryParams) {
val props = AnalyticEventsUtils.discoveryParamsProperties(discoveryParams).toMutableMap()
props[DISCOVER_SORT.contextName] = discoveryParams.sort()?.name?.toLowerCase(Locale.ROOT) ?: ""
props[DISCOVER_SORT.contextName] = discoveryParams.sort()?.name?.lowercase(Locale.ROOT) ?: ""
props[CONTEXT_PAGE.contextName] = DISCOVER.contextName
client.track(PAGE_VIEWED.eventName, props)
}
Expand Down Expand Up @@ -375,6 +376,20 @@ class AnalyticEvents(trackingClients: List<TrackingClientType?>) {
client.track(PAGE_VIEWED.eventName, props)
}

/**
* Sends data associated with the Confirm CTA click event to the client.
*
* @param checkoutData: The checkout data.
* @param pledgeData: The selected pledge data.
*/
fun trackPledgeConfirmCTA(checkoutData: CheckoutData, pledgeData: PledgeData) {
val props: HashMap<String, Any> = hashMapOf(CONTEXT_CTA.contextName to PLEDGE_CONFIRM.contextName)
props[CONTEXT_TYPE.contextName] = CREDIT_CARD.contextName
props[CONTEXT_PAGE.contextName] = CHECKOUT.contextName
props.putAll(AnalyticEventsUtils.checkoutDataProperties(checkoutData, pledgeData, client.loggedInUser()))
client.track(CTA_CLICKED.eventName, props)
}

/**
* Sends data associated with the submit CTA click event to the client.
*
Expand Down
@@ -0,0 +1,250 @@
package com.kickstarter.libs;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.kickstarter.libs.qualifiers.RequiresFragmentViewModel;
import com.kickstarter.libs.utils.BundleUtils;
import com.kickstarter.ui.data.ActivityResult;
import com.trello.rxlifecycle.FragmentEvent;
import com.trello.rxlifecycle.RxLifecycle;
import com.trello.rxlifecycle.components.FragmentLifecycleProvider;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import timber.log.Timber;

public class BaseBottomSheetDialogFragment<ViewModelType extends FragmentViewModel> extends BottomSheetDialogFragment implements FragmentLifecycleProvider, FragmentLifecycleType {
private static final String VIEW_MODEL_KEY = "FragmentViewModel";
private final BehaviorSubject<FragmentEvent> lifecycle = BehaviorSubject.create();
protected ViewModelType viewModel;
private final BroadcastReceiver optimizelyReadyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
BaseBottomSheetDialogFragment.this.viewModel.optimizelyReady();
}
};

/**
* Returns an observable of the fragment's lifecycle events.
*/
@Override
public final @NonNull Observable<FragmentEvent> lifecycle() {
return this.lifecycle.asObservable();
}

/**
* Completes an observable when an {@link FragmentEvent} occurs in the fragment's lifecycle.
*/
@Override
public final @NonNull
<T> Observable.Transformer<T, T> bindUntilEvent(final @NonNull FragmentEvent event) {
return RxLifecycle.bindUntilFragmentEvent(this.lifecycle, event);
}

/**
* Completes an observable when the lifecycle event opposing the current lifecyle event is emitted.
* For example, if a subscription is made during {@link FragmentEvent#CREATE}, the observable will be completed
* in {@link FragmentEvent#DESTROY}.
*/
@Override
public final @NonNull
<T> Observable.Transformer<T, T> bindToLifecycle() {
return RxLifecycle.bindFragment(this.lifecycle);
}

/**
* Called before `onCreate`, when a fragment is attached to its context.
*/
@CallSuper
@Override
public void onAttach(final @NonNull Context context) {
super.onAttach(context);
Timber.d("onAttach %s", this.toString());
this.lifecycle.onNext(FragmentEvent.ATTACH);
}

@CallSuper
@Override
public void onCreate(final @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Timber.d("onCreate %s", this.toString());

this.lifecycle.onNext(FragmentEvent.CREATE);

assignViewModel(savedInstanceState);

this.viewModel.arguments(getArguments());
}

/**
* Called when a fragment instantiates its user interface view, between `onCreate` and `onActivityCreated`.
* Can return null for non-graphical fragments.
*/
@CallSuper
@Override
public @Nullable
View onCreateView(final @NonNull LayoutInflater inflater, final @Nullable ViewGroup container,
final @Nullable Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
Timber.d("onCreateView %s", this.toString());
this.lifecycle.onNext(FragmentEvent.CREATE_VIEW);
return view;
}

@CallSuper
@Override
public void onStart() {
super.onStart();
Timber.d("onStart %s", this.toString());
this.lifecycle.onNext(FragmentEvent.START);
}

@CallSuper
@Override
public void onResume() {
super.onResume();
Timber.d("onResume %s", this.toString());
this.lifecycle.onNext(FragmentEvent.RESUME);

assignViewModel(null);
if (this.viewModel != null) {
this.viewModel.onResume(this);

final FragmentActivity activity = getActivity();
if (activity != null) {
activity.registerReceiver(this.optimizelyReadyReceiver, new IntentFilter(ExperimentsClientTypeKt.EXPERIMENTS_CLIENT_READY));
}
}
}

@CallSuper
@Override
public void onPause() {
this.lifecycle.onNext(FragmentEvent.PAUSE);
super.onPause();
Timber.d("onPause %s", this.toString());

if (this.viewModel != null) {
this.viewModel.onPause();

final FragmentActivity activity = getActivity();
if (activity != null) {
activity.unregisterReceiver(this.optimizelyReadyReceiver);
}
}
}

@CallSuper
@Override
public void onStop() {
this.lifecycle.onNext(FragmentEvent.STOP);
super.onStop();
Timber.d("onStop %s", this.toString());
}

/**
* Called when the view created by `onCreateView` has been detached from the fragment.
* The lifecycle subject must be pinged before it is destroyed by the fragment.
*/
@CallSuper
@Override
public void onDestroyView() {
this.lifecycle.onNext(FragmentEvent.DESTROY_VIEW);
super.onDestroyView();
}

@CallSuper
@Override
public void onDestroy() {
this.lifecycle.onNext(FragmentEvent.DESTROY);
super.onDestroy();
Timber.d("onDestroy %s", this.toString());

if (this.viewModel != null) {
this.viewModel.onDestroy();
}
}

/**
* Called after `onDestroy` when the fragment is no longer attached to its activity.
*/
@CallSuper
@Override
public void onDetach() {
Timber.d("onDetach %s", this.toString());
super.onDetach();

if (getActivity().isFinishing()) {
if (this.viewModel != null) {
// Order of the next two lines is important: the lifecycle should update before we
// complete the view publish subject in the view model.
this.lifecycle.onNext(FragmentEvent.DETACH);
this.viewModel.onDetach();

FragmentViewModelManager.getInstance().destroy(this.viewModel);
this.viewModel = null;
}
}
}

@CallSuper
@Override
public void onSaveInstanceState(final @NonNull Bundle outState) {
super.onSaveInstanceState(outState);

final Bundle viewModelEnvelope = new Bundle();
if (this.viewModel != null) {
FragmentViewModelManager.getInstance().save(this.viewModel, viewModelEnvelope);
}

outState.putBundle(VIEW_MODEL_KEY, viewModelEnvelope);
}

@CallSuper
@Override
public void onActivityResult(final int requestCode, final int resultCode, final @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Timber.d("onActivityResult %s", this.toString());
this.viewModel.activityResult(ActivityResult.create(requestCode, resultCode, data));
}

/**
* setState will indicate if the fragment has suffer any animation changes on the parents container
* - In case no animation externally applied to the fragment call this method with `false` value
* - In case some animation externally applied to the Fragment as is the case for:
* `BackingFragment` and `RewardsFragment` call this method with `true` value when needed
*
* @param state = true in case the fragment is displayed on the screen with full width/height
* state = false in case the fragment is displayed on the screen but hidden or width/Height = 0
*/
public void setState(final Boolean state) {
if (this.viewModel != null) {
this.viewModel.isExpanded(state);
}
}

private void assignViewModel(final @Nullable Bundle viewModelEnvelope) {
if (this.viewModel == null) {
final RequiresFragmentViewModel annotation = getClass().getAnnotation(RequiresFragmentViewModel.class);
final Class<ViewModelType> viewModelClass = annotation == null ? null : (Class<ViewModelType>) annotation.value();
if (viewModelClass != null) {
this.viewModel = FragmentViewModelManager.getInstance().fetch(getContext(),
viewModelClass,
BundleUtils.maybeGetBundle(viewModelEnvelope, VIEW_MODEL_KEY));
}
}
}
}
Expand Up @@ -4,7 +4,9 @@ class OptimizelyExperiment {
enum class Key(val key: String) {
PLEDGE_CTA_COPY("pledge_cta_copy"),
CAMPAIGN_DETAILS("native_project_page_campaign_details"),
CREATOR_DETAILS("native_project_page_conversion_creator_details")
CREATOR_DETAILS("native_project_page_conversion_creator_details"),
SUGGESTED_NO_REWARD_AMOUNT("suggested_no_reward_amount"),
NATIVE_RISK_MESSAGING("native_risk_messaging")
}

enum class Variant(val rawValue: String?) {
Expand Down
Expand Up @@ -28,6 +28,7 @@ class EventContextValues {
ADD_ONS_CONTINUE("add_ons_continue"),
PLEDGE_INITIATE("pledge_initiate"),
PLEDGE_SUBMIT("pledge_submit"),
PLEDGE_CONFIRM("pledge_confirm"),
REWARD_CONTINUE("reward_continue"),
DISCOVER_SORT("discover_sort"),
DISCOVER_FILTER("discover_filter"),
Expand Down
Expand Up @@ -468,7 +468,9 @@ class ProjectActivity :

private fun setFragmentsState(expand: Boolean) {
supportFragmentManager.fragments.map { fragment ->
(fragment as BaseFragment<*>).setState(expand && fragment.isVisible)
if (fragment is BaseFragment<*>) {
fragment.setState(expand && fragment.isVisible)
}
}
}

Expand Down

0 comments on commit ea897c1

Please sign in to comment.