Skip to content

Commit

Permalink
support show contest list
Browse files Browse the repository at this point in the history
  • Loading branch information
YuiHrsw committed Mar 24, 2024
1 parent 245ea62 commit 5be5437
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 7 deletions.
9 changes: 9 additions & 0 deletions lib/backend/cfapi/cf_helper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:cf_partner/backend/cfapi/models/contest.dart';
import 'package:cf_partner/backend/cfapi/models/contests_request.dart';
import 'package:cf_partner/backend/cfapi/models/problem.dart';
import 'package:cf_partner/backend/cfapi/models/standing_request.dart';
import 'package:cf_partner/backend/cfapi/models/submission.dart';
Expand Down Expand Up @@ -63,4 +65,11 @@ class CFHelper {
}
return list[0];
}

static Future<List<Contest>> getContestList() async {
var request =
await WebHelper().get('https://codeforces.com/api/contest.list');
ContestListRequest requestInfo = ContestListRequest.fromJson(request.data);
return requestInfo.result;
}
}
25 changes: 25 additions & 0 deletions lib/backend/cfapi/models/contests_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:cf_partner/backend/cfapi/models/contest.dart';

class ContestListRequest {
ContestListRequest({
required this.status,
required this.result,
});

final String? status;
final List<Contest> result;

factory ContestListRequest.fromJson(Map<String, dynamic> json) {
return ContestListRequest(
status: json["status"],
result: json["result"] == null
? []
: List<Contest>.from(json["result"]!.map((x) => Contest.fromJson(x))),
);
}

Map<String, dynamic> toJson() => {
"status": status,
"result": result.map((x) => x.toJson()).toList(),
};
}
121 changes: 121 additions & 0 deletions lib/pages/explore.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:cf_partner/backend/cfapi/cf_helper.dart';
import 'package:cf_partner/backend/cfapi/models/contest.dart';
import 'package:flutter/material.dart';

class ExplorePage extends StatefulWidget {
Expand All @@ -8,16 +10,135 @@ class ExplorePage extends StatefulWidget {
}

class ExplorePageState extends State<ExplorePage> {
List<Contest> contests = [];
bool locked = true;

@override
void initState() {
super.initState();
init();
}

void init() async {
contests.addAll(await CFHelper.getContestList());
setState(() {
locked = false;
});
}

@override
Widget build(BuildContext context) {
late final colorScheme = Theme.of(context).colorScheme;

return Scaffold(
appBar: AppBar(
title: const Text(
'Explore',
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 26),
),
actions: [
// IconButton(onPressed: () {}, icon: const Icon(Icons.search_rounded)),
locked
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(),
)
: IconButton(
onPressed: () async {
setState(() {
locked = true;
});
contests.clear();
contests.addAll(await CFHelper.getContestList());
setState(() {
locked = false;
});
},
icon: const Icon(Icons.refresh)),
SizedBox(
width: locked ? 14 : 6,
)
],
scrolledUnderElevation: 0,
),
body: ListView.builder(
itemBuilder: (context, index) {
return SizedBox(
height: 50,
child: Card(
child: Row(
children: [
const SizedBox(
width: 4,
),
Ink(
decoration: BoxDecoration(
color: colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(8),
),
height: 20,
width: 40,
child: Center(
child: Text(
contests[index].id!.toString(),
style: TextStyle(
color: colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w500),
),
),
),
const SizedBox(
width: 4,
),
contests[index].phase! == 'FINISHED'
? Ink(
decoration: BoxDecoration(
color: colorScheme.secondaryContainer,
borderRadius: BorderRadius.circular(8),
),
height: 20,
width: 70,
child: Center(
child: Text(
'Finished',
style: TextStyle(
color: colorScheme.onSecondaryContainer,
fontWeight: FontWeight.w500),
),
),
)
: const SizedBox(),
const SizedBox(
width: 4,
),
Ink(
decoration: BoxDecoration(
color: colorScheme.tertiaryContainer,
borderRadius: BorderRadius.circular(8),
),
height: 20,
width: 50,
child: Center(
child: Text(
'${contests[index].durationSeconds! ~/ 3600}h${(contests[index].durationSeconds! % 3600) ~/ 60}m',
style: TextStyle(
color: colorScheme.onTertiaryContainer,
fontWeight: FontWeight.w500),
),
),
),
const SizedBox(
width: 4,
),
Text(contests[index].name!)
],
),
),
);
},
itemCount: contests.length,
),
);
}
}
14 changes: 8 additions & 6 deletions lib/pages/list_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ class ListDetailState extends State<ListDetail> {
),
height: 20,
width: 50,
child: Text(
widget.listItem.items[index].contestId!.toString() +
widget.listItem.items[index].index!,
style: TextStyle(
color: colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w500),
child: Center(
child: Text(
widget.listItem.items[index].contestId!.toString() +
widget.listItem.items[index].index!,
style: TextStyle(
color: colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w500),
),
),
),
const SizedBox(
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class SettingsState extends State<Settings> {
),
title: const Text('CF Partner'),
trailing: const Text(
'v 0.1',
'v 0.2',
style: TextStyle(
fontSize: 16,
),
Expand Down

0 comments on commit 5be5437

Please sign in to comment.