Skip to content

Commit 911cc6f

Browse files
authored
Merge pull request #6 from CoderJava/feature/buat-fitur-pengaturan-launch-at-startup
Feature/buat fitur pengaturan launch at startup
2 parents 347c7b5 + 969e376 commit 911cc6f

File tree

7 files changed

+97
-3
lines changed

7 files changed

+97
-3
lines changed

assets/translations/en-US.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,7 @@
200200
"title_screen_recording_mac": "Screen Recording",
201201
"description_screen_recording_mac": "This app would like to record this computer's screen. Grant access to this app in Security & Privacy preferences. located in System Preferences. If it doesn't exists please add manually or if it exists please delete it.",
202202
"screen_recording_issued": "Screen recording not granted",
203-
"note_description_screen_recording_mac": "Always remember to reopen this app after changing permissions."
203+
"note_description_screen_recording_mac": "Always remember to reopen this app after changing permissions.",
204+
"launch_at_startup": "Launch at Startup",
205+
"subtitle_launch_at_startup": "Dipantau will start running automatically when you turn your computer on"
204206
}

lib/core/util/shared_preferences_manager.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SharedPreferencesManager {
1616
static const keyIsEnableScreenshotNotification = 'is_enable_screenshot_notification';
1717
static const keyAppearanceMode = 'appearance_mode';
1818
static const keyBaseFilePathScreenshot = 'base_file_path_screenshot';
19+
static const keyIsLaunchAtStartup = 'is_launch_at_startup';
1920

2021
SharedPreferencesManager();
2122

lib/feature/presentation/page/setting/setting_page.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:flutter/material.dart';
1818
import 'package:flutter/scheduler.dart';
1919
import 'package:flutter_bloc/flutter_bloc.dart';
2020
import 'package:go_router/go_router.dart';
21+
import 'package:launch_at_startup/launch_at_startup.dart';
2122

2223
class SettingPage extends StatefulWidget {
2324
static const routePath = '/setting';
@@ -33,7 +34,9 @@ class _SettingPageState extends State<SettingPage> {
3334
final helper = sl<Helper>();
3435
final valueNotifierIsEnableScreenshotNotification = ValueNotifier(false);
3536
final valueNotifierAppearanceMode = ValueNotifier(AppearanceMode.light);
37+
final valueNotifierLaunchAtStartup = ValueNotifier(true);
3638
final widgetHelper = WidgetHelper();
39+
final sharedPreferencesManager = sl<SharedPreferencesManager>();
3740

3841
var hostname = '';
3942
late AppearanceBloc appearanceBloc;
@@ -48,6 +51,9 @@ class _SettingPageState extends State<SettingPage> {
4851

4952
@override
5053
void initState() {
54+
launchAtStartup.isEnabled().then((value) {
55+
valueNotifierLaunchAtStartup.value = value;
56+
});
5157
appearanceBloc = BlocProvider.of<AppearanceBloc>(context);
5258
final strUserRole = sharedPreferencesManager.getString(SharedPreferencesManager.keyUserRole) ?? '';
5359
userRole = strUserRole.fromStringUserRole;
@@ -99,6 +105,8 @@ class _SettingPageState extends State<SettingPage> {
99105
const SizedBox(height: 8),
100106
buildWidgetScreenshotNotification(),
101107
const SizedBox(height: 16),
108+
buildWidgetLaunchAtStartup(),
109+
const SizedBox(height: 16),
102110
buildWidgetSetHostName(),
103111
const SizedBox(height: 16),
104112
buildWidgetCheckForUpdate(),
@@ -113,8 +121,56 @@ class _SettingPageState extends State<SettingPage> {
113121
);
114122
}
115123

124+
Widget buildWidgetLaunchAtStartup() {
125+
return Row(
126+
crossAxisAlignment: CrossAxisAlignment.start,
127+
children: [
128+
Expanded(
129+
child: Column(
130+
crossAxisAlignment: CrossAxisAlignment.start,
131+
children: [
132+
Text(
133+
'launch_at_startup'.tr(),
134+
style: Theme.of(context).textTheme.bodyLarge,
135+
),
136+
Text(
137+
'subtitle_launch_at_startup'.tr(),
138+
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
139+
color: Colors.grey,
140+
),
141+
),
142+
],
143+
),
144+
),
145+
const SizedBox(width: 16),
146+
ValueListenableBuilder(
147+
valueListenable: valueNotifierLaunchAtStartup,
148+
builder: (BuildContext context, bool value, _) {
149+
return Switch.adaptive(
150+
value: value,
151+
onChanged: (newValue) async {
152+
if (newValue) {
153+
await launchAtStartup.enable();
154+
} else {
155+
await launchAtStartup.disable();
156+
}
157+
sharedPreferencesManager.putBool(
158+
SharedPreferencesManager.keyIsLaunchAtStartup,
159+
newValue,
160+
);
161+
valueNotifierLaunchAtStartup.value = newValue;
162+
},
163+
activeColor: Theme.of(context).colorScheme.primary,
164+
);
165+
},
166+
),
167+
],
168+
);
169+
}
170+
116171
Widget buildWidgetScreenshotNotification() {
117172
return Row(
173+
crossAxisAlignment: CrossAxisAlignment.start,
118174
children: [
119175
Expanded(
120176
child: Column(

lib/feature/presentation/page/splash/splash_page.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:dipantau_desktop_client/feature/presentation/widget/widget_custo
88
import 'package:dipantau_desktop_client/injection_container.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:go_router/go_router.dart';
11-
import 'package:package_info_plus/package_info_plus.dart';
1211

1312
class SplashPage extends StatefulWidget {
1413
static const routePath = '/splash';
@@ -26,7 +25,6 @@ class _SplashPageState extends State<SplashPage> {
2625
super.initState();
2726
Future.delayed(const Duration(seconds: 1)).then((_) async {
2827
sharedPreferencesManager = await sl.getAsync<SharedPreferencesManager>();
29-
packageInfo = await PackageInfo.fromPlatform();
3028
if (!sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyAppearanceMode)) {
3129
sharedPreferencesManager.putString(SharedPreferencesManager.keyAppearanceMode, AppearanceMode.light.name);
3230
}

lib/main.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:io';
12
import 'dart:ui';
23

34
import 'package:auto_updater/auto_updater.dart';
@@ -31,6 +32,8 @@ import 'package:flutter/material.dart';
3132
import 'package:flutter_bloc/flutter_bloc.dart';
3233
import 'package:go_router/go_router.dart';
3334
import 'package:google_fonts/google_fonts.dart';
35+
import 'package:launch_at_startup/launch_at_startup.dart';
36+
import 'package:package_info_plus/package_info_plus.dart';
3437
import 'package:shared_preferences/shared_preferences.dart';
3538
import 'package:window_manager/window_manager.dart';
3639

@@ -68,6 +71,14 @@ void main() async {
6871
autoUpdater.setFeedURL(feedURL);
6972
autoUpdater.setScheduledCheckInterval(3600);
7073

74+
packageInfo = await PackageInfo.fromPlatform();
75+
76+
// Launch at startup
77+
launchAtStartup.setup(
78+
appName: packageInfo.appName,
79+
appPath: Platform.resolvedExecutable,
80+
);
81+
7182
// Easy localization
7283
await EasyLocalization.ensureInitialized();
7384

@@ -274,6 +285,13 @@ class _MyAppState extends State<MyApp> {
274285
updateAppearanceMode(window, sharedPreferencesManager);
275286
};
276287
}
288+
289+
final isLaunchAtStartupExists =
290+
sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyIsLaunchAtStartup);
291+
if (!isLaunchAtStartupExists) {
292+
await launchAtStartup.enable();
293+
sharedPreferencesManager.putBool(SharedPreferencesManager.keyIsLaunchAtStartup, true);
294+
}
277295
});
278296
super.initState();
279297
}

pubspec.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,14 @@ packages:
557557
url: "https://pub.dev"
558558
source: hosted
559559
version: "6.7.0"
560+
launch_at_startup:
561+
dependency: "direct main"
562+
description:
563+
name: launch_at_startup
564+
sha256: "93fc5638e088290004fae358bae691486673d469957d461d9dae5b12248593eb"
565+
url: "https://pub.dev"
566+
source: hosted
567+
version: "0.2.2"
560568
lints:
561569
dependency: transitive
562570
description:
@@ -1218,6 +1226,14 @@ packages:
12181226
url: "https://pub.dev"
12191227
source: hosted
12201228
version: "5.0.3"
1229+
win32_registry:
1230+
dependency: transitive
1231+
description:
1232+
name: win32_registry
1233+
sha256: e4506d60b7244251bc59df15656a3093501c37fb5af02105a944d73eb95be4c9
1234+
url: "https://pub.dev"
1235+
source: hosted
1236+
version: "1.1.1"
12211237
window_manager:
12221238
dependency: "direct main"
12231239
description:

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ dependencies:
129129
# on iOS or versionCode on Android.
130130
package_info_plus: ^4.0.2
131131

132+
# This plugin allow Flutter desktop apps to Auto launch on startup / login.
133+
launch_at_startup: ^0.2.2
134+
132135
dev_dependencies:
133136
flutter_test:
134137
sdk: flutter

0 commit comments

Comments
 (0)