Skip to content

Commit

Permalink
List Implementation + Details UI
Browse files Browse the repository at this point in the history
  • Loading branch information
HossamElghamry committed Jun 24, 2019
1 parent cb17e4e commit a58ba0f
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 369 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
String convertToMinutes(String minutes) {
String convertTime(String minutes) {
if (minutes.length == 1) {
return "0" + minutes;
} else {
Expand Down
53 changes: 35 additions & 18 deletions lib/src/global_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,68 @@ import 'package:medicine_reminder/src/models/duration.dart';
import 'package:medicine_reminder/src/models/medicine.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './models/day.dart';

class GlobalBloc {
BehaviorSubject<Day> _selectedDay$;
BehaviorSubject<Day> get selectedDay$ => _selectedDay$.stream;
// BehaviorSubject<Day> _selectedDay$;
// BehaviorSubject<Day> get selectedDay$ => _selectedDay$.stream;

BehaviorSubject<Period> _selectedPeriod$;
BehaviorSubject<Period> get selectedPeriod$ => _selectedPeriod$.stream;

BehaviorSubject<Medicine> _medicineList$;
BehaviorSubject<Medicine> get medicineList$ => _medicineList$;
BehaviorSubject<List<Medicine>> _medicineList$;
BehaviorSubject<List<Medicine>> get medicineList$ => _medicineList$;

GlobalBloc() {
makeMedicineList();
_selectedDay$ = BehaviorSubject<Day>.seeded(Day.Saturday);
// _selectedDay$ = BehaviorSubject<Day>.seeded(Day.Saturday);
_selectedPeriod$ = BehaviorSubject<Period>.seeded(Period.Week);
_medicineList$ = BehaviorSubject<Medicine>.seeded(null);
_medicineList$ = BehaviorSubject<List<Medicine>>.seeded([]);
}

void updateSelectedDay(Day day) {
_selectedDay$.add(day);
}
// void updateSelectedDay(Day day) {
// _selectedDay$.add(day);
// }

void updateSelectedPeriod(Period period) {
_selectedPeriod$.add(period);
}

void updateMedicineList(Medicine newMedicine) {
_medicineList$.add(newMedicine);
Future updateMedicineList(Medicine newMedicine) async {
var blocList = _medicineList$.value;
blocList.add(newMedicine);
_medicineList$.add(blocList);
Map<String, dynamic> tempMap = newMedicine.toJson();
SharedPreferences sharedUser = await SharedPreferences.getInstance();
String newMedicineJson = jsonEncode(tempMap);
List<String> medicineJsonList = [];
if (sharedUser.getStringList('medicines') == null) {
medicineJsonList.add(newMedicineJson);
} else {
medicineJsonList = sharedUser.getStringList('medicines');
medicineJsonList.add(newMedicineJson);
}
sharedUser.setStringList('medicines', medicineJsonList);
}

Future makeMedicineList() async {
SharedPreferences sharedUser = await SharedPreferences.getInstance();
String stringJSON = sharedUser.getString('medicine');
if (stringJSON == null) {
List<String> jsonList = sharedUser.getStringList('medicines');
List<Medicine> prefList = [];
if (jsonList == null) {
return;
} else {
Map userMap = jsonDecode(stringJSON);
Medicine newList = Medicine.fromJson(userMap);
_medicineList$.add(newList);
for (String jsonMedicine in jsonList) {
Map userMap = jsonDecode(jsonMedicine);
Medicine tempMedicine = Medicine.fromJson(userMap);
prefList.add(tempMedicine);
}
_medicineList$.add(prefList);
}
}

void dispose() {
_selectedDay$.close();
// _selectedDay$.close();
_selectedPeriod$.close();
_medicineList$.close();
}
}
Loading

0 comments on commit a58ba0f

Please sign in to comment.