[ui] Add auto-mount on first launch functionality #52
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:repertory/helpers.dart';
|
||||
import 'package:repertory/models/auth.dart';
|
||||
import 'package:repertory/models/mount.dart';
|
||||
import 'package:repertory/models/mount_list.dart';
|
||||
import 'package:repertory/models/settings.dart';
|
||||
import 'package:repertory/screens/add_mount_screen.dart';
|
||||
import 'package:repertory/screens/auth_screen.dart';
|
||||
import 'package:repertory/screens/edit_mount_screen.dart';
|
||||
@@ -24,6 +25,7 @@ void main() async {
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(create: (_) => auth),
|
||||
ChangeNotifierProvider(create: (_) => Settings(auth)),
|
||||
ChangeNotifierProvider(create: (_) => MountList(auth)),
|
||||
],
|
||||
child: const MyApp(),
|
||||
@@ -64,18 +66,15 @@ class _MyAppState extends State<MyApp> {
|
||||
title: constants.appTitle,
|
||||
initialRoute: '/auth',
|
||||
routes: {
|
||||
'/':
|
||||
(context) =>
|
||||
const AuthCheck(child: HomeScreen(title: constants.appTitle)),
|
||||
'/add':
|
||||
(context) => const AuthCheck(
|
||||
child: AddMountScreen(title: constants.addMountTitle),
|
||||
),
|
||||
'/': (context) =>
|
||||
const AuthCheck(child: HomeScreen(title: constants.appTitle)),
|
||||
'/add': (context) => const AuthCheck(
|
||||
child: AddMountScreen(title: constants.addMountTitle),
|
||||
),
|
||||
'/auth': (context) => const AuthScreen(title: constants.appTitle),
|
||||
'/settings':
|
||||
(context) => const AuthCheck(
|
||||
child: EditSettingsScreen(title: constants.appSettingsTitle),
|
||||
),
|
||||
'/settings': (context) => const AuthCheck(
|
||||
child: EditSettingsScreen(title: constants.appSettingsTitle),
|
||||
),
|
||||
},
|
||||
onGenerateRoute: (settings) {
|
||||
if (settings.name != '/edit') {
|
||||
|
81
web/repertory/lib/models/settings.dart
Normal file
81
web/repertory/lib/models/settings.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:repertory/helpers.dart';
|
||||
import 'package:repertory/models/auth.dart';
|
||||
|
||||
class Settings with ChangeNotifier {
|
||||
final Auth _auth;
|
||||
bool _autoStart = false;
|
||||
|
||||
Settings(this._auth) {
|
||||
_auth.addListener(() {
|
||||
if (_auth.authenticated) {
|
||||
_fetch();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool get autoStart => _autoStart;
|
||||
|
||||
void _reset() {
|
||||
_autoStart = false;
|
||||
}
|
||||
|
||||
Future<void> setAutoStart(bool value) async {
|
||||
try {
|
||||
final auth = await _auth.createAuth();
|
||||
final response = await http.put(
|
||||
Uri.parse(
|
||||
Uri.encodeFull(
|
||||
'${getBaseUri()}/api/v1/setting?auth=$auth&name=AutoStart&value=$value',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 401) {
|
||||
_auth.logoff();
|
||||
_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
_autoStart = value;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
debugPrint('$e');
|
||||
_reset();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _fetch() async {
|
||||
try {
|
||||
final auth = await _auth.createAuth();
|
||||
final response = await http.get(
|
||||
Uri.parse(Uri.encodeFull('${getBaseUri()}/api/v1/settings?auth=$auth')),
|
||||
);
|
||||
|
||||
if (response.statusCode == 401) {
|
||||
_auth.logoff();
|
||||
_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
_autoStart = jsonDecode(response.body)["AutoStart"] as bool;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
debugPrint('$e');
|
||||
_reset();
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:repertory/constants.dart' as constants;
|
||||
import 'package:repertory/models/auth.dart';
|
||||
import 'package:repertory/models/settings.dart';
|
||||
import 'package:repertory/widgets/mount_list_widget.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
@@ -24,6 +25,18 @@ class _HomeScreeState extends State<HomeScreen> {
|
||||
),
|
||||
title: Text(widget.title),
|
||||
actions: [
|
||||
const Text("Auto-start"),
|
||||
Consumer<Settings>(
|
||||
builder: (context, settings, _) {
|
||||
return IconButton(
|
||||
icon: Icon(
|
||||
settings.autoStart ? Icons.toggle_on : Icons.toggle_off,
|
||||
),
|
||||
onPressed: () => settings.setAutoStart(!settings.autoStart),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(width: constants.padding),
|
||||
Consumer<Auth>(
|
||||
builder: (context, auth, _) {
|
||||
return IconButton(
|
||||
|
Reference in New Issue
Block a user