diff --git a/lib/main.dart b/lib/main.dart index dea4837..a354e5c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,15 +6,7 @@ import 'share.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -final AuthModel _auth = AuthModel(); - -void main() async { - WidgetsFlutterBinding.ensureInitialized(); - try { - await _auth.loadSettings(); - } catch (e) { - print("Error Loading Settings: $e"); - } +void main() { runApp(PaperlessShare()); } @@ -45,8 +37,15 @@ MaterialColor createMaterialColor(Color color) { } class _PaperlessShareState extends State { + final AuthModel _auth = AuthModel(); + @override void initState() { + try { + _auth.loadSettings(); + } catch (e) { + print("Error Loading Settings: $e"); + } super.initState(); } @@ -56,36 +55,38 @@ class _PaperlessShareState extends State { providers: [ ChangeNotifierProvider.value(value: _auth), ], - child: Consumer(builder: (context, model, child) { - return MaterialApp( - title: 'Paperless Share', - theme: ThemeData( - primarySwatch: createMaterialColor(Color(0xFF17541f)), - brightness: Brightness.light, - visualDensity: VisualDensity.adaptivePlatformDensity, - ), - darkTheme: ThemeData( - primarySwatch: createMaterialColor(Color(0xFF17541f)), - accentColor: Colors.green, - brightness: Brightness.dark, - visualDensity: VisualDensity.adaptivePlatformDensity, - ), - initialRoute: model?.user.isValid() ? '/share' : '/login', - routes: { - '/login': (context) => new LoginPage(), - '/share': (context) => new SharePage(), - }, - localizationsDelegates: [ - AppLocalizations.delegate, - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], - supportedLocales: [ - const Locale('en', ''), - const Locale('de', ''), - ], - ); - })); + child: MaterialApp( + title: 'Paperless Share', + theme: ThemeData( + primarySwatch: createMaterialColor(Color(0xFF17541f)), + brightness: Brightness.light, + visualDensity: VisualDensity.adaptivePlatformDensity, + ), + darkTheme: ThemeData( + primarySwatch: createMaterialColor(Color(0xFF17541f)), + accentColor: Colors.green, + brightness: Brightness.dark, + visualDensity: VisualDensity.adaptivePlatformDensity, + ), + home: Consumer(builder: (context, model, child) { + if (model?.user != null && model?.user.isValid()) + return SharePage(); + return LoginPage(); + }), + routes: { + '/login': (context) => new LoginPage(), + '/share': (context) => new SharePage(), + }, + localizationsDelegates: [ + AppLocalizations.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: [ + const Locale('en', ''), + const Locale('de', ''), + ], + )); } } diff --git a/lib/model/auth.dart b/lib/model/auth.dart index 025b907..26141bd 100644 --- a/lib/model/auth.dart +++ b/lib/model/auth.dart @@ -40,7 +40,7 @@ class AuthModel extends ChangeNotifier { String errorMessage = ""; User _user; - Future loadSettings() async { + void loadSettings() async { var _prefs = EncryptedSharedPreferences(); User _savedUser; @@ -50,12 +50,10 @@ class AuthModel extends ChangeNotifier { token: await _prefs.getString("saved_token")); } catch (e) { print("User Not Found: $e"); - return false; } _user = _savedUser; notifyListeners(); - return true; } User get user => _user;