Skip to content

Commit 746648d

Browse files
committed
modified table and fixtures
1 parent e16512f commit 746648d

File tree

6 files changed

+118
-35
lines changed

6 files changed

+118
-35
lines changed

lib/apis/Apis.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Apis {
5050
static String updateTableData = "${apiUrl}table/update/";
5151
static String deleteTableData = "${apiUrl}table/delete/";
5252
static String addTableData = "${apiUrl}table/add";
53+
static String resetTableData = "${apiUrl}table/reset/";
5354

5455
static String castMessage = "${apiUrl}notify/castMessage";
5556
// api route for running a fixture
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import '../models/table_model.dart';
2+
import '../services/table_service.dart';
3+
import '/exports/exports.dart';
4+
5+
class TableController with ChangeNotifier {
6+
bool _loading = true;
7+
bool get loading => _loading;
8+
set loading(bool value) {
9+
_loading = value;
10+
notifyListeners();
11+
}
12+
13+
List<Message> _tableData = [];
14+
List<Message> get tableData => _tableData;
15+
void fetchData(String league) {
16+
TableService().getTeams(league).then((value) {
17+
_tableData = value;
18+
loading = false;
19+
notifyListeners();
20+
});
21+
}
22+
}

lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '/theme/Theme.dart';
99

1010
import '/exports/exports.dart';
1111
import 'controllers/league_controller.dart';
12+
import 'controllers/table_controller.dart';
1213
import 'controllers/team_controller.dart';
1314

1415
void main() async {
@@ -50,6 +51,9 @@ void main() async {
5051
ChangeNotifierProvider(
5152
create: (_) => MatchDateController(),
5253
),
54+
ChangeNotifierProvider(
55+
create: (_) => TableController(),
56+
),
5357
],
5458
child: Consumer<AppController>(
5559
builder: (context, controller, child) {

lib/services/table_service.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,33 @@ class TableService {
6868
// showMessage(msg: e.message);
6969
}
7070
}
71+
72+
// function to reset table data
73+
Future<void> resetTableData(String leagueId) async {
74+
try {
75+
Response response = await Client().put(
76+
Uri.parse(Apis.resetTableData + leagueId),
77+
);
78+
showAdaptive(
79+
const SizedBox.square(
80+
dimension: 100,
81+
child: Row(
82+
children: [
83+
Text("Reset table data"),
84+
CircularProgressIndicator(),
85+
],
86+
),
87+
),
88+
);
89+
if (response.statusCode == 200) {
90+
print(response.body);
91+
Routes.popPage();
92+
showMessage(msg: "Table Data reset successfully");
93+
} else {
94+
showMessage(msg: "Something went wrong");
95+
}
96+
} on ClientException catch (e) {
97+
showMessage(msg: e.message);
98+
}
99+
}
71100
}

lib/services/team_service.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:convert';
12
import 'dart:io';
23

34
import '/models/team.dart';
@@ -13,17 +14,13 @@ class TeamService {
1314
);
1415
if (response.statusCode == 200) {
1516
res = response.body;
17+
return teamModelFromJson(res).message;
18+
} else {
19+
return Future.error(json.decode(response.body)['message']);
1620
}
17-
} on ClientException catch (_) {
18-
debugPrint(_.message);
19-
} on SocketException catch (_) {
20-
debugPrint(_.message);
21-
} on HttpException catch (_) {
22-
debugPrint(_.message);
23-
} catch (e) {
24-
throw Exception(e.toString());
21+
} on Exception catch (e) {
22+
return Future.error(e.toString());
2523
}
26-
return teamModelFromJson(res).message;
2724
}
2825

2926
static void createTeam(Map<String, dynamic> data) async {
@@ -112,7 +109,7 @@ class TeamService {
112109
}
113110

114111
static void deleteTeam(String id) async {
115-
showLoader(text:"Deleting...");
112+
showLoader(text: "Deleting...");
116113
try {
117114
Response res = await Client().delete(Uri.parse(Apis.deleteTeam + id));
118115
if (res.statusCode == 200) {

lib/views/pages/sections/table_data/TableResults.dart

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import 'dart:async';
1+
import '/controllers/table_controller.dart';
22

33
import 'edit_table.dart';
44

5-
import '/models/table_model.dart';
5+
// import '/models/table_model.dart';
66
import '/services/table_service.dart';
77
import '/exports/exports.dart';
88

@@ -18,30 +18,31 @@ class TableResults extends StatefulWidget {
1818
}
1919

2020
class _TableResultsState extends State<TableResults> {
21-
StreamController<List<Message>> _streamController =
22-
StreamController<List<Message>>();
23-
Timer? _timer;
24-
void fetchData() async {
25-
var tableData = await TableService().getTeams(widget.leagueId);
26-
_streamController.add(tableData);
27-
Timer.periodic(const Duration(seconds: 1), (timer) async {
28-
var tableData = await TableService().getTeams(widget.leagueId);
29-
_streamController.add(tableData);
30-
});
31-
}
21+
// final StreamController<List<Message>> _streamController =
22+
// StreamController<List<Message>>();
23+
// Timer? _timer;
24+
// void fetchData() async {
25+
// var tableData = await TableService().getTeams(widget.leagueId);
26+
// _streamController.add(tableData);
27+
// Timer.periodic(const Duration(seconds: 1), (timer) async {
28+
// var tableData = await TableService().getTeams(widget.leagueId);
29+
// _streamController.add(tableData);
30+
// });
31+
// }
3232

3333
@override
3434
void initState() {
3535
super.initState();
36-
fetchData();
36+
Provider.of<TableController>(context, listen: false).dispose();
37+
// fetchData(
3738
}
3839

3940
@override
4041
void dispose() {
41-
if (_streamController.hasListener) {
42-
_streamController.close();
43-
}
44-
_timer?.cancel();
42+
// if (_streamController.hasListener) {
43+
// _streamController.close();
44+
// }
45+
// _timer?.cancel();
4546
super.dispose();
4647
}
4748

@@ -50,21 +51,50 @@ class _TableResultsState extends State<TableResults> {
5051
return Scaffold(
5152
appBar: AppBar(
5253
title: const Text("Table Results"),
54+
actions: [
55+
IconButton(
56+
onPressed: () {
57+
showAdaptiveDialog(
58+
context: context,
59+
builder: (context) => AlertDialog.adaptive(
60+
title: const Text("Reset Data"),
61+
content: const Text("Do you want to refresh the data?"),
62+
actions: [
63+
TextButton(
64+
onPressed: () {
65+
Navigator.pop(context);
66+
},
67+
child: const Text("No"),
68+
),
69+
TextButton(
70+
onPressed: () {
71+
Routes.popPage();
72+
TableService().resetTableData(widget.leagueId);
73+
},
74+
child: const Text("Yes"),
75+
),
76+
],
77+
),
78+
);
79+
},
80+
icon: const Icon(Icons.refresh),
81+
),
82+
],
5383
),
5484
body: Padding(
5585
padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
56-
child: StreamBuilder(
57-
stream: _streamController.stream,
58-
builder: (context, snapshot) {
59-
return snapshot.hasData
60-
? snapshot.data!.isEmpty
86+
child: Consumer<TableController>(
87+
builder: (context, tableController, sna) {
88+
tableController.fetchData(widget.leagueId);
89+
return tableController.loading == false
90+
? tableController.tableData.isEmpty
6191
? const Center(
6292
child: Text("No data found"),
6393
)
6494
: ListView.builder(
65-
itemCount: snapshot.data!.length,
95+
itemCount: tableController.tableData.length,
6696
itemBuilder: (context, index) {
67-
var data = snapshot.data![index];
97+
var data = tableController.tableData[index];
6898
return ProfileWidget(
6999
img: data.team.image,
70100
titleText: data.team.name,

0 commit comments

Comments
 (0)