forked from googlearchive/firebase-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.dart
154 lines (130 loc) · 3.99 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import 'dart:html';
import 'package:firebase/firebase.dart' as fb;
import 'package:firebase/firestore.dart';
import 'package:firebase/src/assets/assets.dart';
main() async {
//Use for firebase package development only
await config();
try {
fb.initializeApp(
apiKey: apiKey,
authDomain: authDomain,
databaseURL: databaseUrl,
storageBucket: storageBucket,
projectId: projectId);
MessagesApp().showMessages();
} on fb.FirebaseJsNotLoadedException catch (e) {
print(e);
}
}
class MessagesApp {
final CollectionReference ref;
final UListElement messages;
final InputElement newMessage;
final InputElement submit;
final FormElement newMessageForm;
final ButtonElement enableNetwork;
final ButtonElement disableNetwork;
MessagesApp()
: ref = fb.firestore().collection("pkg_firestore"),
messages = querySelector("#messages"),
newMessage = querySelector("#new_message"),
submit = querySelector('#submit'),
newMessageForm = querySelector("#new_message_form"),
disableNetwork = querySelector("#disable"),
enableNetwork = querySelector("#enable") {
newMessage.disabled = false;
submit.disabled = false;
disableNetwork.onClick.listen((_) {
fb.firestore().disableNetwork();
disableNetwork.hidden = true;
enableNetwork.hidden = false;
});
enableNetwork.onClick.listen((_) {
fb.firestore().enableNetwork();
disableNetwork.hidden = false;
enableNetwork.hidden = true;
});
this.newMessageForm.onSubmit.listen((e) async {
e.preventDefault();
if (newMessage.value.trim().isNotEmpty) {
// store also created at for purposes of ordering
var map = {"text": newMessage.value, "createdAt": DateTime.now()};
try {
newMessage.value = "";
await ref.add(map);
} catch (e) {
print("Error while writing document, $e");
}
}
});
}
showMessages() {
this.ref.orderBy("createdAt").onSnapshot.listen((querySnapshot) {
for (var change in querySnapshot.docChanges()) {
var docSnapshot = change.doc;
switch (change.type) {
case "added":
_renderItemView(docSnapshot);
break;
case "removed":
_removeItemView(docSnapshot);
break;
case "modified":
_modifyItemView(docSnapshot);
break;
}
}
});
}
_renderItemView(DocumentSnapshot docSnapshot) {
var spanElement = SpanElement()..text = docSnapshot.data()["text"];
var aElementDelete = AnchorElement(href: "#")
..text = "Delete"
..onClick.listen((e) {
e.preventDefault();
_deleteItem(docSnapshot);
});
var aElementUpdate = AnchorElement(href: "#")
..text = "To Uppercase"
..onClick.listen((e) {
e.preventDefault();
_uppercaseItem(docSnapshot);
});
var element = LIElement()
..id = "item-${docSnapshot.id}"
..append(spanElement)
..append(aElementDelete)
..append(aElementUpdate);
messages.append(element);
}
_removeItemView(DocumentSnapshot docSnapshot) {
var element = querySelector("#item-${docSnapshot.id}");
if (element != null) {
element.remove();
}
}
_modifyItemView(DocumentSnapshot docSnapshot) {
var element = querySelector("#item-${docSnapshot.id} span");
if (element != null) {
element.text = docSnapshot.data()["text"];
}
}
_deleteItem(DocumentSnapshot docSnapshot) async {
try {
await this.ref.doc(docSnapshot.id).delete();
} catch (e) {
print("Error while deleting item, $e");
}
}
_uppercaseItem(DocumentSnapshot docSnapshot) async {
var value = docSnapshot.data();
var valueUppercase = value["text"].toString().toUpperCase();
value["text"] = valueUppercase;
try {
await this.ref.doc(docSnapshot.id).update(data: value);
} catch (e) {
print("Error while updating item, $e");
}
}
}