Skip to content
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

Lazy Loading feature added #76

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions lib/ui/views/projects/featured_projects_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_app/ui/components/cv_header.dart';
import 'package:mobile_app/ui/components/cv_primary_button.dart';
import 'package:mobile_app/ui/views/base_view.dart';
import 'package:mobile_app/ui/views/projects/components/featured_project_card.dart';
import 'package:mobile_app/ui/views/projects/project_details_view.dart';
Expand All @@ -21,13 +20,35 @@ class FeaturedProjectsView extends StatefulWidget {
}

class _FeaturedProjectsViewState extends State<FeaturedProjectsView> {
final _scrollController = ScrollController();
bool addedListener =
false; //Flag to know if a listener is already added to the controller or not.

dynamic _scrollListener(FeaturedProjectsViewModel model) {
var maxScroll = _scrollController.position.maxScrollExtent;
var currentScroll = _scrollController.position.pixels;
var delta =
200; // Loads more data if "delta" pixels left to reach maxScrollExtent
if (maxScroll - currentScroll <= delta) {
if (!model.isLoadingMoreCircuits &&
model?.previousFeaturedProjectsBatch?.links?.next != null) {
model.fetchFeaturedProjects();
}
}
addedListener = true;
}

@override
Widget build(BuildContext context) {
return BaseView<FeaturedProjectsViewModel>(
onModelReady: (model) => widget.embed
? model.fetchFeaturedProjects(size: 3)
: model.fetchFeaturedProjects(),
builder: (context, model, child) {
if (!addedListener) {
_scrollController.addListener(() => _scrollListener(model));
}

final _items = <Widget>[];

if (model.isSuccess(model.FETCH_FEATURED_PROJECTS)) {
Expand All @@ -43,16 +64,6 @@ class _FeaturedProjectsViewState extends State<FeaturedProjectsView> {
});
}

if (!widget.embed &&
model?.previousFeaturedProjectsBatch?.links?.next != null) {
_items.add(
CVPrimaryButton(
title: 'Load More',
onPressed: () => model.fetchFeaturedProjects(),
),
);
}

if (widget.embed) {
return Column(
children: _items,
Expand All @@ -67,12 +78,15 @@ class _FeaturedProjectsViewState extends State<FeaturedProjectsView> {
'These circuits have been hand-picked by our authors for their awesomeness.',
),
);

if (model.isLoadingMoreCircuits) {
_items.add(CircularProgressIndicator());
}
return Scaffold(
appBar: widget.showAppBar
? AppBar(title: Text('Featured Circuits'))
: null,
body: SingleChildScrollView(
controller: _scrollController,
padding: const EdgeInsets.all(16),
child: Column(
children: _items,
Expand Down
12 changes: 12 additions & 0 deletions lib/viewmodels/projects/featured_projects_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ class FeaturedProjectsViewModel extends BaseModel {
notifyListeners();
}

bool _isLoadingMoreCircuits = false;

bool get isLoadingMoreCircuits => _isLoadingMoreCircuits;

set isLoadingMoreCircuits(bool loading) {
_isLoadingMoreCircuits = loading;
notifyListeners();
}

Future fetchFeaturedProjects({int size = 5}) async {
try {
if (previousFeaturedProjectsBatch?.links?.next != null) {
// fetch next batch of projects..
isLoadingMoreCircuits = true;
String _nextPageLink = previousFeaturedProjectsBatch.links.next;

var _nextPageNumber =
Expand All @@ -38,6 +48,7 @@ class FeaturedProjectsViewModel extends BaseModel {
page: _nextPageNumber,
size: size,
);
isLoadingMoreCircuits = false;
} else {
// Set State as busy only very first time..
setStateFor(FETCH_FEATURED_PROJECTS, ViewState.Busy);
Expand All @@ -49,6 +60,7 @@ class FeaturedProjectsViewModel extends BaseModel {
featuredProjects.addAll(previousFeaturedProjectsBatch.data);
setStateFor(FETCH_FEATURED_PROJECTS, ViewState.Success);
} on Failure catch (f) {
isLoadingMoreCircuits = false;
setStateFor(FETCH_FEATURED_PROJECTS, ViewState.Error);
setErrorMessageFor(FETCH_FEATURED_PROJECTS, f.message);
}
Expand Down