Skip to content

Commit 80d4954

Browse files
committed
FileViewer: fix coloration issue for logs files.
1 parent 80270d1 commit 80d4954

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

features/viewfolder/impl/src/main/kotlin/io/element/android/features/viewfolder/impl/file/ViewFilePresenter.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ViewFilePresenter @AssistedInject constructor(
4949
@Composable
5050
override fun present(): ViewFileState {
5151
val coroutineScope = rememberCoroutineScope()
52+
val colorationMode = remember { name.toColorationMode() }
5253

5354
fun handleEvent(event: ViewFileEvents) {
5455
when (event) {
@@ -67,6 +68,7 @@ class ViewFilePresenter @AssistedInject constructor(
6768
return ViewFileState(
6869
name = name,
6970
lines = lines,
71+
colorationMode = colorationMode,
7072
eventSink = ::handleEvent,
7173
)
7274
}
@@ -79,3 +81,11 @@ class ViewFilePresenter @AssistedInject constructor(
7981
fileSave.save(path)
8082
}
8183
}
84+
85+
private fun String.toColorationMode(): ColorationMode {
86+
return when {
87+
equals("logcat.log") -> ColorationMode.Logcat
88+
startsWith("logs.") -> ColorationMode.Logs
89+
else -> ColorationMode.None
90+
}
91+
}

features/viewfolder/impl/src/main/kotlin/io/element/android/features/viewfolder/impl/file/ViewFileState.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ import io.element.android.libraries.architecture.AsyncData
2121
data class ViewFileState(
2222
val name: String,
2323
val lines: AsyncData<List<String>>,
24+
val colorationMode: ColorationMode,
2425
val eventSink: (ViewFileEvents) -> Unit,
2526
)
27+
28+
enum class ColorationMode {
29+
Logcat,
30+
Logs,
31+
None,
32+
}

features/viewfolder/impl/src/main/kotlin/io/element/android/features/viewfolder/impl/file/ViewFileStateProvider.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ fun aViewFileState(
5151
) = ViewFileState(
5252
name = name,
5353
lines = lines,
54+
colorationMode = ColorationMode.Logcat,
5455
eventSink = {},
5556
)

features/viewfolder/impl/src/main/kotlin/io/element/android/features/viewfolder/impl/file/ViewFileView.kt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ fun ViewFileView(
114114
is AsyncData.Success -> FileContent(
115115
modifier = Modifier.weight(1f),
116116
lines = state.lines.data.toImmutableList(),
117+
colorationMode = state.colorationMode,
117118
)
118119
is AsyncData.Failure -> AsyncFailure(throwable = state.lines.error, onRetry = null)
119120
}
@@ -125,6 +126,7 @@ fun ViewFileView(
125126
@Composable
126127
private fun FileContent(
127128
lines: ImmutableList<String>,
129+
colorationMode: ColorationMode,
128130
modifier: Modifier = Modifier,
129131
) {
130132
LazyColumn(
@@ -147,6 +149,7 @@ private fun FileContent(
147149
LineRow(
148150
lineNumber = index + 1,
149151
line = line,
152+
colorationMode = colorationMode,
150153
)
151154
}
152155
}
@@ -157,6 +160,7 @@ private fun FileContent(
157160
private fun LineRow(
158161
lineNumber: Int,
159162
line: String,
163+
colorationMode: ColorationMode,
160164
) {
161165
val context = LocalContext.current
162166
Row(
@@ -195,25 +199,41 @@ private fun LineRow(
195199
}
196200
.padding(horizontal = 4.dp),
197201
text = line,
198-
color = line.toColor(),
202+
color = line.toColor(colorationMode),
199203
style = ElementTheme.typography.fontBodyMdRegular
200204
)
201205
}
202206
}
203207

204208
/**
205-
* Convert a logcat line to a color.
206-
* Ex: `01-23 13:14:50.740 25818 25818 D org.matrix.rust.sdk: elementx: SyncIndicator = Hide | RustRoomListService.kt:81`
209+
* Convert a line to a color.
210+
* Ex for logcat:
211+
* `01-23 13:14:50.740 25818 25818 D org.matrix.rust.sdk: elementx: SyncIndicator = Hide | RustRoomListService.kt:81`
212+
* ^ use this char to determine the color
213+
* Ex for logs:
214+
* `2024-01-26T10:22:26.947416Z WARN elementx: Restore with non-empty map | MatrixClientsHolder.kt:68`
215+
* ^ use this char to determine the color, see [LogLevel]
207216
*/
208217
@Composable
209-
private fun String.toColor(): Color {
210-
return when (getOrNull(31)) {
211-
'D' -> Color(0xFF299999)
212-
'I' -> Color(0xFFABC023)
213-
'W' -> Color(0xFFBBB529)
214-
'E' -> Color(0xFFFF6B68)
215-
'A' -> Color(0xFFFF6B68)
216-
else -> ElementTheme.colors.textPrimary
218+
private fun String.toColor(colorationMode: ColorationMode): Color {
219+
return when (colorationMode) {
220+
ColorationMode.Logcat -> when (getOrNull(31)) {
221+
'D' -> Color(0xFF299999)
222+
'I' -> Color(0xFFABC023)
223+
'W' -> Color(0xFFBBB529)
224+
'E' -> Color(0xFFFF6B68)
225+
'A' -> Color(0xFFFF6B68)
226+
else -> ElementTheme.colors.textPrimary
227+
}
228+
ColorationMode.Logs -> when (getOrNull(32)) {
229+
'E' -> ElementTheme.colors.textPrimary
230+
'G' -> Color(0xFF299999)
231+
'0' -> Color(0xFFABC023)
232+
'N' -> Color(0xFFBBB529)
233+
'R' -> Color(0xFFFF6B68)
234+
else -> ElementTheme.colors.textPrimary
235+
}
236+
ColorationMode.None -> ElementTheme.colors.textPrimary
217237
}
218238
}
219239

features/viewfolder/impl/src/test/kotlin/io/element/android/features/viewfolder/test/file/ViewFilePresenterTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import app.cash.molecule.RecompositionMode
2020
import app.cash.molecule.moleculeFlow
2121
import app.cash.turbine.test
2222
import com.google.common.truth.Truth.assertThat
23+
import io.element.android.features.viewfolder.impl.file.ColorationMode
2324
import io.element.android.features.viewfolder.impl.file.FileContentReader
2425
import io.element.android.features.viewfolder.impl.file.FileSave
2526
import io.element.android.features.viewfolder.impl.file.FileShare
@@ -48,13 +49,38 @@ class ViewFilePresenterTest {
4849
val initialState = awaitItem()
4950
assertThat(initialState.name).isEqualTo("aName")
5051
assertThat(initialState.lines).isInstanceOf(AsyncData.Loading::class.java)
52+
assertThat(initialState.colorationMode).isEqualTo(ColorationMode.None)
5153
val loadedState = awaitItem()
5254
val lines = (loadedState.lines as AsyncData.Success).data
5355
assertThat(lines.size).isEqualTo(1)
5456
assertThat(lines.first()).isEqualTo("aLine")
5557
}
5658
}
5759

60+
@Test
61+
fun `present - coloration mode for logcat`() = runTest {
62+
val presenter = createPresenter(name = "logcat.log")
63+
moleculeFlow(RecompositionMode.Immediate) {
64+
presenter.present()
65+
}.test {
66+
val initialState = awaitItem()
67+
assertThat(initialState.colorationMode).isEqualTo(ColorationMode.Logcat)
68+
cancelAndConsumeRemainingEvents()
69+
}
70+
}
71+
72+
@Test
73+
fun `present - coloration mode for logs`() = runTest {
74+
val presenter = createPresenter(name = "logs.date")
75+
moleculeFlow(RecompositionMode.Immediate) {
76+
presenter.present()
77+
}.test {
78+
val initialState = awaitItem()
79+
assertThat(initialState.colorationMode).isEqualTo(ColorationMode.Logs)
80+
cancelAndConsumeRemainingEvents()
81+
}
82+
}
83+
5884
@Test
5985
fun `present - share should not have any side effect`() = runTest {
6086
val fileContentReader = FakeFileContentReader().apply {

0 commit comments

Comments
 (0)