From b2d69be5f87110d74d3e1e3a3324689fd697087f Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 2 Jul 2017 07:14:29 +0200 Subject: [PATCH] This should fix #34 --- .../bou/readerforselfoss/AddSourceActivity.kt | 2 +- .../bou/readerforselfoss/HomeActivity.kt | 11 ++----- .../bou/readerforselfoss/LoginActivity.kt | 14 ++++++++- .../bou/readerforselfoss/SourcesActivity.kt | 2 +- .../api/selfoss/SelfossApi.kt | 31 +++++++++++++------ .../bou/readerforselfoss/utils/Config.kt | 25 ++++++++++++--- app/src/main/res/values-nl/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 60 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/AddSourceActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/AddSourceActivity.kt index 986730d..ceb3f95 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/AddSourceActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/AddSourceActivity.kt @@ -37,7 +37,7 @@ class AddSourceActivity : AppCompatActivity() { var api: SelfossApi? = null try { - api = SelfossApi(this) + api = SelfossApi(this, this@AddSourceActivity) } catch (e: IllegalArgumentException) { mustLoginToAddSource() } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index 3520fbc..74bf602 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -129,7 +129,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { mCustomTabActivityHelper = CustomTabActivityHelper() - api = SelfossApi(this) + api = SelfossApi(this, this@HomeActivity) items = ArrayList() mBottomBar = findViewById(R.id.bottomBar) as BottomBar @@ -700,14 +700,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { return true } R.id.action_disconnect -> { - editor.remove("url") - editor.remove("login") - editor.remove("password") - editor.apply() - val intent = Intent(this, LoginActivity::class.java) - startActivity(intent) - finish() - return true + return Config.logoutAndRedirect(this, this@HomeActivity, editor) } R.id.action_share_the_app -> { if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/LoginActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/LoginActivity.kt index 45f3d0e..69782a3 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/LoginActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/LoginActivity.kt @@ -56,6 +56,18 @@ class LoginActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) + if (intent.getBooleanExtra("baseUrlFail", false)) { + val alertDialog = AlertDialog.Builder(this).create() + alertDialog.setTitle(getString(R.string.warning_wrong_url)) + alertDialog.setMessage(getString(R.string.base_url_error)) + alertDialog.setButton( + AlertDialog.BUTTON_NEUTRAL, + "OK", + { dialog, _ -> dialog.dismiss() }) + alertDialog.show() + } + + settings = getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) if (settings.getString("url", "").isNotEmpty()) { goToMain() @@ -177,7 +189,7 @@ class LoginActivity : AppCompatActivity() { editor.putString("httpPassword", httpPassword) editor.apply() - val api = SelfossApi(this@LoginActivity) + val api = SelfossApi(this, this@LoginActivity) api.login().enqueue(object : Callback { private fun preferenceError() { editor.remove("url") diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt index bf22461..0a682d5 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/SourcesActivity.kt @@ -30,7 +30,7 @@ class SourcesActivity : AppCompatActivity() { val mFab = findViewById(R.id.fab) as FloatingActionButton val mRecyclerView = findViewById(R.id.activity_sources) as RecyclerView val mLayoutManager = LinearLayoutManager(this) - val api = SelfossApi(this) + val api = SelfossApi(this, this@SourcesActivity) var items: ArrayList = ArrayList() mFab.attachToRecyclerView(mRecyclerView) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index be6e98e..2f1a66c 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -1,6 +1,12 @@ package apps.amine.bou.readerforselfoss.api.selfoss +import android.app.Activity import android.content.Context +import android.content.Intent +import android.support.v7.app.AlertDialog +import android.widget.Toast +import apps.amine.bou.readerforselfoss.LoginActivity +import apps.amine.bou.readerforselfoss.R import java.util.concurrent.ConcurrentHashMap import com.burgstaller.okhttp.AuthenticationCacheInterceptor @@ -20,9 +26,9 @@ import apps.amine.bou.readerforselfoss.utils.Config // codebeat:disable[ARITY,TOO_MANY_FUNCTIONS] -class SelfossApi(c: Context) { +class SelfossApi(c: Context, callingActivity: Activity) { - private val service: SelfossService + private lateinit var service: SelfossService private val config: Config = Config(c) private val userName: String private val password: String @@ -59,14 +65,19 @@ class SelfossApi(c: Context) { .setLenient() .create() - val retrofit = - Retrofit - .Builder() - .baseUrl(config.baseUrl) - .client(authenticator.getHttpClien()) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() - service = retrofit.create(SelfossService::class.java) + + try { + val retrofit = + Retrofit + .Builder() + .baseUrl(config.baseUrl) + .client(authenticator.getHttpClien()) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + service = retrofit.create(SelfossService::class.java) + } catch (e: IllegalArgumentException) { + Config.logoutAndRedirect(c, callingActivity, config.settings.edit(), baseUrlFail = true) + } } fun login(): Call = diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt index 7e9f037..45d4eac 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt @@ -1,16 +1,15 @@ package apps.amine.bou.readerforselfoss.utils +import android.app.Activity import android.content.Context +import android.content.Intent import android.content.SharedPreferences +import apps.amine.bou.readerforselfoss.LoginActivity class Config(c: Context) { - private val settings: SharedPreferences - - init { - this.settings = c.getSharedPreferences(settingsName, Context.MODE_PRIVATE) - } + val settings: SharedPreferences = c.getSharedPreferences(settingsName, Context.MODE_PRIVATE) val baseUrl: String get() = settings.getString("url", "") @@ -27,8 +26,24 @@ class Config(c: Context) { val httpUserPassword: String get() = settings.getString("httpPassword", "") + companion object { val settingsName = "paramsselfoss" + fun logoutAndRedirect(c: Context, + callingActivity: Activity, + editor: SharedPreferences.Editor, + baseUrlFail: Boolean = false): Boolean { + editor.remove("url") + editor.remove("login") + editor.remove("password") + editor.apply() + val intent = Intent(c, LoginActivity::class.java) + if (baseUrlFail) + intent.putExtra("baseUrlFail", baseUrlFail) + c.startActivity(intent) + callingActivity.finish() + return true + } } } diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 8ae1d0d..2fa2a6b 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -115,4 +115,5 @@ Loading … Zoeken Can\'t delete the source... + There was an issue when trying to communicate with your Selfoss Instance. If the issue persists, please get in touch with me. \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 91e3961..d7817fb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,4 +117,5 @@ Loading … Search Can\'t delete the source... + There was an issue when trying to communicate with your Selfoss Instance. If the issue persists, please get in touch with me. \ No newline at end of file