Skip to content

Commit

Permalink
Add more logs and remove loading
Browse files Browse the repository at this point in the history
greenart7c3 committed Oct 14, 2024
1 parent 2ed99e2 commit 21c17cf
Showing 3 changed files with 25 additions and 11 deletions.
17 changes: 8 additions & 9 deletions app/src/main/java/com/greenart7c3/citrine/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ class MainActivity : ComponentActivity() {
private lateinit var database: AppDatabase
private var countByKind: Flow<List<EventDao.CountResult>>? = null
private val storageHelper = SimpleStorageHelper(this@MainActivity)
var saveToPreferences = false
private var saveToPreferences = false

@OptIn(DelicateCoroutinesApi::class)
private val requestPermissionLauncher = registerForActivityResult(
@@ -97,7 +97,7 @@ class MainActivity : ComponentActivity() {

private var service: WebSocketServerService? = null
private var bound = false
private var isLoading = mutableStateOf(true)
private var isLoading = mutableStateOf(false)

private val connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
@@ -113,7 +113,7 @@ class MainActivity : ComponentActivity() {

private suspend fun stop() {
try {
isLoading.value = true
isLoading.value = false // TODO: return this to true after finding where its getting the loading stuck forever
val intent = Intent(applicationContext, WebSocketServerService::class.java)
stopService(intent)
if (bound) unbindService(connection)
@@ -130,7 +130,7 @@ class MainActivity : ComponentActivity() {

private suspend fun start() {
try {
isLoading.value = true
isLoading.value = false // TODO: return this to true after finding where its getting the loading stuck forever
val intent = Intent(applicationContext, WebSocketServerService::class.java)
startService(intent)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
@@ -147,8 +147,6 @@ class MainActivity : ComponentActivity() {
val progress = mutableStateOf("")
super.onCreate(savedInstanceState)

requestPermissionLauncher.launch("android.permission.POST_NOTIFICATIONS")

setContent {
val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current
@@ -185,6 +183,10 @@ class MainActivity : ComponentActivity() {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val destinationRoute = navBackStackEntry?.destination?.route ?: ""

LaunchedEffect(Unit) {
requestPermissionLauncher.launch("android.permission.POST_NOTIFICATIONS")
}

Scaffold(
bottomBar = {
if (destinationRoute != Route.Logs.route) {
@@ -390,10 +392,7 @@ class MainActivity : ComponentActivity() {
ElevatedButton(
onClick = {
coroutineScope.launch(Dispatchers.IO) {
isLoading.value = true
start()
delay(1000)
isLoading.value = false
}
},
) {
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ class CustomWebSocketServer(
if (serverSocket == null) return
serverSocket.close()

Log.d(Citrine.TAG, "Starting server on $host:$port isNull: ${server == null}")
if (server == null) {
server = startKtorHttpServer(host, port)
} else {
@@ -68,6 +69,7 @@ class CustomWebSocketServer(
}

suspend fun stop() {
Log.d(Citrine.TAG, "Stopping server")
server?.stop(1000)
server = null
}
Original file line number Diff line number Diff line change
@@ -102,8 +102,10 @@ class WebSocketServerService : Service() {
override fun onCreate() {
super.onCreate()

Log.d(Citrine.TAG, "Starting WebSocket service")
val database = AppDatabase.getDatabase(this@WebSocketServerService)

Log.d(Citrine.TAG, "Starting timer")
timer?.cancel()
timer = Timer()
timer?.schedule(
@@ -140,6 +142,7 @@ class WebSocketServerService : Service() {
300000,
)

Log.d(Citrine.TAG, "Starting WebSocket server")
// Start the WebSocket server
webSocketServer = CustomWebSocketServer(
host = Settings.host,
@@ -148,8 +151,17 @@ class WebSocketServerService : Service() {
)
webSocketServer.start()

// Create a notification to keep the service in the foreground
startForeground(1, createNotification())
var error = true
var attempts = 0
while (error && attempts < 5) {
try {
startForeground(1, createNotification())
error = false
} catch (e: Exception) {
Log.e(Citrine.TAG, "Error starting foreground service attempt $attempts", e)
}
attempts++
}
}

override fun onDestroy() {
@@ -167,6 +179,7 @@ class WebSocketServerService : Service() {
}

private fun createNotification(): Notification {
Log.d(Citrine.TAG, "Creating notification")
val channelId = "WebSocketServerServiceChannel"
val channel = NotificationChannel(channelId, "WebSocket Server", NotificationManager.IMPORTANCE_DEFAULT)
channel.setSound(null, null)

0 comments on commit 21c17cf

Please sign in to comment.