Changed ids to items. #29

Merged
AmineB merged 1 commits from id-to-int into master 2022-08-17 14:31:11 +00:00
7 changed files with 54 additions and 38 deletions

View File

@ -1074,7 +1074,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) {
CoroutineScope(Dispatchers.Main).launch {
val success = repository.markAllAsRead(items.map { it.id })
val success = repository.markAllAsRead(items)
if (success) {
Toast.makeText(
this@HomeActivity,
@ -1153,10 +1153,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
actions.forEach { action ->
when {
action.read -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.unread -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.starred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.unstarred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.read -> doAndReportOnFail(repository.markAsReadById(action.articleId.toInt()), action)
action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleId.toInt()), action)
action.starred -> doAndReportOnFail(repository.starrById(action.articleId.toInt()), action)
action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleId.toInt()), action)
}
}
}

View File

@ -112,7 +112,7 @@ class ReaderActivity : AppCompatActivity(), DIAware {
private fun readItem(item: SelfossModel.Item) {
if (markOnScroll) {
CoroutineScope(Dispatchers.IO).launch {
repository.markAsRead(item.id)
repository.markAsRead(item)
// TODO: Handle failure
}
}
@ -207,13 +207,13 @@ class ReaderActivity : AppCompatActivity(), DIAware {
R.id.star -> {
if (allItems[binding.pager.currentItem].starred) {
CoroutineScope(Dispatchers.IO).launch {
repository.unstarr(allItems[binding.pager.currentItem].id)
repository.unstarr(allItems[binding.pager.currentItem])
// TODO: Handle failure
}
afterUnsave()
} else {
CoroutineScope(Dispatchers.IO).launch {
repository.starr(allItems[binding.pager.currentItem].id)
repository.starr(allItems[binding.pager.currentItem])
// TODO: Handle failure
}
afterSave()

View File

@ -113,14 +113,14 @@ class ItemCardAdapter(
if (c.isNetworkAvailable()) {
if (item.starred) {
CoroutineScope(Dispatchers.IO).launch {
repository.unstarr(item.id)
repository.unstarr(item)
// TODO: Handle failure
}
item.starred = false
binding.favButton.isSelected = false
} else {
CoroutineScope(Dispatchers.IO).launch {
repository.starr(item.id)
repository.starr(item)
// TODO: Handle failure
}
item.starred = true

View File

@ -79,7 +79,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
private fun readItemAtIndex(position: Int, showSnackbar: Boolean = true) {
val i = items[position]
CoroutineScope(Dispatchers.IO).launch {
repository.markAsRead(i.id)
repository.markAsRead(i)
}
if (repository.displayedItems == ItemType.UNREAD) {
items.remove(i)
@ -95,7 +95,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
private fun unreadItemAtIndex(position: Int, showSnackbar: Boolean = true) {
CoroutineScope(Dispatchers.IO).launch {
repository.unmarkAsRead(items[position].id)
repository.unmarkAsRead(items[position])
// Todo: SharedItems.unreadItem(app, api, db, items[position])
// TODO: update db

View File

@ -76,19 +76,19 @@ override fun doWork(): Result {
actions.forEach { action ->
when {
action.read -> doAndReportOnFail(
repository.markAsRead(action.articleId.toInt()),
repository.markAsReadById(action.articleId.toInt()),
action
)
action.unread -> doAndReportOnFail(
repository.unmarkAsRead(action.articleId.toInt()),
repository.unmarkAsReadById(action.articleId.toInt()),
action
)
action.starred -> doAndReportOnFail(
repository.starr(action.articleId.toInt()),
repository.starrById(action.articleId.toInt()),
action
)
action.unstarred -> doAndReportOnFail(
repository.unstarr(action.articleId.toInt()),
repository.unstarrById(action.articleId.toInt()),
action
)
}

View File

@ -169,7 +169,7 @@ class ArticleFragment : Fragment(), DIAware {
R.id.unread_action -> if (context != null) {
if (this@ArticleFragment.item.unread) {
CoroutineScope(Dispatchers.IO).launch {
repository.markAsRead(this@ArticleFragment.item.id)
repository.markAsRead(this@ArticleFragment.item)
}
this@ArticleFragment.item.unread = false
Toast.makeText(
@ -179,7 +179,7 @@ class ArticleFragment : Fragment(), DIAware {
).show()
} else {
CoroutineScope(Dispatchers.IO).launch {
repository.unmarkAsRead(this@ArticleFragment.item.id)
repository.unmarkAsRead(this@ArticleFragment.item)
}
this@ArticleFragment.item.unread = true
Toast.makeText(

View File

@ -121,54 +121,70 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
return api.sources()
}
suspend fun markAsRead(id: Int): Boolean {
suspend fun markAsRead(item: SelfossModel.Item): Boolean {
// TODO: Check internet connection
val success = api.markAsRead(id.toString())?.isSuccess == true
val success = markAsReadById(item.id)
if (success) {
markAsReadLocally(items.first {it.id == id})
markAsReadLocally(item)
}
return success
}
suspend fun unmarkAsRead(id: Int): Boolean {
suspend fun markAsReadById(id: Int): Boolean {
// TODO: Check internet connection
val success = api.unmarkAsRead(id.toString())?.isSuccess == true
return api.markAsRead(id.toString())?.isSuccess == true
}
suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean {
val success = unmarkAsReadById(item.id)
if (success) {
unmarkAsReadLocally(items.first {it.id == id})
unmarkAsReadLocally(item)
}
return success
}
suspend fun starr(id: Int): Boolean {
suspend fun unmarkAsReadById(id: Int): Boolean {
// TODO: Check internet connection
return api.unmarkAsRead(id.toString())?.isSuccess == true
}
suspend fun starr(item: SelfossModel.Item): Boolean {
val success = starrById(item.id)
if (success) {
starrLocally(item)
}
return success
}
suspend fun starrById(id: Int): Boolean {
// TODO: Check success, store in DB
val success = api.starr(id.toString())?.isSuccess == true
return api.starr(id.toString())?.isSuccess == true
}
suspend fun unstarr(item: SelfossModel.Item): Boolean {
val success = unstarrById(item.id)
if (success) {
starrLocally(items.first {it.id == id})
unstarrLocally(item)
}
return success
}
suspend fun unstarr(id: Int): Boolean {
suspend fun unstarrById(id: Int): Boolean {
// TODO: Check internet connection
val success = api.unstarr(id.toString())?.isSuccess == true
if (success) {
unstarrLocally(items.first {it.id == id})
}
return success
return api.unstarr(id.toString())?.isSuccess == true
}
suspend fun markAllAsRead(ids: List<Int>): Boolean {
suspend fun markAllAsRead(items: ArrayList<SelfossModel.Item>): Boolean {
// TODO: Check Internet connectivity, store in DB
val success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true
val success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true
if (success) {
val itemsToMark = items.filter { it.id in ids }
for (item in itemsToMark) {
for (item in items) {
markAsReadLocally(item)
}
}