· 4 years ago · May 27, 2021, 09:18 AM
1//---------------------------------------------------------------------------------------------------------
2// Model
3
4class GameBannerModel {
5 GameBannerModel({
6 this.id,
7 .......
8 this.tImage,
9 });
10 int? id;
11 .....
12 String? tImage;
13
14 factory GameBannerModel.fromJson(Map<String, dynamic> json) {
15 return GameBannerModel(
16 id: json["id"],
17 ...........
18 gamePlateform: json["game"]["plateform"],
19 );
20 }
21
22 factory GameBannerModel.initialData() {
23 return GameBannerModel(
24 id: null,
25 ...........
26 gamePlateform: null,
27 );
28 }
29}
30
31
32//---------------------------------------------------------------------------------------------------------
33// API
34
35class ApiResponse<T> {
36
37 Status status;
38 T? data;
39 String? message;
40 ApiResponse.loading(this.message) : status = Status.LOADING;
41 ApiResponse.completed(this.data) : status = Status.COMPLETED;
42 ApiResponse.error(this.message) : status = Status.ERROR;
43
44 @override
45 String toString() {
46 return "Status : $status \n Message : $message \n Data : $data";
47 }
48}
49
50enum Status { LOADING, COMPLETED, ERROR }
51
52
53//----------------------------------------------------------------------------------------------------------------
54
55class ApiBaseHelper {
56 Future get(String url) async {
57 var responseJson;
58 try {
59 final response = await http.get(Uri.http(ApiRoute.baseApi, url));
60 responseJson = _returnResponse(response);
61 } on SocketException {
62 print('No net');
63 throw FetchDataException('No Internet connection');
64 }
65 print('api get recieved!');
66 return responseJson;
67 }
68}
69
70dynamic _returnResponse(http.Response response) {
71 switch (response.statusCode) {
72 case 200:
73 var responseJson = json.decode(response.body.toString());
74 // print(response.body);
75 return responseJson;
76 case 400:
77 throw BadRequestException(response.body.toString());
78 case 401:
79 case 403:
80 throw UnauthorisedException(response.body.toString());
81 case 500:
82 default:
83 throw FetchDataException('Error occured while Communication with Server with StatusCode : ${response.statusCode}');
84 }
85}
86
87//-------------------------------------------------------------------------------------------------------------
88
89class AppException implements Exception {
90 final _message;
91 final _prefix;
92
93 AppException([this._message, this._prefix]);
94
95 String toString() {
96 return "$_prefix$_message";
97 }
98}
99
100class FetchDataException extends AppException {
101 FetchDataException([String? message]) : super(message, "Error During Communication: ");
102}
103
104class BadRequestException extends AppException {
105 BadRequestException([message]) : super(message, "Invalid Request: ");
106}
107
108class UnauthorisedException extends AppException {
109 UnauthorisedException([message]) : super(message, "Unauthorised: ");
110}
111
112class InvalidInputException extends AppException {
113 InvalidInputException([String? message]) : super(message, "Invalid Input: ");
114}
115
116
117//--------------------------------------------------------------------
118class BannerRepository {
119 ApiBaseHelper _helper = ApiBaseHelper();
120
121 Future<List<GameBannerModel>> fetchBannerList() async {
122 final response = await _helper.get(ApiRoute.gameBannerApi) as List;
123 var _banner = response.map<GameBannerModel>((json) => GameBannerModel.fromJson(json)).toList();
124 return _banner;
125 }
126}
127
128//------------------------------------------------------=---------------------------------------------------------=-----------
129// page
130
131class MyHomeScreen extends StatefulWidget {
132 const MyHomeScreen({Key? key}) : super(key: key);
133
134 @override
135 _MyHomeScreenState createState() => _MyHomeScreenState();
136}
137
138class _MyHomeScreenState extends State<MyHomeScreen> {
139 late BannerBloc? _bannerBloc;
140
141 @override
142 void initState() {
143 super.initState();
144 _bannerBloc = BannerBloc();
145 }
146
147 @override
148 void dispose() {
149 _bannerBloc!.dispose();
150 super.dispose();
151 }
152
153 @override
154 Widget build(BuildContext context) {
155 MyTextStyle _textStyle = MyTextStyle();
156
157 return SafeArea(
158 child: Scaffold(
159 appBar: PreferredSize(
160 preferredSize: Size.fromHeight(kAppBarHeight),
161 child: MyAppBarWidet(textTitle1: "Battle", textTitle2: 'Line'),
162 ),
163 body: RefreshIndicator(
164 onRefresh: () => _bannerBloc!.fetchBannerList(),
165 child: StreamBuilder<ApiResponse<List<GameBannerModel?>>>(
166 stream: _bannerBloc!.bannerListStream,
167 builder: (context, snapshot) {
168 if (snapshot.hasData) {
169 List<GameBannerModel?>? data = snapshot.data!.data;
170 switch (snapshot.data!.status) {
171 case Status.LOADING:
172 return BannerLoadingWidget();
173 case Status.ERROR:
174 return Error(
175 errorMessage: snapshot.data!.message,
176 onRetryPressed: () => _bannerBloc!.fetchBannerList(),
177 );
178 case Status.COMPLETED:
179 return BannerWidget(data: snapshot.data!.data);
180 default:
181 return Container();
182 }
183 }
184 return Container();
185 },
186 ),
187 ),
188 );
189 }
190}
191
192