Skip to content

Commit

Permalink
Let users know if server app is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 7, 2020
1 parent c3590a0 commit 0c35241
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/co/appreactor/news/api/NextcloudNewsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface NextcloudNewsApi {
@GET("feeds")
fun getFeeds(): Call<FeedsPayload>

@GET("feeds")
fun getFeedsRaw(): Call<ResponseBody>

@PUT("feeds/{id}/rename")
fun putFeedRename(
@Path("id") id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ class NextcloudNewsApiAdapter(
}

override suspend fun getNotViewedEntries(): List<Entry> {
val response = api.getUnreadItems().execute()
val response = try {
api.getUnreadItems().execute()
} catch (e: Exception) {
val message = if (e.message == "code < 400: 302") {
"Can not load entries. Make sure you have News app installed on your Nextcloud server."
} else {
e.message
}

throw Exception(message, e)
}

if (!response.isSuccessful) {
throw response.toException()
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/co/appreactor/news/auth/DirectAuthFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.koin.android.ext.koin.androidContext
import org.koin.android.viewmodel.ext.android.viewModel
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import timber.log.Timber

class DirectAuthFragment : Fragment() {

Expand Down Expand Up @@ -65,13 +66,13 @@ class DirectAuthFragment : Fragment() {
lifecycleScope.launchWhenResumed {
progress.isVisible = true

val success = model.isApiAvailable(
serverUrl.text.toString(),
username.text.toString(),
password.text.toString()
)

if (success) {
runCatching {
model.requestFeeds(
serverUrl.text.toString(),
username.text.toString(),
password.text.toString()
)
}.onSuccess {
model.setServer(
serverUrl.text.toString(),
username.text.toString(),
Expand All @@ -89,9 +90,10 @@ class DirectAuthFragment : Fragment() {
popBackStack()
navigate(R.id.entriesFragment)
}
} else {
}.onFailure {
progress.isVisible = false
showDialog(R.string.error, R.string.direct_login_failed)
Timber.e(it)
showDialog(R.string.error, it.message ?: getString(R.string.direct_login_failed))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import co.appreactor.news.common.setServerUrl
import co.appreactor.news.common.setServerUsername
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import okhttp3.MediaType.Companion.toMediaType

class DirectAuthFragmentModel(
private val prefs: Preferences,
) : ViewModel() {

suspend fun isApiAvailable(
suspend fun requestFeeds(
serverUrl: String,
username: String,
password: String
): Boolean {
return try {
val api = DirectNextcloudNewsApiBuilder().build(
serverUrl,
username,
password
)
) {
val api = DirectNextcloudNewsApiBuilder().build(
serverUrl,
username,
password
)

withContext(Dispatchers.IO) {
api.getFeeds().execute().body()!!
}
withContext(Dispatchers.IO) {
val response = api.getFeedsRaw().execute()

true
} catch (e: Exception) {
Timber.e(e)
false
if (!response.isSuccessful) {
throw Exception(response.message())
} else {
if (response.body()?.contentType() == "text/html; charset=UTF-8".toMediaType()) {
throw Exception("Can not fetch data. Make sure News app is also installed on your Nextcloud server.")
}
}
}
}

Expand Down

0 comments on commit 0c35241

Please sign in to comment.