aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main
diff options
context:
space:
mode:
authorMarvin Borner2018-08-23 19:20:11 +0200
committerMarvin Borner2018-08-23 19:20:11 +0200
commit10b5d2691d948022e277be055e3bbe87ff33efdf (patch)
treed8685d5a6f2e5bc031df9a4292c6fff3596da4e8 /app/src/main
parent35c5af9d71cbeff5403c32754c4cf8a37399543f (diff)
Added anko library and improved log in processing
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/AndroidManifest.xml4
-rw-r--r--app/src/main/java/com/no_name/no_name/InitialActivity.kt38
-rw-r--r--app/src/main/java/com/no_name/no_name/LoginActivity.kt15
-rw-r--r--app/src/main/java/com/no_name/no_name/MainActivity.kt17
-rw-r--r--app/src/main/res/values/strings.xml1
5 files changed, 52 insertions, 23 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 29c7f56..1ce29c3 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -17,7 +17,9 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
- <activity android:name=".InitialActivity">
+ <activity
+ android:name=".InitialActivity"
+ android:label="@string/loading">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
diff --git a/app/src/main/java/com/no_name/no_name/InitialActivity.kt b/app/src/main/java/com/no_name/no_name/InitialActivity.kt
index 9e7f27c..ee25d3d 100644
--- a/app/src/main/java/com/no_name/no_name/InitialActivity.kt
+++ b/app/src/main/java/com/no_name/no_name/InitialActivity.kt
@@ -1,39 +1,55 @@
package com.no_name.no_name
-import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.github.kittinunf.fuel.android.extension.responseJson
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.httpGet
import com.madapps.prefrences.EasyPrefrences
+import org.jetbrains.anko.alert
+import org.jetbrains.anko.startActivity
+import java.io.IOException
/**
* Activity which will be run before any other to verify user and choose which activity
* should be started next
*/
class InitialActivity : AppCompatActivity() {
+ private val server_address = "192.168.0.102"
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
- FuelManager.instance.basePath = "http://192.168.0.59"
+ FuelManager.instance.basePath = "http://$server_address"
+ alert("Logging you in.", "Loading...") {
+ isCancelable = false
+ }.show()
verifyLogin()
}
+ @Throws(InterruptedException::class, IOException::class)
+ fun isConnected(): Boolean {
+ val command = "ping -c 1 google.com"
+ return Runtime.getRuntime().exec(command).waitFor() == 0
+ }
+
private fun verifyLogin() {
val accessToken: String? = SecureStorage(this@InitialActivity).get("access_token")
- var intent = Intent(this@InitialActivity, LoginActivity::class.java)
-
- if (accessToken != null) { // TODO: Check if user has internet connection, if not -> show MA without verification
+ // synced function of fuel doesn't work here (#331) -> ugly workaround
+ if (accessToken != null) {
val userID = EasyPrefrences(this@InitialActivity).getString("user_id")
- "/users/$userID".httpGet() // synced function of fuel doesn't work here (#331) -> ugly workaround
+ "/users/$userID".httpGet() // verify by making request to user api
.header("Authorization" to "Bearer $accessToken")
- .responseJson { _, _, result ->
- val (_, error) = result
- if (error == null) intent = Intent(this@InitialActivity, MainActivity::class.java)
- startActivity(intent)
+ .responseJson { _, response, result ->
+ val (_, serverError) = result
+ when {
+ response.httpStatusCode == 200 -> startActivity<MainActivity>()
+ !isConnected() -> startActivity<MainActivity>("notConnected" to true)
+ serverError != null -> startActivity<MainActivity>("serverDown" to true)
+ else -> startActivity<LoginActivity>()
+ }
}
} else {
- startActivity(intent)
+ startActivity<LoginActivity>()
}
}
} \ No newline at end of file
diff --git a/app/src/main/java/com/no_name/no_name/LoginActivity.kt b/app/src/main/java/com/no_name/no_name/LoginActivity.kt
index 78289b4..3076e39 100644
--- a/app/src/main/java/com/no_name/no_name/LoginActivity.kt
+++ b/app/src/main/java/com/no_name/no_name/LoginActivity.kt
@@ -6,7 +6,6 @@ import android.animation.AnimatorListenerAdapter
import android.annotation.TargetApi
import android.app.LoaderManager.LoaderCallbacks
import android.content.CursorLoader
-import android.content.Intent
import android.content.Loader
import android.content.pm.PackageManager
import android.database.Cursor
@@ -18,16 +17,16 @@ import android.provider.ContactsContract
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
-import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import android.widget.TextView
-import android.widget.Toast
import com.github.kittinunf.fuel.android.extension.responseJson
import com.github.kittinunf.fuel.httpPost
import com.madapps.prefrences.EasyPrefrences
import kotlinx.android.synthetic.main.activity_login.*
+import org.jetbrains.anko.longToast
+import org.jetbrains.anko.startActivity
import org.json.JSONObject
import java.util.*
@@ -95,7 +94,6 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
}
}
-
/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
@@ -289,13 +287,8 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
showProgress(false)
if (success!!) {
- val intent = Intent(this@LoginActivity, MainActivity::class.java)
- //intent.putExtra("keyIdentifier", value)
- startActivity(intent)
-
- val toast = Toast.makeText(this@LoginActivity, "Successfully logged in.", Toast.LENGTH_LONG)
- toast.setGravity(Gravity.CENTER, 0, 0)
- toast.show()
+ startActivity<MainActivity>()
+ longToast("Successfully logged in.")
} else {
password.error = getString(R.string.error_incorrect_password)
password.requestFocus()
diff --git a/app/src/main/java/com/no_name/no_name/MainActivity.kt b/app/src/main/java/com/no_name/no_name/MainActivity.kt
index f44e873..b7e61cd 100644
--- a/app/src/main/java/com/no_name/no_name/MainActivity.kt
+++ b/app/src/main/java/com/no_name/no_name/MainActivity.kt
@@ -5,6 +5,8 @@ import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
+import org.jetbrains.anko.alert
+import org.jetbrains.anko.longToast
/**
* Main activity aka home screen of app
@@ -18,6 +20,21 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
+ val serverDown = intent.getBooleanExtra("serverDown", false)
+ if (serverDown) {
+ alert("We are sorry, but our servers do not seem to be working at the moment. Please wait a few minutes before you try again.", "Sorry") {
+ positiveButton("Okay") {
+ finishAffinity() // TODO: Loading activity will somehow still be opened after close
+ System.exit(0)
+ }
+ }.show()
+ }
+
+ val notConnected = intent.getBooleanExtra("notConnected", false)
+ if (notConnected) {
+ longToast("No internet connection!")
+ }
+
fab.setOnClickListener { view ->
// TODO: Set FAB onclick event
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index e25ea27..4840b69 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -15,4 +15,5 @@
<string name="permission_rationale">"Contacts permissions are needed for providing email
completions."
</string>
+ <string name="loading">Loading...</string>
</resources>