From 87388736010e3b4d9a9148db5e5ddbfdd3dceda6 Mon Sep 17 00:00:00 2001
From: iqFareez <60868965+fareezMaple@users.noreply.github.com>
Date: Sun, 20 Sep 2020 23:52:49 +0800
Subject: [PATCH] Works, but need to reformat time
---
.gitignore | 3 +
android/app/src/main/AndroidManifest.xml | 3 +-
lib/blocs/azan_times_today_bloc.dart | 17 +--
lib/blocs/waktusolatapp_bloc.dart | 41 ++++++
lib/main.dart | 2 +-
lib/models/azanproapi.dart | 7 +-
lib/models/waktusolatappapi.dart | 132 ++++++++++++++++++
lib/networking/ApiProvider.dart | 4 +-
.../azan_times_today_repository.dart | 12 --
lib/repository/azanpro_repository.dart | 27 ++++
lib/views/GetPrayerTime.dart | 48 ++++---
11 files changed, 249 insertions(+), 47 deletions(-)
create mode 100644 lib/blocs/waktusolatapp_bloc.dart
create mode 100644 lib/models/waktusolatappapi.dart
delete mode 100644 lib/repository/azan_times_today_repository.dart
create mode 100644 lib/repository/azanpro_repository.dart
diff --git a/.gitignore b/.gitignore
index 5ad64dd..566c8e7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,3 +48,6 @@ app.*.map.json
# key file
key.properties
+
+#scrath file
+test.dart
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 163c3a6..bc07d22 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -6,7 +6,8 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
-
+
+
> get prayDataSink => _prayDataController.sink;
+ StreamSink> get prayDataSink => _prayDataController.sink;
- Stream> get prayDataStream => _prayDataController.stream;
+ Stream> get prayDataStream => _prayDataController.stream;
- PrayTimeBloc(String category, String format) {
- _prayDataController = StreamController>();
+ AzanproBloc(String category, String format) {
+ _prayDataController = StreamController>();
_prayerTimeRepository = AzanTimesTodayRepository();
_isStreaming = true;
- fetchPrayerTime(category, format);
+ format = format == null ? '' : format;
+ // fetchPrayerTime(category, format);
}
fetchPrayerTime(String category, String format) async {
prayDataSink.add(Response.loading('Getting prayer times'));
try {
- PrayerTime prayerTime =
+ AzanPro prayerTime =
await _prayerTimeRepository.fetchAzanToday(category, format);
prayDataSink.add(Response.completed(prayerTime));
} catch (e) {
diff --git a/lib/blocs/waktusolatapp_bloc.dart b/lib/blocs/waktusolatapp_bloc.dart
new file mode 100644
index 0000000..1d8eb07
--- /dev/null
+++ b/lib/blocs/waktusolatapp_bloc.dart
@@ -0,0 +1,41 @@
+import 'dart:async';
+import 'package:waktusolatmalaysia/models/waktusolatappapi.dart';
+import 'package:waktusolatmalaysia/networking/Response.dart';
+import 'package:waktusolatmalaysia/repository/azanpro_repository.dart';
+
+class WaktusolatappBloc {
+ AzanTimesTodayRepository _prayerTimeRepository;
+ StreamController _prayDataController;
+ bool _isStreaming;
+
+ StreamSink> get prayDataSink =>
+ _prayDataController.sink;
+
+ Stream> get prayDataStream =>
+ _prayDataController.stream;
+
+ WaktusolatappBloc(String category, String format) {
+ _prayDataController = StreamController>();
+ _prayerTimeRepository = AzanTimesTodayRepository();
+ _isStreaming = true;
+ // format = format == null ? '' : format;
+ fetchPrayerTime(category, format);
+ }
+
+ fetchPrayerTime(String category, String format) async {
+ prayDataSink.add(Response.loading('Getting prayer times'));
+ try {
+ WaktuSolatApp prayerTime =
+ await _prayerTimeRepository.fetchAzanTodayWSA(category, format);
+ prayDataSink.add(Response.completed(prayerTime));
+ } catch (e) {
+ prayDataSink.add(Response.error(e.toString()));
+ print('Error caught: ' + e.toString());
+ }
+ }
+
+ dispose() {
+ _isStreaming = false;
+ _prayDataController?.close();
+ }
+}
diff --git a/lib/main.dart b/lib/main.dart
index 8a7d959..2889a48 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
- title: 'Malaysia Prayer Time 2020',
+ title: 'My Prayer Time',
theme: ThemeData.light().copyWith(
primaryColor: Colors.teal,
visualDensity: VisualDensity.adaptivePlatformDensity,
diff --git a/lib/models/azanproapi.dart b/lib/models/azanproapi.dart
index 371c958..c4cd394 100644
--- a/lib/models/azanproapi.dart
+++ b/lib/models/azanproapi.dart
@@ -1,14 +1,13 @@
-class PrayerTime {
+class AzanPro {
String zone;
String start;
String end;
List locations;
PrayerTimes prayerTimes;
- PrayerTime(
- {this.zone, this.start, this.end, this.locations, this.prayerTimes});
+ AzanPro({this.zone, this.start, this.end, this.locations, this.prayerTimes});
- PrayerTime.fromJson(Map json) {
+ AzanPro.fromJson(Map json) {
zone = json['zone'];
start = json['start'];
end = json['end'];
diff --git a/lib/models/waktusolatappapi.dart b/lib/models/waktusolatappapi.dart
new file mode 100644
index 0000000..6005027
--- /dev/null
+++ b/lib/models/waktusolatappapi.dart
@@ -0,0 +1,132 @@
+class WaktuSolatApp {
+ bool success;
+ Data data;
+
+ WaktuSolatApp({this.success, this.data});
+
+ WaktuSolatApp.fromJson(Map json) {
+ success = json['success'];
+ data = json['data'] != null ? new Data.fromJson(json['data']) : null;
+ }
+
+ Map toJson() {
+ final Map data = new Map();
+ data['success'] = this.success;
+ if (this.data != null) {
+ data['data'] = this.data.toJson();
+ }
+ return data;
+ }
+}
+
+class Data {
+ Zone zone;
+ int month;
+ int year;
+ List prayTimes;
+
+ Data({this.zone, this.month, this.year, this.prayTimes});
+
+ Data.fromJson(Map json) {
+ zone = json['zone'] != null ? new Zone.fromJson(json['zone']) : null;
+ month = json['month'];
+ year = json['year'];
+ if (json['pray_times'] != null) {
+ prayTimes = new List();
+ json['pray_times'].forEach((v) {
+ prayTimes.add(new PrayTimes.fromJson(v));
+ });
+ }
+ }
+
+ Map toJson() {
+ final Map data = new Map();
+ if (this.zone != null) {
+ data['zone'] = this.zone.toJson();
+ }
+ data['month'] = this.month;
+ data['year'] = this.year;
+ if (this.prayTimes != null) {
+ data['pray_times'] = this.prayTimes.map((v) => v.toJson()).toList();
+ }
+ return data;
+ }
+}
+
+class Zone {
+ String code;
+ String location;
+ String state;
+ String country;
+
+ Zone({this.code, this.location, this.state, this.country});
+
+ Zone.fromJson(Map json) {
+ code = json['code'];
+ location = json['location'];
+ state = json['state'];
+ country = json['country'];
+ }
+
+ Map toJson() {
+ final Map data = new Map();
+ data['code'] = this.code;
+ data['location'] = this.location;
+ data['state'] = this.state;
+ data['country'] = this.country;
+ return data;
+ }
+}
+
+class PrayTimes {
+ String hijriDate;
+ String date;
+ int subuh;
+ int imsak;
+ int dhuha;
+ int syuruk;
+ int maghrib;
+ int isyak;
+ int zohor;
+ int asar;
+
+ PrayTimes(
+ {this.hijriDate,
+ this.date,
+ this.subuh,
+ this.imsak,
+ this.dhuha,
+ this.syuruk,
+ this.maghrib,
+ this.isyak,
+ this.zohor,
+ this.asar});
+
+ PrayTimes.fromJson(Map json) {
+ hijriDate = json['hijri_date'];
+ date = json['date'];
+ subuh = json['subuh'];
+ imsak = json['imsak'];
+ dhuha = json['dhuha'];
+ syuruk = json['syuruk'];
+ maghrib = json['maghrib'];
+ isyak = json['isyak'];
+ zohor = json['zohor'];
+ asar = json['asar'];
+ }
+
+ Map toJson() {
+ final Map data = new Map();
+ data['hijri_date'] = this.hijriDate;
+ data['date'] = this.date;
+ data['subuh'] = this.subuh;
+ data['imsak'] = this.imsak;
+ data['dhuha'] = this.dhuha;
+ data['syuruk'] = this.syuruk;
+ data['maghrib'] = this.maghrib;
+ data['isyak'] = this.isyak;
+ data['zohor'] = this.zohor;
+ data['asar'] = this.asar;
+ return data;
+ }
+}
diff --git a/lib/networking/ApiProvider.dart b/lib/networking/ApiProvider.dart
index 2924f28..f8b383a 100644
--- a/lib/networking/ApiProvider.dart
+++ b/lib/networking/ApiProvider.dart
@@ -5,7 +5,7 @@ import 'CustomException.dart';
import 'package:http/http.dart' as http;
class ApiProvider {
- final String _baseUrl = "https://api.azanpro.com/";
+ final String _baseUrl = "http://waktusolatapp.com/api/v2/waktu-solat";
Future get(String url) async {
var responseJson;
@@ -13,7 +13,7 @@ class ApiProvider {
final response = await http.get(_baseUrl + url);
responseJson = _response(response);
} on SocketException {
- throw FetchDataException('No internet connection');
+ throw FetchDataException('No Internet connection');
}
print(_baseUrl + url);
return responseJson;
diff --git a/lib/repository/azan_times_today_repository.dart b/lib/repository/azan_times_today_repository.dart
deleted file mode 100644
index e4a8089..0000000
--- a/lib/repository/azan_times_today_repository.dart
+++ /dev/null
@@ -1,12 +0,0 @@
-import 'package:waktusolatmalaysia/models/azanproapi.dart';
-import 'package:waktusolatmalaysia/networking/ApiProvider.dart';
-
-class AzanTimesTodayRepository {
- ApiProvider _provider = ApiProvider();
-
- Future fetchAzanToday(String category, String format) async {
- final response =
- await _provider.get("times/today.json?zone=" + category + format);
- return PrayerTime.fromJson(response);
- }
-}
diff --git a/lib/repository/azanpro_repository.dart b/lib/repository/azanpro_repository.dart
new file mode 100644
index 0000000..4ea6cda
--- /dev/null
+++ b/lib/repository/azanpro_repository.dart
@@ -0,0 +1,27 @@
+import 'package:intl/intl.dart';
+import 'package:waktusolatmalaysia/models/azanproapi.dart';
+import 'package:waktusolatmalaysia/models/waktusolatappapi.dart';
+import 'package:waktusolatmalaysia/networking/ApiProvider.dart';
+
+var now = DateTime.now();
+var currentMonthFormatter = DateFormat('MM');
+var currentYearFormatter = DateFormat('y');
+
+class AzanTimesTodayRepository {
+ String currentMonth = currentMonthFormatter.format(now);
+ String currentYear = currentYearFormatter.format(now);
+ ApiProvider _provider = ApiProvider();
+
+ Future fetchAzanToday(String category, String format) async {
+ final response =
+ await _provider.get("times/today.json?zone=" + category + format);
+ return AzanPro.fromJson(response);
+ }
+
+ Future fetchAzanTodayWSA(
+ String category, String format) async {
+ final response = await _provider
+ .get("?month=$currentMonth&year=$currentYear&zone=$category");
+ return WaktuSolatApp.fromJson(response);
+ }
+}
diff --git a/lib/views/GetPrayerTime.dart b/lib/views/GetPrayerTime.dart
index 7fd0b4c..6ef29c5 100644
--- a/lib/views/GetPrayerTime.dart
+++ b/lib/views/GetPrayerTime.dart
@@ -5,7 +5,9 @@ import 'package:get_storage/get_storage.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:waktusolatmalaysia/CONSTANTS.dart';
import 'package:waktusolatmalaysia/blocs/azan_times_today_bloc.dart';
+import 'package:waktusolatmalaysia/blocs/waktusolatapp_bloc.dart';
import 'package:waktusolatmalaysia/models/azanproapi.dart';
+import 'package:waktusolatmalaysia/models/waktusolatappapi.dart';
import 'package:waktusolatmalaysia/utils/cachedPrayerData.dart';
import 'package:waktusolatmalaysia/utils/sizeconfig.dart';
@@ -19,7 +21,7 @@ class GetPrayerTime extends StatefulWidget {
}
class _GetPrayerTimeState extends State {
- PrayTimeBloc _timeBloc;
+ WaktusolatappBloc _timeBloc;
String timeFormat = "&format=12-hour";
@@ -27,15 +29,16 @@ class _GetPrayerTimeState extends State {
void initState() {
super.initState();
initializeDateFormatting('en_US', null);
- _timeBloc = PrayTimeBloc(location, timeFormat);
+ _timeBloc = WaktusolatappBloc(location, null);
}
@override
Widget build(BuildContext context) {
- return StreamBuilder>(
+ return StreamBuilder>(
stream: _timeBloc.prayDataStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
+ print('snapshot.hasData');
switch (snapshot.data.status) {
case Status.LOADING:
return Loading(loadingMessage: snapshot.data.message);
@@ -50,13 +53,14 @@ class _GetPrayerTimeState extends State {
case Status.ERROR:
return Error(
errorMessage: snapshot.data.message,
- onRetryPressed: () =>
- _timeBloc.fetchPrayerTime(location, timeFormat),
+ onRetryPressed: () => _timeBloc.fetchPrayerTime(location, null),
);
break;
}
}
- return Container();
+ return Container(
+ child: Text('empty here'),
+ );
},
);
}
@@ -69,40 +73,45 @@ class _GetPrayerTimeState extends State {
}
class PrayTimeList extends StatelessWidget {
- final PrayerTime prayerTime;
+ // final AzanPro prayerTime;
+ final WaktuSolatApp prayerTime;
const PrayTimeList({Key key, this.prayerTime}) : super(key: key);
@override
Widget build(BuildContext context) {
- String subuhTime = prayerTime.prayerTimes.subuh;
- String zohorTime = prayerTime.prayerTimes.zohor;
- String asarTime = prayerTime.prayerTimes.asar;
- String maghribTime = prayerTime.prayerTimes.maghrib;
- String isyaTime = prayerTime.prayerTimes.isyak;
-
- CachedPrayerTimeData.subuhTime = subuhTime;
- CachedPrayerTimeData.zohorTime = zohorTime;
- CachedPrayerTimeData.asarTime = asarTime;
- CachedPrayerTimeData.maghribTime = maghribTime;
- CachedPrayerTimeData.isyaTime = isyaTime;
+ String subuhTime = prayerTime.data.prayTimes[1].subuh.toString();
+ String zohorTime = prayerTime.data.prayTimes[1].zohor.toString();
+ String asarTime = prayerTime.data.prayTimes[1].asar.toString();
+ String maghribTime = prayerTime.data.prayTimes[1].maghrib.toString();
+ String isyaTime = prayerTime.data.prayTimes[1].isyak.toString();
+
+ // String subuhTime = prayerTime.success.toString();
+
+ // CachedPrayerTimeData.subuhTime = subuhTime;
+ // CachedPrayerTimeData.zohorTime = zohorTime;
+ // CachedPrayerTimeData.asarTime = asarTime;
+ // CachedPrayerTimeData.maghribTime = maghribTime;
+ // CachedPrayerTimeData.isyaTime = isyaTime;
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
+ // solatCard('$subuhTime', 'Status'),
+
solatCard(subuhTime, 'Subuh'),
solatCard(zohorTime, 'Zohor'),
solatCard(asarTime, 'Asr'),
solatCard(maghribTime, 'Maghrib'),
solatCard(isyaTime, 'Isyak'),
+
// RaisedButton(
// child: Text('DEBUG BUTTON'),
// color: Colors.red,
// onPressed: () {
// print('location is ' + location);
- // RestartWidget.restartApp(context);
// },
// )
],
@@ -160,6 +169,7 @@ class Error extends StatelessWidget {
@override
Widget build(BuildContext context) {
+ print(errorMessage);
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,