Skip to content

Commit 0922ff5

Browse files
committed
Work around crashes caused by executeAsOne() failing
1 parent 50448fb commit 0922ff5

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

app/src/main/java/com/farmerbb/notepad/data/NotepadRepository.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.farmerbb.notepad.data
1717

1818
import com.farmerbb.notepad.Database
1919
import com.farmerbb.notepad.model.CrossRef
20+
import com.farmerbb.notepad.model.Defaults
2021
import com.farmerbb.notepad.model.Note
2122
import com.farmerbb.notepad.model.NoteContents
2223
import com.farmerbb.notepad.model.NoteMetadata
@@ -51,9 +52,9 @@ class NotepadRepository(
5152

5253
fun getNote(id: Long): Note = with(database) {
5354
transactionWithResult {
54-
val metadata = noteMetadataQueries.get(id).executeAsOne()
55-
val crossRef = crossRefQueries.get(metadata.metadataId).executeAsOne()
56-
val contents = noteContentsQueries.get(crossRef.contentsId).executeAsOne()
55+
val metadata = noteMetadataQueries.get(id).executeAsList().lastOrNull() ?: Defaults.metadata
56+
val crossRef = crossRefQueries.get(metadata.metadataId).executeAsList().lastOrNull() ?: Defaults.crossRef
57+
val contents = noteContentsQueries.get(crossRef.contentsId).executeAsList().lastOrNull() ?: Defaults.contents
5758

5859
Note(
5960
metadata = metadata,
@@ -67,13 +68,15 @@ class NotepadRepository(
6768
val crossRefList = crossRefQueries.getMultiple(metadataList.map { it.metadataId }).executeAsList()
6869
val contentsList = noteContentsQueries.getMultiple(crossRefList.map { it.contentsId }).executeAsList()
6970

70-
buildList {
71-
metadataList.sortedBy { it.metadataId }.forEachIndexed { index, metadata ->
72-
Note(
73-
metadata = metadata,
74-
contents = contentsList[index]
75-
).also(::add)
76-
}
71+
crossRefList.map { crossRef ->
72+
Note(
73+
metadata = metadataList.find {
74+
it.metadataId == crossRef.metadataId
75+
} ?: Defaults.metadata,
76+
contents = contentsList.find {
77+
it.contentsId == crossRef.contentsId
78+
} ?: Defaults.contents
79+
)
7780
}
7881
}
7982
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright 2022 Braden Farmer
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.farmerbb.notepad.model
17+
18+
import java.util.Date
19+
20+
object Defaults {
21+
val metadata = NoteMetadata(
22+
metadataId = -1,
23+
title = "",
24+
date = Date(),
25+
hasDraft = false
26+
)
27+
28+
val contents = NoteContents(
29+
contentsId = -1,
30+
text = null,
31+
draftText = null
32+
)
33+
34+
val crossRef = CrossRef(
35+
metadataId = -1,
36+
contentsId = -1
37+
)
38+
}

app/src/main/java/com/farmerbb/notepad/model/Note.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,8 @@ package com.farmerbb.notepad.model
1818
import java.util.Date
1919

2020
data class Note(
21-
val metadata: NoteMetadata = NoteMetadata(
22-
metadataId = -1,
23-
title = "",
24-
date = Date(),
25-
hasDraft = false
26-
),
27-
private val contents: NoteContents = NoteContents(
28-
contentsId = -1,
29-
text = null,
30-
draftText = null
31-
)
21+
val metadata: NoteMetadata = Defaults.metadata,
22+
private val contents: NoteContents = Defaults.contents
3223
) {
3324
val id: Long get() = metadata.metadataId
3425
val text: String get() = contents.text ?: ""

0 commit comments

Comments
 (0)