|
import 'dart:async'; |
|
import 'dart:io'; |
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart'; |
|
import 'package:firebase_crashlytics/firebase_crashlytics.dart'; |
|
import 'package:flutter/cupertino.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:nft/services/app/app_loading.dart'; |
|
import 'package:nft/services/app/dynamic_size.dart'; |
|
import 'package:nft/services/firebase/cloud_firestore.dart'; |
|
import 'package:nft/services/firebase/cloud_functions.dart'; |
|
import 'package:nft/services/firebase/cloud_storage.dart'; |
|
import 'package:nft/services/firebase/firebase_error.dart'; |
|
import 'package:nft/services/firebase/firebase_error_type.dart'; |
|
import 'package:nft/services/firebase/firebase_login.dart'; |
|
import 'package:nft/services/firebase/local_push.dart'; |
|
import 'package:nft/utils/app_helper.dart'; |
|
import 'package:nft/utils/app_log.dart'; |
|
import 'package:nft/utils/app_style.dart'; |
|
import 'package:nft/widgets/p_appbar_empty.dart'; |
|
import 'package:nft/widgets/w_dismiss_keyboard.dart'; |
|
import 'package:image_picker/image_picker.dart'; |
|
import 'package:provider/provider.dart'; |
|
|
|
class DemoPage extends StatefulWidget { |
|
@override |
|
_DemoPageState createState() => _DemoPageState(); |
|
} |
|
|
|
class _DemoPageState extends State<DemoPage> |
|
with WidgetsBindingObserver, DynamicSize, FirebaseError { |
|
CloudFireStoreSnapshot _cloudFireStoreSnapshot; |
|
StreamSubscription<DocumentSnapshot> snapshotSubscription; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
print('page1_initState'); |
|
WidgetsBinding.instance.addObserver(this); |
|
WidgetsBinding.instance.addPostFrameCallback((_) { |
|
if (_cloudFireStoreSnapshot != null) { |
|
snapshotSubscription = |
|
CloudFireStore.I.listenUser(_cloudFireStoreSnapshot); |
|
} |
|
}); |
|
} |
|
|
|
@override |
|
void didChangeDependencies() { |
|
super.didChangeDependencies(); |
|
print('page1_didChangeDependencies'); |
|
} |
|
|
|
@override |
|
void didUpdateWidget(covariant DemoPage oldWidget) { |
|
super.didUpdateWidget(oldWidget); |
|
print('page1_didUpdateWidget'); |
|
} |
|
|
|
@override |
|
void deactivate() { |
|
print('page1_deactivate'); |
|
super.deactivate(); |
|
} |
|
|
|
@override |
|
void dispose() { |
|
print('page1_dispose'); |
|
snapshotSubscription?.cancel(); |
|
WidgetsBinding.instance.removeObserver(this); |
|
super.dispose(); |
|
} |
|
|
|
@override |
|
void didChangeAppLifecycleState(AppLifecycleState state) { |
|
super.didChangeAppLifecycleState(state); |
|
print('page1_didChangeAppLifecycleState: $state'); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
initDynamicSize(context); |
|
return PAppBarEmpty( |
|
child: WDismissKeyboard( |
|
child: Container( |
|
width: double.infinity, |
|
child: Column( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
mainAxisSize: MainAxisSize.max, |
|
children: <Widget>[ |
|
FlatButton( |
|
onPressed: () { |
|
FirebaseLogin.I.signInGoogle(); |
|
}, |
|
child: const Text( |
|
'Google Sign in', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
FirebaseLogin.I.signInFacebook(); |
|
}, |
|
child: const Text( |
|
'Facebook Sign in', |
|
), |
|
), |
|
FutureBuilder<bool>( |
|
future: FirebaseLogin.I.isAppleSignInAvailable(), |
|
builder: (_, AsyncSnapshot<bool> isAvailable) { |
|
return isAvailable.data == true |
|
? FlatButton( |
|
onPressed: () { |
|
FirebaseLogin.I.signInApple(); |
|
}, |
|
child: const Text( |
|
'Facebook Apple', |
|
), |
|
) |
|
: Container(); |
|
}, |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
LocalPush.I.schedule( |
|
id: 0, |
|
message: 'local push', |
|
notifyAt: |
|
DateTime.now().add(const Duration(seconds: 3)), |
|
payload: NotyPayload(0, data: 'default')); |
|
}, |
|
child: const Text( |
|
'Local push', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety( |
|
CloudFunctions.I.getHelloWorld, |
|
onStart: () async { |
|
print('Start call function'); |
|
}, |
|
onCompleted: (bool status, void _) async { |
|
print('Call function status: $status'); |
|
}, |
|
onError: (dynamic error) async { |
|
print(error); |
|
}, |
|
); |
|
}, |
|
child: const Text( |
|
'Hello world', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
// Forcing a crash |
|
FirebaseCrashlytics.instance.crash(); |
|
}, |
|
child: const Text('Crash'), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety(CloudFireStore.I.addUserWithId, |
|
onCompleted: (bool status, void _) async { |
|
print('User added with status: $status'); |
|
}); |
|
}, |
|
child: const Text( |
|
'Add User', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety(CloudFireStore.I.updateUser, |
|
onCompleted: (bool status, void _) async { |
|
print('User updated with status: $status'); |
|
}); |
|
}, |
|
child: const Text( |
|
'Update User', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety(CloudFireStore.I.removeExtraField, |
|
onCompleted: (bool status, void _) async { |
|
print('User remove extra field with status: $status'); |
|
}); |
|
}, |
|
child: const Text( |
|
'Remove extra field', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety(CloudFireStore.I.deleteUser, |
|
onCompleted: (bool status, void _) async { |
|
print('User deleted with status: $status'); |
|
}); |
|
}, |
|
child: const Text( |
|
'Delete user', |
|
), |
|
), |
|
FlatButton( |
|
onPressed: () { |
|
firebaseCallSafety(() => CloudFireStore.I.readUser(), |
|
onCompleted: (bool status, String value) async { |
|
print('User read with status: $status, value: $value'); |
|
if (status == true) { |
|
AppHelper.showToast(value); |
|
} |
|
}); |
|
}, |
|
child: const Text( |
|
'Read user info', |
|
), |
|
), |
|
ChangeNotifierProvider<CloudFireStoreSnapshot>( |
|
create: (_) => CloudFireStoreSnapshot(), |
|
builder: (BuildContext context, _) { |
|
_cloudFireStoreSnapshot = |
|
Provider.of<CloudFireStoreSnapshot>(context, |
|
listen: false); |
|
return Selector<CloudFireStoreSnapshot, DocumentSnapshot>( |
|
selector: (_, CloudFireStoreSnapshot p) => p.userSnapshot, |
|
builder: (_, DocumentSnapshot snapshot, __) { |
|
print('update changes page1'); |
|
return Text(snapshot?.exists == true |
|
? snapshot.data()['full_name'] as String |
|
: 'null'); |
|
}, |
|
); |
|
}, |
|
), |
|
IconButton( |
|
icon: const Icon(Icons.photo_library), |
|
onPressed: () { |
|
_selectImageSource(context); |
|
}, |
|
), |
|
], |
|
)), |
|
), |
|
); |
|
} |
|
|
|
Future<void> _selectImageSource(BuildContext context) async { |
|
// Close keyboard |
|
FocusScope.of(context).requestFocus(FocusNode()); |
|
|
|
// Define max size of image |
|
const double maxWidth = 350; |
|
const double maxHeight = 500; |
|
|
|
// Show choose image source |
|
final List<String> source = <String>['Camera', 'Gallery']; |
|
final List<CupertinoActionSheetAction> actions = source |
|
.map((String it) => CupertinoActionSheetAction( |
|
isDefaultAction: true, |
|
onPressed: () { |
|
// pop value |
|
Navigator.pop<int>(context, source.indexOf(it)); |
|
}, |
|
child: Text( |
|
it, |
|
style: boldTextStyle(14, Colors.black), |
|
), |
|
)) |
|
.toList(growable: false); |
|
|
|
final int index = await showCupertinoModalPopup<int>( |
|
context: context, |
|
builder: (BuildContext context) => CupertinoActionSheet( |
|
actions: actions, |
|
cancelButton: CupertinoActionSheetAction( |
|
isDefaultAction: true, |
|
isDestructiveAction: true, |
|
onPressed: () { |
|
// pop null |
|
Navigator.pop(context); |
|
}, |
|
child: Text( |
|
'Close', |
|
style: boldTextStyle(14, Colors.red), |
|
), |
|
), |
|
), |
|
); |
|
|
|
if (index != null) { |
|
final ImagePicker _imagePicker = ImagePicker(); |
|
final PickedFile image = await _imagePicker.getImage( |
|
source: index == 0 ? ImageSource.camera : ImageSource.gallery, |
|
maxWidth: maxWidth, |
|
maxHeight: maxHeight, |
|
); |
|
if (image != null) { |
|
final File file = File(image.path); |
|
|
|
// Upload media image |
|
firebaseCallSafety(() { |
|
return CloudStorage.I.uploadFile(file); |
|
}, onStart: () async { |
|
AppLoadingProvider.show(context); |
|
}, onCompleted: (bool status, void _) async { |
|
AppLoadingProvider.hide(context); |
|
if (status) { |
|
AppHelper.showToast('success'); |
|
} |
|
}); |
|
} |
|
} |
|
} |
|
|
|
@override |
|
Future<void> onFirebaseError(dynamic error) async { |
|
final FirebaseErrorType firebaseErrorType = parseFirebaseErrorType(error); |
|
print(firebaseErrorType); |
|
AppHelper.showToast(firebaseErrorType.message); |
|
} |
|
} |