@@ -17,6 +17,8 @@ import androidx.compose.runtime.*
1717import androidx.compose.ui.Modifier
1818import androidx.compose.ui.platform.LocalContext
1919import androidx.lifecycle.lifecycleScope
20+ import androidx.lifecycle.ViewModelProvider
21+ import com.bitchat.android.mesh.BluetoothMeshService
2022import com.bitchat.android.onboarding.*
2123import com.bitchat.android.ui.ChatScreen
2224import com.bitchat.android.ui.ChatViewModel
@@ -29,7 +31,17 @@ class MainActivity : ComponentActivity() {
2931 private lateinit var permissionManager: PermissionManager
3032 private lateinit var onboardingCoordinator: OnboardingCoordinator
3133 private lateinit var bluetoothStatusManager: BluetoothStatusManager
32- private val chatViewModel: ChatViewModel by viewModels()
34+
35+ // Core mesh service - managed at app level
36+ private lateinit var meshService: BluetoothMeshService
37+ private val chatViewModel: ChatViewModel by viewModels {
38+ object : ViewModelProvider .Factory {
39+ override fun <T : androidx.lifecycle.ViewModel > create (modelClass : Class <T >): T {
40+ @Suppress(" UNCHECKED_CAST" )
41+ return ChatViewModel (application, meshService) as T
42+ }
43+ }
44+ }
3345
3446 // UI state for onboarding flow
3547 private var onboardingState by mutableStateOf(OnboardingState .CHECKING )
@@ -50,6 +62,9 @@ class MainActivity : ComponentActivity() {
5062 override fun onCreate (savedInstanceState : Bundle ? ) {
5163 super .onCreate(savedInstanceState)
5264
65+ // Initialize core mesh service first
66+ meshService = BluetoothMeshService (this )
67+
5368 // Initialize permission management
5469 permissionManager = PermissionManager (this )
5570 bluetoothStatusManager = BluetoothStatusManager (
@@ -289,7 +304,7 @@ class MainActivity : ComponentActivity() {
289304 // This solves the issue where app needs restart to work on first install
290305 delay(1000 ) // Give the system time to process permission grants
291306
292- android.util.Log .d(" MainActivity" , " Permissions verified, starting mesh service " )
307+ android.util.Log .d(" MainActivity" , " Permissions verified, initializing chat system " )
293308
294309 // Ensure all permissions are still granted (user might have revoked in settings)
295310 if (! permissionManager.areAllPermissionsGranted()) {
@@ -299,8 +314,11 @@ class MainActivity : ComponentActivity() {
299314 return @launch
300315 }
301316
302- // Initialize chat view model - this will start the mesh service
303- chatViewModel.meshService.startServices()
317+ // Set up mesh service delegate and start services
318+ meshService.delegate = chatViewModel
319+ meshService.startServices()
320+
321+ android.util.Log .d(" MainActivity" , " Mesh service started successfully" )
304322
305323 // Handle any notification intent
306324 handleNotificationIntent(intent)
@@ -330,6 +348,8 @@ class MainActivity : ComponentActivity() {
330348 super .onResume()
331349 // Check Bluetooth status on resume and handle accordingly
332350 if (onboardingState == OnboardingState .COMPLETE ) {
351+ // Set app foreground state
352+ meshService.connectionManager.setAppBackgroundState(false )
333353 chatViewModel.setAppBackgroundState(false )
334354
335355 // Check if Bluetooth was disabled while app was backgrounded
@@ -347,6 +367,8 @@ class MainActivity : ComponentActivity() {
347367 super .onPause()
348368 // Only set background state if app is fully initialized
349369 if (onboardingState == OnboardingState .COMPLETE ) {
370+ // Set app background state
371+ meshService.connectionManager.setAppBackgroundState(true )
350372 chatViewModel.setAppBackgroundState(true )
351373 }
352374 }
@@ -376,12 +398,32 @@ class MainActivity : ComponentActivity() {
376398 }
377399 }
378400
401+ /* *
402+ * Restart mesh services (for debugging/troubleshooting)
403+ */
404+ fun restartMeshServices () {
405+ if (onboardingState == OnboardingState .COMPLETE ) {
406+ lifecycleScope.launch {
407+ try {
408+ android.util.Log .d(" MainActivity" , " Restarting mesh services" )
409+ meshService.stopServices()
410+ delay(1000 )
411+ meshService.startServices()
412+ android.util.Log .d(" MainActivity" , " Mesh services restarted successfully" )
413+ } catch (e: Exception ) {
414+ android.util.Log .e(" MainActivity" , " Error restarting mesh services: ${e.message} " )
415+ }
416+ }
417+ }
418+ }
419+
379420 override fun onDestroy () {
380421 super .onDestroy()
381- // Only stop mesh services if they were started
422+ // Stop mesh services if app was fully initialized
382423 if (onboardingState == OnboardingState .COMPLETE ) {
383424 try {
384- chatViewModel.meshService.stopServices()
425+ meshService.stopServices()
426+ android.util.Log .d(" MainActivity" , " Mesh services stopped successfully" )
385427 } catch (e: Exception ) {
386428 android.util.Log .w(" MainActivity" , " Error stopping mesh services in onDestroy: ${e.message} " )
387429 }
0 commit comments