2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-13 10:45:29 +00:00

UserProfile

This commit is contained in:
Oliver Walters
2021-02-08 20:32:49 +11:00
parent b18daf3acf
commit b34a91b865
10 changed files with 460 additions and 44 deletions

View File

@ -1,18 +1,27 @@
import 'dart:ffi';
import 'package:InvenTree/widget/dialogs.dart';
import 'package:InvenTree/widget/fields.dart';
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../api.dart';
import '../preferences.dart';
import '../user_profile.dart';
class InvenTreeLoginSettingsWidget extends StatefulWidget {
final SharedPreferences _preferences;
InvenTreeLoginSettingsWidget(this._preferences) : super();
final List<UserProfile> _profiles;
InvenTreeLoginSettingsWidget(this._profiles, this._preferences) : super();
@override
_InvenTreeLoginSettingsState createState() => _InvenTreeLoginSettingsState(_preferences);
_InvenTreeLoginSettingsState createState() => _InvenTreeLoginSettingsState(_profiles, _preferences);
}
@ -20,18 +29,61 @@ class _InvenTreeLoginSettingsState extends State<InvenTreeLoginSettingsWidget> {
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
final _addProfileKey = new GlobalKey<FormState>();
final SharedPreferences _preferences;
List<UserProfile> profiles;
String _server = '';
String _username = '';
String _password = '';
_InvenTreeLoginSettingsState(this._preferences) : super() {
_InvenTreeLoginSettingsState(this.profiles, this._preferences) : super() {
_server = _preferences.getString('server') ?? '';
_username = _preferences.getString('username') ?? '';
_password = _preferences.getString('password') ?? '';
}
void _createProfile(BuildContext context) {
showFormDialog(
context,
I18N.of(context).profileAdd,
key: _addProfileKey,
actions: <Widget> [
FlatButton(
child: Text(I18N.of(context).cancel),
onPressed: () {
Navigator.of(context).pop();
}
),
FlatButton(
child: Text(I18N.of(context).save),
onPressed: () {
// TODO
}
)
],
fields: <Widget> [
StringField(
label: I18N.of(context).name,
initial: "profile",
),
StringField(
label: "Server",
initial: "http://127.0.0.1:8000",
),
StringField(
label: "Username",
),
StringField(
label: "Password"
)
]
);
}
String _validateServer(String value) {
@ -71,11 +123,95 @@ class _InvenTreeLoginSettingsState extends State<InvenTreeLoginSettingsWidget> {
}
}
void _deleteProfile(UserProfile profile) async {
await UserProfileDBManager().deleteProfile(profile);
// Reload profiles
profiles = await UserProfileDBManager().getAllProfiles();
setState(() {
});
}
@override
Widget build(BuildContext context) {
final Size screenSize = MediaQuery.of(context).size;
List<Widget> children = [];
for (int idx = 0; idx < profiles.length; idx++) {
UserProfile profile = profiles[idx];
children.add(ListTile(
title: Text(profile.name),
subtitle: Text(profile.server),
trailing: FaIcon(FontAwesomeIcons.checkCircle),
onLongPress: () {
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(profile.name),
children: <Widget> [
SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop();
// TODO - Mark profile as selected
},
child: Text(I18N.of(context).profileSelect),
),
SimpleDialogOption(
onPressed: () {
//Navigator.of(context).pop();
// TODO - Edit profile!
},
child: Text(I18N.of(context).profileEdit),
),
SimpleDialogOption(
onPressed: () {
// Navigator.of(context, rootNavigator: true).pop();
confirmationDialog(
context,
"Delete",
"Delete this profile?",
onAccept: () {
_deleteProfile(profile);
}
);
},
child: Text(I18N.of(context).profileDelete),
)
],
);
}
);
},
onTap: () {
},
));
}
return Scaffold(
appBar: AppBar(
title: Text(I18N.of(context).profile),
),
body: Container(
child: ListView(
children: children,
)
),
floatingActionButton: FloatingActionButton(
child: Icon(FontAwesomeIcons.plus),
onPressed: () {
_createProfile(context);
},
)
);
return Scaffold(
appBar: AppBar(
title: Text("Login Settings"),

View File

@ -1,6 +1,8 @@
import 'package:InvenTree/settings/about.dart';
import 'package:InvenTree/settings/login.dart';
import 'package:InvenTree/settings/release.dart';
import 'package:InvenTree/user_profile.dart';
import 'package:InvenTree/preferences.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -58,6 +60,25 @@ class _InvenTreeSettingsState extends State<InvenTreeSettingsWidget> {
leading: FaIcon(FontAwesomeIcons.bug),
onTap: null,
),
ListTile(
title: Text("Throw Error"),
onTap: () {
throw("My custom error");
},
),
ListTile(
title: Text("add profile"),
onTap: () {
UserProfileDBManager().addProfile(
UserProfile(
name: "My Profile",
server: "https://127.0.0.1:8000",
username: "Oliver",
password: "hunter2",
)
);
},
)
],
)
)
@ -68,7 +89,9 @@ class _InvenTreeSettingsState extends State<InvenTreeSettingsWidget> {
var prefs = await SharedPreferences.getInstance();
Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeLoginSettingsWidget(prefs)));
List<UserProfile> profiles = await UserProfileDBManager().getAllProfiles();
Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeLoginSettingsWidget(profiles, prefs)));
}
void _about() async {