Skip to content

Commit db5a155

Browse files
committed
Fix initial sync
1 parent 76a3204 commit db5a155

7 files changed

Lines changed: 87 additions & 70 deletions

File tree

app/src/main/kotlin/org/vestifeed/api/miniflux/MinifluxApiAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class MinifluxApiAdapter(
6767
runCatching {
6868
var totalFetched = 0L
6969
val currentBatch = mutableSetOf<EntryJson>()
70-
val batchSize = 5L
70+
val batchSize = 10L
7171
var oldestEntryId = Long.MAX_VALUE
7272

7373
while (true) {

app/src/main/kotlin/org/vestifeed/db/Database.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,39 @@ class EntryQueries(private val conn: SQLiteConnection) {
181181
}
182182

183183
fun selectByReadAndBookmarked(
184-
extRead: List<Boolean>,
184+
extRead: Boolean,
185185
extBookmarked: Boolean
186186
): List<EntriesAdapterRow> {
187-
val readValues = extRead.map { if (it) 1 else 0 }
188-
val placeholders = readValues.joinToString(",") { "?" }
189187
val query = """
190188
SELECT e.*, f.title as feed_title, f.ext_show_preview_images, f.ext_open_entries_in_browser
191189
FROM entry e
192190
JOIN feed f ON f.id = e.feed_id
193-
WHERE e.ext_read IN ($placeholders) AND e.ext_bookmarked = ?
191+
WHERE e.ext_read = ? AND e.ext_bookmarked = ?
194192
ORDER BY e.published DESC
195-
LIMIT 500
196193
""".trimIndent()
197194

198195
val res = mutableListOf<EntriesAdapterRow>()
199196
val stmt = conn.prepare(query)
200-
readValues.forEachIndexed { index, value ->
201-
stmt.bindInt(index + 1, value)
197+
stmt.bindInt(1, if (extRead) 1 else 0)
198+
stmt.bindInt(2, if (extBookmarked) 1 else 0)
199+
while (stmt.step()) {
200+
res.add(statementToEntriesAdapterRow(stmt))
202201
}
203-
stmt.bindInt(1 + readValues.size, if (extBookmarked) 1 else 0)
202+
stmt.close()
203+
return res
204+
}
205+
206+
fun selectBookmarked(): List<EntriesAdapterRow> {
207+
val query = """
208+
SELECT e.*, f.title as feed_title, f.ext_show_preview_images, f.ext_open_entries_in_browser
209+
FROM entry e
210+
JOIN feed f ON f.id = e.feed_id
211+
WHERE e.ext_bookmarked = 1
212+
ORDER BY e.published DESC
213+
""".trimIndent()
214+
215+
val res = mutableListOf<EntriesAdapterRow>()
216+
val stmt = conn.prepare(query)
204217
while (stmt.step()) {
205218
res.add(statementToEntriesAdapterRow(stmt))
206219
}

app/src/main/kotlin/org/vestifeed/entries/EntriesFragment.kt

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
2626
import kotlinx.coroutines.flow.update
2727
import kotlinx.coroutines.launch
2828
import org.vestifeed.R
29-
import org.vestifeed.anim.animateVisibilityChanges
30-
import org.vestifeed.anim.showSmooth
3129
import org.vestifeed.app.db
3230
import org.vestifeed.app.sync
3331
import org.vestifeed.db.Conf
@@ -62,7 +60,6 @@ class EntriesFragment : AppFragment() {
6260
data class ShowingCachedEntries(
6361
val feed: Feed?,
6462
val entries: List<EntriesAdapter.Item>,
65-
val showBackgroundProgress: Boolean,
6663
) : State()
6764
}
6865

@@ -142,29 +139,35 @@ class EntriesFragment : AppFragment() {
142139
// }
143140

144141
private fun updateState(conf: Conf, syncState: Sync.State) {
142+
binding.swipeRefresh.isRefreshing = false
143+
145144
when (syncState) {
146145
is Sync.State.InitialSync -> state.update { State.InitialSync(syncState.message) }
147146

148-
else -> {
149-
val showBgProgress = when (syncState) {
150-
is Sync.State.FollowUpSync -> syncState.args.syncEntries
151-
else -> false
152-
}
147+
is Sync.State.FollowUpSync -> {
148+
binding.swipeRefresh.isRefreshing = true
149+
}
153150

151+
else -> {
154152
val rows: List<EntriesAdapterRow> = if (filter is EntriesFilter.BelongToFeed) {
155153
db().entry.selectByFeedIdAndReadAndBookmarked(
156154
feedId = (filter as EntriesFilter.BelongToFeed).feedId,
157155
extRead = if (conf.showReadEntries) listOf(true, false) else listOf(false),
158156
extBookmarked = false,
159157
)
160158
} else {
161-
val includeRead = (conf.showReadEntries || filter is EntriesFilter.Bookmarked)
162-
val includeBookmarked = filter is EntriesFilter.Bookmarked
163-
164-
db().entry.selectByReadAndBookmarked(
165-
extRead = if (includeRead) listOf(true, false) else listOf(false),
166-
extBookmarked = includeBookmarked,
167-
)
159+
if (filter is EntriesFilter.Bookmarked) {
160+
db().entry.selectBookmarked()
161+
} else {
162+
val includeRead =
163+
(conf.showReadEntries || filter is EntriesFilter.Bookmarked)
164+
val includeBookmarked = filter is EntriesFilter.Bookmarked
165+
166+
db().entry.selectByReadAndBookmarked(
167+
extRead = includeRead,
168+
extBookmarked = includeBookmarked,
169+
)
170+
}
168171
}
169172

170173
val sortedRows = when (conf.sortOrder) {
@@ -181,7 +184,6 @@ class EntriesFragment : AppFragment() {
181184
null
182185
},
183186
entries = sortedRows.map { it.toItem(conf) },
184-
showBackgroundProgress = showBgProgress,
185187
)
186188
}
187189
}
@@ -229,8 +231,6 @@ class EntriesFragment : AppFragment() {
229231
syncEntries = false,
230232
)
231233
)
232-
233-
// refresh()
234234
}
235235
}
236236

@@ -249,8 +249,6 @@ class EntriesFragment : AppFragment() {
249249
syncEntries = false,
250250
)
251251
)
252-
253-
// refresh()
254252
}
255253
}
256254

@@ -344,43 +342,63 @@ class EntriesFragment : AppFragment() {
344342
private fun FragmentEntriesBinding.setState(state: State) {
345343
Log.d("entries_fragment", state.toString())
346344

347-
animateVisibilityChanges(
348-
views = listOf(toolbar, progress, message, retry, swipeRefresh),
349-
visibleViews = when (state) {
350-
is State.InitialSync -> listOf(toolbar, progress)
351-
is State.LoadingCachedEntries -> listOf(toolbar, progress)
352-
is State.ShowingCachedEntries -> listOf(toolbar, swipeRefresh)
353-
},
354-
)
355-
356345
updateToolbar(state)
357346

347+
progress.isVisible = false
348+
message.isVisible = false
349+
swipeRefresh.isVisible = false
350+
358351
when (state) {
359352
is State.InitialSync -> {
360-
if (state.message.isNotEmpty()) {
361-
message.text = state.message
362-
}
353+
progress.isVisible = true
354+
message.isVisible = state.message.isNotBlank()
355+
message.text = state.message
363356
}
364357

365-
State.LoadingCachedEntries -> {}
358+
State.LoadingCachedEntries -> {
359+
progress.isVisible = true
360+
}
366361

367362
is State.ShowingCachedEntries -> {
368-
swipeRefresh.isRefreshing = state.showBackgroundProgress
363+
Log.d("entries_fragment", "cached entries: ${state.entries.size}")
369364

370365
if (state.entries.isEmpty()) {
366+
message.isVisible = true
371367
message.text = getEmptyMessage()
368+
} else {
369+
swipeRefresh.isVisible = true
370+
adapter.submitList(state.entries)
372371
}
373-
374-
adapter.submitList(state.entries)
375372
}
376373
}
377374
}
378375

379376
private fun updateToolbar(state: State) {
380377
binding.toolbar.apply {
381378
when (filter) {
382-
EntriesFilter.Bookmarked -> setTitle(R.string.bookmarks)
383-
EntriesFilter.Unread -> setTitle(R.string.unread)
379+
EntriesFilter.Bookmarked -> {
380+
when (state) {
381+
is State.ShowingCachedEntries -> {
382+
setTitle(getString(R.string.bookmarks) + " (${state.entries.size})")
383+
}
384+
385+
else -> {
386+
setTitle(R.string.bookmarks)
387+
}
388+
}
389+
}
390+
391+
EntriesFilter.Unread -> {
392+
when (state) {
393+
is State.ShowingCachedEntries -> {
394+
setTitle(getString(R.string.unread) + " (${state.entries.size})")
395+
}
396+
397+
else -> {
398+
setTitle(R.string.unread)
399+
}
400+
}
401+
}
384402

385403
is EntriesFilter.BelongToFeed -> {
386404
binding.toolbar.apply {
@@ -499,7 +517,7 @@ class EntriesFragment : AppFragment() {
499517

500518
override fun onOpenGraphImageDownloaded() {
501519
super.onOpenGraphImageDownloaded()
502-
// refresh()
520+
updateState(db().conf.select(), sync().state.value)
503521
}
504522

505523
private fun getShowReadEntriesButtonVisibility(): Boolean {
@@ -536,9 +554,10 @@ class EntriesFragment : AppFragment() {
536554
}
537555

538556
private fun onListItemClick(item: EntriesAdapter.Item) {
539-
setRead(
540-
entryIds = listOf(item.id),
541-
read = true,
557+
db().entry.updateReadAndReadSynced(
558+
id = item.id,
559+
extRead = true,
560+
extReadSynced = false,
542561
)
543562

544563
if (item.openInBrowser) {

app/src/main/kotlin/org/vestifeed/navigation/AppFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class AppFragment : Fragment() {
4040
.build()
4141

4242
while (true) {
43-
val imagesFetched = fetchEntryImages(httpClient, 5)
43+
val imagesFetched = fetchEntryImages(httpClient, 1)
4444

4545
if (imagesFetched.isNotEmpty()) {
4646
onOpenGraphImageDownloaded()

app/src/main/kotlin/org/vestifeed/sync/SyncWorker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SyncWorker(context: Context, workerParams: WorkerParameters) : Worker(cont
3434
runCatching {
3535
val unreadEntries =
3636
applicationContext.db().entry.selectByReadAndBookmarked(
37-
extRead = listOf(false),
37+
extRead = false,
3838
extBookmarked = false,
3939
).size
4040

app/src/main/res/layout/fragment_entries.xml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
android:id="@+id/progress"
1919
android:layout_width="wrap_content"
2020
android:layout_height="wrap_content"
21-
android:alpha="0"
2221
app:layout_constraintBottom_toTopOf="@id/message"
2322
app:layout_constraintEnd_toEndOf="parent"
2423
app:layout_constraintStart_toStartOf="parent"
@@ -29,37 +28,23 @@
2928
android:id="@+id/message"
3029
android:layout_width="wrap_content"
3130
android:layout_height="wrap_content"
32-
android:alpha="0"
3331
android:gravity="center"
3432
android:lineSpacingMultiplier="1.4"
3533
android:paddingStart="40dp"
3634
android:paddingTop="16dp"
3735
android:paddingEnd="40dp"
3836
android:paddingBottom="16dp"
3937
android:textAppearance="?textAppearanceBody1"
40-
app:layout_constraintBottom_toTopOf="@id/retry"
38+
app:layout_constraintBottom_toBottomOf="parent"
4139
app:layout_constraintEnd_toEndOf="parent"
4240
app:layout_constraintStart_toStartOf="parent"
4341
app:layout_constraintTop_toBottomOf="@id/progress"
4442
tools:text="Progress\nMessage" />
4543

46-
<Button
47-
android:id="@+id/retry"
48-
style="?materialButtonOutlinedStyle"
49-
android:layout_width="wrap_content"
50-
android:layout_height="wrap_content"
51-
android:alpha="0"
52-
android:text="@string/retry"
53-
app:layout_constraintBottom_toBottomOf="parent"
54-
app:layout_constraintEnd_toEndOf="parent"
55-
app:layout_constraintStart_toStartOf="parent"
56-
app:layout_constraintTop_toBottomOf="@id/message" />
57-
5844
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
5945
android:id="@+id/swipeRefresh"
6046
android:layout_width="match_parent"
6147
android:layout_height="0dp"
62-
android:alpha="0"
6348
app:layout_constraintBottom_toBottomOf="parent"
6449
app:layout_constraintTop_toBottomOf="@id/toolbar">
6550

app/src/test/kotlin/org.vestifeed/db/EntryQueriesTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class EntryQueriesTest {
5252
assertEquals(
5353
all.filter { !it.extRead && !it.extBookmarked }.map { it.id },
5454
db.entry.selectByReadAndBookmarked(
55-
extRead = listOf(false),
55+
extRead = false,
5656
extBookmarked = false
5757
).map { it.id },
5858
)

0 commit comments

Comments
 (0)