Notification is added
This commit is contained in:
1 parent
afe7fb578a
commit
a80a23e008
7 files changed
+153
-2
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
class VpnNotificationManager {
|
||||
static final FlutterLocalNotificationsPlugin _plugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
static bool _initialized = false;
|
||||
|
||||
static Future<void> init() async {
|
||||
if (_initialized) return;
|
||||
|
||||
const androidSettings =
|
||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const initSettings = InitializationSettings(android: androidSettings);
|
||||
|
||||
await _plugin.initialize(initSettings);
|
||||
|
||||
// Request POST_NOTIFICATIONS permission on Android 13+
|
||||
if (Platform.isAndroid) {
|
||||
final androidPlugin = _plugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>();
|
||||
await androidPlugin?.requestNotificationsPermission();
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
static Future<void> showOrUpdateStatus({
|
||||
required String timeLeft,
|
||||
required String speedKbps,
|
||||
}) async {
|
||||
if (!_initialized) await init();
|
||||
|
||||
const androidDetails = AndroidNotificationDetails(
|
||||
'paladin_vpn_status',
|
||||
'VPN Status',
|
||||
channelDescription: 'Persistent notification shown while VPN is active',
|
||||
importance: Importance.low,
|
||||
priority: Priority.low,
|
||||
// Non-dismissable while VPN is running
|
||||
ongoing: true,
|
||||
autoCancel: false,
|
||||
// Silent updates — no sound or vibration each time stats refresh
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
onlyAlertOnce: true,
|
||||
// Hide the auto-generated timestamp
|
||||
showWhen: false,
|
||||
// Show on lock screen
|
||||
visibility: NotificationVisibility.public,
|
||||
);
|
||||
|
||||
const notificationDetails = NotificationDetails(android: androidDetails);
|
||||
|
||||
await _plugin.show(
|
||||
888, // Fixed ID — always updates the same notification
|
||||
'Paladin VPN Active',
|
||||
'Time: $timeLeft • $speedKbps',
|
||||
notificationDetails,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> cancel() async {
|
||||
if (!_initialized) return;
|
||||
await _plugin.cancel(888);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:http/http.dart' as http;
|
||||
import 'package:paladinvpn/logic/app_config.dart';
|
||||
import 'package:paladinvpn/logic/vpn_connection.dart';
|
||||
import 'package:paladinvpn/logic/crypto_service.dart';
|
||||
import 'package:paladinvpn/components/notification.dart';
|
||||
|
||||
/// SessionTimer acts as the client-side session authority for the VPN tunnel.
|
||||
///
|
||||
@@ -51,6 +52,16 @@ class SessionTimer extends ChangeNotifier {
|
||||
return '${gb.toStringAsFixed(2)} GB';
|
||||
}
|
||||
|
||||
String get formattedDataUsed {
|
||||
if (_usedBytes == 0) return '0.00 MB';
|
||||
if (_usedBytes > 1024 * 1024 * 1024) {
|
||||
final gb = _usedBytes / (1024 * 1024 * 1024);
|
||||
return '${gb.toStringAsFixed(2)} GB';
|
||||
}
|
||||
final mb = _usedBytes / (1024 * 1024);
|
||||
return '${mb.toStringAsFixed(2)} MB';
|
||||
}
|
||||
|
||||
double get progress {
|
||||
// Max duration is generally 60 or 90 minutes. We'll base the ring purely on data usage or just standard time
|
||||
// For UI simplicity, we'll return data progress:
|
||||
@@ -119,6 +130,11 @@ class SessionTimer extends ChangeNotifier {
|
||||
}
|
||||
_lastUsedBytes = _usedBytes;
|
||||
|
||||
VpnNotificationManager.showOrUpdateStatus(
|
||||
timeLeft: formatted,
|
||||
speedKbps: '${_currentSpeedKbps.toStringAsFixed(2)} KB/s',
|
||||
);
|
||||
|
||||
notifyListeners();
|
||||
} else if (response.statusCode == 404) {
|
||||
// Peer not found on server (expired/deleted natively)
|
||||
@@ -159,6 +175,7 @@ class SessionTimer extends ChangeNotifier {
|
||||
_timer = null;
|
||||
_remainingSeconds = 0;
|
||||
_currentSpeedKbps = 0.0;
|
||||
VpnNotificationManager.cancel();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user