Compare commits

...

2 Commits

Author SHA1 Message Date
9712f2846f Show a message when the network connection is lost 2022-08-17 21:17:50 +02:00
c361819664 Update todo comments 2022-08-17 20:26:02 +02:00
2 changed files with 22 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.view.doOnNextLayout import androidx.core.view.doOnNextLayout
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.* import androidx.recyclerview.widget.*
import androidx.room.Room import androidx.room.Room
import androidx.work.Constraints import androidx.work.Constraints
@ -178,6 +179,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
dataBase = AndroidDeviceDatabase(applicationContext) dataBase = AndroidDeviceDatabase(applicationContext)
lifecycleScope.launch {
repository.toastMessageState.collect {
Toast.makeText(baseContext, it, Toast.LENGTH_SHORT).show()
}
}
handleBottomBar() handleBottomBar()
handleDrawer() handleDrawer()

View File

@ -10,6 +10,9 @@ import com.russhwolf.settings.Settings
import io.github.aakira.napier.Napier import io.github.aakira.napier.Napier
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, connectivityStatus: ConnectivityStatus) { class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, connectivityStatus: ConnectivityStatus) {
@ -18,6 +21,9 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
var items = ArrayList<SelfossModel.Item>() var items = ArrayList<SelfossModel.Item>()
private val isConnectionAvailable = connectivityStatus.isNetworkConnected private val isConnectionAvailable = connectivityStatus.isNetworkConnected
private val _toastMessageState = MutableSharedFlow<String>(0)
val toastMessageState = _toastMessageState.asSharedFlow()
var baseUrl = apiDetails.getBaseUrl() var baseUrl = apiDetails.getBaseUrl()
lateinit var dateUtils: DateUtils lateinit var dateUtils: DateUtils
@ -45,6 +51,13 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
updateApiVersion() updateApiVersion()
dateUtils = DateUtils(apiMajorVersion) dateUtils = DateUtils(apiMajorVersion)
reloadBadges() reloadBadges()
isConnectionAvailable.asStateFlow().collect { connectionAvailable ->
if (!connectionAvailable) {
// TODO: Localize this string
_toastMessageState.emit("Network connection lost")
}
}
} }
} }
@ -65,7 +78,6 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
items = ArrayList(fetchedItems) items = ArrayList(fetchedItems)
} }
} else { } else {
// TODO: Provide an error message if the connection is not available.
// TODO: Get items from the database // TODO: Get items from the database
} }
return items return items
@ -88,7 +100,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
appendItems(fetchedItems) appendItems(fetchedItems)
} }
} else { } else {
// TODO: Provide an error message // TODO: Get items from the database
} }
return items return items
} }
@ -105,6 +117,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
null null
) )
} else { } else {
// TODO: Provide an error message
null null
} }
} }
@ -186,7 +199,6 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean { suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean {
// TODO: Check internet connection
val success = unmarkAsReadById(item.id) val success = unmarkAsReadById(item.id)
if (success) { if (success) {
@ -196,7 +208,6 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun unmarkAsReadById(id: Int): Boolean { suspend fun unmarkAsReadById(id: Int): Boolean {
// TODO: Check internet connection
var success = false var success = false
if (isConnectionAvailable.value && !offlineOverride) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.unmarkAsRead(id.toString())?.isSuccess == true success = api.unmarkAsRead(id.toString())?.isSuccess == true