From 7c41a4da70c2aa0ec6ade1c7b3ec0fa74edffbdf Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Sat, 8 Sep 2018 20:18:42 +0200
Subject: Began basic text displaying feature :sparkles:

---
 app/src/main/java/me/texx/Texx/MainActivity.kt | 87 +++++++++++++++++++++-----
 app/src/main/res/layout/content_main.xml       | 14 +++++
 2 files changed, 87 insertions(+), 14 deletions(-)

diff --git a/app/src/main/java/me/texx/Texx/MainActivity.kt b/app/src/main/java/me/texx/Texx/MainActivity.kt
index e621ef8..337ec75 100644
--- a/app/src/main/java/me/texx/Texx/MainActivity.kt
+++ b/app/src/main/java/me/texx/Texx/MainActivity.kt
@@ -1,16 +1,24 @@
 package me.texx.Texx
 
 import android.os.Bundle
+import android.os.StrictMode
 import android.support.v7.app.AppCompatActivity
 import android.view.Menu
 import android.view.MenuItem
+import android.view.ViewGroup
+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
@@ -25,21 +33,11 @@ class MainActivity : AppCompatActivity() {
         dressCodeName = getThemeName(this)
         setContentView(R.layout.activity_main)
         setSupportActionBar(toolbar)
+        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!")
+        displayPosts()
 
-        fab.setOnClickListener { view ->
+        fab.setOnClickListener { _ ->
             startActivity<CameraActivity>()
         }
     }
@@ -69,4 +67,65 @@ class MainActivity : AppCompatActivity() {
             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".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 text: String = (postObject.get("post") as JSONObject).get("text").toString()
+                        val padding = calculatePadding()
+                        val textView = TextView(this)
+                        textView.text = text
+                        textView.setPadding(padding, padding, padding, padding)
+                        textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
+                        post_list.addView(textView)
+                    }
+                    "Media" -> {
+
+                    }
+                }
+            }
+        }
+    }
+
+    private fun calculatePadding(): Int {
+        val paddingDp = 16
+        val screenDensity = this.resources.displayMetrics.density
+        return (paddingDp * screenDensity).toInt()
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml
index c649451..c892763 100644
--- a/app/src/main/res/layout/content_main.xml
+++ b/app/src/main/res/layout/content_main.xml
@@ -8,4 +8,18 @@
     tools:context=".MainActivity"
     tools:showIn="@layout/activity_main">
 
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent">
+
+        <LinearLayout
+            android:id="@+id/post_list"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical" />
+    </ScrollView>
+
 </android.support.constraint.ConstraintLayout>
\ No newline at end of file
-- 
cgit v1.2.3