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

74 lines
2.4 KiB
Kotlin

package net.adphi.apps.parseapplication
import android.app.AlertDialog
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.parse.ParseConfig
import com.parse.ParseQuery
import com.parse.ParseUser
import net.adphi.apps.parseapplication.Models.CustomObject
import net.adphi.apps.parseapplication.Utils.LOG
import net.adphi.apps.parseapplication.Utils.getTAG
class MainActivity : AppCompatActivity() {
private val TAG: String = getTAG(this)
private val LAST_VERSION_CODE = "LAST_VERSION_CODE"
private lateinit var config: ParseConfig
private var user: ParseUser? = ParseUser.getCurrentUser()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
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()
}
}
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
}
LOG.d(TAG, "Query results count: ${objects.size}")
LOG.d(TAG, "Query results: $objects")
objects.forEach { o -> Log.d(TAG, "CustomObject : $o ${o.user?.username}") }
}
}
}