Skip to content

Commit

Permalink
samuelclay#1922 Cursor management
Browse files Browse the repository at this point in the history
  • Loading branch information
sictiru committed Jan 2, 2025
1 parent 057e78e commit 80a016c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newsblur.activity;

import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -21,15 +20,15 @@
import com.newsblur.widget.WidgetUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

abstract public class FeedChooser extends NbActivity {

protected FeedChooserAdapter adapter;
protected ArrayList<Feed> feeds;
protected ArrayList<Folder> folders;
protected List<Feed> feeds;
protected List<Folder> folders;
protected Map<String, Feed> feedMap = new HashMap<>();
protected ArrayList<String> folderNames = new ArrayList<>();
protected ArrayList<ArrayList<Feed>> folderChildren = new ArrayList<>();
Expand All @@ -39,7 +38,7 @@ abstract public class FeedChooser extends NbActivity {

abstract void setupList();

abstract void processFeeds(Cursor cursor);
abstract void processFeeds(List<Feed> feeds);

abstract void processData();

Expand Down Expand Up @@ -147,7 +146,7 @@ protected void setAdapterData() {
}

private void setupObservers() {
feedFolderViewModel.getFoldersLiveData().observe(this, this::processFolders);
feedFolderViewModel.getFolders().observe(this, this::processFolders);
feedFolderViewModel.getFeedsLiveData().observe(this, this::processFeeds);
}

Expand Down Expand Up @@ -176,16 +175,9 @@ private void loadData() {
feedFolderViewModel.getData();
}

private void processFolders(Cursor cursor) {
ArrayList<Folder> folders = new ArrayList<>();
while (cursor != null && cursor.moveToNext()) {
Folder folder = Folder.fromCursor(cursor);
if (!folder.feedIds.isEmpty()) {
folders.add(folder);
}
}
private void processFolders(List<Folder> folders) {
this.folders = folders;
Collections.sort(this.folders, (o1, o2) -> Folder.compareFolderNames(o1.flatName(), o2.flatName()));
this.folders.sort((o1, o2) -> Folder.compareFolderNames(o1.flatName(), o2.flatName()));
processData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
Expand Down Expand Up @@ -84,11 +85,8 @@ void setupList() {
}

@Override
void processFeeds(Cursor cursor) {
ArrayList<Feed> feeds = new ArrayList<>();
while (cursor != null && cursor.moveToNext()) {
Feed feed = Feed.fromCursor(cursor);
feeds.add(feed);
void processFeeds(List<Feed> feeds) {
for (Feed feed : feeds) {
feedMap.put(feed.feedId, feed);
}
this.feeds = feeds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
Expand Down Expand Up @@ -87,16 +88,15 @@ void setupList() {
}

@Override
void processFeeds(Cursor cursor) {
ArrayList<Feed> feeds = new ArrayList<>();
while (cursor != null && cursor.moveToNext()) {
Feed feed = Feed.fromCursor(cursor);
void processFeeds(List<Feed> feeds) {
List<Feed> activeFeeds = new ArrayList<>();
for (Feed feed : feeds) {
if (feed.active) {
feeds.add(feed);
activeFeeds.add(feed);
feedMap.put(feed.feedId, feed);
}
}
this.feeds = feeds;
this.feeds = activeFeeds;
processData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class StoryUserTagsFragment : DialogFragment(), TagsAdapter.OnTagClickListener {
fs = requireArguments().getSerializable("feed_set") as FeedSet

storyUserTagsViewModel.savedStoryCountsLiveData.observe(this) {
setCursor(it)
setStarredCount(it)
}

binding.textAddNewTag.setOnClickListener {
Expand Down Expand Up @@ -127,17 +127,8 @@ class StoryUserTagsFragment : DialogFragment(), TagsAdapter.OnTagClickListener {
return builder.create()
}

private fun setCursor(cursor: Cursor) {
if (!cursor.isBeforeFirst) return
val starredTags = ArrayList<StarredCount>()
while (cursor.moveToNext()) {
val sc = StarredCount.fromCursor(cursor)
if (sc.tag != null && !sc.isTotalCount) {
starredTags.add(sc)
}
}
Collections.sort(starredTags, StarredCount.StarredCountComparatorByTag)
setTags(starredTags)
private fun setStarredCount(starredCount: List<StarredCount>) {
setTags(starredCount)
}

private fun processNewTag(newTag: StarredCount) {
Expand Down Expand Up @@ -168,7 +159,7 @@ class StoryUserTagsFragment : DialogFragment(), TagsAdapter.OnTagClickListener {
binding.inputTagName.text.clear()
}

private fun setTags(starredTags: ArrayList<StarredCount>) {
private fun setTags(starredTags: List<StarredCount>) {
otherTags.clear()
starredTags.forEach { otherTags[it.tag] = it }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.newsblur.viewModel

import android.database.Cursor
import android.os.CancellationSignal
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.newsblur.database.BlurDatabaseHelper
import com.newsblur.domain.Feed
import com.newsblur.domain.Folder
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -18,30 +19,39 @@ class FeedFolderViewModel

private val cancellationSignal = CancellationSignal()

private val _folders = MutableLiveData<Cursor>()
val foldersLiveData: LiveData<Cursor> = _folders
private val _feeds = MutableLiveData<Cursor>()
val feedsLiveData: LiveData<Cursor> = _feeds
private val _folders = MutableLiveData<List<Folder>>()
val folders: LiveData<List<Folder>> = _folders
private val _feeds = MutableLiveData<List<Feed>>()
val feedsLiveData: LiveData<List<Feed>> = _feeds

fun getData() {
viewModelScope.launch(Dispatchers.IO) {
launch {
dbHelper.getFoldersCursor(cancellationSignal).let {
_folders.postValue(it)
}
}
launch {
dbHelper.getFeedsCursor(cancellationSignal).let {
_feeds.postValue(it)
dbHelper.getFoldersCursor(cancellationSignal).use { cursor ->
val folders = mutableListOf<Folder>()
while (cursor.moveToNext()) {
val folder = Folder.fromCursor(cursor)
if (folder.feedIds.isNotEmpty()) {
folders.add(folder)
}
}
_folders.postValue(folders)
}
}

getFeeds()
}
}

fun getFeeds() {
viewModelScope.launch(Dispatchers.IO) {
dbHelper.getFeedsCursor(cancellationSignal).let {
_feeds.postValue(it)
dbHelper.getFeedsCursor(cancellationSignal).use { cursor ->
val feeds = mutableListOf<Feed>()
while (cursor.moveToNext()) {
val feed = Feed.fromCursor(cursor)
feeds.add(feed)
}
_feeds.postValue(feeds)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class NotificationsViewModel

private fun loadFeeds() {
viewModelScope.launch(Dispatchers.IO) {
val cursor = dbHelper.getFeedsCursor(cancellationSignal)
val feeds = extractFeeds(cursor).filterValues(notificationFeedFilter)
_feeds.emit(feeds)
dbHelper.getFeedsCursor(cancellationSignal).use { cursor ->
val feeds = extractFeeds(cursor).filterValues(notificationFeedFilter)
_feeds.emit(feeds)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class StoriesViewModel
fun getActiveStories(fs: FeedSet, cursorFilters: CursorFilters) {
viewModelScope.launch(Dispatchers.IO) {
try {
// TODO cursor management
dbHelper.getActiveStoriesCursor(fs, cursorFilters, cancellationSignal).let {
_activeStoriesLiveData.postValue(it)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package com.newsblur.viewModel

import android.database.Cursor
import android.os.CancellationSignal
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.newsblur.database.BlurDatabaseHelper
import com.newsblur.domain.StarredCount
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Collections
import javax.inject.Inject

@HiltViewModel
class StoryUserTagsViewModel
@Inject constructor(private val dbHelper: BlurDatabaseHelper): ViewModel() {
@Inject constructor(private val dbHelper: BlurDatabaseHelper) : ViewModel() {

private val cancellationSignal = CancellationSignal()
private val _savedStoryCountsLiveData = MutableLiveData<Cursor>()
val savedStoryCountsLiveData: LiveData<Cursor> = _savedStoryCountsLiveData
private val _savedStoryCountsLiveData = MutableLiveData<List<StarredCount>>()
val savedStoryCountsLiveData: LiveData<List<StarredCount>> = _savedStoryCountsLiveData

fun getSavedStoryCounts() {
viewModelScope.launch(Dispatchers.IO) {
val cursor = dbHelper.getSavedStoryCountsCursor(cancellationSignal)
_savedStoryCountsLiveData.postValue(cursor)
dbHelper.getSavedStoryCountsCursor(cancellationSignal).use { cursor ->
if (!cursor.isBeforeFirst) return@use
val starredTags = mutableListOf<StarredCount>()
while (cursor.moveToNext()) {
val sc = StarredCount.fromCursor(cursor)
if (sc.tag != null && !sc.isTotalCount) {
starredTags.add(sc)
}
}
Collections.sort(starredTags, StarredCount.StarredCountComparatorByTag)
_savedStoryCountsLiveData.postValue(starredTags)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,12 @@ class WidgetRemoteViewsFactory(context: Context, intent: Intent) : RemoteViewsFa
private fun processStories(stories: Array<Story>) = storiesLock.withLock {
Log.d(this.javaClass.name, "processStories")
val feedMap = mutableMapOf<String, Feed>()
val cursor = dbHelper.getFeedsCursor(cancellationSignal)
while (cursor != null && cursor.moveToNext()) {
val feed = Feed.fromCursor(cursor)
if (feed.active) {
feedMap[feed.feedId] = feed
dbHelper.getFeedsCursor(cancellationSignal).use { cursor ->
while (cursor.moveToNext()) {
val feed = Feed.fromCursor(cursor)
if (feed.active) {
feedMap[feed.feedId] = feed
}
}
}

Expand Down

0 comments on commit 80a016c

Please sign in to comment.