Commit 0f261e01 by Paktalin

Word editing/addition can be cancelled

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