Skip to content

Commit

Permalink
Apply search page controller to release event page
Browse files Browse the repository at this point in the history
  • Loading branch information
up2code committed Jan 13, 2021
1 parent 5506e5d commit 1b09f47
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 54 deletions.
51 changes: 3 additions & 48 deletions lib/src/controllers/release_event_search_controller.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vocadb_app/controllers.dart';
import 'package:vocadb_app/models.dart';
import 'package:vocadb_app/repositories.dart';
import 'package:vocadb_app/services.dart';
import 'package:vocadb_app/utils.dart';

class ReleaseEventSearchController extends GetxController {
final int maxResults = 50;

final initialLoading = true.obs;

/// List of results from search
final results = <ReleaseEventModel>[].obs;

/// Query input string
final query = ''.obs;

/// Set to True when user tap search icon.
final openQuery = false.obs;

class ReleaseEventSearchController
extends SearchPageController<ReleaseEventModel> {
/// Filter parameter
final category = ''.obs;

Expand All @@ -32,9 +21,6 @@ class ReleaseEventSearchController extends GetxController {

final tags = <TagModel>[].obs;

/// If set to [True], no fetch more data from server. Default is [False].
final noFetchMore = false.obs;

final ReleaseEventRepository releaseEventRepository;

TextEditingController textSearchController;
Expand All @@ -43,22 +29,11 @@ class ReleaseEventSearchController extends GetxController {

@override
void onInit() {
initialFetch();
[category, sort, artists, tags, fromDate, toDate]
.forEach((element) => ever(element, (_) => initialFetch()));
debounce(query, (_) => initialFetch(), time: Duration(seconds: 1));
textSearchController = TextEditingController();
super.onInit();
}

void initialFetch() {
Future.value(noFetchMore(false))
.then((_) => fetchApi())
.then(verifyShouldFetchMore)
.then(results)
.then(initialLoadingDone);
}

Future<List<ReleaseEventModel>> fetchApi({int start}) =>
releaseEventRepository.findReleaseEvents(
start: (start == null) ? 0 : start,
Expand All @@ -72,24 +47,4 @@ class ReleaseEventSearchController extends GetxController {
beforeDate: DateTimeUtils.toUtcDateString(toDate.value),
afterDate: DateTimeUtils.toUtcDateString(fromDate.value),
);

clearQuery() {
query('');
textSearchController.clear();
}

initialLoadingDone(_) => initialLoading(false);

List<ReleaseEventModel> verifyShouldFetchMore(List<ReleaseEventModel> items) {
if (items == null || items.isEmpty || items.length < maxResults)
noFetchMore(true);
return items;
}

onReachLastItem() {
if (noFetchMore.value) return;
fetchApi(start: results.length + 1)
.then(verifyShouldFetchMore)
.then(results.addAll);
}
}
12 changes: 7 additions & 5 deletions lib/src/pages/release_event_search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ class ReleaseEventSearchPage extends GetView<ReleaseEventSearchController> {
body: Obx(
() => (controller.initialLoading.value)
? CenterLoading()
: ReleaseEventListView(
events: controller.results.toList(),
onSelect: this._onTapReleaseEvent,
onReachLastItem: controller.onReachLastItem,
),
: (controller.errorMessage.string.isNotEmpty)
? CenterText(controller.errorMessage.string)
: ReleaseEventListView(
events: controller.results.toList(),
onSelect: this._onTapReleaseEvent,
onReachLastItem: controller.onReachLastItem,
emptyWidget: CenterText('searchResultNotMatched'.tr)),
));
}
}
13 changes: 12 additions & 1 deletion lib/src/widgets/release_event_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import 'package:vocadb_app/widgets.dart';

class ReleaseEventListView extends StatelessWidget {
ReleaseEventListView(
{Key key, this.events, this.onSelect, this.onReachLastItem})
{Key key,
this.events,
this.onSelect,
this.onReachLastItem,
this.emptyWidget})
: super(key: key);

/// List of events to display.
Expand All @@ -15,8 +19,15 @@ class ReleaseEventListView extends StatelessWidget {
/// A callback function when user react to last item. Only worked on list view with vertical direction.
final Function onReachLastItem;

/// A widget that display when songs is empty
final Widget emptyWidget;

@override
Widget build(BuildContext context) {
if (events.isEmpty && this.emptyWidget != null) {
return this.emptyWidget;
}

return InfiniteListView(
onReachLastItem: this.onReachLastItem,
itemCount: this.events.length,
Expand Down

0 comments on commit 1b09f47

Please sign in to comment.