Niedzielski has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/326172 )
Change subject: Demo: refactor description edit tutorial page views ...................................................................... Demo: refactor description edit tutorial page views Bug: T148205 Change-Id: I0458a687e097421351aff51b79509b9b15b4d42d --- M app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialFragment.java M app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPage.java M app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPageView.java M app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPagerAdapter.java A app/src/main/res/layout/inflate_description_edit_tutorial_page_one.xml A app/src/main/res/layout/inflate_description_edit_tutorial_page_two.xml D app/src/main/res/layout/view_description_edit_tutorial_1_children.xml D app/src/main/res/layout/view_description_edit_tutorial_2_children.xml M app/src/main/res/layout/view_description_edit_tutorial_page.xml M app/src/main/res/values/attrs.xml 10 files changed, 208 insertions(+), 188 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/72/326172/1 diff --git a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialFragment.java b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialFragment.java index eb1a87c..be546ac 100644 --- a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialFragment.java +++ b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialFragment.java @@ -2,7 +2,9 @@ import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; @@ -15,9 +17,11 @@ import butterknife.ButterKnife; import butterknife.Unbinder; -public class DescriptionEditTutorialFragment extends Fragment implements DescriptionEditTutorialPagerAdapter.Callback { +public class DescriptionEditTutorialFragment extends Fragment { @BindView(R.id.fragment_description_edit_tutorial_view_pager) ViewPager viewPager; private Unbinder unbinder; + + private PagerAdapter adapter; public interface Callback { void onStartEditingClick(); @@ -27,31 +31,38 @@ return new DescriptionEditTutorialFragment(); } + @Override public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + adapter = new DescriptionEditTutorialPagerAdapter(new PageViewCallback()); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.fragment_description_edit_tutorial, container, false); unbinder = ButterKnife.bind(this, view); - viewPager.setAdapter(new DescriptionEditTutorialPagerAdapter(this)); + viewPager.setAdapter(adapter); return view; } @Override public void onDestroyView() { + viewPager.setAdapter(null); unbinder.unbind(); unbinder = null; super.onDestroyView(); } - @Override public int getCurrentItem() { - return viewPager.getCurrentItem(); + @Override public void onDestroy() { + adapter = null; + super.onDestroy(); } - @Override public void onStartEditingClick() { + private void onStartEditingClick() { if (callback() != null) { callback().onStartEditingClick(); } } - @Override public void onContinueClick() { + private void advancePage() { int nextPageIndex = viewPager.getCurrentItem() + 1; int lastPageIndex = viewPager.getAdapter().getCount() - 1; viewPager.setCurrentItem(Math.min(nextPageIndex, lastPageIndex), true); @@ -60,4 +71,14 @@ private Callback callback() { return FragmentUtil.getCallback(this, DescriptionEditTutorialFragment.Callback.class); } -} + + private class PageViewCallback implements DescriptionEditTutorialPagerAdapter.Callback { + @Override public void onButtonClick(@NonNull DescriptionEditTutorialPage page) { + if (page.isLast()) { + onStartEditingClick(); + } else { + advancePage(); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPage.java b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPage.java index 0e99ac7..5c21497 100644 --- a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPage.java +++ b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPage.java @@ -2,26 +2,36 @@ import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; +import android.view.LayoutInflater; +import android.view.ViewGroup; import org.wikipedia.R; import org.wikipedia.model.EnumCode; import org.wikipedia.model.EnumCodeMap; enum DescriptionEditTutorialPage implements EnumCode { - PAGE_ONE(R.layout.view_description_edit_tutorial_1_children), - PAGE_TWO(R.layout.view_description_edit_tutorial_2_children); + PAGE_ONE(R.layout.inflate_description_edit_tutorial_page_one), + PAGE_TWO(R.layout.inflate_description_edit_tutorial_page_two); private static EnumCodeMap<DescriptionEditTutorialPage> MAP = new EnumCodeMap<>(DescriptionEditTutorialPage.class); - @LayoutRes private final int childLayout; - - @LayoutRes int getChildLayout() { - return childLayout; - } + @LayoutRes private final int layout; @NonNull public static DescriptionEditTutorialPage of(int code) { return MAP.get(code); + } + + public boolean isLast() { + return this == PAGE_TWO; + } + + // todo: this should probably move back to the adapter? There's no logic here and it's a View concern + @NonNull public DescriptionEditTutorialPageView inflate(@NonNull ViewGroup parent) { + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + DescriptionEditTutorialPageView view = (DescriptionEditTutorialPageView) inflater.inflate(layout, parent, false); + parent.addView(view); + return view; } public static int size() { @@ -32,7 +42,7 @@ return ordinal(); } - DescriptionEditTutorialPage(@LayoutRes int childLayout) { - this.childLayout = childLayout; + DescriptionEditTutorialPage(@LayoutRes int layout) { + this.layout = layout; } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPageView.java b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPageView.java index b7aad84..f579364 100644 --- a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPageView.java +++ b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPageView.java @@ -2,55 +2,84 @@ import android.annotation.TargetApi; import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.os.Build; -import android.support.annotation.LayoutRes; +import android.support.annotation.AttrRes; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.StyleRes; import android.util.AttributeSet; -import android.view.LayoutInflater; +import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.TextView; import org.wikipedia.R; +import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class DescriptionEditTutorialPageView extends LinearLayout { - interface Callback { - void onClickForPosition(); + void onButtonClick(@NonNull DescriptionEditTutorialPageView view); } - private Callback callback; + @BindView(R.id.view_description_edit_tutorial_page_image) ImageView imageView; + @BindView(R.id.view_description_edit_tutorial_page_title_description) TextView titleDescription; + @BindView(R.id.view_description_edit_tutorial_page_description_summary) TextView descriptionSummary; + @BindView(R.id.view_description_edit_tutorial_page_button) TextView button; + + @Nullable private Callback callback; public DescriptionEditTutorialPageView(Context context) { super(context); + init(null, 0, 0); } public DescriptionEditTutorialPageView(Context context, AttributeSet attrs) { super(context, attrs); + init(attrs, 0, 0); } public DescriptionEditTutorialPageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + init(attrs, defStyleAttr, 0); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public DescriptionEditTutorialPageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - } - - public void inflateChildren(@LayoutRes int childLayout) { - LayoutInflater.from(getContext()).inflate(childLayout, this, true); - ButterKnife.bind(this); + init(attrs, defStyleAttr, defStyleRes); } public void setCallback(@Nullable Callback callback) { this.callback = callback; } - @OnClick(R.id.description_edit_tutorial_button) void onClick() { + @OnClick(R.id.view_description_edit_tutorial_page_button) void onButtonClick() { if (callback != null) { - callback.onClickForPosition(); + callback.onButtonClick(this); } } -} + + private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { + inflate(getContext(), R.layout.view_description_edit_tutorial_page, this); + ButterKnife.bind(this); + if (attrs != null) { + TypedArray array = getContext().obtainStyledAttributes(attrs, + R.styleable.DescriptionEditTutorialPageView, defStyleAttr, defStyleRes); + Drawable image = array.getDrawable(R.styleable.DescriptionEditTutorialPageView_image); + String titleDescriptionText = array.getString(R.styleable.DescriptionEditTutorialPageView_titleDescription); + String descriptionSummaryText = array.getString(R.styleable.DescriptionEditTutorialPageView_descriptionSummary); + String buttonText = array.getString(R.styleable.DescriptionEditTutorialPageView_buttonText); + + imageView.setImageDrawable(image); + titleDescription.setText(titleDescriptionText); + descriptionSummary.setText(descriptionSummaryText); + button.setText(buttonText); + + array.recycle(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPagerAdapter.java b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPagerAdapter.java index 6bfb4ea..1b95df4 100644 --- a/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPagerAdapter.java +++ b/app/src/main/java/org/wikipedia/descriptions/DescriptionEditTutorialPagerAdapter.java @@ -1,45 +1,34 @@ package org.wikipedia.descriptions; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.view.PagerAdapter; -import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import org.wikipedia.R; - -class DescriptionEditTutorialPagerAdapter extends PagerAdapter implements DescriptionEditTutorialPageView.Callback { - - public interface Callback { - int getCurrentItem(); - void onContinueClick(); - void onStartEditingClick(); +class DescriptionEditTutorialPagerAdapter extends PagerAdapter { + interface Callback { + void onButtonClick(@NonNull DescriptionEditTutorialPage page); } - @NonNull private Callback callback; + @Nullable private Callback callback; + @NonNull private final ViewCallback viewCallback = new ViewCallback(); - DescriptionEditTutorialPagerAdapter(@NonNull Callback callback) { + DescriptionEditTutorialPagerAdapter(@Nullable Callback callback) { this.callback = callback; } @Override public Object instantiateItem(ViewGroup container, int position) { - LayoutInflater inflater = LayoutInflater.from(container.getContext()); - DescriptionEditTutorialPage tutorialPage = DescriptionEditTutorialPage.of(position); - DescriptionEditTutorialPageView view = (DescriptionEditTutorialPageView) - inflater.inflate(R.layout.view_description_edit_tutorial_page, container, false); - view.inflateChildren(tutorialPage.getChildLayout()); - view.setCallback(this); - container.addView(view); + DescriptionEditTutorialPage page = DescriptionEditTutorialPage.of(position); + DescriptionEditTutorialPageView view = page.inflate(container); + view.setTag(position); + view.setCallback(viewCallback); return view; } - @Override public void onClickForPosition() { - boolean last = callback.getCurrentItem() == getCount() - 1; - if (last) { - callback.onStartEditingClick(); - } else { - callback.onContinueClick(); - } + @Override public void destroyItem(ViewGroup container, int position, Object object) { + ((DescriptionEditTutorialPageView) object).setCallback(null); + super.destroyItem(container, position, object); } @Override public int getCount() { @@ -49,4 +38,12 @@ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } -} + + private class ViewCallback implements DescriptionEditTutorialPageView.Callback { + @Override public void onButtonClick(@NonNull DescriptionEditTutorialPageView view) { + if (callback != null) { + callback.onButtonClick(DescriptionEditTutorialPage.of((int) view.getTag())); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/inflate_description_edit_tutorial_page_one.xml b/app/src/main/res/layout/inflate_description_edit_tutorial_page_one.xml new file mode 100644 index 0000000..fab3ed4 --- /dev/null +++ b/app/src/main/res/layout/inflate_description_edit_tutorial_page_one.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<org.wikipedia.descriptions.DescriptionEditTutorialPageView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical" + android:background="@drawable/description_edit_tutorial_background" + android:gravity="bottom|center_horizontal" + android:fillViewport="true" + app:image="@drawable/ic_illustration_description_onboard_1" + app:titleDescription="@string/description_edit_tutorial_title_descriptions" + app:descriptionSummary="@string/description_edit_tutorial_title_descriptions_summary" + app:buttonText="@string/description_edit_tutorial_button_label_continue" /> \ No newline at end of file diff --git a/app/src/main/res/layout/inflate_description_edit_tutorial_page_two.xml b/app/src/main/res/layout/inflate_description_edit_tutorial_page_two.xml new file mode 100644 index 0000000..0264f74 --- /dev/null +++ b/app/src/main/res/layout/inflate_description_edit_tutorial_page_two.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<org.wikipedia.descriptions.DescriptionEditTutorialPageView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical" + android:background="@drawable/description_edit_tutorial_background" + android:gravity="bottom|center_horizontal" + android:fillViewport="true" + app:image="@drawable/ic_illustration_description_onboard_2" + app:titleDescription="@string/description_edit_tutorial_keep_it_short" + app:descriptionSummary="@string/description_edit_tutorial_keep_it_short_instructions" + app:buttonText="@string/description_edit_tutorial_button_label_start_editing"> + <org.wikipedia.views.AppTextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/description_edit_tutorial_promise" + android:textAlignment="center" + android:textSize="12sp" + android:textColor="#54595d"/> +</org.wikipedia.descriptions.DescriptionEditTutorialPageView> \ No newline at end of file diff --git a/app/src/main/res/layout/view_description_edit_tutorial_1_children.xml b/app/src/main/res/layout/view_description_edit_tutorial_1_children.xml deleted file mode 100644 index 2165be4..0000000 --- a/app/src/main/res/layout/view_description_edit_tutorial_1_children.xml +++ /dev/null @@ -1,57 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<merge - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto"> - - <ImageView - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="50" - android:layout_marginTop="72dp" - android:layout_marginLeft="45dp" - android:layout_marginRight="45dp" - app:srcCompat="@drawable/ic_illustration_description_onboard_1" - android:contentDescription="@null"/> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="50" - android:orientation="vertical" - android:background="@color/light_blue" - android:gravity="center" > - - <org.wikipedia.views.AppTextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_margin="8dp" - android:textColor="@android:color/black" - android:textSize="24sp" - android:fontFamily="sans-serif-medium" - android:text="@string/description_edit_tutorial_title_descriptions"/> - - <org.wikipedia.views.AppTextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_marginLeft="48dp" - android:layout_marginRight="48dp" - android:textSize="16sp" - android:textAlignment="center" - android:textColor="#8A000000" - android:text="@string/description_edit_tutorial_title_descriptions_summary"/> - - <TextView - android:id="@+id/description_edit_tutorial_button" - android:layout_width="106dp" - android:layout_height="36dp" - android:fontFamily="sans-serif-medium" - android:text="@string/description_edit_tutorial_button_label_continue" - android:textAlignment="center" - android:layout_marginTop="28dp" - android:textSize="14sp" - android:textAllCaps="true" - style="@style/ButtonBlue"/> - - </LinearLayout> - -</merge> \ No newline at end of file diff --git a/app/src/main/res/layout/view_description_edit_tutorial_2_children.xml b/app/src/main/res/layout/view_description_edit_tutorial_2_children.xml deleted file mode 100644 index 4dac0ea..0000000 --- a/app/src/main/res/layout/view_description_edit_tutorial_2_children.xml +++ /dev/null @@ -1,66 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<merge - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto"> - - <ImageView - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="50" - android:layout_marginTop="72dp" - android:layout_marginLeft="45dp" - android:layout_marginRight="45dp" - app:srcCompat="@drawable/ic_illustration_description_onboard_2" - android:contentDescription="@null"/> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="50" - android:orientation="vertical" - android:background="@color/light_blue" - android:gravity="center" > - - <org.wikipedia.views.AppTextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_margin="8dp" - android:textColor="@android:color/black" - android:textSize="24sp" - android:fontFamily="sans-serif-medium" - android:text="@string/description_edit_tutorial_keep_it_short"/> - - <org.wikipedia.views.AppTextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_marginLeft="24dp" - android:layout_marginRight="24dp" - android:textSize="16sp" - android:textAlignment="center" - android:textColor="#8A000000" - android:text="@string/description_edit_tutorial_keep_it_short_instructions"/> - - <TextView - android:id="@+id/description_edit_tutorial_button" - android:layout_width="136dp" - android:layout_height="36dp" - android:fontFamily="sans-serif-medium" - android:text="@string/description_edit_tutorial_button_label_start_editing" - android:textAlignment="center" - android:layout_marginTop="32dp" - android:layout_marginBottom="10dp" - android:textSize="14sp" - android:textAllCaps="true" - style="@style/ButtonBlue"/> - - <org.wikipedia.views.AppTextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/description_edit_tutorial_promise" - android:textAlignment="center" - android:textSize="12sp" - android:textColor="#54595d"/> - - </LinearLayout> - -</merge> \ No newline at end of file diff --git a/app/src/main/res/layout/view_description_edit_tutorial_page.xml b/app/src/main/res/layout/view_description_edit_tutorial_page.xml index bd8051d..37af15a 100644 --- a/app/src/main/res/layout/view_description_edit_tutorial_page.xml +++ b/app/src/main/res/layout/view_description_edit_tutorial_page.xml @@ -1,9 +1,53 @@ <?xml version="1.0" encoding="utf-8"?> -<org.wikipedia.descriptions.DescriptionEditTutorialPageView - xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="vertical" - android:background="@drawable/description_edit_tutorial_background" - android:gravity="bottom|center_horizontal" - android:fillViewport="true" /> \ No newline at end of file +<merge xmlns:android="http://schemas.android.com/apk/res/android"> + <ImageView + android:id="@+id/view_description_edit_tutorial_page_image" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="50" + android:layout_marginTop="72dp" + android:layout_marginLeft="45dp" + android:layout_marginRight="45dp" + android:contentDescription="@null" /> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="50" + android:orientation="vertical" + android:background="@color/light_blue" + android:gravity="center"> + + <org.wikipedia.views.AppTextView + android:id="@+id/view_description_edit_tutorial_page_title_description" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_margin="8dp" + android:textColor="@android:color/black" + android:textSize="24sp" + android:fontFamily="sans-serif-medium" /> + + <!-- todo: try to use @color --> + <org.wikipedia.views.AppTextView + android:id="@+id/view_description_edit_tutorial_page_description_summary" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginLeft="48dp" + android:layout_marginRight="48dp" + android:textSize="16sp" + android:textAlignment="center" + android:textColor="#8a000000" /> + + <TextView + style="@style/ButtonBlue" + android:id="@+id/view_description_edit_tutorial_page_button" + android:layout_width="106dp" + android:layout_height="36dp" + android:fontFamily="sans-serif-medium" + android:textAlignment="center" + android:layout_marginTop="28dp" + android:textSize="14sp" + android:textAllCaps="true" /> + + </LinearLayout> +</merge> \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index e0f9b64..5db57b9 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -68,4 +68,10 @@ <attr name="navTabViewStyle" format="reference" /> </declare-styleable> -</resources> + <declare-styleable name="DescriptionEditTutorialPageView"> + <attr name="image" format="reference" /> + <attr name="titleDescription" format="string" /> + <attr name="descriptionSummary" format="string" /> + <attr name="buttonText" format="string" /> + </declare-styleable> +</resources> \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/326172 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0458a687e097421351aff51b79509b9b15b4d42d Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org> Gerrit-Reviewer: Sniedzielski <sniedziel...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits