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() {
|
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) {
|
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,19 +365,49 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
|||||||
call: Call<List<Item>>,
|
call: Call<List<Item>>,
|
||||||
response: Response<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 {
|
thread {
|
||||||
if (response.body() != null) {
|
if (response.body() != null) {
|
||||||
val apiItems = (response.body() as ArrayList<Item>).filter {
|
val apiItems = (response.body() as ArrayList<Item>).filter {
|
||||||
maybeTagFilter != null || filter(it.tags.tags)
|
maybeTagFilter != null || filter(it.tags.tags)
|
||||||
} as ArrayList<Item>
|
} as ArrayList<Item>
|
||||||
|
if (clearDatabase) {
|
||||||
db.itemsDao().deleteAllItems()
|
db.itemsDao().deleteAllItems()
|
||||||
|
}
|
||||||
db.itemsDao()
|
db.itemsDao()
|
||||||
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
super.onStop()
|
super.onStop()
|
||||||
|
@ -172,6 +172,15 @@ class SelfossApi(
|
|||||||
fun allItems(): Call<List<Item>> =
|
fun allItems(): Call<List<Item>> =
|
||||||
service.allItems(userName, password)
|
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(
|
private fun getItems(
|
||||||
type: String,
|
type: String,
|
||||||
tag: String?,
|
tag: String?,
|
||||||
|
@ -66,7 +66,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
|||||||
sharedPref.getString("api_timeout", "-1")!!.toLong()
|
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) {
|
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||||
Timer("", false).schedule(4000) {
|
Timer("", false).schedule(4000) {
|
||||||
notificationManager.cancel(1)
|
notificationManager.cancel(1)
|
||||||
@ -77,42 +77,38 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
|||||||
call: Call<List<Item>>,
|
call: Call<List<Item>>,
|
||||||
response: Response<List<Item>>
|
response: Response<List<Item>>
|
||||||
) {
|
) {
|
||||||
thread {
|
storeItems(response, true, notifyNewItems, notificationManager)
|
||||||
if (response.body() != null) {
|
|
||||||
val apiItems = (response.body() as ArrayList<Item>)
|
|
||||||
db.itemsDao().deleteAllItems()
|
|
||||||
db.itemsDao()
|
|
||||||
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
|
|
||||||
|
|
||||||
val newSize = apiItems.filter { it.unread }.size
|
|
||||||
if (notifyNewItems && newSize > 0) {
|
|
||||||
|
|
||||||
val intent = Intent(context, MainActivity::class.java).apply {
|
|
||||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
||||||
}
|
|
||||||
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
|
|
||||||
|
|
||||||
val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId)
|
|
||||||
.setContentTitle(context.getString(R.string.new_items_notification_title))
|
|
||||||
.setContentText(context.getString(R.string.new_items_notification_text, newSize))
|
|
||||||
.setPriority(PRIORITY_DEFAULT)
|
|
||||||
.setChannelId(Config.newItemsChannelId)
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.setAutoCancel(true)
|
|
||||||
.setSmallIcon(R.drawable.ic_tab_fiber_new_black_24dp)
|
|
||||||
|
|
||||||
Timer("", false).schedule(4000) {
|
|
||||||
notificationManager.notify(2, newItemsNotification.build())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
apiItems.map {it.preloadImages(context)}
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
api.allReadItems().enqueue(object : Callback<List<Item>> {
|
||||||
|
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||||
Timer("", false).schedule(4000) {
|
Timer("", false).schedule(4000) {
|
||||||
notificationManager.cancel(1)
|
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 {
|
thread {
|
||||||
val actions = db.actionsDao().actions()
|
val actions = db.actionsDao().actions()
|
||||||
|
|
||||||
@ -132,6 +128,46 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
|
|||||||
return Result.success()
|
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 (newItems && notifyNewItems && newSize > 0) {
|
||||||
|
|
||||||
|
val intent = Intent(context, MainActivity::class.java).apply {
|
||||||
|
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||||
|
}
|
||||||
|
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
|
||||||
|
|
||||||
|
val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId)
|
||||||
|
.setContentTitle(context.getString(R.string.new_items_notification_title))
|
||||||
|
.setContentText(context.getString(R.string.new_items_notification_text, newSize))
|
||||||
|
.setPriority(PRIORITY_DEFAULT)
|
||||||
|
.setChannelId(Config.newItemsChannelId)
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setSmallIcon(R.drawable.ic_tab_fiber_new_black_24dp)
|
||||||
|
|
||||||
|
Timer("", false).schedule(4000) {
|
||||||
|
notificationManager.notify(2, newItemsNotification.build())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
apiItems.map { it.preloadImages(context) }
|
||||||
|
}
|
||||||
|
Timer("", false).schedule(4000) {
|
||||||
|
notificationManager.cancel(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun <T> doAndReportOnFail(call: Call<T>, action: ActionEntity) {
|
private fun <T> doAndReportOnFail(call: Call<T>, action: ActionEntity) {
|
||||||
call.enqueue(object : Callback<T> {
|
call.enqueue(object : Callback<T> {
|
||||||
override fun onResponse(
|
override fun onResponse(
|
||||||
|
Loading…
Reference in New Issue
Block a user