This should fix #34

This commit is contained in:
Amine 2017-07-02 07:14:29 +02:00
parent dc970bbf3c
commit b2d69be5f8
8 changed files with 60 additions and 27 deletions

View File

@ -37,7 +37,7 @@ class AddSourceActivity : AppCompatActivity() {
var api: SelfossApi? = null
try {
api = SelfossApi(this)
api = SelfossApi(this, this@AddSourceActivity)
} catch (e: IllegalArgumentException) {
mustLoginToAddSource()
}

View File

@ -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) {

View File

@ -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<SuccessResponse> {
private fun preferenceError() {
editor.remove("url")

View File

@ -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<Sources> = ArrayList()
mFab.attachToRecyclerView(mRecyclerView)

View File

@ -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<SuccessResponse> =

View File

@ -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
}
}
}

View File

@ -115,4 +115,5 @@
<string name="drawer_loading">Loading …</string>
<string name="menu_home_search">Zoeken</string>
<string name="can_delete_source">Can\'t delete the source...</string>
<string name="base_url_error">There was an issue when trying to communicate with your Selfoss Instance. If the issue persists, please get in touch with me.</string>
</resources>

View File

@ -117,4 +117,5 @@
<string name="drawer_loading">Loading …</string>
<string name="menu_home_search">Search</string>
<string name="can_delete_source">Can\'t delete the source...</string>
<string name="base_url_error">There was an issue when trying to communicate with your Selfoss Instance. If the issue persists, please get in touch with me.</string>
</resources>