Skip to content

Commit

Permalink
code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed May 24, 2024
1 parent 8b47d3a commit be7f24c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
16 changes: 13 additions & 3 deletions lib/common/tray_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ String systemTrayIcon(bool connected) {


class TrayHandler with TrayListener {
TrayHandler() {
factory TrayHandler() => _getInstance();

static TrayHandler get instance => _getInstance();
static TrayHandler? _instance;

static TrayHandler _getInstance() {
_instance ??= TrayHandler._internal();
return _instance!;
}

TrayHandler._internal() {
if (isDesktop()) {
setupTray(false);
}
}

Future<void> setupTray(bool isConnected) async {
await trayManager.setIcon(systemTrayIcon(isConnected));
await trayManager.setIcon(systemTrayIcon(isConnected));
Menu menu = Menu(
items: [
MenuItem(
Expand Down Expand Up @@ -56,7 +66,7 @@ class TrayHandler with TrayListener {
void onTrayMenuItemClick(MenuItem menuItem) {
switch (menuItem.key) {
case 'show':
windowManager.show();
windowManager.focus();
windowManager.setSkipTaskbar(false);
case 'exit':
windowManager.destroy();
Expand Down
9 changes: 8 additions & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WindowListener {
class _HomePageState extends State<HomePage> with TrayListener, WindowListener {

Function()? _cancelEventSubscription;

Expand All @@ -40,6 +40,8 @@ class _HomePageState extends State<HomePage> with WindowListener {
// This is a mobile device
channelListener();
} else {
// This is a desktop device
setupTrayManager();
_initWindowManager();
}
}
Expand Down Expand Up @@ -94,6 +96,10 @@ class _HomePageState extends State<HomePage> with WindowListener {
setState(() {});
}

void setupTrayManager() async {
trayManager.addListener(TrayHandler.instance);
}

@override
void onWindowClose() async {
bool _isPreventClose = await windowManager.isPreventClose();
Expand Down Expand Up @@ -143,6 +149,7 @@ class _HomePageState extends State<HomePage> with WindowListener {
@override
void dispose() {
if (isDesktop()) {
trayManager.removeListener(TrayHandler.instance);
windowManager.removeListener(this);
}
if (_cancelEventSubscription != null) {
Expand Down
26 changes: 3 additions & 23 deletions lib/vpn/vpn_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:lantern/ad_helper.dart';
import 'package:lantern/common/common.dart';
import 'package:lantern/common/common_desktop.dart';
import 'package:lantern/vpn/vpn.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

class VPNSwitch extends StatefulWidget {
const VPNSwitch({super.key});
Expand All @@ -12,25 +10,7 @@ class VPNSwitch extends StatefulWidget {
State<VPNSwitch> createState() => _VPNSwitchState();
}

class _VPNSwitchState extends State<VPNSwitch> with TrayListener, WindowListener {

TrayHandler trayHandler = TrayHandler();

@override
void initState() {
if (isDesktop()) {
trayManager.addListener(trayHandler);
}
super.initState();
}

@override
void dispose() {
if (isDesktop()) {
trayManager.removeListener(trayHandler);
}
}

class _VPNSwitchState extends State<VPNSwitch> {
final adHelper = AdHelper();
String vpnStatus = 'disconnected';

Expand All @@ -41,10 +21,10 @@ class _VPNSwitchState extends State<VPNSwitch> with TrayListener, WindowListener
bool isConnected = vpnStatus == 'connected';
if (isConnected) {
sysProxyOff();
await trayHandler.setupTray(false);
await TrayHandler.instance.setupTray(false);
} else {
sysProxyOn();
await trayHandler.setupTray(true);
await TrayHandler.instance.setupTray(true);
}
}

Expand Down

0 comments on commit be7f24c

Please sign in to comment.