Skip to content

Commit

Permalink
Works, but need to reformat time
Browse files Browse the repository at this point in the history
  • Loading branch information
iqfareez committed Sep 20, 2020
1 parent 497cff2 commit 8738873
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ app.*.map.json

# key file
key.properties

#scrath file
test.dart
3 changes: 2 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:name="io.flutter.app.FlutterApplication"
Expand Down
17 changes: 9 additions & 8 deletions lib/blocs/azan_times_today_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import 'dart:async';
import 'package:waktusolatmalaysia/repository/azan_times_today_repository.dart';
import 'package:waktusolatmalaysia/networking/Response.dart';
import 'package:waktusolatmalaysia/models/azanproapi.dart';
import 'package:waktusolatmalaysia/repository/azanpro_repository.dart';

class PrayTimeBloc {
class AzanproBloc {
AzanTimesTodayRepository _prayerTimeRepository;
StreamController _prayDataController;
bool _isStreaming;

StreamSink<Response<PrayerTime>> get prayDataSink => _prayDataController.sink;
StreamSink<Response<AzanPro>> get prayDataSink => _prayDataController.sink;

Stream<Response<PrayerTime>> get prayDataStream => _prayDataController.stream;
Stream<Response<AzanPro>> get prayDataStream => _prayDataController.stream;

PrayTimeBloc(String category, String format) {
_prayDataController = StreamController<Response<PrayerTime>>();
AzanproBloc(String category, String format) {
_prayDataController = StreamController<Response<AzanPro>>();
_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) {
Expand Down
41 changes: 41 additions & 0 deletions lib/blocs/waktusolatapp_bloc.dart
Original file line number Diff line number Diff line change
@@ -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<Response<WaktuSolatApp>> get prayDataSink =>
_prayDataController.sink;

Stream<Response<WaktuSolatApp>> get prayDataStream =>
_prayDataController.stream;

WaktusolatappBloc(String category, String format) {
_prayDataController = StreamController<Response<WaktuSolatApp>>();
_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();
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions lib/models/azanproapi.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class PrayerTime {
class AzanPro {
String zone;
String start;
String end;
List<String> 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<String, dynamic> json) {
AzanPro.fromJson(Map<String, dynamic> json) {
zone = json['zone'];
start = json['start'];
end = json['end'];
Expand Down
132 changes: 132 additions & 0 deletions lib/models/waktusolatappapi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
class WaktuSolatApp {
bool success;
Data data;

WaktuSolatApp({this.success, this.data});

WaktuSolatApp.fromJson(Map<String, dynamic> json) {
success = json['success'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
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> prayTimes;

Data({this.zone, this.month, this.year, this.prayTimes});

Data.fromJson(Map<String, dynamic> 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<PrayTimes>();
json['pray_times'].forEach((v) {
prayTimes.add(new PrayTimes.fromJson(v));
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
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<String, dynamic> json) {
code = json['code'];
location = json['location'];
state = json['state'];
country = json['country'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
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<String, dynamic> 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<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
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;
}
}
4 changes: 2 additions & 2 deletions lib/networking/ApiProvider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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<dynamic> get(String url) async {
var responseJson;
try {
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;
Expand Down
12 changes: 0 additions & 12 deletions lib/repository/azan_times_today_repository.dart

This file was deleted.

27 changes: 27 additions & 0 deletions lib/repository/azanpro_repository.dart
Original file line number Diff line number Diff line change
@@ -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<AzanPro> fetchAzanToday(String category, String format) async {
final response =
await _provider.get("times/today.json?zone=" + category + format);
return AzanPro.fromJson(response);
}

Future<WaktuSolatApp> fetchAzanTodayWSA(
String category, String format) async {
final response = await _provider
.get("?month=$currentMonth&year=$currentYear&zone=$category");
return WaktuSolatApp.fromJson(response);
}
}
Loading

0 comments on commit 8738873

Please sign in to comment.