-
Notifications
You must be signed in to change notification settings - Fork 265
Further abstraction #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
89442f9
Initial commit
leonardo2204 891f40f
Fixed manifest
leonardo2204 bfb0c70
Final example
leonardo2204 401f6fb
Added docs and changed a couple of stuff
leonardo2204 c81afa3
- moved common methods to AbstractStepAdapter
ca3c07a
- removed getStepView methods
f6d0f23
cleared destroyed views in AbstractStepAdapter
14e6cd5
- extracted logic from AbstractStepAdapter to the sample adapter
d21e6d6
Merge pull request #1 from zawadz88/master
leonardo2204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...rial-stepper/src/main/java/com/stepstone/stepper/adapter/AbstractFragmentStepAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2016 StepStone Services | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.stepstone.stepper.adapter; | ||
|
||
import android.support.annotation.StringRes; | ||
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.support.v4.view.ViewPager; | ||
|
||
import com.stepstone.stepper.Step; | ||
|
||
/** | ||
* A base adapter class which returns step fragments to use inside of the {@link com.stepstone.stepper.StepperLayout}. | ||
*/ | ||
public abstract class AbstractFragmentStepAdapter | ||
extends FragmentPagerAdapter | ||
implements StepAdapter { | ||
|
||
private final FragmentManager mFragmentManager; | ||
|
||
public AbstractFragmentStepAdapter(FragmentManager fm) { | ||
super(fm); | ||
mFragmentManager = fm; | ||
} | ||
|
||
@Override | ||
public final Fragment getItem(int position) { | ||
return (Fragment) createStep(position); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@SuppressWarnings("unchecked") | ||
public Step findStep(ViewPager viewPager, int position) { | ||
String fragmentTag = "android:switcher:" + viewPager.getId() + ":" + this.getItemId(position); | ||
return (Step) mFragmentManager.findFragmentByTag(fragmentTag); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@StringRes | ||
public int getNextButtonText(int position) { | ||
return DEFAULT_NEXT_BUTTON_TEXT; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public final PagerAdapter getPagerAdapter() { | ||
return this; | ||
} | ||
} |
74 changes: 11 additions & 63 deletions
74
material-stepper/src/main/java/com/stepstone/stepper/adapter/AbstractStepAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,23 @@ | ||
/* | ||
Copyright 2016 StepStone Services | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.stepstone.stepper.adapter; | ||
|
||
import android.support.annotation.StringRes; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentPagerAdapter; | ||
import android.support.v4.view.ViewPager; | ||
|
||
import com.stepstone.stepper.Step; | ||
import android.support.v4.view.PagerAdapter; | ||
|
||
/** | ||
* A base adapter class which returns step fragments to use inside of the {@link com.stepstone.stepper.StepperLayout}. | ||
* A base adapter class which returns step to use inside of the {@link com.stepstone.stepper.StepperLayout}. | ||
* This class is intended to be inherited if you need to use {@link com.stepstone.stepper.StepperLayout} without fragments. | ||
* Otherwise, you should use {@link AbstractFragmentStepAdapter} | ||
*/ | ||
public abstract class AbstractStepAdapter<T extends Fragment & Step> extends FragmentPagerAdapter { | ||
|
||
public static final int DEFAULT_NEXT_BUTTON_TEXT = -1; | ||
|
||
private final FragmentManager mFragmentManager; | ||
|
||
public AbstractStepAdapter(FragmentManager fm) { | ||
super(fm); | ||
mFragmentManager = fm; | ||
} | ||
public abstract class AbstractStepAdapter extends PagerAdapter implements StepAdapter { | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public final T getItem(int position) { | ||
return createStep(position); | ||
} | ||
|
||
protected abstract T createStep(int position); | ||
|
||
/** | ||
* Finds the given step without creating it. | ||
* @see FragmentPagerAdapter#makeFragmentName(int, long) | ||
* @param viewPager view pager to use for displaying step fragments | ||
* @param position step position | ||
* @return step fragment | ||
*/ | ||
public Step findStep(ViewPager viewPager, int position) { | ||
String fragmentTag = "android:switcher:" + viewPager.getId() + ":" + this.getItemId(position); | ||
return (Step) mFragmentManager.findFragmentByTag(fragmentTag); | ||
} | ||
|
||
/** | ||
* Allows to override the text on the Next button per step. | ||
* For a given step position you need to return a String resource ID for the label to be used. | ||
* If you wish to change the text for selected steps only (and leave the default for the rest) | ||
* then return {@link #DEFAULT_NEXT_BUTTON_TEXT} for the default ones. | ||
* By default this method returns {@link #DEFAULT_NEXT_BUTTON_TEXT} for all steps. | ||
* This method is not invoked for the last step. | ||
* @param position step position | ||
* @return a String resource ID to override the default button text or {@link #DEFAULT_NEXT_BUTTON_TEXT} if the default text should be kept | ||
*/ | ||
@StringRes | ||
public int getNextButtonText(int position) { | ||
return DEFAULT_NEXT_BUTTON_TEXT; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public final PagerAdapter getPagerAdapter() { | ||
return this; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
material-stepper/src/main/java/com/stepstone/stepper/adapter/StepAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.stepstone.stepper.adapter; | ||
|
||
import android.support.annotation.StringRes; | ||
import android.support.v4.app.FragmentPagerAdapter; | ||
import android.support.v4.view.PagerAdapter; | ||
import android.support.v4.view.ViewPager; | ||
|
||
import com.stepstone.stepper.Step; | ||
|
||
/** | ||
* Interface to be used as model to {@link com.stepstone.stepper.StepperLayout}. | ||
*/ | ||
public interface StepAdapter { | ||
|
||
int DEFAULT_NEXT_BUTTON_TEXT = -1; | ||
|
||
/** | ||
* Create each step of the {@link com.stepstone.stepper.StepperLayout}. | ||
* @param position The position of the {@link PagerAdapter} to be used inside the {@link ViewPager}. | ||
* @return the step to be used inside the {@link com.stepstone.stepper.StepperLayout}. | ||
*/ | ||
Step createStep(int position); | ||
|
||
/** | ||
* Finds the given step without creating it. | ||
* @see FragmentPagerAdapter#makeFragmentName(int, long) | ||
* @param viewPager view pager to use for displaying step fragments | ||
* @param position step position | ||
* @return step fragment | ||
*/ | ||
Step findStep(ViewPager viewPager, int position); | ||
|
||
/** | ||
* Allows to override the text on the Next button per step. | ||
* For a given step position you need to return a String resource ID for the label to be used. | ||
* If you wish to change the text for selected steps only (and leave the default for the rest) | ||
* then return {@link #DEFAULT_NEXT_BUTTON_TEXT} for the default ones. | ||
* By default this method returns {@link #DEFAULT_NEXT_BUTTON_TEXT} for all steps. | ||
* This method is not invoked for the last step. | ||
* @param position step position | ||
* @return a String resource ID to override the default button text or {@link #DEFAULT_NEXT_BUTTON_TEXT} if the default text should be kept | ||
*/ | ||
@StringRes int getNextButtonText(int position); | ||
|
||
/** | ||
* Get the step count. | ||
* @return the quantity of steps | ||
*/ | ||
int getCount(); | ||
|
||
/** | ||
* Method for internal purpose. Should not be inherited. | ||
* @return the adapter to be used in the {@link ViewPager}. | ||
*/ | ||
PagerAdapter getPagerAdapter(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to the fact that StepAdapter no longer gets a this is needed, however this feels kind of nasty. I'm wondering if there's a better way of handling this. @fabiozo @leonardo2204 any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean here, do you mean the casting ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The object returned from stepAdapter.createStep(i) is casted to Step. This seems kind of weird since I would assume the createStep method to actually return a Step instance. I'm not sure if I explained this well enough :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I get it, you're right. I was thinking about making public interface
StepAdapter<T extends Step>
, but then all the Frags example would break.Do you have any ideas ? Gonna think about it for a time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've given some thought about it, maybe throwing an error on getItem because of invalid cast ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a new proposition: leonardo2204#1 please give me your thoughts ;)