Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
feat: using get_it for singletons (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
teaishealthy authored Nov 28, 2022
1 parent 4a8323d commit 9f47a73
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 116 deletions.
17 changes: 17 additions & 0 deletions lib/common.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yaru/yaru.dart';

final getIt = GetIt.instance;

Future<void> requestFilePermissions() async {
if (Platform.isAndroid) {
final status = await Permission.storage.request();
Expand All @@ -25,3 +29,16 @@ class DefaultYaru extends StatelessWidget {
);
}
}

class APIConfig {
get httpUrl =>
getIt<SharedPreferences>().getString('http-url') ?? defaultHttpUrl;
get wsUrl =>
getIt<SharedPreferences>().getString('gateway-url') ?? defaultWsUrl;
get effisUrl =>
getIt<SharedPreferences>().getString('effis-url') ?? defaultEffisUrl;

static const defaultHttpUrl = 'https://eludris.tooty.xyz';
static const defaultWsUrl = 'wss://eludris.tooty.xyz/ws/';
static const defaultEffisUrl = 'https://effis.tooty.xyz';
}
12 changes: 11 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import 'package:eludris/common.dart';
import 'package:eludris/lua/manager.dart';
import 'package:eludris/routes/home.dart';
import 'package:eludris/routes/settings.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yaru/yaru.dart';
import 'package:get_it/get_it.dart';

void main() {
final getIt = GetIt.instance;

Future<void> main() async {
getIt.registerSingleton<PluginManager>(PluginManager());
getIt.registerSingleton<APIConfig>(APIConfig());
getIt.registerSingleton<SharedPreferences>(
await SharedPreferences.getInstance());
runApp(const App());
}

Expand Down
70 changes: 28 additions & 42 deletions lib/routes/loggedin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,28 @@ import 'package:eludris/lua/manager.dart';
import 'package:eludris/models/gateway/message.dart';
import 'package:eludris/widgets/message.dart';
import 'package:file_picker/file_picker.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' show post, MultipartRequest, MultipartFile;
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yaru/yaru.dart';

final getIt = GetIt.instance;

class ConnectionStatus extends StatelessWidget {
final String name;

const ConnectionStatus({Key? key, required this.name}) : super(key: key);

@override
Widget build(BuildContext context) {
return FutureBuilder<SharedPreferences>(
future: SharedPreferences.getInstance(),
builder: (context, snapshot) {
if (snapshot.hasError) return const Icon(Icons.heart_broken);
if (!snapshot.hasData) return const Icon(Icons.hourglass_empty);

final prefs = snapshot.data!;

final httpUrl =
prefs.getString('http-url') ?? 'https://eludris.tooty.xyz';

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Connected as $name'),
Text('Using ${Uri.parse(httpUrl).host}')
],
);
});
final config = getIt<APIConfig>();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Connected as $name'),
Text('Using ${Uri.parse(config.httpUrl).host}')
],
);
}
}

Expand All @@ -52,18 +43,16 @@ class LoggedIn extends StatefulWidget {
}

class _LoggedInState extends State<LoggedIn> {
final pluginManager = PluginManager();
final _messages = <MessageData>[];
StreamSubscription? _stream;

final _textController = TextEditingController();
final FocusNode _focusNode = FocusNode();
final _scrollController = ScrollController();

bool textEnabled = true;
String effisUrl = 'https://effis.tooty.xyz';
String httpUrl = "https://eludris.tooty.xyz";
String gatewayUrl = "wss://eludris.tooty.xyz/ws/";
bool _textEnabled = true;

get _config => getIt<APIConfig>();

@override
void dispose() {
Expand All @@ -79,9 +68,9 @@ class _LoggedInState extends State<LoggedIn> {
_textController.clear();

final message = MessageData(widget.name, text, true);
for (final plugin in pluginManager.plugins) {
for (final plugin in getIt<PluginManager>().plugins) {
final messages =
plugin.runPreSendMessage(http: httpUrl, message: message);
plugin.runPreSendMessage(http: _config.httpUrl, message: message);
setState(() {
_messages.addAll(messages);
});
Expand All @@ -91,7 +80,7 @@ class _LoggedInState extends State<LoggedIn> {
_messages.add(message);
});
await post(
Uri.parse('$httpUrl/messages'),
Uri.parse('${_config.httpUrl}/messages'),
body: jsonEncode({
'author': message.author,
'content': message.content,
Expand All @@ -103,14 +92,11 @@ class _LoggedInState extends State<LoggedIn> {
}

_initState() async {
final prefs = await SharedPreferences.getInstance();
httpUrl = prefs.getString('http-url') ?? httpUrl;
gatewayUrl = prefs.getString('gateway-url') ?? gatewayUrl;
effisUrl = prefs.getString('effis-url') ?? effisUrl;
final config = getIt<APIConfig>();

final ws = await WebSocket.connect(gatewayUrl);
final ws = await WebSocket.connect(config.wsUrl);
ws.pingInterval = const Duration(seconds: 10);
pluginManager.loadPlugins();
getIt<PluginManager>().loadPlugins();

_stream = ws.listen(_wsListen);
}
Expand All @@ -133,11 +119,11 @@ class _LoggedInState extends State<LoggedIn> {
}
});

for (final plugin in pluginManager.plugins) {
for (final plugin in getIt<PluginManager>().plugins) {
if (plugin.hooks.contains('postGotMessage') &&
plugin.manifest.permissions.contains('READ_MESSAGES')) {
final pMessages =
plugin.runPostGotMessage(http: httpUrl, message: message);
plugin.runPostGotMessage(http: _config.httpUrl, message: message);
setState(() {
_messages.addAll(pMessages);
});
Expand Down Expand Up @@ -204,12 +190,12 @@ class _LoggedInState extends State<LoggedIn> {
final files = await FilePicker.platform.pickFiles();
if (files != null) {
setState(() {
textEnabled = false;
_textEnabled = false;
});
final file = File(files.files.single.path!);

final request = MultipartRequest(
"POST", Uri.parse("$effisUrl/upload"));
final request = MultipartRequest("POST",
Uri.parse("${_config.effisUrl}/upload"));
request.fields['name'] = file.path.split('/').last;
request.files.add(await MultipartFile.fromPath(
'file', file.path));
Expand All @@ -219,15 +205,15 @@ class _LoggedInState extends State<LoggedIn> {
final match =
RegExp(r"\d+").firstMatch(data)!.group(0);

final uri = Uri.parse(effisUrl);
final uri = Uri.parse(_config.effisUrl);
_textController.text += Uri(
host: uri.host,
scheme: uri.scheme,
path: match!,
).toString();

setState(() {
textEnabled = true;
_textEnabled = true;
});

_focusNode.requestFocus();
Expand All @@ -238,7 +224,7 @@ class _LoggedInState extends State<LoggedIn> {
child: TextField(
autofocus: true,
controller: _textController,
enabled: textEnabled,
enabled: _textEnabled,
focusNode: _focusNode,
onSubmitted: (data) {
_sendMessage();
Expand Down
23 changes: 13 additions & 10 deletions lib/routes/plugins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import 'package:eludris/common.dart';
import 'package:eludris/lua/manager.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
// ignore: depend_on_referenced_packages
import 'package:path/path.dart' as path;

final getIt = GetIt.instance;

const Map<String, String> permissionsExplained = {
"READ_MESSAGES": "This plugin can read ALL messages",
"SEND_MESSAGES": "This plugin can send messages",
Expand Down Expand Up @@ -93,30 +96,29 @@ class Plugins extends StatefulWidget {
enum PluginState { enabled, disabled, missingManifest }

class _PluginsState extends State<Plugins> {
final manager = PluginManager();

Future<void> _loadPlugins() async {
await manager.loadPlugins();
await getIt<PluginManager>().loadPlugins();
setState(() {});
}

Future<PluginState> _addPlugin(PlatformFile file) async {
await manager.addPlugin(path.basenameWithoutExtension(file.name), file);
final dir =
await manager.getPluginDir(path.basenameWithoutExtension(file.name));
final manifest = await manager.getManifest(dir.path);
await getIt<PluginManager>()
.addPlugin(path.basenameWithoutExtension(file.name), file);
final dir = await getIt<PluginManager>()
.getPluginDir(path.basenameWithoutExtension(file.name));
final manifest = await getIt<PluginManager>().getManifest(dir.path);

if (manifest == null) {
return PluginState.missingManifest;
}
final plugin = PluginInfo(dir, manager);
final plugin = PluginInfo(dir, getIt<PluginManager>());

bool accepted = await _askAcceptPlugin(plugin) ?? false;
if (!accepted) {
plugin.delete();
return PluginState.disabled;
} else {
await manager.loadPlugins();
await getIt<PluginManager>().loadPlugins();
}

return PluginState.enabled;
Expand Down Expand Up @@ -172,7 +174,8 @@ class _PluginsState extends State<Plugins> {
),
Expanded(
child: ListView(
children: manager.plugins
children: getIt<PluginManager>()
.plugins
.map((p) => Plugin(p, _loadPlugins))
.toList(),
),
Expand Down
Loading

0 comments on commit 9f47a73

Please sign in to comment.