344 lines
9.6 KiB
Dart
344 lines
9.6 KiB
Dart
import 'package:aitrainer_app/model/auth.dart';
|
|
import 'package:aitrainer_app/service/api.dart';
|
|
import 'package:aitrainer_app/service/customer_service.dart';
|
|
import 'package:aitrainer_app/view/customer_modify_page.dart';
|
|
import 'package:aitrainer_app/view/customer_new_page.dart';
|
|
import 'package:aitrainer_app/view/login.dart';
|
|
import 'package:aitrainer_app/view/exercise_new_page.dart';
|
|
import 'package:aitrainer_app/view/exercise_type_modify_page.dart';
|
|
import 'package:aitrainer_app/view/exercise_type_new_page.dart';
|
|
import 'package:aitrainer_app/view/registration.dart';
|
|
import 'package:aitrainer_app/viewmodel/exercise_changing_view_model.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:aitrainer_app/view/customer_list_page.dart';
|
|
import 'package:aitrainer_app/view/exercise_type_list_page.dart';
|
|
import 'package:aitrainer_app/widgets/nav_drawer.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
runApp(
|
|
MultiProvider(
|
|
// Initialize the model in the builder. That way, Provider
|
|
// can own Models's lifecycle, making sure to call `dispose`
|
|
// when not needed anymore.
|
|
providers: [
|
|
ChangeNotifierProvider(create: (context) => ExerciseChangingViewModel(null)),
|
|
],
|
|
child: AitrainerApp(),
|
|
),
|
|
);
|
|
}
|
|
|
|
class AitrainerApp extends StatelessWidget {
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
localizationsDelegates: [
|
|
// ... app-specific localization delegate[s] here
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
],
|
|
supportedLocales: [
|
|
const Locale('en'), // English
|
|
const Locale('hu'), // Hungarian
|
|
// ... other locales the app supports
|
|
],
|
|
routes: {
|
|
'home': (context) => AitrainerHome(),
|
|
'customersPage': (context) => CustomerListPage(),
|
|
'customerNewPage': (context) => CustomerNewPage(),
|
|
'customerModifyPage': (context) => CustomerModifyPage(),
|
|
'exerciseTypeListPage': (context) => ExerciseTypeListPage(),
|
|
'exerciseTypeNewPage': (context) => ExerciseTypeNewPage(),
|
|
'exerciseTypeModifyPage': (context) => ExerciseTypeModifyPage(),
|
|
'exerciseNewPage': (context) => ExerciseNewPage(),
|
|
'login': (context) => LoginPage(),
|
|
'registration': (context) => RegistrationPage(),
|
|
},
|
|
initialRoute: 'home',
|
|
title: 'Aitrainer Demo',
|
|
theme: ThemeData(
|
|
primarySwatch: Colors.green,
|
|
),
|
|
home: AitrainerHome(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class AitrainerHome extends StatefulWidget {
|
|
static final String routeName = 'home';
|
|
|
|
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return new _HomePageState();
|
|
}
|
|
}
|
|
|
|
class _HomePageState extends State<AitrainerHome> {
|
|
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
|
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
|
Auth _auth = Auth();
|
|
SharedPreferences _sharedPreferences;
|
|
var _authToken;
|
|
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchSessionAndNavigate();
|
|
}
|
|
|
|
_fetchSessionAndNavigate() async {
|
|
_sharedPreferences = await _prefs;
|
|
String authToken = Auth.getToken(_sharedPreferences);
|
|
var customerId = _sharedPreferences.getInt(Auth.customerIdKey);
|
|
|
|
if ( _auth.firstLoad ) {
|
|
_fetchToken(_sharedPreferences);
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
Auth flow of the user, see auth.dart
|
|
*/
|
|
_fetchToken(SharedPreferences prefs) async {
|
|
var responseJson = await APIClient.authenticateUser(
|
|
Auth.username,
|
|
Auth.password
|
|
);
|
|
|
|
if(responseJson['error'] != null) {
|
|
showSnackBar(_scaffoldKey, responseJson['error']);
|
|
print("************** Here big error - no authentication");
|
|
} else if (responseJson['token'] != null) {
|
|
prefs.setString(Auth.authTokenKey, responseJson['token']);
|
|
Auth auth = Auth();
|
|
auth.authToken = responseJson['token'];
|
|
if ( prefs.get(Auth.customerIdKey) == null ) {
|
|
print("************** Registration");
|
|
// registration
|
|
Navigator.of(context).pushNamed('registration');
|
|
prefs.setBool(Auth.isRegisteredKey, true);
|
|
} else {
|
|
DateTime now = DateTime.now();
|
|
DateTime lastStoreDate = DateTime.parse( prefs.get(Auth.lastStoreDateKey) );
|
|
DateTime minStoreDate = now.add(Duration(days: -10));
|
|
|
|
if ( lastStoreDate == null ||
|
|
lastStoreDate.difference(minStoreDate) > Duration(days: 10) ||
|
|
prefs.get(Auth.isLoggedInKey) == null ||
|
|
prefs.get(Auth.isLoggedInKey) == false ) {
|
|
print("************* Login");
|
|
Navigator.of(context).pushNamed('login');
|
|
|
|
} else {
|
|
print("************** Store SharedPreferences");
|
|
// get API customer
|
|
await CustomerApi().getCustomer( prefs.getInt(Auth.customerIdKey));
|
|
|
|
}
|
|
}
|
|
|
|
setState(() {
|
|
_auth.firstLoad = false;
|
|
_authToken = auth.authToken;
|
|
_sharedPreferences.setString(Auth.authTokenKey, _authToken);
|
|
});
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static showSnackBar(GlobalKey<ScaffoldState> scaffoldKey, String message) {
|
|
scaffoldKey.currentState.showSnackBar(
|
|
new SnackBar(
|
|
content: new Text(message ?? 'You are offline'),
|
|
)
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
key: _scaffoldKey,
|
|
drawer: NavDrawer(),
|
|
appBar: AppBar(
|
|
title: Text('Home'),
|
|
backgroundColor: Colors.transparent
|
|
),
|
|
body: Image.asset('asset/WT01_loading_layers.png',
|
|
fit: BoxFit.fill,
|
|
height: double.infinity,
|
|
width: double.infinity,
|
|
alignment: Alignment.center,
|
|
),
|
|
|
|
|
|
);
|
|
}
|
|
}
|
|
|
|
/*
|
|
class CustomerScreen extends StatelessWidget {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
drawer: NavDrawer(),
|
|
appBar: AppBar(
|
|
title: Text('Customers'),
|
|
),
|
|
body: Center(
|
|
child: CustomerListPage(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
*/
|
|
|
|
/* class AitrainerApp extends StatelessWidget {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: "AITRAINER customers",
|
|
home:
|
|
ChangeNotifierProvider(
|
|
create: (context) => CustomerListViewModel(),
|
|
child: CustomerListPage(),
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
// #docregion MyApp
|
|
class MyApp extends StatelessWidget {
|
|
// #docregion build
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Startup Name Generator',
|
|
theme: ThemeData( // Add the 3 lines from here...
|
|
primaryColor: Colors.white,
|
|
),
|
|
home: RandomWords(),
|
|
);
|
|
}
|
|
// #enddocregion build
|
|
}
|
|
// #enddocregion MyApp
|
|
|
|
// #docregion RWS-var
|
|
class RandomWordsState extends State<RandomWords> {
|
|
final _suggestions = <WordPair>[];
|
|
final _biggerFont = const TextStyle(fontSize: 18.0);
|
|
final Set<WordPair> _saved = Set<WordPair>();
|
|
|
|
// #enddocregion RWS-var
|
|
|
|
// #docregion _buildSuggestions
|
|
Widget _buildSuggestions() {
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(16.0),
|
|
itemBuilder: /*1*/ (context, i) {
|
|
if (i.isOdd) return Divider(); /*2*/
|
|
|
|
final index = i ~/ 2; /*3*/
|
|
if (index >= _suggestions.length) {
|
|
_suggestions.addAll(generateWordPairs().take(10)); /*4*/
|
|
}
|
|
return _buildRow(_suggestions[index]);
|
|
});
|
|
}
|
|
// #enddocregion _buildSuggestions
|
|
|
|
// #docregion _buildRow
|
|
Widget _buildRow(WordPair pair) {
|
|
final bool alreadySaved = _saved.contains(pair);
|
|
return ListTile(
|
|
title: Text(
|
|
pair.asPascalCase,
|
|
style: _biggerFont,
|
|
),
|
|
trailing: Icon( // Add the lines from here...
|
|
alreadySaved ? Icons.favorite : Icons.favorite_border,
|
|
color: alreadySaved ? Colors.red : null,
|
|
),
|
|
onTap: () { // Add 9 lines from here...
|
|
setState(() {
|
|
if (alreadySaved) {
|
|
_saved.remove(pair);
|
|
} else {
|
|
_saved.add(pair);
|
|
}
|
|
});
|
|
},
|
|
);
|
|
}
|
|
// #enddocregion _buildRow
|
|
|
|
// #docregion RWS-build
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('Startup Name Generator'),
|
|
actions: <Widget>[ // Add 3 lines from here...
|
|
IconButton(icon: Icon(Icons.list), onPressed: _pushSaved),
|
|
],
|
|
),
|
|
body: _buildSuggestions(),
|
|
);
|
|
}
|
|
|
|
void _pushSaved() {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>( // Add 20 lines from here...
|
|
builder: (BuildContext context) {
|
|
final Iterable<ListTile> tiles = _saved.map(
|
|
(WordPair pair) {
|
|
return ListTile(
|
|
title: Text(
|
|
pair.asPascalCase,
|
|
style: _biggerFont,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
final List<Widget> divided = ListTile
|
|
.divideTiles(
|
|
context: context,
|
|
tiles: tiles,
|
|
).toList();
|
|
return Scaffold( // Add 6 lines from here...
|
|
appBar: AppBar(
|
|
title: Text('Saved Suggestions'),
|
|
),
|
|
body: ListView(children: divided),
|
|
);
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
}
|
|
// #enddocregion RWS-build
|
|
// #docregion RWS-var
|
|
}
|
|
// #enddocregion RWS-var
|
|
|
|
class RandomWords extends StatefulWidget {
|
|
@override
|
|
RandomWordsState createState() => new RandomWordsState();
|
|
}
|
|
|
|
*/
|