Skip to content

Commit f853882

Browse files
fix(ADFA-936): Address PR comments
1 parent 81ea603 commit f853882

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

‎app/src/main/java/com/itsaky/androidide/fragments/output/BuildOutputFragment.kt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class BuildOutputFragment :
6060
// so a re-render never misses or duplicates a concurrently flushed batch.
6161
private val editorContentMutex = Mutex()
6262

63+
private var editorContentGeneration = 0
64+
6365
override fun onViewCreated(
6466
view: View,
6567
savedInstanceState: Bundle?,
@@ -87,6 +89,7 @@ class BuildOutputFragment :
8789
/** Re-renders the editor window from the session file, filtered by [query]. */
8890
private suspend fun renderFiltered(query: String) {
8991
editorContentMutex.withLock {
92+
editorContentGeneration++
9093
val window = withContext(Dispatchers.IO) { buildOutputViewModel.getWindowForEditor() }
9194
val filtered =
9295
withContext(Dispatchers.Default) {
@@ -285,11 +288,16 @@ class BuildOutputFragment :
285288
emptyStateViewModel.setEmpty(false)
286289
} else {
287290
// Timeout: defer append until layout is ready (same as restoreWindowFromViewModel)
291+
val generationAtFlush = editorContentGeneration
288292
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
289293
editor?.run {
290294
awaitLayout(onForceVisible = { emptyStateViewModel.setEmpty(false) })
291-
appendBatch(visibleText)
292-
emptyStateViewModel.setEmpty(false)
295+
editorContentMutex.withLock {
296+
if (editorContentGeneration == generationAtFlush) {
297+
appendBatch(visibleText)
298+
emptyStateViewModel.setEmpty(false)
299+
}
300+
}
293301
}
294302
}
295303
}

‎app/src/main/java/com/itsaky/androidide/viewmodel/LogViewModel.kt‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ abstract class LogViewModel : ViewModel() {
151151
submit(level = null, line = line)
152152
}
153153

154+
// Keeps buffer mutation and live emission atomic
155+
private val eventLock = Any()
156+
154157
/**
155158
* Submit a log line.
156159
*
@@ -162,8 +165,10 @@ abstract class LogViewModel : ViewModel() {
162165
line: String,
163166
) {
164167
val text = if (line.endsWith("\n")) line else "$line\n"
165-
val entry = buffer.append(level, text)
166-
liveEntries.tryEmit(entry)
168+
synchronized(eventLock) {
169+
val entry = buffer.append(level, text)
170+
liveEntries.tryEmit(entry)
171+
}
167172
}
168173

169174
fun setFilter(filter: LogFilter) {
@@ -172,8 +177,10 @@ abstract class LogViewModel : ViewModel() {
172177

173178
/** Clear the retained log history and re-render the (now empty) view. */
174179
fun clear() {
175-
buffer.clear()
176-
generation.update { it + 1 }
180+
synchronized(eventLock) {
181+
buffer.clear()
182+
generation.update { it + 1 }
183+
}
177184
}
178185

179186
/** The full retained history, ignoring the active filter. */

‎editor/src/main/java/com/itsaky/androidide/editor/ui/EditorSearchLayout.kt‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import com.itsaky.androidide.utils.SingleTextWatcher
4242
import com.itsaky.androidide.utils.applyLongPressRecursively
4343
import io.github.rosemoe.sora.widget.EditorSearcher.SearchOptions
4444
import java.util.regex.Pattern
45+
import java.util.regex.PatternSyntaxException
4546

4647
/**
4748
* The search layout in [IDEEditor].
@@ -261,7 +262,12 @@ class EditorSearchLayout(
261262
query.isNotBlank() &&
262263
(
263264
searchOptions.type != SearchOptions.TYPE_REGULAR_EXPRESSION ||
264-
runCatching { Pattern.compile(query) }.isSuccess
265+
try {
266+
Pattern.compile(query)
267+
true
268+
} catch (_: PatternSyntaxException) {
269+
false
270+
}
265271
)
266272

267273
if (isValidQuery) {
@@ -323,7 +329,7 @@ class EditorSearchLayout(
323329
try {
324330
Pattern.compile(it)
325331
it
326-
} catch (_: Throwable) {
332+
} catch (_: PatternSyntaxException) {
327333
""
328334
}
329335
} else {

‎logger/src/main/java/com/itsaky/androidide/logging/GlobalBufferAppender.kt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.itsaky.androidide.logging
1919

20+
import android.util.Log
2021
import ch.qos.logback.classic.Level
2122
import ch.qos.logback.classic.spi.ILoggingEvent
2223
import ch.qos.logback.core.AppenderBase
@@ -92,7 +93,11 @@ class GlobalBufferAppender : AppenderBase<ILoggingEvent>() {
9293
message: String,
9394
) {
9495
if (level.levelInt < consumer.logLevel.levelInt) return
95-
runCatching { consumer.consume(level, message) }
96+
try {
97+
consumer.consume(level, message)
98+
} catch (e: Exception) {
99+
Log.e("GlobalBufferAppender", "Log consumer failed", e)
100+
}
96101
}
97102
}
98103

0 commit comments

Comments
 (0)