@@ -19,7 +19,7 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
1919 bool _isLoadingServers = false ;
2020 bool _isProxyMode = false ;
2121 bool _isInitializing = true ; // New flag to track initialization state
22-
22+
2323 // Method channel for VPN control
2424 static const platform = MethodChannel ('com.cloud.pira/vpn_control' );
2525
@@ -41,11 +41,11 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
4141 V2RayProvider () {
4242 WidgetsBinding .instance.addObserver (this );
4343 _initialize ();
44-
44+
4545 // Set up method channel handler
4646 platform.setMethodCallHandler (_handleMethodCall);
4747 }
48-
48+
4949 // Handle method calls from native side
5050 Future <dynamic > _handleMethodCall (MethodCall call) async {
5151 switch (call.method) {
@@ -60,7 +60,7 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
6060 Future <void > _handleNotificationDisconnect () async {
6161 // Actually disconnect the VPN service
6262 await _v2rayService.disconnect ();
63-
63+
6464 // Update config status when disconnected from notification
6565 for (int i = 0 ; i < _configs.length; i++ ) {
6666 _configs[i].isConnected = false ;
@@ -85,7 +85,7 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
8585 _setLoading (true );
8686 _isInitializing = true ; // Set initialization flag
8787 notifyListeners ();
88-
88+
8989 try {
9090 await _v2rayService.initialize ();
9191
@@ -121,23 +121,25 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
121121 Future <void > _enhancedSyncWithVpnServiceState () async {
122122 try {
123123 print ('Enhanced synchronization with VPN service state...' );
124-
124+
125125 // First, check if VPN is actually running using the improved method
126126 final isActuallyConnected = await _v2rayService.isActuallyConnected ();
127- print ('VPN service status - Actually connected (primary check): $isActuallyConnected ' );
128-
127+ print (
128+ 'VPN service status - Actually connected (primary check): $isActuallyConnected ' ,
129+ );
130+
129131 // Reset all connection states first
130132 for (int i = 0 ; i < _configs.length; i++ ) {
131133 _configs[i].isConnected = false ;
132134 }
133-
135+
134136 if (isActuallyConnected) {
135137 print ('VPN is actually running, synchronizing config states...' );
136-
138+
137139 // Try to get the active config from service
138140 final activeConfigFromService = _v2rayService.activeConfig;
139141 print ('Active config from service: ${activeConfigFromService ?.remark }' );
140-
142+
141143 if (activeConfigFromService != null ) {
142144 bool configFound = false ;
143145
@@ -161,7 +163,9 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
161163 config.isConnected = true ;
162164 _selectedConfig = config;
163165 configFound = true ;
164- print ('Found matching config by address/port: ${config .remark }' );
166+ print (
167+ 'Found matching config by address/port: ${config .remark }' ,
168+ );
165169 break ;
166170 }
167171 }
@@ -174,14 +178,18 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
174178 _configs.add (activeConfigFromService);
175179 activeConfigFromService.isConnected = true ;
176180 _selectedConfig = activeConfigFromService;
177- print ('Added active config to list: ${activeConfigFromService .remark }' );
181+ print (
182+ 'Added active config to list: ${activeConfigFromService .remark }' ,
183+ );
178184 }
179185 } else {
180186 // VPN is running but we don't have the config details
181187 // Try to find any config that might be connected
182- print ('VPN is running but no active config in service, checking configs...' );
188+ print (
189+ 'VPN is running but no active config in service, checking configs...' ,
190+ );
183191 V2RayConfig ? foundConnectedConfig;
184-
192+
185193 // Check if any config has connection details that match a running service
186194 for (var config in _configs) {
187195 try {
@@ -196,14 +204,18 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
196204 print ('Error checking config ${config .remark }: $e ' );
197205 }
198206 }
199-
207+
200208 if (foundConnectedConfig == null ) {
201- print ('Could not identify which config is connected, marking first available' );
209+ print (
210+ 'Could not identify which config is connected, marking first available' ,
211+ );
202212 // As a fallback, mark the first config as connected if we have configs
203213 if (_configs.isNotEmpty) {
204214 _configs.first.isConnected = true ;
205215 _selectedConfig = _configs.first;
206- print ('Marked first config as connected: ${_configs .first .remark }' );
216+ print (
217+ 'Marked first config as connected: ${_configs .first .remark }' ,
218+ );
207219 }
208220 }
209221 }
@@ -214,7 +226,7 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
214226 config.isConnected = false ;
215227 }
216228 _selectedConfig = null ;
217-
229+
218230 // Clear active config from service if it exists
219231 if (_v2rayService.activeConfig != null ) {
220232 await _v2rayService.disconnect ();
@@ -422,7 +434,7 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
422434 try {
423435 // Parse multiple configurations from text (similar to subscription parsing)
424436 final configs = await _v2rayService.parseSubscriptionContent (configText);
425-
437+
426438 if (configs.isEmpty) {
427439 throw Exception ('No valid configurations found' );
428440 }
@@ -757,7 +769,8 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
757769 debugPrint ('Connection successful for ${config .remark }' );
758770 // Verify the connection is actually established
759771 await Future .delayed (const Duration (seconds: 1 ));
760- final connectionVerified = await _v2rayService.isActuallyConnected ();
772+ final connectionVerified = await _v2rayService
773+ .isActuallyConnected ();
761774 if (connectionVerified) {
762775 debugPrint ('Connection verified for ${config .remark }' );
763776 break ;
@@ -1025,7 +1038,10 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
10251038 return await _v2rayService.parseSubscriptionContent (content);
10261039 }
10271040
1028- Future <void > addSubscriptionFromFile (String name, List <V2RayConfig > configs) async {
1041+ Future <void > addSubscriptionFromFile (
1042+ String name,
1043+ List <V2RayConfig > configs,
1044+ ) async {
10291045 _setLoading (true );
10301046 _errorMessage = '' ;
10311047 try {
@@ -1038,7 +1054,8 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
10381054 final subscription = Subscription (
10391055 id: DateTime .now ().millisecondsSinceEpoch.toString (),
10401056 name: name,
1041- url: 'file://subscription' , // Special indicator for file-based subscriptions
1057+ url:
1058+ 'file://subscription' , // Special indicator for file-based subscriptions
10421059 lastUpdated: DateTime .now (),
10431060 configIds: newConfigIds,
10441061 );
@@ -1066,4 +1083,4 @@ class V2RayProvider with ChangeNotifier, WidgetsBindingObserver {
10661083 _setLoading (false );
10671084 }
10681085 }
1069- }
1086+ }
0 commit comments