Last active
December 20, 2018 06:48
-
-
Save axross/87bebccb101d2a4d9aa31148d09850ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'package:barcode_scan/barcode_scan.dart'; | |
import '../entity/friend_code.dart'; | |
import '../entity/scaned_friend_code.dart'; | |
class FriendCodeScanService { | |
Future<FriendCode> scan() async { | |
String data; | |
try { | |
data = await BarcodeScanner.scan(); | |
} catch (error) { | |
throw ScanCancelled(); | |
} | |
return ScanedFriendCode.parseScanedData(data); | |
} | |
} | |
class ScanCancelled implements Exception { | |
ScanCancelled(); | |
String toString() => 'ScanCancelled: scaning barcode has been cancelled.'; | |
} |
This file contains hidden or 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 'dart:async'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:meta/meta.dart'; | |
import '../entity/friend_code.dart'; | |
import '../entity/user.dart'; | |
import '../entity/database_user.dart'; | |
import '../service/friend_code_scan_service.dart'; | |
class NewFriendModel { | |
final Firestore _database; | |
final FriendCodeScanService _friendCodeScanService; | |
final User _user; | |
final StreamController<void> _scanRequest = StreamController(); | |
NewFriendModel( | |
User user, { | |
@required Firestore database, | |
@required FriendCodeScanService friendCodeScanService, | |
}) : _database = database, | |
_friendCodeScanService = friendCodeScanService, | |
_user = user { | |
_scanRequest.stream.listen((_) async { | |
FriendCode friendCode; | |
try { | |
friendCode = await _friendCodeScanService.scan(); | |
} on ScanCancelled catch (__) { | |
return; | |
} | |
final friend = await _getUserByFriendCode(friendCode); | |
await _addUserAsFriend(friend); | |
}); | |
} | |
Sink<void> get scanRequest => _scanRequest.sink; | |
void dispose() { | |
_scanRequest.close(); | |
} | |
Future<User> _getUserByFriendCode(FriendCode friendCode) async { | |
final friendCodeDocumentSnapshot = | |
await _database.document('friendCodes/${friendCode.code}').get(); | |
final DocumentReference userDocumentReference = | |
friendCodeDocumentSnapshot['user']; | |
assert(userDocumentReference is DocumentReference); | |
final userDocumentSnapshot = await userDocumentReference.get(); | |
return DatabaseUser.fromDocumentSnapshot(userDocumentSnapshot); | |
} | |
Future<void> _addUserAsFriend(User someone) async { | |
await _database | |
.document('users/${_user.uid}/friends/${someone.uid}') | |
.setData({ | |
'user': _database.document('users/${someone.uid}'), | |
}); | |
} | |
} |
This file contains hidden or 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 './friend_code.dart'; | |
class ScanedFriendCode extends FriendCode { | |
ScanedFriendCode._(String code) : super(code); | |
ScanedFriendCode factory ScanedFriendCode.parseScanedData(String maybeCode) { | |
if (!regexp.hasMatch(maybeCode)) { | |
throw FriendCodeParsingFailure(maybeCode); | |
} | |
return ScanedFriendCode._(maybeCode); | |
} | |
static final regexp = RegExp(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'); | |
} | |
class FriendCodeParsingFailure implements Exception { | |
String code; | |
FriendCodeParsingFailure(this.code); | |
String toString() => 'FriendCodeParsingFailure: "$code" is not a valid code.'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment