MainActivity.kt 3.46 KB
Newer Older
Paul Bird's avatar
Paul Bird committed
1 2 3 4 5 6 7 8
package tech.essensys.operateapiexample


import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
Paul Bird's avatar
Paul Bird committed
9
import android.util.Log
Paul Bird's avatar
Paul Bird committed
10 11 12 13 14 15 16 17
import android.view.View
import android.view.Menu
import android.view.MenuItem
import android.widget.Button


class MainActivity : AppCompatActivity() {

Paul Bird's avatar
Paul Bird committed
18 19 20 21 22
    private val clientId = ""
    private val clientSecret = ""
    private val userId = ""
    private val userPassword = ""

Paul Bird's avatar
Paul Bird committed
23 24 25 26 27 28 29 30 31 32 33 34
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)

        val fab = findViewById(R.id.fab) as FloatingActionButton
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

Paul Bird's avatar
Paul Bird committed
35 36 37 38 39
        val loginButton = findViewById(R.id.loginButton) as Button
        loginButton.setOnClickListener { login() }

        val refreshAccessTokenButton = findViewById(R.id.refreshAccessTokenButton) as Button
        refreshAccessTokenButton.setOnClickListener { refreshAccessToken() }
Paul Bird's avatar
Paul Bird committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        val getArea1 = findViewById(R.id.getArea1) as Button
        getArea1.setOnClickListener { getArea() }

        val putAccount1 = findViewById(R.id.putAccount1) as Button
        putAccount1.setOnClickListener { putAccount() }

        val deleteAccount1 = findViewById(R.id.deleteAccount1) as Button
        deleteAccount1.setOnClickListener {
            //deleteAccount();
            deleteArea()
        }


    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.itemId


        if (id == R.id.action_settings) {
            return true
        }

        return super.onOptionsItemSelected(item)
    }

    private fun login() {
Paul Bird's avatar
Paul Bird committed
77 78 79 80 81 82 83 84
        Log.d("login","called")
        val authenticate = Authenticate(this)
        authenticate.login(clientId, clientSecret, userId, userPassword)
    }

    private fun refreshAccessToken() {
        val sharedPrefs = SharedPrefs(this)
        val refreshToken = sharedPrefs.refreshToken
Paul Bird's avatar
Paul Bird committed
85
        val authenticate = Authenticate(this)
Paul Bird's avatar
Paul Bird committed
86
        authenticate.refreshAccessToken(clientId, clientSecret, refreshToken)
Paul Bird's avatar
Paul Bird committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }

    private fun getArea() {
        val operateAPI1 = OperateAPI1(this)
        operateAPI1["area", ""]
    }

    private fun putAccount() {
        val operateAPI1 = OperateAPI1(this)
        val data = "data={\"accounts\": [{\"clientname\": \"The Client Name\",\"clienttype\": \"2\",\"enddate\": \"22/10/2013\",\"startdate\": \"22/10/2013\"}]}"
        operateAPI1.put("account", data)
    }

    private fun deleteAccount() {
        val operateAPI1 = OperateAPI1(this)
        operateAPI1.delete("account", 775785)
    }

    private fun deleteArea() {
        val operateAPI1 = OperateAPI1(this)
        operateAPI1.delete("area", 12)
    }

}