mirror of
https://github.com/git-touch/git-touch
synced 2025-03-25 15:40:09 +01:00
refactor: simplify list data payload
This commit is contained in:
parent
17df5d634c
commit
b320a8e6dd
@ -39,19 +39,6 @@ class DataWithPage<T> {
|
||||
bool hasMore;
|
||||
int total;
|
||||
DataWithPage({
|
||||
/*required*/ required this.data,
|
||||
/*required*/ required this.cursor,
|
||||
/*required*/ required this.hasMore,
|
||||
required this.total,
|
||||
});
|
||||
}
|
||||
|
||||
class BbPagePayload<T> {
|
||||
T data;
|
||||
String? cursor;
|
||||
bool hasMore;
|
||||
int total;
|
||||
BbPagePayload({
|
||||
required this.data,
|
||||
required this.cursor,
|
||||
required this.hasMore,
|
||||
@ -583,13 +570,12 @@ class AuthModel with ChangeNotifier {
|
||||
return json.decode(utf8.decode(res.bodyBytes));
|
||||
}
|
||||
|
||||
Future<BbPagePayload<List?>> fetchBbWithPage(String p) async {
|
||||
Future<ListPayload<dynamic, String?>> fetchBbWithPage(String p) async {
|
||||
final data = await fetchBbJson(p);
|
||||
final v = BbPagination.fromJson(data);
|
||||
return BbPagePayload(
|
||||
return ListPayload(
|
||||
cursor: v.next,
|
||||
total: v.size ?? TOTAL_COUNT_FALLBACK,
|
||||
data: v.values,
|
||||
items: v.values,
|
||||
hasMore: v.next != null,
|
||||
);
|
||||
}
|
||||
|
@ -3,12 +3,9 @@ part 'bitbucket.g.dart';
|
||||
|
||||
@JsonSerializable(fieldRename: FieldRename.snake)
|
||||
class BbPagination {
|
||||
int? pagelen;
|
||||
int? size;
|
||||
int? page;
|
||||
String? next;
|
||||
List? values;
|
||||
BbPagination();
|
||||
List values;
|
||||
BbPagination({required this.values});
|
||||
factory BbPagination.fromJson(Map<String, dynamic> json) =>
|
||||
_$BbPaginationFromJson(json);
|
||||
}
|
||||
|
@ -7,19 +7,13 @@ part of 'bitbucket.dart';
|
||||
// **************************************************************************
|
||||
|
||||
BbPagination _$BbPaginationFromJson(Map<String, dynamic> json) {
|
||||
return BbPagination()
|
||||
..pagelen = json['pagelen'] as int?
|
||||
..size = json['size'] as int?
|
||||
..page = json['page'] as int?
|
||||
..next = json['next'] as String?
|
||||
..values = json['values'] as List<dynamic>?;
|
||||
return BbPagination(
|
||||
values: json['values'] as List<dynamic>,
|
||||
)..next = json['next'] as String?;
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$BbPaginationToJson(BbPagination instance) =>
|
||||
<String, dynamic>{
|
||||
'pagelen': instance.pagelen,
|
||||
'size': instance.size,
|
||||
'page': instance.page,
|
||||
'next': instance.next,
|
||||
'values': instance.values,
|
||||
};
|
||||
|
@ -8,18 +8,7 @@ import 'package:provider/provider.dart';
|
||||
import 'package:git_touch/widgets/error_reload.dart';
|
||||
import 'package:git_touch/widgets/loading.dart';
|
||||
import 'package:git_touch/widgets/empty.dart';
|
||||
|
||||
class ListPayload<T, K> {
|
||||
K cursor;
|
||||
Iterable<T> items;
|
||||
bool hasMore;
|
||||
|
||||
ListPayload({
|
||||
required this.items,
|
||||
required this.cursor,
|
||||
required this.hasMore,
|
||||
});
|
||||
}
|
||||
export 'package:git_touch/utils/utils.dart';
|
||||
|
||||
// This is a scaffold for infinite scroll screens
|
||||
class ListStatefulScaffold<T, K> extends StatefulWidget {
|
||||
|
@ -5,6 +5,7 @@ import 'package:git_touch/scaffolds/list_stateful.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/commit_item.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:git_touch/utils/utils.dart';
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
|
||||
class BbCommitsScreen extends StatelessWidget {
|
||||
@ -25,7 +26,7 @@ class BbCommitsScreen extends StatelessWidget {
|
||||
cursor: res.cursor,
|
||||
hasMore: res.hasMore,
|
||||
items: <BbCommit>[
|
||||
for (var v in res.data!) BbCommit.fromJson(v),
|
||||
for (var v in res.items) BbCommit.fromJson(v),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -5,6 +5,7 @@ import 'package:git_touch/scaffolds/list_stateful.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/repository_item.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:git_touch/utils/utils.dart';
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
|
||||
class BbExploreScreen extends StatelessWidget {
|
||||
@ -19,7 +20,7 @@ class BbExploreScreen extends StatelessWidget {
|
||||
cursor: res.cursor,
|
||||
hasMore: res.hasMore,
|
||||
items: <BbRepo>[
|
||||
for (var v in res.data!) BbRepo.fromJson(v),
|
||||
for (var v in res.items) BbRepo.fromJson(v),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -30,7 +30,7 @@ class BbIssuesScreen extends StatelessWidget {
|
||||
cursor: res.cursor,
|
||||
hasMore: res.hasMore,
|
||||
items: <BbIssues>[
|
||||
for (var v in res.data!) BbIssues.fromJson(v),
|
||||
for (var v in res.items) BbIssues.fromJson(v),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -5,6 +5,7 @@ import 'package:git_touch/scaffolds/list_stateful.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/issue_item.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:git_touch/utils/utils.dart';
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
|
||||
class BbPullsScreen extends StatelessWidget {
|
||||
@ -24,7 +25,7 @@ class BbPullsScreen extends StatelessWidget {
|
||||
cursor: res.cursor,
|
||||
hasMore: res.hasMore,
|
||||
items: <BbPulls>[
|
||||
for (var v in res.data!) BbPulls.fromJson(v),
|
||||
for (var v in res.items) BbPulls.fromJson(v),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -36,7 +36,7 @@ class BbRepoScreen extends StatelessWidget {
|
||||
final branches = await auth
|
||||
.fetchBbWithPage('/repositories/$owner/$name/refs/branches')
|
||||
.then((v) {
|
||||
return [for (var branch in v.data!) BbBranch.fromJson(branch)];
|
||||
return [for (var branch in v.items) BbBranch.fromJson(branch)];
|
||||
});
|
||||
return Tuple3(repo, readme, branches);
|
||||
},
|
||||
|
@ -6,6 +6,7 @@ import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/user_item.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:timeago/timeago.dart' as timeago;
|
||||
import 'package:git_touch/utils/utils.dart';
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
|
||||
class BbTeamsScreen extends StatelessWidget {
|
||||
@ -21,7 +22,7 @@ class BbTeamsScreen extends StatelessWidget {
|
||||
cursor: res.cursor,
|
||||
hasMore: res.hasMore,
|
||||
items: <BbUser>[
|
||||
for (var v in res.data!) BbUser.fromJson(v),
|
||||
for (var v in res.items) BbUser.fromJson(v),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -201,35 +201,34 @@ class GlProjectScreen extends StatelessWidget {
|
||||
url:
|
||||
'/gitlab/projects/$id/commits?prefix=$prefix&branch=${branch ?? p.defaultBranch}', // EDIT
|
||||
),
|
||||
if (branches != null)
|
||||
TableViewItem(
|
||||
leftIconData: Octicons.git_branch,
|
||||
text: Text(AppLocalizations.of(context)!.branches),
|
||||
rightWidget: Text(((branch ?? p.defaultBranch) ??
|
||||
'' /** empty project */) +
|
||||
' • ' +
|
||||
branches.length.toString()),
|
||||
onTap: () async {
|
||||
if (branches.length < 2) return;
|
||||
TableViewItem(
|
||||
leftIconData: Octicons.git_branch,
|
||||
text: Text(AppLocalizations.of(context)!.branches),
|
||||
rightWidget: Text(
|
||||
((branch ?? p.defaultBranch) ?? '' /** empty project */) +
|
||||
' • ' +
|
||||
branches.length.toString()),
|
||||
onTap: () async {
|
||||
if (branches.length < 2) return;
|
||||
|
||||
await theme.showPicker(
|
||||
context,
|
||||
PickerGroupItem(
|
||||
value: branch,
|
||||
items: branches
|
||||
.map((b) => PickerItem(b.name, text: b.name))
|
||||
.toList(),
|
||||
onClose: (ref) {
|
||||
if (ref != branch) {
|
||||
theme.push(
|
||||
context, '/gitlab/projects/$id?branch=$ref',
|
||||
replace: true);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
await theme.showPicker(
|
||||
context,
|
||||
PickerGroupItem(
|
||||
value: branch,
|
||||
items: branches
|
||||
.map((b) => PickerItem(b.name, text: b.name))
|
||||
.toList(),
|
||||
onClose: (ref) {
|
||||
if (ref != branch) {
|
||||
theme.push(
|
||||
context, '/gitlab/projects/$id?branch=$ref',
|
||||
replace: true);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
CommonStyle.verticalGap,
|
||||
|
@ -186,3 +186,15 @@ int sortByKey<T>(T key, T a, T b) {
|
||||
}
|
||||
|
||||
const TOTAL_COUNT_FALLBACK = 999; // TODO:
|
||||
|
||||
class ListPayload<T, K> {
|
||||
K cursor;
|
||||
Iterable<T> items;
|
||||
bool hasMore;
|
||||
|
||||
ListPayload({
|
||||
required this.items,
|
||||
required this.cursor,
|
||||
required this.hasMore,
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user