[android] Added business logic to sightseeing subscription screen

[android] Extracted duplicate logic in all pass and sightseeing subscripions into subscription delegate
This commit is contained in:
Александр Зацепин 2019-11-13 16:28:24 +03:00 committed by yoksnod
parent c61cbb6be1
commit 09e7a8e93a
11 changed files with 288 additions and 72 deletions

View file

@ -152,4 +152,31 @@ extern "C"
std::vector<std::string> items = BOOKMARK_INAPP_IDS;
return jni::ToJavaStringArray(env, items);
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_PrivateVariables_bookmarksSubscriptionSightsServerId(JNIEnv * env, jclass)
{
return env->NewStringUTF(BOOKMARKS_SUBSCRIPTION_SIGHTS_SERVER_ID);
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_PrivateVariables_bookmarksSubscriptionSightsYearlyProductId(JNIEnv * env,
jclass)
{
return env->NewStringUTF(BOOKMARKS_SUBSCRIPTION_SIGHTS_YEARLY_PRODUCT_ID);
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_PrivateVariables_bookmarksSubscriptionSightsMonthlyProductId(JNIEnv * env,
jclass)
{
return env->NewStringUTF(BOOKMARKS_SUBSCRIPTION_SIGHTS_MONTHLY_PRODUCT_ID);
}
JNIEXPORT jobjectArray JNICALL
Java_com_mapswithme_maps_PrivateVariables_bookmarksSubscriptionSightsNotUsedList(JNIEnv * env, jclass)
{
std::vector<std::string> items = BOOKMARKS_SUBSCRIPTION_SIGHTS_NOT_USED_LIST;
return jni::ToJavaStringArray(env, items);
}
}

View file

@ -83,7 +83,7 @@
android:layout_marginTop="@dimen/margin_base_plus"
android:orientation="horizontal" />
<com.mapswithme.maps.widget.SubscriptionButton
android:id="@+id/annual_sub_btn"
android:id="@+id/annual_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_base_plus"
@ -96,7 +96,7 @@
app:progressColor="@color/white_primary"
app:showSale="true" />
<com.mapswithme.maps.widget.SubscriptionButton
android:id="@+id/month_sub_btn"
android:id="@+id/monthly_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_base_plus"

View file

@ -542,7 +542,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
mBookmarkInappPurchaseController.initialize(this);
mBookmarksSubscriptionController
= PurchaseFactory.createBookmarksSubscriptionPurchaseController(this);
= PurchaseFactory.createBookmarksAllSubscriptionController(this);
mBookmarksSubscriptionController.initialize(this);
// To reduce number of parasite validation requests during orientation change.

View file

@ -49,6 +49,14 @@ public class PrivateVariables
public static native String bookmarksSubscriptionMonthlyProductId();
@NonNull
public static native String[] bookmarksSubscriptionNotUsedList();
@NonNull
public static native String bookmarksSubscriptionSightsServerId();
@NonNull
public static native String bookmarksSubscriptionSightsYearlyProductId();
@NonNull
public static native String bookmarksSubscriptionSightsMonthlyProductId();
@NonNull
public static native String[] bookmarksSubscriptionSightsNotUsedList();
/**
* @return interval in seconds
*/

View file

@ -1,12 +1,21 @@
package com.mapswithme.maps.purchase;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
public class AllPassSubscriptionActivity extends BaseMwmFragmentActivity
{
public static void startForResult(@NonNull FragmentActivity activity)
{
Intent intent = new Intent(activity, AllPassSubscriptionActivity.class);
activity.startActivityForResult(intent, PurchaseUtils.REQ_CODE_PAY_SUBSCRIPTION);
}
@Override
protected Class<? extends Fragment> getFragmentClass()
{

View file

@ -16,9 +16,7 @@ import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.DotPager;
import com.mapswithme.maps.widget.ParallaxBackgroundPageListener;
import com.mapswithme.maps.widget.ParallaxBackgroundViewPager;
import com.mapswithme.maps.widget.SubscriptionButton;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import java.util.ArrayList;
import java.util.List;
@ -27,18 +25,13 @@ public class AllPassSubscriptionPagerFragment extends AbstractBookmarkSubscripti
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mAnnualButton;
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mMonthlyButton;
@NonNull
private PurchaseUtils.Period mSelectedPeriod = PurchaseUtils.Period.P1Y;
private SubscriptionFragmentDelegate mDelegate;
@NonNull
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
{
return PurchaseFactory.createBookmarksSubscriptionPurchaseController(requireContext());
return PurchaseFactory.createBookmarksAllSubscriptionController(requireContext());
}
@Nullable
@ -48,17 +41,8 @@ public class AllPassSubscriptionPagerFragment extends AbstractBookmarkSubscripti
{
View root = inflater.inflate(R.layout.pager_fragment_all_pass_subscription, container,
false);
mAnnualButton = root.findViewById(R.id.annual_sub_btn);
mAnnualButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1Y;
pingBookmarkCatalog();
});
mMonthlyButton = root.findViewById(R.id.month_sub_btn);
mMonthlyButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1M;
pingBookmarkCatalog();
});
mDelegate = new SubscriptionFragmentDelegate(this);
mDelegate.onSubscriptionCreateView(root);
setTopStatusBarOffset(root);
initViewPager(root);
@ -68,21 +52,21 @@ public class AllPassSubscriptionPagerFragment extends AbstractBookmarkSubscripti
@Override
void onSubscriptionDestroyView()
{
// Do nothing by default.
mDelegate.onSubscriptionDestroyView();
}
@NonNull
@Override
SubscriptionType getSubscriptionType()
{
return SubscriptionType.BOOKMARKS;
return SubscriptionType.BOOKMARKS_ALL;
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
return mSelectedPeriod;
return mDelegate.getSelectedPeriod();
}
private void setTopStatusBarOffset(@NonNull View view)
@ -138,64 +122,31 @@ public class AllPassSubscriptionPagerFragment extends AbstractBookmarkSubscripti
public void onProductDetailsLoading()
{
super.onProductDetailsLoading();
mAnnualButton.showProgress();
mMonthlyButton.showProgress();
mDelegate.onProductDetailsLoading();
}
@Override
public void onReset()
{
// Do nothing
mDelegate.onReset();
}
@Override
public void onPriceSelection()
{
mAnnualButton.hideProgress();
mMonthlyButton.hideProgress();
updatePaymentButtons();
mDelegate.onPriceSelection();
}
@Override
void showButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.showProgress();
else
mMonthlyButton.showProgress();
mDelegate.showButtonProgress();
}
@Override
void hideButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.hideProgress();
else
mMonthlyButton.hideProgress();
}
private void updatePaymentButtons()
{
updateYearlyButton();
updateMonthlyButton();
}
private void updateMonthlyButton()
{
ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mAnnualButton.setPrice(price);
mAnnualButton.setName(getString(R.string.annual_subscription_title));
String sale = getString(R.string.annual_save_component, calculateYearlySaving());
mAnnualButton.setSale(sale);
}
private void updateYearlyButton()
{
ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mMonthlyButton.setPrice(price);
mMonthlyButton.setName(getString(R.string.montly_subscription_title));
mDelegate.hideButtonProgress();
}
private class ParallaxFragmentPagerAdapter extends FragmentPagerAdapter

View file

@ -52,7 +52,7 @@ public class BookmarkSubscriptionFragment extends AbstractBookmarkSubscriptionFr
@Override
SubscriptionType getSubscriptionType()
{
return SubscriptionType.BOOKMARKS;
return SubscriptionType.BOOKMARKS_ALL;
}
private void onContinueButtonClicked()
@ -118,7 +118,7 @@ public class BookmarkSubscriptionFragment extends AbstractBookmarkSubscriptionFr
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
{
return PurchaseFactory.createBookmarksSubscriptionPurchaseController(requireContext());
return PurchaseFactory.createBookmarksAllSubscriptionController(requireContext());
}
@Override

View file

@ -20,10 +20,16 @@ public class PurchaseFactory
return createSubscriptionPurchaseController(context, SubscriptionType.ADS_REMOVAL);
}
public static PurchaseController<PurchaseCallback> createBookmarksSubscriptionPurchaseController(
public static PurchaseController<PurchaseCallback> createBookmarksAllSubscriptionController(
@NonNull Context context)
{
return createSubscriptionPurchaseController(context, SubscriptionType.BOOKMARKS);
return createSubscriptionPurchaseController(context, SubscriptionType.BOOKMARKS_ALL);
}
public static PurchaseController<PurchaseCallback> createBookmarksSightsSubscriptionController(
@NonNull Context context)
{
return createSubscriptionPurchaseController(context, SubscriptionType.BOOKMARKS_ALL);
}
@NonNull

View file

@ -8,15 +8,81 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmFragment;
public class SightseeingSubscriptionFragment extends BaseMwmFragment
public class SightseeingSubscriptionFragment extends AbstractBookmarkSubscriptionFragment
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionFragmentDelegate mDelegate;
@NonNull
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
{
return PurchaseFactory.createBookmarksSightsSubscriptionController(requireContext());
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View onSubscriptionCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View root = inflater.inflate(R.layout.fragment_sightseeing_subscription, container, false);
mDelegate = new SubscriptionFragmentDelegate(this);
mDelegate.onSubscriptionCreateView(root);
return root;
}
@Override
void onSubscriptionDestroyView()
{
mDelegate.onSubscriptionDestroyView();
}
@NonNull
@Override
SubscriptionType getSubscriptionType()
{
return SubscriptionType.BOOKMARKS_SIGHTS;
}
@Override
public void onProductDetailsLoading()
{
super.onProductDetailsLoading();
mDelegate.onProductDetailsLoading();
}
@Override
void hideButtonProgress()
{
mDelegate.hideButtonProgress();
}
@Override
void showButtonProgress()
{
mDelegate.showButtonProgress();
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
return mDelegate.getSelectedPeriod();
}
@Override
public void onReset()
{
mDelegate.onReset();
}
@Override
public void onPriceSelection()
{
mDelegate.onPriceSelection();
}
}

View file

@ -0,0 +1,110 @@
package com.mapswithme.maps.purchase;
import android.view.View;
import androidx.annotation.NonNull;
import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.SubscriptionButton;
import com.mapswithme.util.Utils;
class SubscriptionFragmentDelegate
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mAnnualButton;
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mMonthlyButton;
@NonNull
private PurchaseUtils.Period mSelectedPeriod = PurchaseUtils.Period.P1Y;
@NonNull
private final AbstractBookmarkSubscriptionFragment mFragment;
SubscriptionFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
mFragment = fragment;
}
void onSubscriptionCreateView(@NonNull View root)
{
mAnnualButton = root.findViewById(R.id.annual_button);
mAnnualButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1Y;
mFragment.pingBookmarkCatalog();
});
mMonthlyButton = root.findViewById(R.id.monthly_button);
mMonthlyButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1M;
mFragment.pingBookmarkCatalog();
});
}
void onSubscriptionDestroyView()
{
// Do nothing by default.
}
@NonNull
PurchaseUtils.Period getSelectedPeriod()
{
return mSelectedPeriod;
}
void onProductDetailsLoading()
{
mAnnualButton.showProgress();
mMonthlyButton.showProgress();
}
void onPriceSelection()
{
mAnnualButton.hideProgress();
mMonthlyButton.hideProgress();
updatePaymentButtons();
}
void onReset()
{
// Do nothing by default.
}
private void updatePaymentButtons()
{
updateYearlyButton();
updateMonthlyButton();
}
private void updateMonthlyButton()
{
ProductDetails details = mFragment.getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mAnnualButton.setPrice(price);
mAnnualButton.setName(mFragment.getString(R.string.annual_subscription_title));
String sale = mFragment.getString(R.string.annual_save_component, mFragment.calculateYearlySaving());
mAnnualButton.setSale(sale);
}
private void updateYearlyButton()
{
ProductDetails details = mFragment.getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mMonthlyButton.setPrice(price);
mMonthlyButton.setName(mFragment.getString(R.string.montly_subscription_title));
}
void showButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.showProgress();
else
mMonthlyButton.showProgress();
}
void hideButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.hideProgress();
else
mMonthlyButton.hideProgress();
}
}

View file

@ -46,7 +46,7 @@ public enum SubscriptionType
return PrivateVariables.adsRemovalMonthlyProductId();
}
},
BOOKMARKS
BOOKMARKS_ALL
{
@NonNull
@Override
@ -84,6 +84,45 @@ public enum SubscriptionType
{
return PrivateVariables.bookmarksSubscriptionMonthlyProductId();
}
},
BOOKMARKS_SIGHTS
{
@NonNull
@Override
String getServerId()
{
return PrivateVariables.bookmarksSubscriptionSightsServerId();
}
@NonNull
@Override
String getVendor()
{
return PrivateVariables.bookmarksSubscriptionVendor();
}
@NonNull
@Override
String[] getProductIds()
{
return Utils.concatArrays(PrivateVariables.bookmarksSubscriptionSightsNotUsedList(),
PrivateVariables.bookmarksSubscriptionSightsYearlyProductId(),
PrivateVariables.bookmarksSubscriptionSightsMonthlyProductId());
}
@NonNull
@Override
String getYearlyProductId()
{
return PrivateVariables.bookmarksSubscriptionSightsYearlyProductId();
}
@NonNull
@Override
String getMonthlyProductId()
{
return PrivateVariables.bookmarksSubscriptionSightsMonthlyProductId();
}
};
@NonNull