From 8e2cf1b4a93c0152e9f3d44a77a66fe4d1584dc2 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Wed, 5 Sep 2018 01:01:54 +0200
Subject: Finished adding text on photo feature :sparkles:

---
 .../main/java/me/texx/Texx/PhotoEditorActivity.kt  | 67 +++++++++++++++++++++-
 app/src/main/res/layout/activity_photo_editor.xml  | 15 +++++
 2 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt b/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
index 18f0626..dd3a090 100644
--- a/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
+++ b/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
@@ -1,5 +1,8 @@
 package me.texx.Texx
 
+import android.content.Context
+import android.graphics.Color.RED
+import android.graphics.Typeface
 import android.net.Uri
 import android.os.Bundle
 import android.support.v4.content.ContextCompat
@@ -7,13 +10,18 @@ import android.support.v7.app.AppCompatActivity
 import android.view.View
 import android.view.Window
 import android.view.WindowManager
+import android.view.inputmethod.EditorInfo
+import android.view.inputmethod.InputMethodManager
 import daio.io.dresscode.dressCodeName
 import daio.io.dresscode.matchDressCode
+import ja.burhanrashid52.photoeditor.OnPhotoEditorListener
 import ja.burhanrashid52.photoeditor.PhotoEditor
 import ja.burhanrashid52.photoeditor.PhotoEditorView
+import ja.burhanrashid52.photoeditor.ViewType
 import kotlinx.android.synthetic.main.activity_photo_editor.*
 import me.texx.Texx.util.ThemeUtil.getThemeName
 
+
 /**
  * Activity which will be shown after you've taken a picture
  * Previews the taken picture and posts it if you want
@@ -34,6 +42,10 @@ class PhotoEditorActivity : AppCompatActivity() {
         initPhotoEditor()
     }
 
+    private var currentlyDrawing = false
+    private var currentlyTyping = false
+    private var typingColor = RED
+
     private fun initPhotoEditor() {
         val filepath = intent.getStringExtra("filepath")
         val imageEditorView = findViewById<PhotoEditorView>(R.id.imageEditor)
@@ -41,17 +53,21 @@ class PhotoEditorActivity : AppCompatActivity() {
 
         val photoEditor = PhotoEditor.Builder(this, imageEditorView)
                 .setPinchTextScalable(true)
+                .setDefaultTextTypeface(Typeface.DEFAULT)
                 .build()
 
         setButtonListeners(photoEditor)
     }
 
     private fun setButtonListeners(photoEditor: PhotoEditor) {
-        var currentlyDrawing = false
 
+        // undo button
         undoButton.setOnClickListener { photoEditor.undo() }
+
+        // redo button
         redoButton.setOnClickListener { photoEditor.redo() }
 
+        // draw button
         drawButton.setOnClickListener {
             currentlyDrawing = !currentlyDrawing
             photoEditor.setBrushDrawingMode(currentlyDrawing)
@@ -63,15 +79,62 @@ class PhotoEditorActivity : AppCompatActivity() {
             }
         }
 
+        // type button
         typeButton.setOnClickListener {
-            photoEditor.addText("", 123)
+            currentlyTyping = !currentlyTyping
+            if (currentlyTyping) showTextEditor("")
+            else hideTextEditor()
         }
 
+        // text editing "view" for on photo typing
+        editText.setOnEditorActionListener { _, actionId, _ ->
+            if (actionId == EditorInfo.IME_ACTION_DONE) {
+                photoEditor.addText(editText.text.toString(), typingColor)
+                editText.visibility = View.GONE
+            }
+            return@setOnEditorActionListener true
+        }
+
+        // text editing box on photo (long click)
+        photoEditor.setOnPhotoEditorListener(object : OnPhotoEditorListener {
+            override fun onEditTextChangeListener(rootView: View, text: String, colorCode: Int) {
+                currentlyTyping = !currentlyTyping
+                if (!currentlyTyping) showTextEditor("") // TODO: Fix editing of text
+                else hideTextEditor()
+            }
+
+            override fun onAddViewListener(viewType: ViewType, numberOfAddedViews: Int) {}
+            override fun onRemoveViewListener(numberOfAddedViews: Int) {}
+            override fun onRemoveViewListener(viewType: ViewType, numberOfAddedViews: Int) {}
+            override fun onStartViewChangeListener(viewType: ViewType) {}
+            override fun onStopViewChangeListener(viewType: ViewType) {}
+        })
+
+        // color seekbar
         colorSeekbar.setOnColorChangeListener { _, _, color ->
             if (currentlyDrawing) {
                 photoEditor.brushColor = color
                 drawButton.setBackgroundColor(color)
+            } else if (currentlyTyping) {
+                typingColor = color
+                typeButton.setBackgroundColor(color)
             }
         }
     }
+
+    private fun showTextEditor(text: String) {
+        colorSeekbar.visibility = View.VISIBLE
+        editText.visibility = View.VISIBLE
+        editText.imeOptions = EditorInfo.IME_ACTION_DONE
+        editText.setText(text)
+        editText.requestFocusFromTouch() // set focus
+        val inputManager = this@PhotoEditorActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+        inputManager.showSoftInput(editText, 0)
+    }
+
+    private fun hideTextEditor() {
+        typeButton.background = ContextCompat.getDrawable(this, R.drawable.ic_text_fields_white_24dp)
+        colorSeekbar.visibility = View.GONE
+        editText.visibility = View.GONE
+    }
 }
diff --git a/app/src/main/res/layout/activity_photo_editor.xml b/app/src/main/res/layout/activity_photo_editor.xml
index 2217663..9561214 100644
--- a/app/src/main/res/layout/activity_photo_editor.xml
+++ b/app/src/main/res/layout/activity_photo_editor.xml
@@ -89,4 +89,19 @@
         app:layout_constraintTop_toBottomOf="@+id/drawButton"
         app:thumbHeight="40dp" />
 
+    <EditText
+        android:id="@+id/editText"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="32dp"
+        android:ems="10"
+        android:inputType="text|textAutoCorrect|textAutoComplete|textShortMessage"
+        android:singleLine="true"
+        android:visibility="gone"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintHorizontal_bias="0.5"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
 </android.support.constraint.ConstraintLayout>
\ No newline at end of file
-- 
cgit v1.2.3