-
Notifications
You must be signed in to change notification settings - Fork 15
/
attendance_details_page_viewmodel.dart
110 lines (107 loc) · 3.63 KB
/
attendance_details_page_viewmodel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'dart:async';
import 'dart:convert';
import 'package:attendance/AttendanceDatailsPage/attendance_details_page.dart';
import 'package:flutter/material.dart';
import 'package:attendance/backend/attend_model.dart';
import 'package:attendance/backend/session.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/services.dart';
import 'package:attendance/scan_exceptions.dart';
import 'package:firebase_admob/firebase_admob.dart';
/// view model of attendance page
abstract class AttendanceDetailsPageViewModel
extends State<AttendanceDetailsPage> {
/// constructor
AttendanceDetailsPageViewModel() {
FirebaseAdMob
.instance.initialize(appId: 'ca-app-pub-5308838739950508~2647148134');
_myBanner
..load()
..show(
anchorOffset: 60,
anchorType: AnchorType.bottom,
);
_model = AttendModel.selected;
className = _model.className;
session = _model.date;
arriveDate = 'Arrived: ${_model.arriveDate}';
leaveDate = 'Leaved: ${_model.leaveDate}';
if(_model.leaveDate == 'NULL') {
isLeaved = false;
}
}
/// check if session scanned for leaving
bool isLeaved = true;
AttendModel _model;
/// name of the current class
String className;
/// name of the current session
String session;
/// date of arriving
String arriveDate;
/// date of leaving
String leaveDate;
/// result of scanning
String scanResult;
final BannerAd _myBanner = BannerAd(
adUnitId: 'ca-app-pub-5308838739950508/6605562688',
size: AdSize.smartBanner,
listener: (MobileAdEvent event) {
print('BannerAd event is $event');
},
);
/// sacn
Future<void> scan() async {
try {
final String barcode = await BarcodeScanner.scan();
final Session session = Session.fromMap(json.decode(barcode));
// @todo #51 save locally and call api.leaveSession and
// remove firebase admin usage
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
final String uid = user.uid;
final DatabaseReference attendanceRef =
FirebaseDatabase.instance
.reference().child('attendances').child(_model.key);
final DataSnapshot oldModel = await attendanceRef.once();
if(oldModel.value['session'] != session.key) {
throw InvalidSessionException('this is not the same session code');
}
final DateTime now = DateTime.now();
final Map<String, dynamic> map = <String, dynamic>{
'session' : session.key,
'sessionClass' : session.classKey,
'sessionAdmin' : session.adminUID,
'user' : uid,
'arriveTime' : _model.arriveDate,
'leaveTime' : now.toIso8601String()
};
await attendanceRef.set(map);
await FirebaseDatabase.instance
.reference().child(uid).child('attended').push().set(attendanceRef.key);
setState(() {
isLeaved = true;
scanResult = '';
_model.leaveDate = now.toIso8601String();
leaveDate = 'Leaved: ${_model.leaveDate}';
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
scanResult = 'You did not grant the camera permission!';
});
} else {
setState(() => scanResult = 'Unknown error: $e');
}
} on FormatException{
setState(() => scanResult = 'Scan cancelled');
} on InvalidSessionException catch(e){
setState(() {
scanResult = e.cause;
});
} on Exception catch (e) {
setState(() => scanResult = 'Unknown error: $e');
}
}
}