SlideShare a Scribd company logo
1 of 16
RECYCLER VIEW - MVP
By Bhavya Rattan
Handling Pagination, Tap To
Retry, No Internet Connection
and Swipe Refresh
simultaneously
The first and most confusing question when you start working on
a screen to display list of items is “Where do I keep the Data
List?”. The answer is debatable and there are 2 approaches to it :
1. Keep data list in the Presenter Implementer class
2. Keep data list in the Activity that implements
View class
Where do I keep the Data List ?
Pros :
● Presenter gets full control over data and data manipulation is easy
● Lesser to and fro between view, presenter and interactor
● Data manipulation is easy
● Easy to implement pagination
Cons :
● State maintenance is tricky
Approach 1 - Keeping data list in Presenter
Implementer :
Pros :
● Easy state maintenance - as activity life cycle is automatically followed
● Eliminates data manipulation operations from presenter
Cons :
● Additional overhead to fetch data from presenter - making the cycle like : view
requests data - calls presenter - presenter calls interactor - presenter returns data
back to view
● Data manipulation involves tedious to and fro between presenter and view
● Difficult to implement pagination as count and skip values are again to be fetched
from View - resulting in presenter-view communication overhead
Approach 2 - Keeping data list in Activity :
PS : We have followed Approach 1 in the project repository and would be
discussing same in upcoming slides
Handling Pagination
● We have handled pagination using limit and skip, where :
● Limit : specifies the number of items you want to fetch from server
● Skip : specifies the number of items to be skipped, beginning from 1st item.
Server skips items upto skip value and returns next consecutive items
● Total count : this value is provided by server in response and it specifies the
total number of items in the data list
● By default we have initialized limit to 10, skip to 0 and total count to -1 in the
project, after response from server updating values to : skip = dataList.size()
and total count = value fetched from server
To stop loading after all data is fetched :
if (totalCount != -1 && totalCount <= skip) {
mNotificationView.hideRecyclerLoader();
return;
}
Handling Screen
states through
Recycler Items
● VIEW_ITEM : How the default data item
would be displayed
● VIEW_PROG : To display progress loader
at bottom while fetching next page data
● VIEW_RETRY : To display Retry image
view at bottom when any failure occurs
while fetching next page
● VIEW_ERROR : To display error message
in case of failure
● VIEW_NO_DATA : To display “No Data
message” in case the list returned from
server is empty
Methods in recycler :
● showLoading
● dismissLoading
● addAll
● addItemMore
● displayErrorMessage
● displayNoDataString
● displayRetryView
● hideRetryView
Methods in recycler Explained:
/**
* show progress loader at bottom while recycler view
* is loading more items on scroll
*/
public void showLoading() {
if (isMoreLoading && notifications != null && onLoadMoreListener != null) {
isMoreLoading = false;
new Handler().post(new Runnable() {
@Override
public void run() {
notifications.add(new Notification());
notifications.get(notifications.size() - 1).setItemType(VIEW_PROG);
notifyItemInserted(notifications.size() - 1);
onLoadMoreListener.onLoadMore();
}
});
}
}
Methods in recycler Explained:
/**
* dismiss progress loader shown at bottom
* after the onLoadMore() method has performed its task
* and new data is added to recycler view.
* This function removes the progress loader item added at end of recycler view list
*/
public void dismissLoading() {
if (notifications != null && notifications.size() > 0
&& notifications.get(notifications.size() - 1).getItemType() ==
VIEW_PROG) {
notifications.remove(notifications.size() - 1);
notifyItemRemoved(notifications.size());
}
}
Methods in recycler Explained:
/**
* add all items to notification list, called
* to initialize the list a fresh
*
* @param allNotifications notification list
*/
public void addAll(final ArrayList<Notification> allNotifications) {
notifications.clear();
notifications.addAll(allNotifications);
notifyDataSetChanged();
}
Methods in recycler Explained:
/**
* add more items to notification list on scroll
*
* @param moreNotifications notification list
*/
public void addItemMore(final ArrayList<Notification> moreNotifications) {
int sizeInit = notifications.size();
notifications.addAll(moreNotifications);
notifyItemRangeChanged(sizeInit, notifications.size());
}
Methods in recycler Explained:
/**
* In case of an error
* clear the recycler view list and add a null value item
* notify adapter that data set has changed
* show error message to user
*
* @param message error message to be displayed
*/
public void displayErrorMessage(final String message) {
errorMessage = message;
notifications.clear();
notifications.add(new Notification());
notifications.get(notifications.size() - 1).setItemType(VIEW_ERROR);
notifyDataSetChanged();
}
Methods in recycler Explained:
/**
* In case the list of data received from server
* is empty
* show no data message to user
*
* @param message no data message to be displayed
*/
public void displayNoDataString(final String message) {
noDataString = message;
notifications.clear();
notifications.add(new Notification());
notifications.get(notifications.size() - 1).setItemType(VIEW_NO_DATA);
notifyDataSetChanged();
}
Methods in recycler Explained:
/**
*
*/
public void displayRetryView() {
if (notifications.get(notifications.size() - 1).getItemType() != VIEW_RETRY) {
notifications.add(new Notification());
notifications.get(notifications.size() - 1).setItemType(VIEW_RETRY);
notifyItemInserted(notifications.size() - 1);
}
}
Methods in recycler Explained:
/**
*
*/
public void hideRetryView() {
if (notifications != null && notifications.size() > 0
&& notifications.get(notifications.size() - 1).getItemType() ==
VIEW_RETRY) {
notifications.remove(notifications.size() - 1);
notifyItemRemoved(notifications.size());
}
}
References :
● Find the code at :
https://git.clicklabs.in/ClickLabs/
juggernaut-android-mvp.git,
recycler_module branch
● https://android.jlelse.eu/recyclerv
iew-in-mvp-passive-views-
approach-8dd74633158
● http://bajicdusko.com/2017/recy
cler-view-in-MVP/
● http://codetoart.com/implementat
ion-of-model-view-presenter-mvp-
design-pattern-on-android/
Recycler   mvp

More Related Content

What's hot

Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intentDiego Grancini
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
Bpel activities to upload club oracle
Bpel activities to upload club oracleBpel activities to upload club oracle
Bpel activities to upload club oracleXAVIERCONSULTANTS
 
BroadcastReceivers in Android
BroadcastReceivers in AndroidBroadcastReceivers in Android
BroadcastReceivers in AndroidPerfect APK
 

What's hot (6)

React hooks
React hooksReact hooks
React hooks
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
Bpel activities to upload club oracle
Bpel activities to upload club oracleBpel activities to upload club oracle
Bpel activities to upload club oracle
 
Hot React Hooks
Hot React HooksHot React Hooks
Hot React Hooks
 
BroadcastReceivers in Android
BroadcastReceivers in AndroidBroadcastReceivers in Android
BroadcastReceivers in Android
 

Similar to Recycler mvp

SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxMongoDB
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivitiesmaamir farooq
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activitiesmaamir farooq
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdfssusere71a07
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 ThreadingDiego Grancini
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projectsIgnacio Martín
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsEvangelia Mitsopoulou
 
Thinking metrics on React apps
Thinking metrics on React appsThinking metrics on React apps
Thinking metrics on React appsJean Carlo Emer
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialKaty Slemon
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Gabor Varadi
 
android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semaswinbiju1652
 
Android app performance
Android app performanceAndroid app performance
Android app performanceSaksham Keshri
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities Ahsanul Karim
 
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.AngularEvan Schultz
 
Generic steps in informatica
Generic steps in informaticaGeneric steps in informatica
Generic steps in informaticaBhuvana Priya
 

Similar to Recycler mvp (20)

SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivities
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activities
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdf
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Thinking metrics on React apps
Thinking metrics on React appsThinking metrics on React apps
Thinking metrics on React apps
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorial
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last sem
 
Android app performance
Android app performanceAndroid app performance
Android app performance
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.Angular
 
Generic steps in informatica
Generic steps in informaticaGeneric steps in informatica
Generic steps in informatica
 

Recently uploaded

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Recycler mvp

  • 1. RECYCLER VIEW - MVP By Bhavya Rattan Handling Pagination, Tap To Retry, No Internet Connection and Swipe Refresh simultaneously
  • 2. The first and most confusing question when you start working on a screen to display list of items is “Where do I keep the Data List?”. The answer is debatable and there are 2 approaches to it : 1. Keep data list in the Presenter Implementer class 2. Keep data list in the Activity that implements View class Where do I keep the Data List ?
  • 3. Pros : ● Presenter gets full control over data and data manipulation is easy ● Lesser to and fro between view, presenter and interactor ● Data manipulation is easy ● Easy to implement pagination Cons : ● State maintenance is tricky Approach 1 - Keeping data list in Presenter Implementer :
  • 4. Pros : ● Easy state maintenance - as activity life cycle is automatically followed ● Eliminates data manipulation operations from presenter Cons : ● Additional overhead to fetch data from presenter - making the cycle like : view requests data - calls presenter - presenter calls interactor - presenter returns data back to view ● Data manipulation involves tedious to and fro between presenter and view ● Difficult to implement pagination as count and skip values are again to be fetched from View - resulting in presenter-view communication overhead Approach 2 - Keeping data list in Activity : PS : We have followed Approach 1 in the project repository and would be discussing same in upcoming slides
  • 5. Handling Pagination ● We have handled pagination using limit and skip, where : ● Limit : specifies the number of items you want to fetch from server ● Skip : specifies the number of items to be skipped, beginning from 1st item. Server skips items upto skip value and returns next consecutive items ● Total count : this value is provided by server in response and it specifies the total number of items in the data list ● By default we have initialized limit to 10, skip to 0 and total count to -1 in the project, after response from server updating values to : skip = dataList.size() and total count = value fetched from server To stop loading after all data is fetched : if (totalCount != -1 && totalCount <= skip) { mNotificationView.hideRecyclerLoader(); return; }
  • 6. Handling Screen states through Recycler Items ● VIEW_ITEM : How the default data item would be displayed ● VIEW_PROG : To display progress loader at bottom while fetching next page data ● VIEW_RETRY : To display Retry image view at bottom when any failure occurs while fetching next page ● VIEW_ERROR : To display error message in case of failure ● VIEW_NO_DATA : To display “No Data message” in case the list returned from server is empty Methods in recycler : ● showLoading ● dismissLoading ● addAll ● addItemMore ● displayErrorMessage ● displayNoDataString ● displayRetryView ● hideRetryView
  • 7. Methods in recycler Explained: /** * show progress loader at bottom while recycler view * is loading more items on scroll */ public void showLoading() { if (isMoreLoading && notifications != null && onLoadMoreListener != null) { isMoreLoading = false; new Handler().post(new Runnable() { @Override public void run() { notifications.add(new Notification()); notifications.get(notifications.size() - 1).setItemType(VIEW_PROG); notifyItemInserted(notifications.size() - 1); onLoadMoreListener.onLoadMore(); } }); } }
  • 8. Methods in recycler Explained: /** * dismiss progress loader shown at bottom * after the onLoadMore() method has performed its task * and new data is added to recycler view. * This function removes the progress loader item added at end of recycler view list */ public void dismissLoading() { if (notifications != null && notifications.size() > 0 && notifications.get(notifications.size() - 1).getItemType() == VIEW_PROG) { notifications.remove(notifications.size() - 1); notifyItemRemoved(notifications.size()); } }
  • 9. Methods in recycler Explained: /** * add all items to notification list, called * to initialize the list a fresh * * @param allNotifications notification list */ public void addAll(final ArrayList<Notification> allNotifications) { notifications.clear(); notifications.addAll(allNotifications); notifyDataSetChanged(); }
  • 10. Methods in recycler Explained: /** * add more items to notification list on scroll * * @param moreNotifications notification list */ public void addItemMore(final ArrayList<Notification> moreNotifications) { int sizeInit = notifications.size(); notifications.addAll(moreNotifications); notifyItemRangeChanged(sizeInit, notifications.size()); }
  • 11. Methods in recycler Explained: /** * In case of an error * clear the recycler view list and add a null value item * notify adapter that data set has changed * show error message to user * * @param message error message to be displayed */ public void displayErrorMessage(final String message) { errorMessage = message; notifications.clear(); notifications.add(new Notification()); notifications.get(notifications.size() - 1).setItemType(VIEW_ERROR); notifyDataSetChanged(); }
  • 12. Methods in recycler Explained: /** * In case the list of data received from server * is empty * show no data message to user * * @param message no data message to be displayed */ public void displayNoDataString(final String message) { noDataString = message; notifications.clear(); notifications.add(new Notification()); notifications.get(notifications.size() - 1).setItemType(VIEW_NO_DATA); notifyDataSetChanged(); }
  • 13. Methods in recycler Explained: /** * */ public void displayRetryView() { if (notifications.get(notifications.size() - 1).getItemType() != VIEW_RETRY) { notifications.add(new Notification()); notifications.get(notifications.size() - 1).setItemType(VIEW_RETRY); notifyItemInserted(notifications.size() - 1); } }
  • 14. Methods in recycler Explained: /** * */ public void hideRetryView() { if (notifications != null && notifications.size() > 0 && notifications.get(notifications.size() - 1).getItemType() == VIEW_RETRY) { notifications.remove(notifications.size() - 1); notifyItemRemoved(notifications.size()); } }
  • 15. References : ● Find the code at : https://git.clicklabs.in/ClickLabs/ juggernaut-android-mvp.git, recycler_module branch ● https://android.jlelse.eu/recyclerv iew-in-mvp-passive-views- approach-8dd74633158 ● http://bajicdusko.com/2017/recy cler-view-in-MVP/ ● http://codetoart.com/implementat ion-of-model-view-presenter-mvp- design-pattern-on-android/