Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade and fix errors #14

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration_test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ void main() {
await tester.pumpAndSettle();
expect(
find.byWidgetPredicate((Widget widget) =>
widget is Text && widget.data.startsWith('Login')),
widget is Text && widget.data!.startsWith('Login')),
findsOneWidget);
final loginFinder = find.byWidgetPredicate((widget) =>
widget is RaisedButton &&
widget.child is Text &&
(widget.child as Text).data.startsWith('Login'));
(widget.child as Text).data!.startsWith('Login'));
expect(loginFinder, findsOneWidget);
await tester.tap(loginFinder);
await tester.pumpAndSettle();
Expand Down
4 changes: 2 additions & 2 deletions lib/core/presentation/res/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AnalyticsScreenNames {

FirebaseAnalytics _getAnalytics(BuildContext context) => Provider.of<FirebaseAnalytics>(context, listen: false);

Future<void> logEvent(BuildContext context, String name, {Map<String,dynamic> params}) {
Future<void> logEvent(BuildContext context, String name, {Map<String,dynamic>? params}) {
return _getAnalytics(context).logEvent(name: name, parameters: params);
}

Future<void> setCurrentScreen(BuildContext context, String name) {
return _getAnalytics(context).setCurrentScreen(screenName: name);
}

Future<void> setUserProperties(BuildContext context, {String id, String name, String email}) async {
Future<void> setUserProperties(BuildContext context, {String? id, String? name, String? email}) async {
await _getAnalytics(context).setUserId(id);
await _getAnalytics(context).setUserProperty(name: "email", value: email);
await _getAnalytics(context).setUserProperty(name: "name", value: name);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/presentation/res/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class AppConfig {
final String appTitle;
final AppFlavor buildFlavor;

AppConfig({@required this.appTitle, @required this.buildFlavor});
AppConfig({required this.appTitle, required this.buildFlavor});
}
3 changes: 2 additions & 1 deletion lib/core/presentation/res/routes.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:firebasestarter/features/profile/data/model/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:firebasestarter/features/auth/presentation/pages/home.dart';
Expand Down Expand Up @@ -25,7 +26,7 @@ class AppRoutes {
case userInfo:
return UserInfoPage();
case editProfile:
return EditProfile(user: settings.arguments,);
return EditProfile(user: settings.arguments as UserModel?,);
case profile:
return UserProfile();
case splash:
Expand Down
2 changes: 1 addition & 1 deletion lib/core/presentation/res/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:firebasestarter/core/presentation/res/colors.dart';
import 'package:firebasestarter/core/presentation/res/sizes.dart';

class AppThemes {
static BuildContext context;
static late BuildContext context;
static final ThemeData defaultTheme = ThemeData(
primaryColor: AppColors.primaryColor,
accentColor: AppColors.accentColor,
Expand Down
91 changes: 54 additions & 37 deletions lib/features/auth/data/model/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
Expand All @@ -18,15 +19,15 @@ enum Status { Uninitialized, Authenticated, Authenticating, Unauthenticated }

class UserRepository with ChangeNotifier {
FirebaseAuth _auth;
User _user;
User? _user;
GoogleSignIn _googleSignIn;
Status _status = Status.Uninitialized;
String _error;
StreamSubscription _userListener;
UserModel _fsUser;
Device currentDevice;
String? _error;
late StreamSubscription _userListener;
UserModel? _fsUser;
Device? currentDevice;
final PushNotificationService pnService;
bool _loading;
bool? _loading;

UserRepository.instance(this.pnService)
: _auth = FirebaseAuth.instance,
Expand All @@ -36,11 +37,11 @@ class UserRepository with ChangeNotifier {
_auth.authStateChanges().listen(_onAuthStateChanged);
}

String get error => _error;
String? get error => _error;
Status get status => _status;
User get fbUser => _user;
UserModel get user => _fsUser;
bool get isLoading => _loading;
User? get fbUser => _user;
UserModel? get user => _fsUser;
bool? get isLoading => _loading;

Future<bool> signIn(String email, String password) async {
try {
Expand All @@ -50,7 +51,7 @@ class UserRepository with ChangeNotifier {
_error = '';
return true;
} catch (e) {
_error = e.message;
_error = e.toString();
_status = Status.Unauthenticated;
notifyListeners();
return false;
Expand All @@ -66,7 +67,7 @@ class UserRepository with ChangeNotifier {
_error = '';
return true;
} catch (e) {
_error = e.message;
_error = e.toString();
_status = Status.Unauthenticated;
notifyListeners();
return false;
Expand All @@ -77,18 +78,29 @@ class UserRepository with ChangeNotifier {
try {
_status = Status.Authenticating;
notifyListeners();
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _auth.signInWithCredential(credential);
if (kIsWeb) {
GoogleAuthProvider googleProvider = GoogleAuthProvider();
await _auth.signInWithPopup(googleProvider);
} else {
final GoogleSignInAccount? googleUser = await (_googleSignIn.signIn());
if (googleUser == null) {
_error = 'Google sign in failed';
_status = Status.Unauthenticated;
notifyListeners();
return false;
}
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
) as GoogleAuthCredential;
await _auth.signInWithCredential(credential);
}
_error = '';
return true;
} catch (e) {
_error = e.message;
_error = e.toString();
_status = Status.Unauthenticated;
notifyListeners();
return false;
Expand All @@ -105,15 +117,15 @@ class UserRepository with ChangeNotifier {
return Future.delayed(Duration.zero);
}

Future<void> _onAuthStateChanged(User firebaseUser) async {
Future<void> _onAuthStateChanged(User? firebaseUser) async {
if (firebaseUser == null) {
_status = Status.Unauthenticated;
_fsUser = null;
_user = null;
} else {
_user = firebaseUser;
_saveUserRecord();
_userListener = userDBS.streamSingle(_user.uid).listen((user) {
_userListener = userDBS.streamSingle(_user!.uid).listen((user) {
_fsUser = user;
_loading = false;
notifyListeners();
Expand All @@ -125,24 +137,29 @@ class UserRepository with ChangeNotifier {

Future<void> _saveUserRecord() async {
if (_user == null) return;
PackageInfo packageInfo = await PackageInfo.fromPlatform();
int buildNumber = int.parse(packageInfo.buildNumber);
int buildNumber;
if (kIsWeb) {
buildNumber = 1;
} else {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
buildNumber = int.parse(packageInfo.buildNumber);
}
UserModel user = UserModel(
email: _user.email,
name: _user.displayName,
photoUrl: _user.photoURL,
id: _user.uid,
email: _user!.email,
name: _user!.displayName,
photoUrl: _user!.photoURL,
id: _user!.uid,
registrationDate: DateTime.now().toUtc(),
lastLoggedIn: DateTime.now().toUtc(),
buildNumber: buildNumber,
introSeen: false,
);
UserModel existing = await userDBS.getSingle(_user.uid);
UserModel? existing = await userDBS.getSingle(_user!.uid);
if (existing == null) {
await userDBS.createItem(user, id: _user.uid);
await userDBS.create(user.toMap(), id: _user!.uid);
_fsUser = user;
} else {
await userDBS.updateData(_user.uid, {
await userDBS.updateData(_user!.uid, {
UserFields.lastLoggedIn: FieldValue.serverTimestamp(),
UserFields.buildNumber: buildNumber,
});
Expand All @@ -152,8 +169,8 @@ class UserRepository with ChangeNotifier {

Future<void> _saveDevice(UserModel user) async {
DeviceInfoPlugin devicePlugin = DeviceInfoPlugin();
String deviceId;
DeviceDetails deviceDescription;
String? deviceId;
DeviceDetails? deviceDescription;
if (Platform.isAndroid) {
AndroidDeviceInfo deviceInfo = await devicePlugin.androidInfo;
deviceId = deviceInfo.androidId;
Expand All @@ -178,14 +195,14 @@ class UserRepository with ChangeNotifier {
int buildNumber = int.parse(packageInfo.buildNumber);
final nowMS = DateTime.now().toUtc().millisecondsSinceEpoch;
if (user.buildNumber != buildNumber) {
userDBS.updateData(user.id, {
userDBS.updateData(user.id!, {
UserFields.buildNumber: buildNumber,
UserFields.lastUpdated: nowMS,
});
}
userDeviceDBS.collection =
"${AppDBConstants.usersCollection}/${user.id}/devices";
Device exsiting = await userDeviceDBS.getSingle(deviceId);
Device? exsiting = await userDeviceDBS.getSingle(deviceId!);
if (exsiting != null) {
var token = exsiting.token ?? await pnService.init();
await userDeviceDBS.updateData(deviceId, {
Expand All @@ -206,7 +223,7 @@ class UserRepository with ChangeNotifier {
lastUpdatedAt: nowMS,
uninstalled: false,
);
await userDeviceDBS.createItem(device, id: deviceId);
await userDeviceDBS.create(device.toMap(), id: deviceId);
currentDevice = device;
}
notifyListeners();
Expand Down
2 changes: 1 addition & 1 deletion lib/features/auth/presentation/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AuthHomePage extends StatelessWidget {
case Status.Authenticated:
setUserProperties(context,id: user.fbUser?.uid, name: user.fbUser?.displayName, email: user.fbUser?.email);
setCurrentScreen(context, AnalyticsScreenNames.userInfo);
if(user.isLoading) return Splash();
if(user.isLoading!) return Splash();
return user.user?.introSeen ?? false ? HomePage() : IntroPage();
case Status.Uninitialized:
default:
Expand Down
24 changes: 12 additions & 12 deletions lib/features/auth/presentation/pages/login.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
import 'package:auth_buttons/auth_buttons.dart';
import 'package:firebasestarter/generated/l10n.dart';
import '../../data/model/user_repository.dart';
import '../widgets/auth_dialog.dart';
Expand All @@ -12,8 +12,8 @@ class WelcomePage extends StatefulWidget {

class _WelcomePageState extends State<WelcomePage> {
final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
bool _authVisible;
int _selectedTab;
late bool _authVisible;
int? _selectedTab;

@override
void initState() {
Expand All @@ -40,14 +40,14 @@ class _WelcomePageState extends State<WelcomePage> {
children: <Widget>[
const SizedBox(height: kToolbarHeight),
Text(
S.of(context).loginPageTitleText,
style: Theme.of(context).textTheme.display2.copyWith(
S.of(context)!.loginPageTitleText,
style: Theme.of(context).textTheme.display2!.copyWith(
color: Colors.white,
fontWeight: FontWeight.w900,
fontFamily: "Frank"),
),
Text(
S.of(context).loginPageSubtitleText,
S.of(context)!.loginPageSubtitleText,
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
const SizedBox(height: 40.0),
Expand All @@ -59,7 +59,7 @@ class _WelcomePageState extends State<WelcomePage> {
child: RaisedButton(
elevation: 0,
highlightElevation: 0,
child: Text(S.of(context).loginButtonText),
child: Text(S.of(context)!.loginButtonText),
onPressed: () => setState(() {
_authVisible = true;
_selectedTab = 0;
Expand All @@ -70,7 +70,7 @@ class _WelcomePageState extends State<WelcomePage> {
Expanded(
child: OutlineButton(
textColor: Colors.white,
child: Text(S.of(context).signupButtonText),
child: Text(S.of(context)!.signupButtonText),
onPressed: () => setState(() {
_authVisible = true;
_selectedTab = 1;
Expand All @@ -82,12 +82,12 @@ class _WelcomePageState extends State<WelcomePage> {
),
),
const SizedBox(height: 50.0),
GoogleSignInButton(
text: S.of(context).googleButtonText,
GoogleAuthButton(
text: S.of(context)!.googleButtonText,
onPressed: () async {
if (!await user.signInWithGoogle())
_key.currentState.showSnackBar(SnackBar(
content: Text("Something is wrong"),
_key.currentState!.showSnackBar(SnackBar(
content: Text(user.error!),
));
},
),
Expand Down
10 changes: 5 additions & 5 deletions lib/features/auth/presentation/pages/user_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import 'package:firebasestarter/generated/l10n.dart';
import '../../data/model/user_repository.dart';

class UserInfoPage extends StatelessWidget {
final User user;
final User? user;

const UserInfoPage({Key key, this.user}) : super(key: key);
const UserInfoPage({Key? key, this.user}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).profilePageTitle),
title: Text(S.of(context)!.profilePageTitle),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(user.email),
Text(user!.email!),
RaisedButton(
child: Text(S.of(context).logoutButtonText),
child: Text(S.of(context)!.logoutButtonText),
onPressed: () {
logEvent(context, AppAnalyticsEvents.logOut);
Provider.of<UserRepository>(context, listen: false).signOut();
Expand Down
Loading