[ui] Add auto-mount on first launch functionality #52
This commit is contained in:
@@ -477,6 +477,7 @@ inline constexpr auto JSON_API_PARENT{"ApiParent"};
|
||||
inline constexpr auto JSON_API_PASSWORD{"ApiPassword"};
|
||||
inline constexpr auto JSON_API_PATH{"ApiPath"};
|
||||
inline constexpr auto JSON_API_PORT{"ApiPort"};
|
||||
inline constexpr auto JSON_AUTO_START{"AutoStart"};
|
||||
inline constexpr auto JSON_API_USER{"ApiUser"};
|
||||
inline constexpr auto JSON_BUCKET{"Bucket"};
|
||||
inline constexpr auto JSON_CLIENT_POOL_SIZE{"ClientPoolSize"};
|
||||
|
@@ -113,6 +113,9 @@ private:
|
||||
void handle_put_settings(const httplib::Request &req,
|
||||
httplib::Response &res) const;
|
||||
|
||||
void handle_put_setting(const httplib::Request &req,
|
||||
httplib::Response &res) const;
|
||||
|
||||
auto launch_process(provider_type prov, std::string_view name,
|
||||
std::vector<std::string> args,
|
||||
bool background = false) const
|
||||
|
@@ -37,6 +37,7 @@ private:
|
||||
atomic<std::string> api_password_{"repertory"};
|
||||
std::atomic<std::uint16_t> api_port_{default_ui_mgmt_port};
|
||||
atomic<std::string> api_user_{"repertory"};
|
||||
std::atomic<bool> auto_start_{false};
|
||||
std::unordered_map<provider_type,
|
||||
std::unordered_map<std::string, std::string>>
|
||||
locations_;
|
||||
@@ -56,6 +57,8 @@ public:
|
||||
|
||||
[[nodiscard]] auto get_api_user() const -> std::string { return api_user_; }
|
||||
|
||||
[[nodiscard]] auto get_auto_start() const -> bool { return auto_start_; }
|
||||
|
||||
[[nodiscard]] auto get_hidden() const -> bool { return hidden_; }
|
||||
|
||||
[[nodiscard]] auto get_launch_only() const -> bool { return launch_only_; }
|
||||
@@ -70,6 +73,8 @@ public:
|
||||
|
||||
void set_api_user(std::string_view api_user);
|
||||
|
||||
void set_auto_start(bool auto_start);
|
||||
|
||||
void set_hidden(bool hidden);
|
||||
|
||||
void set_launch_only(bool launch_only);
|
||||
|
@@ -261,6 +261,9 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
||||
handle_put_set_value_by_name(req, res);
|
||||
});
|
||||
|
||||
server->Put("/api/v1/setting",
|
||||
[this](auto &&req, auto &&res) { handle_put_setting(req, res); });
|
||||
|
||||
server->Put("/api/v1/settings", [this](auto &&req, auto &&res) {
|
||||
handle_put_settings(req, res);
|
||||
});
|
||||
@@ -649,6 +652,18 @@ void handlers::handle_put_set_value_by_name(const httplib::Request &req,
|
||||
res.status = http_error_codes::ok;
|
||||
}
|
||||
|
||||
void handlers::handle_put_setting(const httplib::Request &req,
|
||||
httplib::Response &res) const {
|
||||
auto name = req.get_param_value("name");
|
||||
auto value = req.get_param_value("value");
|
||||
|
||||
if (name == JSON_AUTO_START) {
|
||||
config_->set_auto_start(utils::string::to_bool(value));
|
||||
}
|
||||
|
||||
res.status = http_error_codes::ok;
|
||||
}
|
||||
|
||||
void handlers::handle_put_settings(const httplib::Request &req,
|
||||
httplib::Response &res) const {
|
||||
auto data = nlohmann::json::parse(req.get_param_value("data"));
|
||||
|
@@ -94,7 +94,15 @@ mgmt_app_config::mgmt_app_config(bool hidden, bool launch_only)
|
||||
api_password_ = data.at(JSON_API_PASSWORD).get<std::string>();
|
||||
api_port_ = data.at(JSON_API_PORT).get<std::uint16_t>();
|
||||
api_user_ = data.at(JSON_API_USER).get<std::string>();
|
||||
auto_start_ = data.contains(JSON_AUTO_START)
|
||||
? data.at(JSON_AUTO_START).get<bool>()
|
||||
: false;
|
||||
locations_ = from_json(data.at(JSON_MOUNT_LOCATIONS));
|
||||
|
||||
if (not data.contains(JSON_AUTO_START)) {
|
||||
save();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -198,8 +206,18 @@ void mgmt_app_config::set_mount_location(provider_type prov,
|
||||
save();
|
||||
}
|
||||
|
||||
void mgmt_app_config::set_auto_start(bool auto_start) {
|
||||
if (auto_start_ == auto_start) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto_start_ = auto_start;
|
||||
save();
|
||||
}
|
||||
|
||||
auto mgmt_app_config::to_json() const -> nlohmann::json {
|
||||
nlohmann::json data;
|
||||
data[JSON_AUTO_START] = auto_start_;
|
||||
data[JSON_API_PASSWORD] = api_password_;
|
||||
data[JSON_API_PORT] = api_port_;
|
||||
data[JSON_API_USER] = api_user_;
|
||||
|
@@ -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