diff options
Diffstat (limited to 'library/src/main/java/moe')
9 files changed, 474 insertions, 0 deletions
diff --git a/library/src/main/java/moe/yuuta/flow/FlowFragment.java b/library/src/main/java/moe/yuuta/flow/FlowFragment.java new file mode 100644 index 0000000..d7d4776 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/FlowFragment.java @@ -0,0 +1,194 @@ +package moe.yuuta.flow; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; + +import java.util.ArrayList; +import java.util.List; + +/** + * The Host fragment + */ +public class FlowFragment extends Fragment implements IFlowFragment, View.OnClickListener { + private List<PageFragment> mPages = new ArrayList<>(0); + // true: after the view settles, update the UI immediately. + private volatile boolean mUIUpdateScheduled; + + private Header mHeader; + private ViewPager mPager; + private NavigationBar mNav; + + /** + * Should only be called once. + */ + public void setPages(@NonNull List<PageFragment> pages) { + if (mPages != null && mPages.size() > 0) { + throw new IllegalStateException("This method should only be called once. Current size: " + mPages.size()); + } + mPages = pages; + // Because this method can be called before setting up the layout, so we need to schedule it until the layout is set up. + notifyCurrentFlowInfoUpdated(); + } + + /** + * Update the WHOLE UI. + */ + private void updateUI() { + final int currentIndex = mPager.getCurrentItem(); + final PageFragment currentFragment = mPages.get(currentIndex); + if (currentFragment.mInfo == null) { + throw new NullPointerException("Info is null"); + } + if (currentFragment.mInfo.getNavigationBarConfig() != null) { + mNav.applyInfo(currentFragment.mInfo.getNavigationBarConfig()); + } else { + mNav.applyInfo(new NavigationBarConfig(getString(R.string.flow_nav_bar_next), + getString(R.string.flow_nav_bar_previous), + currentIndex == 0 ? View.GONE : View.VISIBLE, + currentIndex >= (mPages.size() - 1) ? View.GONE : View.VISIBLE, + View.VISIBLE, + this, + this)); + } + mHeader.applyInfo(currentFragment.mInfo.getHeaderConfig()); + } + + @Override + public void onClick(View v) { + final int currentIndex = mPager.getCurrentItem(); + if (v.getId() == R.id.flow_nav_left_button) { + if (currentIndex > 0) previousFlow(); + } else if (v.getId() == R.id.flow_nav_right_button) { + if (currentIndex < (mPages.size() - 1)) nextFlow(); + } + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.fragment_flow, container, false); + mHeader = new Header(view.<ConstraintLayout>findViewById(R.id.flow_host_header)); + mNav = new NavigationBar(view.<ConstraintLayout>findViewById(R.id.flow_host_nav)); + mPager = view.findViewById(R.id.flow_host_pager); + mPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { + @Override + public Fragment getItem(int position) { + PageFragment fragment = mPages.get(position); + return fragment; + } + + @Override + public int getCount() { + return mPages.size(); + } + }); + mPager.addOnPageChangeListener(mPageListener); + getChildFragmentManager().registerFragmentLifecycleCallbacks(mCallback, false); + return view; + } + + @Override + public void onDestroyView() { + getChildFragmentManager().unregisterFragmentLifecycleCallbacks(mCallback); + mPager.removeOnPageChangeListener(mPageListener); + super.onDestroyView(); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mUIUpdateScheduled) { + updateUI(); + mUIUpdateScheduled = false; + } + } + + @Override + public void notifyCurrentFlowInfoUpdated() { + if (getView() != null) { + mUIUpdateScheduled = false; + updateUI(); + } else { + mUIUpdateScheduled = true; + } + } + + @Override + public void nextFlow() { + switchToFlow(mPager.getCurrentItem() + 1); + } + + @Override + public void previousFlow() { + switchToFlow(mPager.getCurrentItem() - 1); + } + + @Override + public int getFlowCount() { + return mPages.size(); + } + + @Override + public void switchToFlow(int index) { + mPager.setCurrentItem(index, true); + } + + private FragmentManager.FragmentLifecycleCallbacks mCallback = new FragmentManager.FragmentLifecycleCallbacks() { + @Override + public void onFragmentPreCreated(@NonNull FragmentManager fm, @NonNull Fragment f, @Nullable Bundle savedInstanceState) { + if (f instanceof PageFragment) { + final PageFragment pf = (PageFragment) f; + pf.setHostFragment(FlowFragment.this); + } + } + }; + + private ViewPager.OnPageChangeListener mPageListener = new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + notifyCurrentFlowInfoUpdated(); + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }; + + /** + * @return true: the fragment handled this event and you do not need to call super. false: call super. + */ + public boolean onBackPressed() { + if (getView() == null) return false; + if (mPages.size() <= 0) return false; + final PageFragment pf = mPages.get(mPager.getCurrentItem()); + if (pf != null) { + if (pf.onBackPressed()) return true; + } + // Default handling + if (mPager.getCurrentItem() == 0) return false; + mNav.getButton(NavigationBar.ButtonPosition.LEFT).performClick(); + return true; + } + + @Override + @NonNull + public View.OnClickListener getGeneralFlowNavListener() { + return this; + } +} diff --git a/library/src/main/java/moe/yuuta/flow/FlowInfo.kt b/library/src/main/java/moe/yuuta/flow/FlowInfo.kt new file mode 100644 index 0000000..d12228c --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/FlowInfo.kt @@ -0,0 +1,10 @@ +package moe.yuuta.flow; + +/** + * Stores basic info, for instance, title and subtitle for the flow. These data will + * be auto applied when user switch to the related flow and will be reset after leaving the flow. + */ +data class FlowInfo( + var headerConfig: HeaderConfig, + var navigationBarConfig: NavigationBarConfig? +)
\ No newline at end of file diff --git a/library/src/main/java/moe/yuuta/flow/Header.java b/library/src/main/java/moe/yuuta/flow/Header.java new file mode 100644 index 0000000..42d9124 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/Header.java @@ -0,0 +1,54 @@ +package moe.yuuta.flow; + +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +class Header { + public enum WhichView { + TITLE, + SUBTITLE + } + + private ViewGroup mRoot; + private TextView mTitle; + private TextView mSubtitle; + private ProgressBar mProgressBar; + + Header() {} + + Header(@NonNull ViewGroup root) { + attach(root); + } + + /** + * The config will be reset after re-attaching. + */ + void attach(@NonNull ViewGroup root) { + mRoot = root; + mTitle = mRoot.findViewById(R.id.flow_header_title); + mSubtitle = mRoot.findViewById(R.id.flow_header_subtitle); + mProgressBar = mRoot.findViewById(R.id.flow_header_progressbar); + } + + @NonNull + private TextView getText(@NonNull WhichView position) { + switch (position) { + case TITLE: + return mTitle; + case SUBTITLE: + return mSubtitle; + default: + throw new IllegalArgumentException("Unexpected position"); + } + } + + void applyInfo(@NonNull HeaderConfig config) { + mTitle.setText(config.getTitleText()); + mSubtitle.setText(config.getSubtitleText()); + mProgressBar.setVisibility(config.getShowProgressBar() ? View.VISIBLE : View.GONE); + } +} diff --git a/library/src/main/java/moe/yuuta/flow/HeaderConfig.kt b/library/src/main/java/moe/yuuta/flow/HeaderConfig.kt new file mode 100644 index 0000000..0133897 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/HeaderConfig.kt @@ -0,0 +1,7 @@ +package moe.yuuta.flow + +class HeaderConfig( + var titleText: CharSequence, + var subtitleText: CharSequence?, + var showProgressBar: Boolean = false +)
\ No newline at end of file diff --git a/library/src/main/java/moe/yuuta/flow/IFlowFragment.java b/library/src/main/java/moe/yuuta/flow/IFlowFragment.java new file mode 100644 index 0000000..79f67f6 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/IFlowFragment.java @@ -0,0 +1,17 @@ +package moe.yuuta.flow; + +import android.view.View; + +import androidx.annotation.NonNull; + +/** + * A bridge which is exposed to {@link PageFragment} for controlling the {@link FlowFragment} + */ +public interface IFlowFragment { + void notifyCurrentFlowInfoUpdated(); + void nextFlow(); + void previousFlow(); + int getFlowCount(); + void switchToFlow(int index); + @NonNull View.OnClickListener getGeneralFlowNavListener(); +} diff --git a/library/src/main/java/moe/yuuta/flow/NavigationBar.java b/library/src/main/java/moe/yuuta/flow/NavigationBar.java new file mode 100644 index 0000000..14fae26 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/NavigationBar.java @@ -0,0 +1,82 @@ +package moe.yuuta.flow; + +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +class NavigationBar { + public enum ButtonPosition { + LEFT, + RIGHT + } + + private ViewGroup mRoot; + private Button mLeftButton; + private Button mRightButton; + + NavigationBar() {} + + NavigationBar(@NonNull ViewGroup navBarRoot) { + attach(navBarRoot); + } + + /** + * The config will be reset after re-attaching. + */ + void attach(@NonNull ViewGroup navBarRoot) { + mRoot = navBarRoot; + mLeftButton = mRoot.findViewById(R.id.flow_nav_left_button); + mRightButton = mRoot.findViewById(R.id.flow_nav_right_button); + } + + @NonNull + Button getButton(@NonNull ButtonPosition position) { + switch (position) { + case LEFT: + return mLeftButton; + case RIGHT: + return mRightButton; + default: + throw new IllegalArgumentException("Unexpected position"); + } + } + + private void setListener(@NonNull ButtonPosition position, @Nullable View.OnClickListener listener) { + switch (position) { + case LEFT: + mLeftButton.setOnClickListener(listener); + break; + case RIGHT: + mRightButton.setOnClickListener(listener); + break; + } + } + + void applyInfo(@NonNull NavigationBarConfig config) { + mLeftButton.setText(config.getLeftButtonText()); + mRightButton.setText(config.getRightButtonText()); + setNavigationBarVisibility(config.getNavBarVisibility()); + setButtonVisibility(ButtonPosition.LEFT, config.getLeftButtonVisibility()); + setButtonVisibility(ButtonPosition.RIGHT, config.getRightButtonVisibility()); + setListener(ButtonPosition.LEFT, config.getLeftListener()); + setListener(ButtonPosition.RIGHT, config.getRightListener()); + } + + private void setNavigationBarVisibility(@View.Visibility int visibility) { + mRoot.setVisibility(visibility); + } + + private void setButtonVisibility(@NonNull ButtonPosition position, @View.Visibility int visibility) { + switch (position) { + case LEFT: + mLeftButton.setVisibility(visibility); + break; + case RIGHT: + mRightButton.setVisibility(visibility); + break; + } + } +} diff --git a/library/src/main/java/moe/yuuta/flow/NavigationBarConfig.kt b/library/src/main/java/moe/yuuta/flow/NavigationBarConfig.kt new file mode 100644 index 0000000..e027b73 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/NavigationBarConfig.kt @@ -0,0 +1,16 @@ +package moe.yuuta.flow + +import android.view.View + +class NavigationBarConfig( + var rightButtonText: CharSequence, + var leftButtonText: CharSequence, + @field:View.Visibility + var leftButtonVisibility: Int, + @field:View.Visibility + var rightButtonVisibility: Int, + @field:View.Visibility + var navBarVisibility: Int, + var leftListener: View.OnClickListener?, + var rightListener: View.OnClickListener? +)
\ No newline at end of file diff --git a/library/src/main/java/moe/yuuta/flow/PageFragment.java b/library/src/main/java/moe/yuuta/flow/PageFragment.java new file mode 100644 index 0000000..dcc9f21 --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/PageFragment.java @@ -0,0 +1,30 @@ +package moe.yuuta.flow; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +public abstract class PageFragment extends Fragment { + private IFlowFragment mHostFragment; + + /** + * Once mInfo is changed, you should call {@link IFlowFragment#notifyCurrentFlowInfoUpdated()} to publish it. + * Note: it will be permanently change the recorded info. + */ + protected FlowInfo mInfo; + + final void setHostFragment(@NonNull IFlowFragment hostFragment) { + this.mHostFragment = hostFragment; + } + + @NonNull + protected final IFlowFragment getHostFragment() { + return mHostFragment; + } + + /** + * @return true: the fragment handled this event and you do not need to call super. false: call super. + */ + public boolean onBackPressed() { + return false; + } +}
\ No newline at end of file diff --git a/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java b/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java new file mode 100644 index 0000000..c821eef --- /dev/null +++ b/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java @@ -0,0 +1,64 @@ +package moe.yuuta.flow.widgets; + +import android.content.Context; +import android.os.Build; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; + +public class FlowPager extends ViewPager { + public FlowPager(@NonNull Context context) { + super(context); + } + + public FlowPager(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + // Thanks to https://stackoverflow.com/a/32488566/6792243 + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + if(null != getAdapter()) { + int height = 0; + View child = ((FragmentPagerAdapter) getAdapter()).getItem(getCurrentItem()).getView(); + if (child != null) { + child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + height = child.getMeasuredHeight(); + // TODO: Support api-14 and api-15? + if (Build.VERSION.SDK_INT >= 16 && height < getMinimumHeight()) { + height = getMinimumHeight(); + } + } + + int newHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); + if (getLayoutParams().height != 0 && heightMeasureSpec != newHeight) { + getLayoutParams().height = height; + + } else { + heightMeasureSpec = newHeight; + } + } + + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + + // Thanks to https://stackoverflow.com/a/13437997/6792243 + @Override + public boolean onTouchEvent(MotionEvent event) { + return false; + } + + // Thanks to https://stackoverflow.com/a/13437997/6792243 + @Override + public boolean onInterceptTouchEvent(MotionEvent event) { + return false; + } + +} |