-
-
Save yaizudamashii/6e0d2f07461268fcfbf2c61c0e13116e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* import略 */ | |
String documentIdFromCurrentDate() => DateTime.now().toIso8601String(); | |
/* | |
...略... | |
*/ | |
class FirestoreService { | |
FirestoreService({@required this.uid}) | |
// 1 | |
: assert(uid != null, 'Cannot create FirestoreDatabase with null uid'); | |
final String uid; | |
final _service = FirebaseFirestore.instance; | |
// 2 | |
static Future<void> createUser(FirAuth.UserCredential userCredential) async { | |
FirAuth.User user = userCredential.user; | |
String email = user.email; | |
String displayName = user.email.split('@')[0]; | |
await FirebaseFirestore.instance.collection('/users/').doc(user.uid).set({ | |
'description': null, | |
'displayName': displayName, | |
'email': email, | |
'imageUrl': null, | |
'numFollowers': 0, | |
'numFollowing': 0 | |
}); | |
} | |
// 3 | |
Stream<User> userStream({ String uid }) { | |
if (uid == null) { | |
uid = this.uid; | |
} | |
return _service.collection('/users/').doc(uid).snapshots().map((snapshot) => User.fromMap(snapshot.id, snapshot.data())); | |
} | |
/* | |
...中略... | |
*/ | |
// 5 | |
Stream<List<Room>> roomsStream() { | |
return _service.collection('rooms').snapshots().map((snapshot) => snapshot.docs.map((doc) => Room.fromMap(doc.data(), doc.id)).toList()); | |
} | |
// 6 | |
Stream<RoomDetailViewModel> roomStream({ AgoraService agoraService, Room room, User currentUser }) { | |
return _service.doc('/rooms/${room.identifier}').snapshots() | |
.map((snapshot) => Room.fromMap(snapshot.data(), snapshot.id)) | |
.map((room) => RoomDetailViewModel(room: room, currentUser: currentUser, database: this, agora: agoraService)); | |
} | |
// 7 | |
Stream<SpeakerTileViewModel> participantStream({ User user }) { | |
return _service.doc('/users/${user.identifier}').snapshots() | |
.map((snapshot) => User.fromMap(snapshot.id, snapshot.data())) | |
.map((speaker) => SpeakerTileViewModel(speaker: speaker)); | |
} | |
/* | |
...略... | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment