This should fix #34
This commit is contained in:
parent
dc970bbf3c
commit
b2d69be5f8
@ -37,7 +37,7 @@ class AddSourceActivity : AppCompatActivity() {
|
||||
var api: SelfossApi? = null
|
||||
|
||||
try {
|
||||
api = SelfossApi(this)
|
||||
api = SelfossApi(this, this@AddSourceActivity)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
mustLoginToAddSource()
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
|
@ -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> =
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
@ -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>
|
Loading…
Reference in New Issue
Block a user