Compare commits

..

No commits in common. "5eb46dc4e98737494071df74482c1ae8a11bd11f" and "9712f2846f78a19b509ff19957d326d151ef8bf7" have entirely different histories.

4 changed files with 86 additions and 93 deletions

View File

@ -1044,7 +1044,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show()
// TODO: Use Dispatchers.IO // TODO: Use Dispatchers.IO
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
repository.offlineOverride = false
val updatedRemote = repository.updateRemote() val updatedRemote = repository.updateRemote()
if (updatedRemote) { if (updatedRemote) {
// TODO: Send toast messages from the repository // TODO: Send toast messages from the repository

View File

@ -43,7 +43,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
override fun doWork(): Result { override fun doWork(): Result {
val settings = Settings() val settings = Settings()
val periodicRefresh = settings.getBoolean("periodic_refresh", false) val periodicRefresh = settings.getBoolean("periodic_refresh", false)
if (periodicRefresh && repository.isNetworkAvailable()) { if (periodicRefresh) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val notificationManager = val notificationManager =

View File

@ -278,89 +278,84 @@ class ArticleFragment : Fragment(), DIAware {
binding.progressBar.visibility = View.VISIBLE binding.progressBar.visibility = View.VISIBLE
val parser = MercuryApi() val parser = MercuryApi()
if (repository.isNetworkAvailable()) { parser.parseUrl(url).enqueue(
parser.parseUrl(url).enqueue( object : Callback<ParsedContent> {
object : Callback<ParsedContent> { override fun onResponse(
override fun onResponse( call: Call<ParsedContent>,
call: Call<ParsedContent>, response: Response<ParsedContent>
response: Response<ParsedContent> ) {
) { // TODO: clean all the following after finding the mercury content issue
// TODO: clean all the following after finding the mercury content issue try {
try { if (response.body() != null && response.body()!!.content != null && !response.body()!!.content.isNullOrEmpty()) {
if (response.body() != null && response.body()!!.content != null && !response.body()!!.content.isNullOrEmpty()) { try {
binding.titleView.text = response.body()!!.title
if (typeface != null) {
binding.titleView.typeface = typeface
}
try { try {
binding.titleView.text = response.body()!!.title // Note: Mercury may return relative urls... If it does the url val will not be changed.
if (typeface != null) { URL(response.body()!!.url)
binding.titleView.typeface = typeface url = response.body()!!.url
} } catch (e: MalformedURLException) {
// Mercury returned a relative url. We do nothing.
}
} catch (e: Exception) {
}
try {
contentText = response.body()!!.content.orEmpty()
htmlToWebview()
} catch (e: Exception) {
}
try {
if (response.body()!!.lead_image_url != null && !response.body()!!.lead_image_url.isNullOrEmpty() && context != null) {
binding.imageView.visibility = View.VISIBLE
try { try {
// Note: Mercury may return relative urls... If it does the url val will not be changed. Glide
URL(response.body()!!.url) .with(requireContext())
url = response.body()!!.url .asBitmap()
} catch (e: MalformedURLException) { .loadMaybeBasicAuth(config, response.body()!!.lead_image_url.orEmpty())
// Mercury returned a relative url. We do nothing. .apply(RequestOptions.fitCenterTransform())
.into(binding.imageView)
} catch (e: Exception) {
} }
} catch (e: Exception) { } else {
binding.imageView.visibility = View.GONE
} }
} catch (e: Exception) {
try { if (context != null) {
contentText = response.body()!!.content.orEmpty()
htmlToWebview()
} catch (e: Exception) {
}
try {
if (response.body()!!.lead_image_url != null && !response.body()!!.lead_image_url.isNullOrEmpty() && context != null) {
binding.imageView.visibility = View.VISIBLE
try {
Glide
.with(requireContext())
.asBitmap()
.loadMaybeBasicAuth(
config,
response.body()!!.lead_image_url.orEmpty()
)
.apply(RequestOptions.fitCenterTransform())
.into(binding.imageView)
} catch (e: Exception) {
}
} else {
binding.imageView.visibility = View.GONE
}
} catch (e: Exception) {
if (context != null) {
}
}
try {
binding.nestedScrollView.scrollTo(0, 0)
binding.progressBar.visibility = View.GONE
} catch (e: Exception) {
if (context != null) {
}
}
} else {
try {
openInBrowserAfterFailing(customTabsIntent)
} catch (e: Exception) {
if (context != null) {
}
} }
} }
} catch (e: Exception) {
if (context != null) { try {
binding.nestedScrollView.scrollTo(0, 0)
binding.progressBar.visibility = View.GONE
} catch (e: Exception) {
if (context != null) {
}
}
} else {
try {
openInBrowserAfterFailing(customTabsIntent)
} catch (e: Exception) {
if (context != null) {
}
} }
} }
} catch (e: Exception) {
if (context != null) {
}
} }
override fun onFailure(
call: Call<ParsedContent>,
t: Throwable
) = openInBrowserAfterFailing(customTabsIntent)
} }
)
} override fun onFailure(
call: Call<ParsedContent>,
t: Throwable
) = openInBrowserAfterFailing(customTabsIntent)
}
)
} }
private fun htmlToWebview() { private fun htmlToWebview() {

View File

@ -63,7 +63,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> { suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
// TODO: Use the updatedSince parameter // TODO: Use the updatedSince parameter
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
val fetchedItems = api.getItems( val fetchedItems = api.getItems(
displayedItems.type, displayedItems.type,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
@ -84,7 +84,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> { suspend fun getOlderItems(): ArrayList<SelfossModel.Item> {
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
val offset = items.size val offset = items.size
val fetchedItems = api.getItems( val fetchedItems = api.getItems(
displayedItems.type, displayedItems.type,
@ -106,7 +106,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? { suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? {
return if (isNetworkAvailable()) { return if (isConnectionAvailable.value && !offlineOverride) {
api.getItems( api.getItems(
itemType.type, itemType.type,
200, 200,
@ -137,7 +137,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun reloadBadges(): Boolean { suspend fun reloadBadges(): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
val response = api.stats() val response = api.stats()
if (response != null) { if (response != null) {
badgeUnread = response.unread badgeUnread = response.unread
@ -153,7 +153,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun getTags(): List<SelfossModel.Tag>? { suspend fun getTags(): List<SelfossModel.Tag>? {
// TODO: Store in DB // TODO: Store in DB
return if (isNetworkAvailable()) { return if (isConnectionAvailable.value && !offlineOverride) {
api.tags() api.tags()
} else { } else {
// TODO: Compute from database // TODO: Compute from database
@ -163,7 +163,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? { suspend fun getSpouts(): Map<String, SelfossModel.Spout>? {
// TODO: Store in DB // TODO: Store in DB
return if (isNetworkAvailable()) { return if (isConnectionAvailable.value && !offlineOverride) {
api.spouts() api.spouts()
} else { } else {
// TODO: Compute from database // TODO: Compute from database
@ -173,7 +173,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun getSources(): ArrayList<SelfossModel.Source>? { suspend fun getSources(): ArrayList<SelfossModel.Source>? {
// TODO: Store in DB // TODO: Store in DB
return if (isNetworkAvailable()) { return if (isConnectionAvailable.value && !offlineOverride) {
api.sources() api.sources()
} else { } else {
// TODO: Compute from database // TODO: Compute from database
@ -192,7 +192,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun markAsReadById(id: Int): Boolean { suspend fun markAsReadById(id: Int): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.markAsRead(id.toString())?.isSuccess == true success = api.markAsRead(id.toString())?.isSuccess == true
} }
return success return success
@ -209,7 +209,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun unmarkAsReadById(id: Int): Boolean { suspend fun unmarkAsReadById(id: Int): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.unmarkAsRead(id.toString())?.isSuccess == true success = api.unmarkAsRead(id.toString())?.isSuccess == true
} }
return success return success
@ -226,7 +226,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun starrById(id: Int): Boolean { suspend fun starrById(id: Int): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.starr(id.toString())?.isSuccess == true success = api.starr(id.toString())?.isSuccess == true
} }
return success return success
@ -243,7 +243,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun unstarrById(id: Int): Boolean { suspend fun unstarrById(id: Int): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.unstarr(id.toString())?.isSuccess == true success = api.unstarr(id.toString())?.isSuccess == true
} }
return success return success
@ -252,7 +252,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun markAllAsRead(items: ArrayList<SelfossModel.Item>): Boolean { suspend fun markAllAsRead(items: ArrayList<SelfossModel.Item>): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true
} }
@ -304,7 +304,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
filter: String filter: String
): Boolean { ): Boolean {
var response = false var response = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
response = api.createSourceForVersion( response = api.createSourceForVersion(
title, title,
url, url,
@ -321,7 +321,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun deleteSource(id: Int): Boolean { suspend fun deleteSource(id: Int): Boolean {
// TODO: Store in DB // TODO: Store in DB
var success = false var success = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
val response = api.deleteSource(id) val response = api.deleteSource(id)
if (response != null) { if (response != null) {
success = response.isSuccess success = response.isSuccess
@ -333,6 +333,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun updateRemote(): Boolean { suspend fun updateRemote(): Boolean {
var response = false var response = false
offlineOverride = false
if (isConnectionAvailable.value) { if (isConnectionAvailable.value) {
response = api.update()?.isSuccess == true response = api.update()?.isSuccess == true
} }
@ -341,7 +342,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun login(): Boolean { suspend fun login(): Boolean {
var result = false var result = false
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
try { try {
val response = api.login() val response = api.login()
result = response?.isSuccess == true result = response?.isSuccess == true
@ -368,7 +369,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
private suspend fun updateApiVersion() { private suspend fun updateApiVersion() {
apiMajorVersion = settings.getInt("apiVersionMajor", 0) apiMajorVersion = settings.getInt("apiVersionMajor", 0)
if (isNetworkAvailable()) { if (isConnectionAvailable.value && !offlineOverride) {
val fetchedVersion = api.version() val fetchedVersion = api.version()
if (fetchedVersion != null) { if (fetchedVersion != null) {
apiMajorVersion = fetchedVersion.getApiMajorVersion() apiMajorVersion = fetchedVersion.getApiMajorVersion()
@ -377,7 +378,5 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
} }
fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride
// TODO: Handle offline actions // TODO: Handle offline actions
} }