Download read and starred items (#331)
* Save read and starred articles during background sync * Use getItems * Cache images of read articles * Remove unused function * Refactor functions
This commit is contained in:
parent
c3148c6744
commit
e6b5ea4e67
@ -357,7 +357,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
}
|
||||
|
||||
private fun getAndStoreAllItems() {
|
||||
api.allItems().enqueue(object : Callback<List<Item>> {
|
||||
api.allNewItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
}
|
||||
|
||||
@ -365,19 +365,49 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
enqueueArticles(response, true)
|
||||
}
|
||||
})
|
||||
|
||||
api.allReadItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
enqueueArticles(response, false)
|
||||
}
|
||||
})
|
||||
|
||||
api.allStarredItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
enqueueArticles(response, false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun enqueueArticles(response: Response<List<Item>>, clearDatabase: Boolean) {
|
||||
thread {
|
||||
if (response.body() != null) {
|
||||
val apiItems = (response.body() as ArrayList<Item>).filter {
|
||||
maybeTagFilter != null || filter(it.tags.tags)
|
||||
} as ArrayList<Item>
|
||||
if (clearDatabase) {
|
||||
db.itemsDao().deleteAllItems()
|
||||
}
|
||||
db.itemsDao()
|
||||
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
@ -172,6 +172,15 @@ class SelfossApi(
|
||||
fun allItems(): Call<List<Item>> =
|
||||
service.allItems(userName, password)
|
||||
|
||||
fun allNewItems(): Call<List<Item>> =
|
||||
getItems("unread", null, null, null, 200, 0)
|
||||
|
||||
fun allReadItems(): Call<List<Item>> =
|
||||
getItems("read", null, null, null, 200, 0)
|
||||
|
||||
fun allStarredItems(): Call<List<Item>> =
|
||||
getItems("read", null, null, null, 200, 0)
|
||||
|
||||
private fun getItems(
|
||||
type: String,
|
||||
tag: String?,
|
||||
|
@ -66,7 +66,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
||||
sharedPref.getString("api_timeout", "-1")!!.toLong()
|
||||
)
|
||||
|
||||
api.allItems().enqueue(object : Callback<List<Item>> {
|
||||
api.allNewItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
Timer("", false).schedule(4000) {
|
||||
notificationManager.cancel(1)
|
||||
@ -77,15 +77,70 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
storeItems(response, true, notifyNewItems, notificationManager)
|
||||
}
|
||||
})
|
||||
api.allReadItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
Timer("", false).schedule(4000) {
|
||||
notificationManager.cancel(1)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
storeItems(response, false, notifyNewItems, notificationManager)
|
||||
}
|
||||
})
|
||||
api.allStarredItems().enqueue(object : Callback<List<Item>> {
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
Timer("", false).schedule(4000) {
|
||||
notificationManager.cancel(1)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
storeItems(response, false, notifyNewItems, notificationManager)
|
||||
}
|
||||
})
|
||||
|
||||
thread {
|
||||
val actions = db.actionsDao().actions()
|
||||
|
||||
actions.forEach { action ->
|
||||
when {
|
||||
action.read -> doAndReportOnFail(api.markItem(action.articleId), action)
|
||||
action.unread -> doAndReportOnFail(api.unmarkItem(action.articleId), action)
|
||||
action.starred -> doAndReportOnFail(api.starrItem(action.articleId), action)
|
||||
action.unstarred -> doAndReportOnFail(
|
||||
api.unstarrItem(action.articleId),
|
||||
action
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun storeItems(response: Response<List<Item>>, newItems: Boolean, notifyNewItems: Boolean, notificationManager: NotificationManager) {
|
||||
thread {
|
||||
if (response.body() != null) {
|
||||
val apiItems = (response.body() as ArrayList<Item>)
|
||||
|
||||
if (newItems) {
|
||||
db.itemsDao().deleteAllItems()
|
||||
}
|
||||
db.itemsDao()
|
||||
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
||||
|
||||
val newSize = apiItems.filter { it.unread }.size
|
||||
if (notifyNewItems && newSize > 0) {
|
||||
if (newItems && notifyNewItems && newSize > 0) {
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
@ -112,25 +167,6 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
thread {
|
||||
val actions = db.actionsDao().actions()
|
||||
|
||||
actions.forEach { action ->
|
||||
when {
|
||||
action.read -> doAndReportOnFail(api.markItem(action.articleId), action)
|
||||
action.unread -> doAndReportOnFail(api.unmarkItem(action.articleId), action)
|
||||
action.starred -> doAndReportOnFail(api.starrItem(action.articleId), action)
|
||||
action.unstarred -> doAndReportOnFail(
|
||||
api.unstarrItem(action.articleId),
|
||||
action
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun <T> doAndReportOnFail(call: Call<T>, action: ActionEntity) {
|
||||
call.enqueue(object : Callback<T> {
|
||||
|
Loading…
Reference in New Issue
Block a user