Commit 0f261e01 by Paktalin

Word editing/addition can be cancelled

parent 15af5219
......@@ -8,6 +8,7 @@ android {
targetSdkVersion 28
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
......@@ -30,6 +31,7 @@ dependencies {
implementation 'com.android.support:support-annotations:28.0.0-rc02'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
......
package com.paktalin.vocabularynotebook.appsetup;
import android.app.Application;
import android.support.multidex.MultiDexApplication;
import com.firebase.client.Firebase;
public class VocabularyApplication extends Application {
public class VocabularyApplication extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
......
package com.paktalin.vocabularynotebook.ui.fragments
import android.view.View
import android.os.Bundle
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.shortToast
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
class AddWordFragment : WordFragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
word.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout); false }
translation.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout); false }
}
override fun cancelEditing() {
clearFields()
gone(mainActivity.btnSubmitLayout)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
}
override fun saveToFirestore(word:String, translation:String, vocabularyId: String) {
val wordPojo = WordItem.Pojo(word, translation, null)
FirestoreManager().saveNewWord(
......@@ -17,26 +31,22 @@ class AddWordFragment : WordFragment() {
wordPojo, vocabularyId)
}
private fun onSuccessfulSave(wordPojo: WordItem.Pojo, vocabularyId: String, documentId: String) {
clearFields()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, documentId, vocabularyId)
updateRecycleView(wordItem)
}
override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.addWord(wordItem)
}
override fun updateButtons() {
super.updateButtons()
if (!wordEmpty || !translationEmpty) showClearButton()
if (wordEmpty && translationEmpty) hideClearButton()
if (!wordEmpty || !translationEmpty) visible(btnClear)
if (wordEmpty && translationEmpty) invisible(btnClear)
}
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun onSuccessfulSave(wordPojo: WordItem.Pojo, vocabularyId: String, documentId: String) {
clearFields()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, documentId, vocabularyId)
updateRecycleView(wordItem)
}
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
}
\ No newline at end of file
......@@ -17,11 +17,18 @@ import kotlinx.android.synthetic.main.word_item.view.*
class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem
private var container: ViewGroup? = null
override fun cancelEditing() {
stop()
showPreviousViews()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
mainActivity.inEditMode = true
hidePreviousViews(container)
this.container = container
hidePreviousViews()
wordItem = arguments!!["wordItem"] as WordItem
return super.onCreateView(inflater, container, savedInstanceState)
}
......@@ -31,6 +38,11 @@ class EditWordFragment : WordFragment() {
setWordItemData()
disableScrolling(mainActivity)
setFocusOnWord()
visible(mainActivity.btnSubmitLayout)
}
override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.updateWord(wordItem)
}
private fun setWordItemData() {
......@@ -45,15 +57,19 @@ class EditWordFragment : WordFragment() {
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
private fun hidePreviousViews(container: ViewGroup?) {
if (container != null) {
container.line.visibility = View.GONE
container.word.visibility = View.GONE
container.translation.visibility = View.GONE
}
private fun hidePreviousViews() {
gone(container!!.line)
gone(container!!.word)
gone(container!!.translation)
}
override fun saveToFirestore(word:String, translation:String, vocabularyId: String) {
private fun showPreviousViews() {
visible(container!!.line)
visible(container!!.word)
visible(container!!.translation)
}
override fun saveToFirestore(word: String, translation: String, vocabularyId: String) {
val wordPojo = WordItem.Pojo(word, translation, wordItem.pojo.time)
FirestoreManager().updateWord(
{ onSuccessfulSave(wordPojo) },
......@@ -62,7 +78,7 @@ class EditWordFragment : WordFragment() {
}
private fun onSuccessfulSave(wordPojo: WordItem.Pojo) {
hideSubmitButton()
gone(mainActivity.btnSubmitLayout)
mainActivity.removeProgressBar()
wordItem.pojo = wordPojo
updateRecycleView(wordItem)
......@@ -77,15 +93,13 @@ class EditWordFragment : WordFragment() {
private fun stop() {
// set onClickListener from AddWordFragment
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() }
enableScrolling(mainActivity)
removeFragment(mainActivity.supportFragmentManager, this)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout)
enableScrolling(mainActivity)
mainActivity.inEditMode = false
}
override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.updateWord(wordItem)
}
companion object {
private val TAG = "VN/" + EditWordFragment::class.java.simpleName
}
......
......@@ -11,6 +11,8 @@ import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.content_main.*
......@@ -31,6 +33,7 @@ abstract class WordFragment : Fragment() {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.btnSubmit.setOnClickListener { submitWord() }
mainActivity.btnCancel.setOnClickListener { cancelEditing() }
word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() })
......@@ -41,8 +44,8 @@ abstract class WordFragment : Fragment() {
}
open fun updateButtons() {
if (!wordEmpty && !translationEmpty) showSubmitButton()
if (wordEmpty || translationEmpty) hideSubmitButton()
if (!wordEmpty && !translationEmpty) visible(mainActivity.btnSubmit)
if (wordEmpty || translationEmpty) gone(mainActivity.btnSubmit)
}
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
......@@ -53,14 +56,9 @@ abstract class WordFragment : Fragment() {
}
}
private fun showSubmitButton() {
mainActivity.btnSubmitLayout.visibility = View.VISIBLE }
protected fun hideSubmitButton() {
mainActivity.btnSubmitLayout.visibility = View.GONE }
fun submitWord() {
mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout)
val word = word.text.toString()
val translation = translation.text.toString()
......@@ -75,6 +73,7 @@ abstract class WordFragment : Fragment() {
translation.text.clear()
}
protected abstract fun cancelEditing()
protected abstract fun saveToFirestore(word:String, translation:String, vocabularyId:String)
protected abstract fun updateRecycleView(wordItem: WordItem)
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
......@@ -52,4 +53,16 @@ fun shortToast(context: Context, text: String) {
fun startActivity(context: Context, cls: Class<*>) {
context.startActivity(Intent(context, cls))
}
fun visible(view: View) {
view.visibility = View.VISIBLE
}
fun invisible(view: View) {
view.visibility = View.INVISIBLE
}
fun gone(view: View) {
view.visibility = View.GONE
}
\ No newline at end of file
......@@ -70,6 +70,7 @@
app:layout_constraintBottom_toBottomOf="parent">
<ImageButton
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_close_icon"
......@@ -90,6 +91,7 @@
android:layout_gravity="center_vertical"
android:layout_margin="@dimen/small_margin"
android:background="@android:color/transparent"
android:visibility="gone"
app:srcCompat="@drawable/ic_done_icon"
tools:ignore="ContentDescription" />
</LinearLayout>
......
......@@ -3,6 +3,7 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editable_word_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment