Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
ViewModelの責務について #19 の解消
Browse files Browse the repository at this point in the history
  • Loading branch information
YumNumm committed Nov 24, 2022
1 parent 54b4874 commit ade49bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
24 changes: 3 additions & 21 deletions lib/ui/page/search_view/component/repository_search_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,9 @@ class RepositorySearchList extends ConsumerWidget {
return data.when<Widget>(
data: (items) {
return NotificationListener<ScrollEndNotification>(
onNotification: (notification) {
// 検索結果を取得していない場合・検索結果がない場合は
// 何もしない
if (!data.hasValue) {
return false;
}
if (notification.metrics.extentAfter == 0 &&
(data.value!.isNotEmpty)) {
// 既に検索結果を全て読み込んでいる場合は、
// 次のページを読み込む処理を行わない
if (data.value!.length ==
(ref.read(totalRepositoryCountProvider).value ?? 0)) {
return true;
}
ref
.read(repositorySearchViewModel.notifier)
.loadMoreRepositories();
return true;
}
return false;
},
onNotification: (notification) => ref
.read(repositorySearchViewModel.notifier)
.onNotification(notification),
child: Scrollbar(
child: ListView.builder(
itemCount: items.length + (data.isLoading ? 1 : 0),
Expand Down
22 changes: 22 additions & 0 deletions lib/ui/page/search_view/search_view.viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:github_repository_search/model/github/search_param.dart';
import 'package:github_repository_search/model/github/search_response/search_response_item.dart';
import 'package:github_repository_search/repository/github_repository.dart';
Expand Down Expand Up @@ -88,4 +89,25 @@ class RepositorySearchViewModel
page = 1;
await fetch();
}

/// 既定のスクロール位置に達した際に発火
/// 追加読み込み可能な場合 loadMoreRepositories() を呼び出す
bool onNotification(ScrollEndNotification notification) {
// 検索結果を取得していない場合・検索結果がない場合は
// 何もしない
if (!state.hasValue) {
return false;
}
if (notification.metrics.extentAfter == 0 && (state.value!.isNotEmpty)) {
// 既に検索結果を全て読み込んでいる場合は、
// 次のページを読み込む処理を行わない
if (state.value!.length ==
(ref.read(totalRepositoryCountProvider).value ?? 0)) {
return true;
}
loadMoreRepositories();
return true;
}
return false;
}
}

0 comments on commit ade49bd

Please sign in to comment.