aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/me/texx/Texx/MainActivity.kt
blob: 59f0fe0292e5ce6d0f58bf84daf265f396d0cda6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package me.texx.Texx

import android.os.Bundle
import android.os.StrictMode
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import com.github.kittinunf.fuel.android.extension.responseJson
import com.github.kittinunf.fuel.httpGet
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import me.texx.Texx.util.ThemeUtil.getThemeName
import org.jetbrains.anko.alert
import org.jetbrains.anko.longToast
import org.jetbrains.anko.startActivity
import org.json.JSONArray
import org.json.JSONObject

/**
 * Main activity aka home screen of app
 */
class MainActivity : AppCompatActivity() {
    /**
     * Set initial configuration
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        matchDressCode()
        super.onCreate(savedInstanceState)
        dressCodeName = getThemeName(this)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        intentExtraActions()

        displayPosts()

        fab.setOnClickListener { _ ->
            startActivity<CameraActivity>()
        }
    }


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

    /**
     * Handling action bar [item] clicks
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_settings -> {
                startActivity<SettingsActivity>()
                true
            }
            R.id.action_bug_report -> {
                startActivity<BugReportActivity>()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    private fun intentExtraActions() {
        if (intent.getBooleanExtra("serverDown", false)) {
            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") {
                isCancelable = false
                positiveButton("Okay") {
                    finishAndRemoveTask()
                    System.exit(0)
                }
            }.show()
        }

        if (intent.getBooleanExtra("notConnected", false))
            longToast("No internet connection!")
    }

    private fun getPosts(): JSONObject? {
        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)
        val (_, _, result) = "/posts?includes[]=user".httpGet().responseJson()
        val (data, error) = result
        return if (error == null) {
            data?.obj()
        } else {
            longToast("Error fetching posts!")
            null
        }
    }

    private fun displayPosts() {
        val data = getPosts()
        val postsArray = data?.get("posts") as JSONArray?

        postsArray?.let {
            for (i in 0 until postsArray.length()) {
                val postObject = postsArray.get(i) as JSONObject
                val postType = (postObject.get("post_type") as JSONObject).get("type")

                when (postType) {
                    "Text" -> {
                        val postTemplate = LayoutInflater.from(this).inflate(R.layout.template_text_post, null)

                        val postComposer: String = (postObject.get("user") as JSONObject).get("name").toString()
                        val postComposerTextView: TextView = postTemplate.findViewById(R.id.text_post_composer)
                        postComposerTextView.text = postComposer

                        val postDate: String = postObject.get("created_at").toString()
                        val postDateTextView: TextView = postTemplate.findViewById(R.id.text_post_date)
                        postDateTextView.text = postDate

                        val postContent: String = (postObject.get("post") as JSONObject).get("text").toString()
                        val postContentTextView: TextView = postTemplate.findViewById(R.id.text_post_content)
                        postContentTextView.text = postContent

                        post_list.addView(postTemplate)
                    }
                    "Media" -> {

                    }
                }
            }
        }
    }
}