ParseApplication/app/src/main/java/net/adphi/apps/parseapplication/MainActivity.kt

74 lines
2.4 KiB
Kotlin
Raw Normal View History

2018-03-25 11:13:01 +00:00
package net.adphi.apps.parseapplication
2018-03-25 14:30:27 +00:00
import android.app.AlertDialog
2018-03-25 11:13:01 +00:00
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
2018-03-25 13:53:32 +00:00
import android.util.Log
2018-03-25 14:30:27 +00:00
import com.parse.ParseConfig
2018-03-25 13:36:45 +00:00
import com.parse.ParseQuery
import com.parse.ParseUser
import net.adphi.apps.parseapplication.Models.CustomObject
import net.adphi.apps.parseapplication.Utils.LOG
2018-03-25 11:13:01 +00:00
import net.adphi.apps.parseapplication.Utils.getTAG
class MainActivity : AppCompatActivity() {
private val TAG: String = getTAG(this)
2018-03-25 14:30:27 +00:00
private val LAST_VERSION_CODE = "LAST_VERSION_CODE"
private lateinit var config: ParseConfig
2018-03-25 13:36:45 +00:00
private var user: ParseUser? = ParseUser.getCurrentUser()
2018-03-25 11:13:01 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
2018-03-25 13:36:45 +00:00
2018-03-25 14:30:27 +00:00
ParseConfig.getInBackground { config, e ->
if (e != null) {
this.config = ParseConfig.getCurrentConfig()
}
else {
this.config = config
}
val lastVersionCode = this.config.getInt(LAST_VERSION_CODE)
if(BuildConfig.VERSION_CODE < lastVersionCode) {
AlertDialog.Builder(this).setTitle("Update").setMessage("New Version Available").create().show()
}
}
2018-03-25 13:36:45 +00:00
if(user == null) {
ParseUser.logInInBackground("Adphi", "azertest", { user, e ->
if (e != null) {
LOG.err(TAG, "Authentication Error ${e.message!!}")
return@logInInBackground
}
this.user = user
LOG.inf(TAG, "Authentication Succes. User: ${user.username} ")
test()
})
}
else {
LOG.inf(TAG, "Authentication Already Logged. User: ${user!!.username}")
test()
}
}
fun test() {
LOG.v(TAG, "Sending Test Query.")
val query: ParseQuery<CustomObject> = ParseQuery.getQuery(CustomObject::class.java)
query.include("user")
query.findInBackground { objects, e ->
if(e != null) {
LOG.d(TAG, "Query Error: ${e.message}")
return@findInBackground
}
2018-03-25 13:53:32 +00:00
LOG.d(TAG, "Query results count: ${objects.size}")
2018-03-25 13:36:45 +00:00
LOG.d(TAG, "Query results: $objects")
2018-03-25 13:53:32 +00:00
objects.forEach { o -> Log.d(TAG, "CustomObject : $o ${o.user?.username}") }
2018-03-25 13:36:45 +00:00
}
2018-03-25 11:13:01 +00:00
}
}