From 1f5c06e1ba4cfa50e7690e698bef741b49f45660 Mon Sep 17 00:00:00 2001 From: fgei Date: Tue, 2 May 2017 16:20:16 +0200 Subject: [PATCH 1/4] Improvement on the Banner view pager: - Switch the view pager adapter from FragmentPagerAdapter to a PagerAdapter, to avoid any problems with fragment inside fragment. - Correctly stop the handler that animates the view pager to remove any callbacks when the view holder is detached so we can avoid any possible leak. - Create an onboaridng class to handle all the on boarding data images and text. - Remove some commented code and unnecessary log showing in the crash report. --- .../mobile/android/activity/DuckDuckGo.java | 2 +- .../android/activity/OnboardingActivity.java | 15 +-- .../adapters/BannerOnboardingAdapter.java | 55 ++++++++-- .../adapters/RecyclerMainFeedAdapter.java | 58 +++++++--- .../mobile/android/fragment/FeedFragment.java | 101 +----------------- .../onboarding/BaseOnboardingFragment.java | 51 +++++++-- .../onboarding/EndOnboardingFragment.java | 26 ++--- .../fragment/onboarding/NoAdsFragment.java | 25 +---- .../onboarding/NoTrackingFragment.java | 25 +---- .../fragment/onboarding/PrivacyFragment.java | 25 +---- .../fragment/onboarding/RightFragment.java | 25 +---- .../mobile/android/util/Onboarding.java | 98 +++++++++++++++++ .../views/webview/DDGWebViewClient.java | 2 - 13 files changed, 259 insertions(+), 249 deletions(-) create mode 100644 src/com/duckduckgo/mobile/android/util/Onboarding.java diff --git a/src/com/duckduckgo/mobile/android/activity/DuckDuckGo.java b/src/com/duckduckgo/mobile/android/activity/DuckDuckGo.java index a68c76f8..88123be7 100644 --- a/src/com/duckduckgo/mobile/android/activity/DuckDuckGo.java +++ b/src/com/duckduckgo/mobile/android/activity/DuckDuckGo.java @@ -244,7 +244,7 @@ public void onBackStackChanged() { DDGActionBarManager.getInstance().updateActionBar(fragmentManager, tag, backPressed); backPressed = false; } - Log.e(TAG, "Fragment Back Stack current tag: " + DDGControlVar.mDuckDuckGoContainer.currentFragmentTag); + Log.d(TAG, "Fragment Back Stack current tag: " + DDGControlVar.mDuckDuckGoContainer.currentFragmentTag); showAllFragments(); } } diff --git a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java index 64ad672b..bd31a8ac 100644 --- a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java +++ b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java @@ -6,7 +6,6 @@ import android.os.Build; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -22,15 +21,10 @@ import com.duckduckgo.mobile.android.R; import com.duckduckgo.mobile.android.adapters.OnboardingAdapter; import com.duckduckgo.mobile.android.dialogs.InstructionDialogFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.EndOnboardingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.NoAdsFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.NoTrackingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.PrivacyFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.RightFragment; import com.duckduckgo.mobile.android.util.CompatUtils; +import com.duckduckgo.mobile.android.util.Onboarding; import com.duckduckgo.mobile.android.util.OnboardingHelper; import com.duckduckgo.mobile.android.util.OnboardingTransformer; -import com.duckduckgo.mobile.android.util.PreferencesManager; import com.duckduckgo.mobile.android.views.pageindicator.OnboardingPageIndicator; import java.util.Arrays; @@ -115,12 +109,7 @@ public void onPageScrollStateChanged(int state) { }); viewPager.setAdapter(adapter); - int[] backgroundColors = new int[] { - ContextCompat.getColor(this, PrivacyFragment.BACKGROUND_COLOR), - ContextCompat.getColor(this, NoAdsFragment.BACKGROUND_COLOR), - ContextCompat.getColor(this, NoTrackingFragment.BACKGROUND_COLOR), - ContextCompat.getColor(this, RightFragment.BACKGROUND_COLOR), - ContextCompat.getColor(this, EndOnboardingFragment.BACKGROUND_COLOR)}; + int[] backgroundColors = Onboarding.getBackgroundColors(this); pageIndicator = (OnboardingPageIndicator) findViewById(R.id.page_indicator); pageIndicator.setViewPager(viewPager, adapter.getCount() - 1); diff --git a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java b/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java index 18d44585..9c34925e 100644 --- a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java @@ -1,40 +1,77 @@ package com.duckduckgo.mobile.android.adapters; +import android.content.Context; +import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.PagerAdapter; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; +import com.duckduckgo.mobile.android.R; import com.duckduckgo.mobile.android.fragment.onboarding.NoAdsFragment; import com.duckduckgo.mobile.android.fragment.onboarding.NoTrackingFragment; import com.duckduckgo.mobile.android.fragment.onboarding.PrivacyFragment; import com.duckduckgo.mobile.android.fragment.onboarding.RightFragment; +import com.duckduckgo.mobile.android.util.Onboarding; +import com.squareup.picasso.Picasso; /** * Created by fgei on 4/11/17. */ -public class BannerOnboardingAdapter extends FragmentPagerAdapter { +public class BannerOnboardingAdapter extends PagerAdapter { private static final int NUM_PAGES = 4; - public BannerOnboardingAdapter(FragmentManager fm) { - super(fm); + public BannerOnboardingAdapter() { } @Override - public Fragment getItem(int position) { + public Object instantiateItem(ViewGroup container, int position) { + LayoutInflater inflater = LayoutInflater.from(container.getContext()); + Onboarding.OnboardingValue onboardingValue; switch(position) { case 0: - return PrivacyFragment.newInstance(position, true); + onboardingValue = Onboarding.getPrivacy(); + break; case 1: - return NoAdsFragment.newInstance(position, true); + onboardingValue = Onboarding.getNoAds(); + break; case 2: - return NoTrackingFragment.newInstance(position, true); + onboardingValue = Onboarding.getNoTracking(); + break; case 3: - return RightFragment.newInstance(position, true); + onboardingValue = Onboarding.getRight(); + break; default: - throw new IllegalArgumentException("Wrong position for Onboarding adapter: "+position); + throw new IllegalArgumentException("Invalid position for "+getClass().getSimpleName()+": "+position); } + + ViewGroup root = (ViewGroup) inflater.inflate(R.layout.viewholder_onboarding, container, false); + TextView title = (TextView) root.findViewById(R.id.title_text_view); + title.setText(onboardingValue.title); + TextView subtitle = (TextView) root.findViewById(R.id.subtitle_text_view); + subtitle.setText(onboardingValue.subtitle); + ImageView icon = (ImageView) root.findViewById(R.id.icon_image_view); + icon.setImageResource(onboardingValue.icon); + container.addView(root); + return root; + } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + container.removeView((View) object); + } + + @Override + public boolean isViewFromObject(View view, Object o) { + return view == o; } @Override diff --git a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java index 4cb5afb2..1b1a0e88 100644 --- a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java @@ -45,7 +45,7 @@ import java.util.ArrayList; import java.util.List; -public class RecyclerMainFeedAdapter extends RecyclerView.Adapter { +public class RecyclerMainFeedAdapter extends RecyclerView.Adapter { private static final String TAG = "MainFeedAdapter"; private static final int ITEM_TYPE_FEED = 0; @@ -62,7 +62,15 @@ public class RecyclerMainFeedAdapter extends RecyclerView.Adapter { public List data; - public class FeedViewHolder extends RecyclerView.ViewHolder { + public abstract class BaseViewHolder extends RecyclerView.ViewHolder { + public BaseViewHolder(View itemView) { + super(itemView); + } + public abstract void attach(); + public abstract void detach(); + } + + public class FeedViewHolder extends BaseViewHolder { public final TextView textViewTitle; public final FrameLayout frameCategoryContainer; @@ -82,14 +90,23 @@ public FeedViewHolder(View v) { this.imageViewBackground = (AsyncImageView) v.findViewById(R.id.feedItemBackground); this.imageViewFeedIcon = (AsyncImageView) v.findViewById(R.id.feedItemSourceIcon); } + + @Override + public void attach() { + + } + + @Override + public void detach() { + + } } - public class HeaderViewHolder extends RecyclerView.ViewHolder { + public class HeaderViewHolder extends BaseViewHolder { private final int SLIDE_TIME = 10000; public final ViewPager viewPager; - public final FragmentPagerAdapter adapter; public final BannerOnboardingPageIndicator pageIndicator; public final Button instructionbutton; public final ImageButton dismissImageButton; @@ -103,9 +120,7 @@ public HeaderViewHolder(View itemView) { instructionbutton = (Button) itemView.findViewById(R.id.instruction_button); dismissImageButton = (ImageButton) itemView.findViewById(R.id.dismiss_image_button); viewPager = (ViewPager) itemView.findViewById(R.id.view_pager); - adapter = new BannerOnboardingAdapter(fragmentManager); - viewPager.setAdapter(adapter); - viewPager.setPageTransformer(false, new OnboardingTransformer()); + viewPager.setAdapter(new BannerOnboardingAdapter()); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { @@ -126,6 +141,7 @@ public void onPageScrollStateChanged(int i) { } }); pageIndicator = (BannerOnboardingPageIndicator) itemView.findViewById(R.id.page_indicator); + //initViewPager(); pageIndicator.setViewPager(viewPager); handler = new Handler(); slideRunnable = new Runnable() { @@ -137,9 +153,18 @@ public void run() { handler.postDelayed(this, SLIDE_TIME); } }; + } + + @Override + public void attach() { startSlidingViewPager(); } + @Override + public void detach() { + stopSlidingViewPager(); + } + private void startSlidingViewPager() { isRunning = true; handler.postDelayed(slideRunnable, SLIDE_TIME); @@ -164,13 +189,13 @@ public RecyclerMainFeedAdapter(Context context, FragmentManager fragmentManager) } @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType == ITEM_TYPE_HEADER) return new HeaderViewHolder(inflater.inflate(R.layout.viewholder_feed_header, parent, false)); return new FeedViewHolder(inflater.inflate(R.layout.item_main_feed, parent, false)); } @Override - public void onBindViewHolder(final ViewHolder holder, int position) { + public void onBindViewHolder(final BaseViewHolder holder, int position) { if(position == 0 && isOnboardingBannerVisible) { //that's the header; final HeaderViewHolder headerHolder = (HeaderViewHolder) holder; @@ -314,7 +339,6 @@ public FeedObject getItem(int position) { int index = position; if(isOnboardingBannerVisible) index -= 1; return data.get(index); - //return data.get(position - 1);//header } @Override @@ -322,14 +346,24 @@ public int getItemCount() { int size = data.size(); if(isOnboardingBannerVisible) return size+ 1; return size; - //return data.size() + 1; //header } @Override public int getItemViewType(int position) { if(position == 0 && isOnboardingBannerVisible) return ITEM_TYPE_HEADER; return ITEM_TYPE_FEED; - //return super.getItemViewType(position); + } + + @Override + public void onViewAttachedToWindow(BaseViewHolder holder) { + super.onViewAttachedToWindow(holder); + holder.attach(); + } + + @Override + public void onViewDetachedFromWindow(BaseViewHolder holder) { + holder.detach(); + super.onViewDetachedFromWindow(holder); } private void dismissOnboardingBanner() { diff --git a/src/com/duckduckgo/mobile/android/fragment/FeedFragment.java b/src/com/duckduckgo/mobile/android/fragment/FeedFragment.java index e59b5e8d..a2263df2 100644 --- a/src/com/duckduckgo/mobile/android/fragment/FeedFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/FeedFragment.java @@ -66,12 +66,10 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre private Activity activity = null; private RecyclerView recyclerView = null; - //private MainFeedListView feedView = null; private SwipeRefreshLayout swipeRefreshLayout = null; private View fragmentView; private RecyclerMainFeedAdapter recyclerAdapter = null; - //private MainFeedAdapter feedAdapter = null; private MainFeedTask mainFeedTask = null; private RecyclerView.LayoutManager layoutManager; @@ -81,11 +79,6 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre public int source_m_itemHeight; public int source_m_yOffset; - // for keeping filter category at same position - public String category_m_objectId = null; - public int category_m_itemHeight; - public int category_m_yOffset; - private Menu feedMenu = null; private DDGOverflowMenu overflowMenu = null; @@ -117,7 +110,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setRetainInstance(true); - //setHasOptionsMenu(true); activity = getActivity(); init(); } @@ -133,6 +125,7 @@ public void onHiddenChanged(boolean hidden) { @Override public void onResume() { super.onResume(); + recyclerView.setAdapter(recyclerAdapter); // lock button etc. can cause MainFeedTask results to be useless for the Activity // which is restarted (onPostExecute becomes invalid for the new Activity instance) @@ -144,19 +137,13 @@ public void onResume() { @Override public void onPause() { super.onPause(); + recyclerView.setAdapter(null); swipeRefreshLayout.setRefreshing(false); if (mainFeedTask != null) { mainFeedTask.cancel(false); mainFeedTask = null; } } -/* - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - inflater.inflate(R.menu.main, menu); - menu.findItem(R.id.action_stories).setEnabled(false); - super.onCreateOptionsMenu(menu, inflater); - }*/ @Override public boolean onOptionsItemSelected(MenuItem item) { @@ -171,11 +158,6 @@ public void onRefresh() { } public void init() { - - //SourceClickListener sourceClickListener = new SourceClickListener(); - //CategoryClickListener categoryClickListener = new CategoryClickListener(); - //feedAdapter = new MainFeedAdapter(activity, sourceClickListener, categoryClickListener); - //recyclerAdapter = new RecyclerMainFeedAdapter(activity, sourceClickListener, categoryClickListener); recyclerAdapter = new RecyclerMainFeedAdapter(activity, getFragmentManager()); mainFeedTask = null; @@ -188,81 +170,12 @@ public void init() { layoutManager = new LinearLayoutManager(activity); recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(recyclerAdapter); feedMenu = new MenuBuilder(activity); activity.getMenuInflater().inflate(R.menu.main, feedMenu); } -/* - class SourceClickListener implements View.OnClickListener { - public void onClick(View v) { - // source filtering - - if(DDGControlVar.targetSource != null){ - cancelSourceFilter(); - } - else { - - View itemParent = (View) v.getParent().getParent(); - int pos = feedView.getPositionForView(itemParent); - source_m_objectId = ((FeedObject) feedView.getItemAtPosition(pos)).getId(); - FeedObject feedObject = (FeedObject) feedView.getItemAtPosition(pos); - source_m_itemHeight = itemParent.getHeight(); - - Rect r = new Rect(); - Point offset = new Point(); - feedView.getChildVisibleRect(itemParent, r, offset); - source_m_yOffset = offset.y; - - String sourceType = ((AsyncImageView) v).getType(); - DDGControlVar.targetSource = sourceType; - - DDGControlVar.hasUpdatedFeed = false; - keepFeedUpdated(); - } - - } - }*/ -/* - class CategoryClickListener implements View.OnClickListener { - public void onClick(final View v) { - // category filtering - - if(DDGControlVar.targetCategory != null){ - cancelCategoryFilter(); - } - else { - - final View itemParent = (View) v.getParent(); - int pos = feedView.getPositionForView(itemParent); - category_m_objectId = ((FeedObject) feedView.getItemAtPosition(pos)).getId(); - FeedObject feedObject = (FeedObject) feedView.getItemAtPosition(pos); - category_m_itemHeight = itemParent.getHeight(); - - Rect r = new Rect(); - Point offset = new Point(); - feedView.getChildVisibleRect(itemParent, r, offset); - category_m_yOffset = offset.y; - - DDGControlVar.targetCategory = feedObject.getCategory(); - - for(int i=0; i Date: Wed, 3 May 2017 11:58:29 +0200 Subject: [PATCH 2/4] More improvements: - Rename Onboard class to OnboardingPageConfiguration and remove and merge inner class. - Store the OnboardingPageConfiguration in BannerOnboardingAdapter inside an array. - Store items for the OnboardingAdapter too. --- .../android/activity/OnboardingActivity.java | 7 +-- .../adapters/BannerOnboardingAdapter.java | 62 +++++++------------ .../android/adapters/OnboardingAdapter.java | 44 ++++--------- .../adapters/RecyclerMainFeedAdapter.java | 5 -- .../onboarding/BaseOnboardingFragment.java | 45 ++++---------- .../onboarding/EndOnboardingFragment.java | 7 +-- .../fragment/onboarding/NoAdsFragment.java | 10 +-- .../onboarding/NoTrackingFragment.java | 10 +-- .../fragment/onboarding/PrivacyFragment.java | 11 +--- .../fragment/onboarding/RightFragment.java | 10 +-- ....java => OnboardingPageConfiguration.java} | 45 +++++++------- 11 files changed, 87 insertions(+), 169 deletions(-) rename src/com/duckduckgo/mobile/android/util/{Onboarding.java => OnboardingPageConfiguration.java} (72%) diff --git a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java index bd31a8ac..202309e8 100644 --- a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java +++ b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java @@ -10,7 +10,6 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.transition.Fade; -import android.transition.TransitionManager; import android.view.Display; import android.view.View; import android.view.ViewTreeObserver; @@ -22,7 +21,7 @@ import com.duckduckgo.mobile.android.adapters.OnboardingAdapter; import com.duckduckgo.mobile.android.dialogs.InstructionDialogFragment; import com.duckduckgo.mobile.android.util.CompatUtils; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; import com.duckduckgo.mobile.android.util.OnboardingHelper; import com.duckduckgo.mobile.android.util.OnboardingTransformer; import com.duckduckgo.mobile.android.views.pageindicator.OnboardingPageIndicator; @@ -38,7 +37,7 @@ public static Intent getStartIntent(Context context) { private FrameLayout activityContainer; private LinearLayout containerLayout; private ViewPager viewPager; - private FragmentPagerAdapter adapter; + private OnboardingAdapter adapter; private OnboardingPageIndicator pageIndicator; private Button addToHomeScreenButton; @@ -109,7 +108,7 @@ public void onPageScrollStateChanged(int state) { }); viewPager.setAdapter(adapter); - int[] backgroundColors = Onboarding.getBackgroundColors(this); + int[] backgroundColors = OnboardingPageConfiguration.getBackgroundColors(this); pageIndicator = (OnboardingPageIndicator) findViewById(R.id.page_indicator); pageIndicator.setViewPager(viewPager, adapter.getCount() - 1); diff --git a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java b/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java index 9c34925e..8d376c31 100644 --- a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java @@ -1,25 +1,14 @@ package com.duckduckgo.mobile.android.adapters; -import android.content.Context; -import android.graphics.Color; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.fragment.onboarding.NoAdsFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.NoTrackingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.PrivacyFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.RightFragment; -import com.duckduckgo.mobile.android.util.Onboarding; -import com.squareup.picasso.Picasso; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/11/17. @@ -27,39 +16,23 @@ public class BannerOnboardingAdapter extends PagerAdapter { - private static final int NUM_PAGES = 4; + private final OnboardingPageConfiguration[] items; public BannerOnboardingAdapter() { + items = new OnboardingPageConfiguration[] { + OnboardingPageConfiguration.getPrivacy(), + OnboardingPageConfiguration.getNoAds(), + OnboardingPageConfiguration.getNoTracking(), + OnboardingPageConfiguration.getRight() + }; } @Override public Object instantiateItem(ViewGroup container, int position) { + OnboardingPageConfiguration onboardingPageConfiguration = getItem(position); LayoutInflater inflater = LayoutInflater.from(container.getContext()); - Onboarding.OnboardingValue onboardingValue; - switch(position) { - case 0: - onboardingValue = Onboarding.getPrivacy(); - break; - case 1: - onboardingValue = Onboarding.getNoAds(); - break; - case 2: - onboardingValue = Onboarding.getNoTracking(); - break; - case 3: - onboardingValue = Onboarding.getRight(); - break; - default: - throw new IllegalArgumentException("Invalid position for "+getClass().getSimpleName()+": "+position); - } - ViewGroup root = (ViewGroup) inflater.inflate(R.layout.viewholder_onboarding, container, false); - TextView title = (TextView) root.findViewById(R.id.title_text_view); - title.setText(onboardingValue.title); - TextView subtitle = (TextView) root.findViewById(R.id.subtitle_text_view); - subtitle.setText(onboardingValue.subtitle); - ImageView icon = (ImageView) root.findViewById(R.id.icon_image_view); - icon.setImageResource(onboardingValue.icon); + populate(root, onboardingPageConfiguration); container.addView(root); return root; } @@ -76,6 +49,19 @@ public boolean isViewFromObject(View view, Object o) { @Override public int getCount() { - return NUM_PAGES; + return items.length; + } + + public OnboardingPageConfiguration getItem(int position) { + return items[position]; + } + + private void populate(View rootView, OnboardingPageConfiguration onboardingPageConfiguration) { + TextView title = (TextView) rootView.findViewById(R.id.title_text_view); + title.setText(onboardingPageConfiguration.title); + TextView subtitle = (TextView) rootView.findViewById(R.id.subtitle_text_view); + subtitle.setText(onboardingPageConfiguration.subtitle); + ImageView icon = (ImageView) rootView.findViewById(R.id.icon_image_view); + icon.setImageResource(onboardingPageConfiguration.icon); } } diff --git a/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java b/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java index 02728c81..a008bc13 100644 --- a/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java @@ -1,15 +1,10 @@ package com.duckduckgo.mobile.android.adapters; -import android.os.Build; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.graphics.ColorUtils; -import android.support.v4.view.ViewPager; -import android.util.Log; -import android.view.View; -import com.duckduckgo.mobile.android.R; +import com.duckduckgo.mobile.android.fragment.onboarding.BaseOnboardingFragment; import com.duckduckgo.mobile.android.fragment.onboarding.EndOnboardingFragment; import com.duckduckgo.mobile.android.fragment.onboarding.NoAdsFragment; import com.duckduckgo.mobile.android.fragment.onboarding.NoTrackingFragment; @@ -22,41 +17,26 @@ public class OnboardingAdapter extends FragmentPagerAdapter { - private static final int NUM_PAGES = 5; - //private boolean useMiniLayout = false; - public OnboardingAdapter(FragmentManager fm) { - super(fm); - //NUM_PAGES = 5; - //Log.e("NUM_PAGES", "numPages: "+NUM_PAGES); - } + private final BaseOnboardingFragment[] items; - private OnboardingAdapter(FragmentManager fm, boolean useMiniLayout) { + public OnboardingAdapter(FragmentManager fm) { super(fm); - //this.useMiniLayout = useMiniLayout; - //NUM_PAGES = useMiniLayout ? 4 : 5; - //Log.e("NUM_PAGES", "numPages: "+NUM_PAGES); + items = new BaseOnboardingFragment[] { + PrivacyFragment.newInstance(0), + NoAdsFragment.newInstance(1), + NoTrackingFragment.newInstance(2), + RightFragment.newInstance(3), + EndOnboardingFragment.newInstance(4) + }; } @Override public Fragment getItem(int position) { - switch(position) { - case 0: - return PrivacyFragment.newInstance(position); - case 1: - return NoAdsFragment.newInstance(position); - case 2: - return NoTrackingFragment.newInstance(position); - case 3: - return RightFragment.newInstance(position); - case 4: - return EndOnboardingFragment.newInstance(position); - default: - throw new IllegalArgumentException("Wrong position for Onboarding adapter: "+position); - } + return items[position]; } @Override public int getCount() { - return NUM_PAGES; + return items.length; } } diff --git a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java index 1b1a0e88..5269f82a 100644 --- a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java @@ -5,12 +5,9 @@ import android.graphics.Bitmap; import android.os.Handler; import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.view.menu.MenuBuilder; import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.RecyclerView.ViewHolder; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; @@ -34,7 +31,6 @@ import com.duckduckgo.mobile.android.util.DDGControlVar; import com.duckduckgo.mobile.android.util.DDGUtils; import com.duckduckgo.mobile.android.util.OnboardingHelper; -import com.duckduckgo.mobile.android.util.OnboardingTransformer; import com.duckduckgo.mobile.android.util.PreferencesManager; import com.duckduckgo.mobile.android.views.DDGOverflowMenu; import com.duckduckgo.mobile.android.views.pageindicator.BannerOnboardingPageIndicator; @@ -141,7 +137,6 @@ public void onPageScrollStateChanged(int i) { } }); pageIndicator = (BannerOnboardingPageIndicator) itemView.findViewById(R.id.page_indicator); - //initViewPager(); pageIndicator.setViewPager(viewPager); handler = new Handler(); slideRunnable = new Runnable() { diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java index 29872c2f..6be296f5 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java @@ -1,11 +1,6 @@ package com.duckduckgo.mobile.android.fragment.onboarding; -import android.content.Context; -import android.graphics.Color; -import android.graphics.Paint; import android.graphics.Point; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; @@ -13,10 +8,6 @@ import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; -import android.text.Layout; -import android.text.StaticLayout; -import android.text.TextPaint; -import android.util.Log; import android.view.Display; import android.view.LayoutInflater; import android.view.View; @@ -24,17 +15,11 @@ import android.view.ViewTreeObserver; import android.widget.FrameLayout; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.TextView; import com.duckduckgo.mobile.android.R; import com.duckduckgo.mobile.android.util.CompatUtils; -import com.duckduckgo.mobile.android.util.DimenUtils; -import com.duckduckgo.mobile.android.util.Onboarding; -import com.squareup.picasso.Picasso; - -import java.util.Arrays; -import java.util.List; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/4/17. @@ -48,9 +33,9 @@ public abstract class BaseOnboardingFragment extends Fragment { private static final float BOTTOM_MARGIN_CONSTRAINT_PORTRAIT = 3.819f; private static final float BOTTOM_MARGIN_CONSTRAINT_LANDSCAPE = 4.228f; - protected abstract Onboarding.OnboardingValue getOnboardingValue(); + protected abstract OnboardingPageConfiguration getOnboardingPageConfiguration(); - private Onboarding.OnboardingValue onboardingValue; + private OnboardingPageConfiguration onboardingPageConfiguration = getOnboardingPageConfiguration(); private boolean hasContent = true; private ViewGroup backgroundFrameLayout; @@ -58,12 +43,6 @@ public abstract class BaseOnboardingFragment extends Fragment { private TextView subtitleTextView; private ImageView iconImageView; - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - onboardingValue = getOnboardingValue(); - } - @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -88,7 +67,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if(!useMiniLayout()) { - backgroundFrameLayout.setBackgroundColor(getBackgroundColor()); + backgroundFrameLayout.setBackgroundColor(ContextCompat.getColor(getContext(), getBackgroundColor())); } if(!hasContent) return; String title = getTitle(); @@ -141,25 +120,25 @@ public void onGlobalLayout() { } } - private String getTitle() { + public String getTitle() { if(!hasContent) return ""; - return getString(onboardingValue.title); + return getString(onboardingPageConfiguration.title); } - private String getSubtitle() { + public String getSubtitle() { if(!hasContent) return ""; - return getString(onboardingValue.subtitle); + return getString(onboardingPageConfiguration.subtitle); } @DrawableRes - private int getIcon() { + public int getIcon() { if(!hasContent) return 0; - return onboardingValue.icon; + return onboardingPageConfiguration.icon; } @ColorInt - private int getBackgroundColor() { - return onboardingValue.backgroundColor; + public int getBackgroundColor() { + return ContextCompat.getColor(getContext(), onboardingPageConfiguration.backgroundColor); } protected static Bundle createArgs(int indexPosition) { diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java index 70bf71c9..06e76d82 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java @@ -3,8 +3,7 @@ import android.os.Bundle; import android.support.annotation.Nullable; -import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/10/17. @@ -25,7 +24,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) { } @Override - protected Onboarding.OnboardingValue getOnboardingValue() { - return Onboarding.getFadeOnboarding(); + protected OnboardingPageConfiguration getOnboardingPageConfiguration() { + return OnboardingPageConfiguration.getFadeOnboarding(); } } diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java index 90b7fb61..133e979f 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java @@ -1,10 +1,6 @@ package com.duckduckgo.mobile.android.fragment.onboarding; -import android.graphics.Color; -import android.support.v4.content.ContextCompat; - -import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/4/17. @@ -25,7 +21,7 @@ public static NoAdsFragment newInstance(int indexPosition, boolean isHeader) { } @Override - protected Onboarding.OnboardingValue getOnboardingValue() { - return Onboarding.getNoAds(); + protected OnboardingPageConfiguration getOnboardingPageConfiguration() { + return OnboardingPageConfiguration.getNoAds(); } } diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java index 9ab83610..3402d5a1 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java @@ -1,10 +1,6 @@ package com.duckduckgo.mobile.android.fragment.onboarding; -import android.graphics.Color; -import android.support.v4.content.ContextCompat; - -import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/4/17. @@ -25,7 +21,7 @@ public static NoTrackingFragment newInstance(int indexPosition, boolean isHeader } @Override - protected Onboarding.OnboardingValue getOnboardingValue() { - return Onboarding.getNoTracking(); + protected OnboardingPageConfiguration getOnboardingPageConfiguration() { + return OnboardingPageConfiguration.getNoTracking(); } } diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java index e2f61e79..7689619b 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java @@ -1,11 +1,6 @@ package com.duckduckgo.mobile.android.fragment.onboarding; -import android.graphics.Color; -import android.support.v4.content.ContextCompat; -import android.support.v4.graphics.ColorUtils; - -import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/4/17. @@ -26,7 +21,7 @@ public static PrivacyFragment newInstance(int indexPosition, boolean isHeader) { } @Override - protected Onboarding.OnboardingValue getOnboardingValue() { - return Onboarding.getPrivacy(); + protected OnboardingPageConfiguration getOnboardingPageConfiguration() { + return OnboardingPageConfiguration.getPrivacy(); } } diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java index 4fab395f..b8412cb9 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java @@ -1,10 +1,6 @@ package com.duckduckgo.mobile.android.fragment.onboarding; -import android.graphics.Color; -import android.support.v4.content.ContextCompat; - -import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.util.Onboarding; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; /** * Created by fgei on 4/4/17. @@ -25,7 +21,7 @@ public static RightFragment newInstance(int indexPosition, boolean isHeader) { } @Override - protected Onboarding.OnboardingValue getOnboardingValue() { - return Onboarding.getRight(); + protected OnboardingPageConfiguration getOnboardingPageConfiguration() { + return OnboardingPageConfiguration.getRight(); } } diff --git a/src/com/duckduckgo/mobile/android/util/Onboarding.java b/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java similarity index 72% rename from src/com/duckduckgo/mobile/android/util/Onboarding.java rename to src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java index b678e597..c9e9e992 100644 --- a/src/com/duckduckgo/mobile/android/util/Onboarding.java +++ b/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java @@ -1,7 +1,6 @@ package com.duckduckgo.mobile.android.util; import android.content.Context; -import android.support.annotation.ColorInt; import android.support.v4.content.ContextCompat; import com.duckduckgo.mobile.android.R; @@ -10,7 +9,7 @@ * Created by fgei on 4/30/17. */ -public class Onboarding { +public class OnboardingPageConfiguration { private static final int PRIVACY_TITLE = R.string.privacy_title; private static final int PRIVACY_SUBTITLE = R.string.privacy_subtitle; @@ -32,8 +31,8 @@ public class Onboarding { private static final int RIGHT_ICON = R.drawable.illustration_4; private static final int RIGHT_BACKGROUND_COLOR = R.color.onboarding_right_background; - public static OnboardingValue getPrivacy() { - return new OnboardingValue( + public static OnboardingPageConfiguration getPrivacy() { + return new OnboardingPageConfiguration( PRIVACY_TITLE, PRIVACY_SUBTITLE, PRIVACY_ICON, @@ -41,8 +40,8 @@ public static OnboardingValue getPrivacy() { ); } - public static OnboardingValue getNoAds() { - return new OnboardingValue( + public static OnboardingPageConfiguration getNoAds() { + return new OnboardingPageConfiguration( NO_ADS_TITLE, NO_ADS_SUBTITLE, NO_ADS_ICON, @@ -50,8 +49,8 @@ public static OnboardingValue getNoAds() { ); } - public static OnboardingValue getNoTracking() { - return new OnboardingValue( + public static OnboardingPageConfiguration getNoTracking() { + return new OnboardingPageConfiguration( NO_TRACKING_TITLE, NO_TRACKING_SUBTITLE, NO_TRACKING_ICON, @@ -59,8 +58,8 @@ public static OnboardingValue getNoTracking() { ); } - public static OnboardingValue getRight() { - return new OnboardingValue( + public static OnboardingPageConfiguration getRight() { + return new OnboardingPageConfiguration( RIGHT_TITLE, RIGHT_SUBTITLE, RIGHT_ICON, @@ -68,8 +67,8 @@ public static OnboardingValue getRight() { ); } - public static OnboardingValue getFadeOnboarding() { - return new OnboardingValue(0, 0, 0, RIGHT_BACKGROUND_COLOR); + public static OnboardingPageConfiguration getFadeOnboarding() { + return new OnboardingPageConfiguration(0, 0, 0, RIGHT_BACKGROUND_COLOR); } public static int[] getBackgroundColors(Context context) { @@ -82,17 +81,15 @@ public static int[] getBackgroundColors(Context context) { }; } - public static class OnboardingValue { - public int title; - public int subtitle; - public int icon; - public int backgroundColor; - - public OnboardingValue(int title, int subtitle, int icon, int backgroundColor) { - this.title = title; - this.subtitle = subtitle; - this.icon = icon; - this.backgroundColor = backgroundColor; - } + public final int title; + public final int subtitle; + public final int icon; + public final int backgroundColor; + + public OnboardingPageConfiguration(int title, int subtitle, int icon, int backgroundColor) { + this.title = title; + this.subtitle = subtitle; + this.icon = icon; + this.backgroundColor = backgroundColor; } } From bdc90e618dedd3fd61728a4ea9470d987b10e2fc Mon Sep 17 00:00:00 2001 From: fgei Date: Wed, 3 May 2017 16:22:39 +0200 Subject: [PATCH 3/4] More improvements: - Use just a single base fragment for on boarding (OnboardingPageFragment). - Add "hasContent" field to the OnboardingPageConfiguration to handle the last empty page in the main on boarding flow - Provide the background colors to the PageTransformer from the OnobardingAdapter rather then OnboardingPageConfiguration class, pass a context to the adapter to provide the color from the res id. - Rename adapters to OnboardingBannerAdapter and OnboardingPageAdapter to be consistent with the rest onboarding naming conventions. --- .../android/activity/OnboardingActivity.java | 11 +- .../android/adapters/OnboardingAdapter.java | 42 ------- ...pter.java => OnboardingBannerAdapter.java} | 20 ++-- .../adapters/OnboardingPageAdapter.java | 49 ++++++++ .../adapters/RecyclerMainFeedAdapter.java | 2 +- ...gment.java => OnboardingPageFragment.java} | 110 ++++++++---------- .../onboarding/EndOnboardingFragment.java | 30 ----- .../fragment/onboarding/NoAdsFragment.java | 27 ----- .../onboarding/NoTrackingFragment.java | 27 ----- .../fragment/onboarding/PrivacyFragment.java | 27 ----- .../fragment/onboarding/RightFragment.java | 27 ----- .../util/OnboardingPageConfiguration.java | 64 +++++++--- .../android/util/OnboardingTransformer.java | 36 ++---- 13 files changed, 178 insertions(+), 294 deletions(-) delete mode 100644 src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java rename src/com/duckduckgo/mobile/android/adapters/{BannerOnboardingAdapter.java => OnboardingBannerAdapter.java} (68%) create mode 100644 src/com/duckduckgo/mobile/android/adapters/OnboardingPageAdapter.java rename src/com/duckduckgo/mobile/android/fragment/{onboarding/BaseOnboardingFragment.java => OnboardingPageFragment.java} (52%) delete mode 100644 src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java delete mode 100644 src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java delete mode 100644 src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java delete mode 100644 src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java delete mode 100644 src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java diff --git a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java index 202309e8..cbd556ab 100644 --- a/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java +++ b/src/com/duckduckgo/mobile/android/activity/OnboardingActivity.java @@ -5,7 +5,6 @@ import android.graphics.Point; import android.os.Build; import android.support.v4.app.ActivityCompat; -import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -18,10 +17,9 @@ import android.widget.LinearLayout; import com.duckduckgo.mobile.android.R; -import com.duckduckgo.mobile.android.adapters.OnboardingAdapter; +import com.duckduckgo.mobile.android.adapters.OnboardingPageAdapter; import com.duckduckgo.mobile.android.dialogs.InstructionDialogFragment; import com.duckduckgo.mobile.android.util.CompatUtils; -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; import com.duckduckgo.mobile.android.util.OnboardingHelper; import com.duckduckgo.mobile.android.util.OnboardingTransformer; import com.duckduckgo.mobile.android.views.pageindicator.OnboardingPageIndicator; @@ -37,7 +35,7 @@ public static Intent getStartIntent(Context context) { private FrameLayout activityContainer; private LinearLayout containerLayout; private ViewPager viewPager; - private OnboardingAdapter adapter; + private OnboardingPageAdapter adapter; private OnboardingPageIndicator pageIndicator; private Button addToHomeScreenButton; @@ -87,7 +85,7 @@ public void onClick(View v) { } }); - adapter = new OnboardingAdapter(getSupportFragmentManager()); + adapter = new OnboardingPageAdapter(this, getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.view_pager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @@ -108,11 +106,10 @@ public void onPageScrollStateChanged(int state) { }); viewPager.setAdapter(adapter); - int[] backgroundColors = OnboardingPageConfiguration.getBackgroundColors(this); pageIndicator = (OnboardingPageIndicator) findViewById(R.id.page_indicator); pageIndicator.setViewPager(viewPager, adapter.getCount() - 1); - viewPager.setPageTransformer(false, new OnboardingTransformer(backgroundColors, pageIndicator, Arrays.asList(pageIndicator, addToHomeScreenButton))); + viewPager.setPageTransformer(false, new OnboardingTransformer(adapter, pageIndicator, Arrays.asList(pageIndicator, addToHomeScreenButton))); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); diff --git a/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java b/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java deleted file mode 100644 index a008bc13..00000000 --- a/src/com/duckduckgo/mobile/android/adapters/OnboardingAdapter.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.duckduckgo.mobile.android.adapters; - -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; - -import com.duckduckgo.mobile.android.fragment.onboarding.BaseOnboardingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.EndOnboardingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.NoAdsFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.NoTrackingFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.PrivacyFragment; -import com.duckduckgo.mobile.android.fragment.onboarding.RightFragment; - -/** - * Created by fgei on 4/4/17. - */ - -public class OnboardingAdapter extends FragmentPagerAdapter { - - private final BaseOnboardingFragment[] items; - - public OnboardingAdapter(FragmentManager fm) { - super(fm); - items = new BaseOnboardingFragment[] { - PrivacyFragment.newInstance(0), - NoAdsFragment.newInstance(1), - NoTrackingFragment.newInstance(2), - RightFragment.newInstance(3), - EndOnboardingFragment.newInstance(4) - }; - } - - @Override - public Fragment getItem(int position) { - return items[position]; - } - - @Override - public int getCount() { - return items.length; - } -} diff --git a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java b/src/com/duckduckgo/mobile/android/adapters/OnboardingBannerAdapter.java similarity index 68% rename from src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java rename to src/com/duckduckgo/mobile/android/adapters/OnboardingBannerAdapter.java index 8d376c31..5e64d11e 100644 --- a/src/com/duckduckgo/mobile/android/adapters/BannerOnboardingAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/OnboardingBannerAdapter.java @@ -1,5 +1,6 @@ package com.duckduckgo.mobile.android.adapters; +import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.LayoutInflater; import android.view.View; @@ -14,11 +15,11 @@ * Created by fgei on 4/11/17. */ -public class BannerOnboardingAdapter extends PagerAdapter { +public class OnboardingBannerAdapter extends PagerAdapter { private final OnboardingPageConfiguration[] items; - public BannerOnboardingAdapter() { + public OnboardingBannerAdapter() { items = new OnboardingPageConfiguration[] { OnboardingPageConfiguration.getPrivacy(), OnboardingPageConfiguration.getNoAds(), @@ -57,11 +58,14 @@ public OnboardingPageConfiguration getItem(int position) { } private void populate(View rootView, OnboardingPageConfiguration onboardingPageConfiguration) { - TextView title = (TextView) rootView.findViewById(R.id.title_text_view); - title.setText(onboardingPageConfiguration.title); - TextView subtitle = (TextView) rootView.findViewById(R.id.subtitle_text_view); - subtitle.setText(onboardingPageConfiguration.subtitle); - ImageView icon = (ImageView) rootView.findViewById(R.id.icon_image_view); - icon.setImageResource(onboardingPageConfiguration.icon); + Context context = rootView.getContext(); + TextView titleTextView = (TextView) rootView.findViewById(R.id.title_text_view); + String title = context.getString(onboardingPageConfiguration.title).replaceAll("\\n", " "); + titleTextView.setText(title); + TextView subtitleTextView = (TextView) rootView.findViewById(R.id.subtitle_text_view); + String subtitle = context.getString(onboardingPageConfiguration.subtitle).replaceAll("\\n", " "); + subtitleTextView.setText(subtitle); + ImageView iconImageView = (ImageView) rootView.findViewById(R.id.icon_image_view); + iconImageView.setImageResource(onboardingPageConfiguration.icon); } } diff --git a/src/com/duckduckgo/mobile/android/adapters/OnboardingPageAdapter.java b/src/com/duckduckgo/mobile/android/adapters/OnboardingPageAdapter.java new file mode 100644 index 00000000..2f117283 --- /dev/null +++ b/src/com/duckduckgo/mobile/android/adapters/OnboardingPageAdapter.java @@ -0,0 +1,49 @@ +package com.duckduckgo.mobile.android.adapters; + +import android.content.Context; +import android.support.annotation.ColorInt; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.content.ContextCompat; + +import com.duckduckgo.mobile.android.fragment.OnboardingPageFragment; +import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; + +/** + * Created by fgei on 4/4/17. + */ + +public class OnboardingPageAdapter extends FragmentPagerAdapter { + + private final OnboardingPageConfiguration[] items; + private Context context; + + public OnboardingPageAdapter(Context context, FragmentManager fm) { + super(fm); + this.context = context; + items = new OnboardingPageConfiguration[] { + OnboardingPageConfiguration.getPrivacy(), + OnboardingPageConfiguration.getNoAds(), + OnboardingPageConfiguration.getNoTracking(), + OnboardingPageConfiguration.getRight(), + OnboardingPageConfiguration.getFadeOnboarding() + }; + } + + @Override + public Fragment getItem(int position) { + OnboardingPageConfiguration onboardingPageConfiguration = items[position]; + return OnboardingPageFragment.newInstance(onboardingPageConfiguration, position); + } + + @Override + public int getCount() { + return items.length; + } + + @ColorInt + public int getBackgroundColor(int position) { + return ContextCompat.getColor(context, items[position].backgroundColor); + } +} diff --git a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java index 5269f82a..2ca83f31 100644 --- a/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java +++ b/src/com/duckduckgo/mobile/android/adapters/RecyclerMainFeedAdapter.java @@ -116,7 +116,7 @@ public HeaderViewHolder(View itemView) { instructionbutton = (Button) itemView.findViewById(R.id.instruction_button); dismissImageButton = (ImageButton) itemView.findViewById(R.id.dismiss_image_button); viewPager = (ViewPager) itemView.findViewById(R.id.view_pager); - viewPager.setAdapter(new BannerOnboardingAdapter()); + viewPager.setAdapter(new OnboardingBannerAdapter()); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java b/src/com/duckduckgo/mobile/android/fragment/OnboardingPageFragment.java similarity index 52% rename from src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java rename to src/com/duckduckgo/mobile/android/fragment/OnboardingPageFragment.java index 6be296f5..9d648bc4 100644 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/BaseOnboardingFragment.java +++ b/src/com/duckduckgo/mobile/android/fragment/OnboardingPageFragment.java @@ -1,9 +1,8 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; +package com.duckduckgo.mobile.android.fragment; import android.graphics.Point; import android.os.Bundle; import android.support.annotation.ColorInt; -import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -25,29 +24,41 @@ * Created by fgei on 4/4/17. */ -public abstract class BaseOnboardingFragment extends Fragment { +public class OnboardingPageFragment extends Fragment { private static final String EXTRA_INDEX_POSITION = "index_position"; - private static final String EXTRA_MINI_LAYOUT = "mini_layout"; + private static final String EXTRA_PAGE_CONFIGURATION = "page_configuration"; private static final float BOTTOM_MARGIN_CONSTRAINT_PORTRAIT = 3.819f; private static final float BOTTOM_MARGIN_CONSTRAINT_LANDSCAPE = 4.228f; - protected abstract OnboardingPageConfiguration getOnboardingPageConfiguration(); + public static OnboardingPageFragment newInstance(OnboardingPageConfiguration onboardingPageConfiguration, int indexPosition) { + OnboardingPageFragment f = new OnboardingPageFragment(); + Bundle args = new Bundle(); + args.putParcelable(EXTRA_PAGE_CONFIGURATION, onboardingPageConfiguration); + args.putInt(EXTRA_INDEX_POSITION, indexPosition); + f.setArguments(args); + return f; + } - private OnboardingPageConfiguration onboardingPageConfiguration = getOnboardingPageConfiguration(); - private boolean hasContent = true; + private OnboardingPageConfiguration onboardingPageConfiguration; private ViewGroup backgroundFrameLayout; private TextView titleTextView; private TextView subtitleTextView; private ImageView iconImageView; + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + onboardingPageConfiguration = getOnboardingPageConfiguration(); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate( - useMiniLayout() ? R.layout.viewholder_onboarding : R.layout.fragment_onboarding, + R.layout.fragment_onboarding, container, false); backgroundFrameLayout = (ViewGroup) rootView; @@ -66,73 +77,63 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - if(!useMiniLayout()) { - backgroundFrameLayout.setBackgroundColor(ContextCompat.getColor(getContext(), getBackgroundColor())); - } - if(!hasContent) return; + backgroundFrameLayout.setBackgroundColor(getBackgroundColor()); + if(!onboardingPageConfiguration.hasContent) return; String title = getTitle(); - if(useMiniLayout()) title = title.replaceAll("\\n", " "); titleTextView.setText(title); String subtitle = getSubtitle(); - if(useMiniLayout()) subtitle = subtitle.replaceAll("\\n", " "); subtitleTextView.setText(subtitle); iconImageView.setImageResource(getIcon()); - initLayout(); } - protected boolean useMiniLayout() { - return getArguments().getBoolean(EXTRA_MINI_LAYOUT); + private int getIndexPosition() { + return getArguments().getInt(EXTRA_INDEX_POSITION); } - protected void setHasContent(boolean hasContent) { - this.hasContent = hasContent; - } + private OnboardingPageConfiguration getOnboardingPageConfiguration() { + return getArguments().getParcelable(EXTRA_PAGE_CONFIGURATION); - private int getIndexPosition() { - return getArguments().getInt(EXTRA_INDEX_POSITION); } private void initLayout() { - if(!useMiniLayout()) { - Display display = getActivity().getWindowManager().getDefaultDisplay(); - Point size = new Point(); - CompatUtils.getDisplaySize(display, size); - int width = size.x; - int height = size.y; - final FrameLayout containerLayout = (FrameLayout) backgroundFrameLayout.findViewById(R.id.container_layout); - final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) containerLayout.getLayoutParams(); - - boolean isPortrait = width < height; - //float portraitBottomRatio = 3.819f; - //float landscapeBottomRatio = 4.228f; - float bottomMarginConstraint = isPortrait ? BOTTOM_MARGIN_CONSTRAINT_PORTRAIT : BOTTOM_MARGIN_CONSTRAINT_LANDSCAPE; - final int bottomMargin = (int) (height / bottomMarginConstraint); - final int topMargin = height - (int) (height / 1.278); - containerLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - CompatUtils.removeOnGlobalLayoutListener(containerLayout.getViewTreeObserver(), this); - params.bottomMargin = bottomMargin; - containerLayout.setLayoutParams(params); - } - }); - } + Display display = getActivity().getWindowManager().getDefaultDisplay(); + Point size = new Point(); + CompatUtils.getDisplaySize(display, size); + int width = size.x; + int height = size.y; + final FrameLayout containerLayout = (FrameLayout) backgroundFrameLayout.findViewById(R.id.container_layout); + final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) containerLayout.getLayoutParams(); + + boolean isPortrait = width < height; + //float portraitBottomRatio = 3.819f; + //float landscapeBottomRatio = 4.228f; + float bottomMarginConstraint = isPortrait ? BOTTOM_MARGIN_CONSTRAINT_PORTRAIT : BOTTOM_MARGIN_CONSTRAINT_LANDSCAPE; + final int bottomMargin = (int) (height / bottomMarginConstraint); + final int topMargin = height - (int) (height / 1.278); + containerLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + CompatUtils.removeOnGlobalLayoutListener(containerLayout.getViewTreeObserver(), this); + params.bottomMargin = bottomMargin; + containerLayout.setLayoutParams(params); + } + }); } public String getTitle() { - if(!hasContent) return ""; + if(!onboardingPageConfiguration.hasContent) return ""; return getString(onboardingPageConfiguration.title); } public String getSubtitle() { - if(!hasContent) return ""; + if(!onboardingPageConfiguration.hasContent) return ""; return getString(onboardingPageConfiguration.subtitle); } @DrawableRes public int getIcon() { - if(!hasContent) return 0; + if(!onboardingPageConfiguration.hasContent) return 0; return onboardingPageConfiguration.icon; } @@ -140,15 +141,4 @@ public int getIcon() { public int getBackgroundColor() { return ContextCompat.getColor(getContext(), onboardingPageConfiguration.backgroundColor); } - - protected static Bundle createArgs(int indexPosition) { - return createArgs(indexPosition, false); - } - - protected static Bundle createArgs(int indexPosition, boolean miniLayout) { - Bundle args = new Bundle(); - args.putInt(EXTRA_INDEX_POSITION, indexPosition); - args.putBoolean(EXTRA_MINI_LAYOUT, miniLayout); - return args; - } } diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java deleted file mode 100644 index 06e76d82..00000000 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/EndOnboardingFragment.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; - -import android.os.Bundle; -import android.support.annotation.Nullable; - -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; - -/** - * Created by fgei on 4/10/17. - */ - -public class EndOnboardingFragment extends BaseOnboardingFragment { - - public static EndOnboardingFragment newInstance(int indexPosition) { - EndOnboardingFragment f = new EndOnboardingFragment(); - f.setArguments(createArgs(indexPosition, false)); - return f; - } - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasContent(false); - } - - @Override - protected OnboardingPageConfiguration getOnboardingPageConfiguration() { - return OnboardingPageConfiguration.getFadeOnboarding(); - } -} diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java deleted file mode 100644 index 133e979f..00000000 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoAdsFragment.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; - -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; - -/** - * Created by fgei on 4/4/17. - */ - -public class NoAdsFragment extends BaseOnboardingFragment { - - public static final String TAG = "no_ads_fragment"; - - public static NoAdsFragment newInstance(int indexPosition) { - return newInstance(indexPosition, false); - } - - public static NoAdsFragment newInstance(int indexPosition, boolean isHeader) { - NoAdsFragment f = new NoAdsFragment(); - f.setArguments(createArgs(indexPosition, isHeader)); - return f; - } - - @Override - protected OnboardingPageConfiguration getOnboardingPageConfiguration() { - return OnboardingPageConfiguration.getNoAds(); - } -} diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java deleted file mode 100644 index 3402d5a1..00000000 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/NoTrackingFragment.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; - -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; - -/** - * Created by fgei on 4/4/17. - */ - -public class NoTrackingFragment extends BaseOnboardingFragment { - - public static final String TAG = "no_tracking_fragment"; - - public static NoTrackingFragment newInstance(int indexPosition) { - return newInstance(indexPosition, false); - } - - public static NoTrackingFragment newInstance(int indexPosition, boolean isHeader) { - NoTrackingFragment f = new NoTrackingFragment(); - f.setArguments(createArgs(indexPosition, isHeader)); - return f; - } - - @Override - protected OnboardingPageConfiguration getOnboardingPageConfiguration() { - return OnboardingPageConfiguration.getNoTracking(); - } -} diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java deleted file mode 100644 index 7689619b..00000000 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/PrivacyFragment.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; - -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; - -/** - * Created by fgei on 4/4/17. - */ - -public class PrivacyFragment extends BaseOnboardingFragment { - - public static final String TAG = "privacy_fragment"; - - public static PrivacyFragment newInstance(int indexPosition) { - return newInstance(indexPosition, false); - } - - public static PrivacyFragment newInstance(int indexPosition, boolean isHeader) { - PrivacyFragment f = new PrivacyFragment(); - f.setArguments(createArgs(indexPosition, isHeader)); - return f; - } - - @Override - protected OnboardingPageConfiguration getOnboardingPageConfiguration() { - return OnboardingPageConfiguration.getPrivacy(); - } -} diff --git a/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java b/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java deleted file mode 100644 index b8412cb9..00000000 --- a/src/com/duckduckgo/mobile/android/fragment/onboarding/RightFragment.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.duckduckgo.mobile.android.fragment.onboarding; - -import com.duckduckgo.mobile.android.util.OnboardingPageConfiguration; - -/** - * Created by fgei on 4/4/17. - */ - -public class RightFragment extends BaseOnboardingFragment { - - public static final String TAG = "right_fragment"; - - public static RightFragment newInstance(int indexPosition) { - return newInstance(indexPosition, false); - } - - public static RightFragment newInstance(int indexPosition, boolean isHeader) { - RightFragment f = new RightFragment(); - f.setArguments(createArgs(indexPosition, isHeader)); - return f; - } - - @Override - protected OnboardingPageConfiguration getOnboardingPageConfiguration() { - return OnboardingPageConfiguration.getRight(); - } -} diff --git a/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java b/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java index c9e9e992..c122bf37 100644 --- a/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java +++ b/src/com/duckduckgo/mobile/android/util/OnboardingPageConfiguration.java @@ -1,7 +1,7 @@ package com.duckduckgo.mobile.android.util; -import android.content.Context; -import android.support.v4.content.ContextCompat; +import android.os.Parcel; +import android.os.Parcelable; import com.duckduckgo.mobile.android.R; @@ -9,7 +9,7 @@ * Created by fgei on 4/30/17. */ -public class OnboardingPageConfiguration { +public class OnboardingPageConfiguration implements Parcelable { private static final int PRIVACY_TITLE = R.string.privacy_title; private static final int PRIVACY_SUBTITLE = R.string.privacy_subtitle; @@ -31,6 +31,8 @@ public class OnboardingPageConfiguration { private static final int RIGHT_ICON = R.drawable.illustration_4; private static final int RIGHT_BACKGROUND_COLOR = R.color.onboarding_right_background; + private static final int INVALID_RES_ID = -1; + public static OnboardingPageConfiguration getPrivacy() { return new OnboardingPageConfiguration( PRIVACY_TITLE, @@ -68,28 +70,62 @@ public static OnboardingPageConfiguration getRight() { } public static OnboardingPageConfiguration getFadeOnboarding() { - return new OnboardingPageConfiguration(0, 0, 0, RIGHT_BACKGROUND_COLOR); - } - - public static int[] getBackgroundColors(Context context) { - return new int[] { - ContextCompat.getColor(context, PRIVACY_BACKGROUND_COLOR), - ContextCompat.getColor(context, NO_ADS_BACKGROUND_COLOR), - ContextCompat.getColor(context, NO_TRACKING_BACKGROUND_COLOR), - ContextCompat.getColor(context, RIGHT_BACKGROUND_COLOR), - ContextCompat.getColor(context, RIGHT_BACKGROUND_COLOR) - }; + return new OnboardingPageConfiguration(RIGHT_BACKGROUND_COLOR); } public final int title; public final int subtitle; public final int icon; public final int backgroundColor; + public final boolean hasContent; public OnboardingPageConfiguration(int title, int subtitle, int icon, int backgroundColor) { this.title = title; this.subtitle = subtitle; this.icon = icon; this.backgroundColor = backgroundColor; + this.hasContent = true; } + + public OnboardingPageConfiguration(int backgroundColor) { + this.title = INVALID_RES_ID; + this.subtitle = INVALID_RES_ID; + this.icon = INVALID_RES_ID; + this.backgroundColor = backgroundColor; + this.hasContent = false; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(this.title); + dest.writeInt(this.subtitle); + dest.writeInt(this.icon); + dest.writeInt(this.backgroundColor); + dest.writeByte(this.hasContent ? (byte) 1 : (byte) 0); + } + + protected OnboardingPageConfiguration(Parcel in) { + this.title = in.readInt(); + this.subtitle = in.readInt(); + this.icon = in.readInt(); + this.backgroundColor = in.readInt(); + this.hasContent = in.readByte() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public OnboardingPageConfiguration createFromParcel(Parcel source) { + return new OnboardingPageConfiguration(source); + } + + @Override + public OnboardingPageConfiguration[] newArray(int size) { + return new OnboardingPageConfiguration[size]; + } + }; } diff --git a/src/com/duckduckgo/mobile/android/util/OnboardingTransformer.java b/src/com/duckduckgo/mobile/android/util/OnboardingTransformer.java index 1b5a911a..94ee64cb 100644 --- a/src/com/duckduckgo/mobile/android/util/OnboardingTransformer.java +++ b/src/com/duckduckgo/mobile/android/util/OnboardingTransformer.java @@ -8,6 +8,7 @@ import android.view.View; import com.duckduckgo.mobile.android.R; +import com.duckduckgo.mobile.android.adapters.OnboardingPageAdapter; import com.duckduckgo.mobile.android.views.pageindicator.OnboardingPageIndicator; import java.util.List; @@ -18,22 +19,12 @@ public class OnboardingTransformer implements ViewPager.PageTransformer { - private boolean doFullTrasformation = true; - private int[] backgroundColors; + private OnboardingPageAdapter onboardingPageAdapter; private OnboardingPageIndicator pageIndicator; private List fadeViews; - public OnboardingTransformer() { - doFullTrasformation = false; - } - - private OnboardingTransformer(int[] backgroundColors, OnboardingPageIndicator pageIndicator) { - this.backgroundColors = backgroundColors; - this.pageIndicator = pageIndicator; - } - - public OnboardingTransformer(int[] backgroundColors, OnboardingPageIndicator pageIndicator, List fadeViews) { - this.backgroundColors = backgroundColors; + public OnboardingTransformer(OnboardingPageAdapter onboardingPageAdapter, OnboardingPageIndicator pageIndicator, List fadeViews) { + this.onboardingPageAdapter = onboardingPageAdapter; this.pageIndicator = pageIndicator; this.fadeViews = fadeViews; } @@ -45,17 +36,16 @@ public void transformPage(View page, float position) { int pageIndex = (Integer) page.getTag(); if(position >= -1.0 && position <= 1.0) { - if(!doFullTrasformation) return; if(position > 0) { - int leftColor = backgroundColors[pageIndex - 1]; - int rightColor = backgroundColors[pageIndex]; + int leftColor = onboardingPageAdapter.getBackgroundColor(pageIndex - 1); + int rightColor = onboardingPageAdapter.getBackgroundColor(pageIndex); int color = blendARGB(rightColor, leftColor, position); page.setBackgroundColor(color); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { image.setScaleX(1 - absolutePosition); image.setScaleY(1 - absolutePosition); - if(pageIndex == (backgroundColors.length - 1)) { + if(pageIndex == (onboardingPageAdapter.getCount() - 1)) { for(View v : fadeViews) { v.setAlpha(absolutePosition); } @@ -65,15 +55,15 @@ public void transformPage(View page, float position) { pageIndicator.setPositionSelected(pageIndex, absolutePosition); } else if(position < 0) { int nextIndex = pageIndex + 1; - int leftColor = backgroundColors[pageIndex]; - int rightColor = backgroundColors[nextIndex]; + int leftColor = onboardingPageAdapter.getBackgroundColor(pageIndex); + int rightColor = onboardingPageAdapter.getBackgroundColor(nextIndex); int color = blendARGB(rightColor, leftColor, 1 - absolutePosition); page.setBackgroundColor(color); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { image.setScaleX(1 - absolutePosition); image.setScaleY(1 - absolutePosition); - if(nextIndex == (backgroundColors.length - 1)) { + if(nextIndex == (onboardingPageAdapter.getCount() - 1)) { for(View v : fadeViews) { v.setAlpha(1 - absolutePosition); } @@ -82,12 +72,10 @@ public void transformPage(View page, float position) { pageIndicator.setPositionSelected(pageIndex, absolutePosition); pageIndicator.setPositionSelected(pageIndex + 1, 1 - absolutePosition); } else { - page.setBackgroundColor(backgroundColors[pageIndex]); + page.setBackgroundColor(onboardingPageAdapter.getBackgroundColor(pageIndex)); } } else { - if(doFullTrasformation) { - page.setBackgroundColor(backgroundColors[pageIndex]); - } + page.setBackgroundColor(onboardingPageAdapter.getBackgroundColor(pageIndex)); } } From 8f9f4f599baa781eece1625f1e8e08f849253a97 Mon Sep 17 00:00:00 2001 From: fgei Date: Thu, 4 May 2017 11:27:12 +0200 Subject: [PATCH 4/4] bump version code and version name --- AndroidManifest.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f3da1150..e3da86e3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,8 +2,8 @@ + android:versionCode="79" + android:versionName="3.0.17">