Commit f08675b2 by Paktalin

Split SubmitFragment into Add and Edit Fragments

parent 08e99dc1
......@@ -11,6 +11,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.fragments.SubmitAddedFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
......@@ -19,6 +20,7 @@ import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.word_item.*
class MainActivity : AppCompatActivity() {
......@@ -33,6 +35,7 @@ class MainActivity : AppCompatActivity() {
recyclerView.layoutManager = LockableLayoutManager(this@MainActivity)
setUpSwipeRefresh()
setUpNavigationView()
setUpAddWordTouchListeners()
hideKeyboard()
}
......@@ -96,6 +99,17 @@ class MainActivity : AppCompatActivity() {
{ removeProgressBar(supportFragmentManager) }, this)
}
private fun setUpAddWordTouchListeners() {
word.setOnTouchListener { _, _ ->
addSubmitFragment(supportFragmentManager, SubmitAddedFragment(), R.id.main_activity_container)
false
}
translation.setOnTouchListener { _, _ ->
addSubmitFragment(supportFragmentManager, SubmitAddedFragment(), R.id.main_activity_container)
false
}
}
private fun hideKeyboard() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
......
package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone
import kotlinx.android.synthetic.main.submit.*
import kotlinx.android.synthetic.main.word_item.*
open class AddWordFragment : Fragment() {
private lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.word_item, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
/*mainActivity.btnSubmitWord.setOnClickListener { submit() }
mainActivity.btnCancelWord.setOnClickListener { cancel() }
word.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout);false }
translation.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout); false }*/
}
private fun submit() {
finish()
mainActivity.vocabularyAdapter.addWord(WordPojo(word.text.toString(), translation.text.toString()))
this.word.requestFocus()
return
}
private fun cancel() {
finish()
mainActivity.hideKeyboardNotFromActivity(mainActivity)
}
private fun finish() {
word.text.clear()
translation.text.clear()
gone(mainActivity.btnSubmitLayout)
}
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui.fragments
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.removeFragment
import kotlinx.android.synthetic.main.word_item.*
class SubmitAddedFragment : SubmitFragment() {
override fun init() {
mainActivity.vocabularyAdapter.showPopupMenu = false
wordEt = mainActivity.word
translationEt = mainActivity.translation
}
override fun submit() {
mainActivity.vocabularyAdapter.addWord(WordPojo(wordEt.text.toString(), translationEt.text.toString()))
wordEt.requestFocus()
finish()
return
}
override fun cancel() {
mainActivity.hideKeyboardNotFromActivity(mainActivity)
finish()
}
override fun finish() {
wordEt.text.clear()
translationEt.text.clear()
mainActivity.vocabularyAdapter.showPopupMenu = true
removeFragment(mainActivity.supportFragmentManager, this)
return
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui.fragments
import android.view.View
import android.widget.EditText
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.showKeyboard
import com.paktalin.vocabularynotebook.utils.visible
class SubmitEditedFragment : SubmitFragment() {
private lateinit var clickableView: View
private lateinit var wordPojo: WordPojo
override fun init() {
mainActivity.vocabularyAdapter.showPopupMenu = false
// visible(mainActivity.clickable_view)
gone(clickableView)
wordEt.requestFocus()
showKeyboard(mainActivity)
}
override fun submit() {
wordPojo.word = wordEt.text.toString()
wordPojo.translation = translationEt.text.toString()
mainActivity.vocabularyAdapter.updateWord(wordPojo)
finish()
}
override fun cancel() {
wordEt.setText(wordPojo.word)
translationEt.setText(wordPojo.translation)
finish()
}
override fun finish() {
wordEt.clearFocus()
translationEt.clearFocus()
visible(clickableView)
// gone(mainActivity.clickable_view)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
mainActivity.vocabularyAdapter.showPopupMenu = true
removeFragment(mainActivity.supportFragmentManager, this)
return
}
fun setData(wordEt: EditText, translationEt: EditText, wordPojo: WordPojo, clickableView: View) {
this.wordEt = wordEt
this.translationEt = translationEt
this.wordPojo = wordPojo
this.clickableView = clickableView
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui.fragments
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
......@@ -7,23 +8,19 @@ import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.showKeyboard
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.submit.*
class SubmitFragment : Fragment() {
private lateinit var mainActivity: MainActivity
abstract class SubmitFragment : Fragment() {
private lateinit var wordEt: EditText
private lateinit var translationEt: EditText
private var clickableView: View? = null
protected lateinit var mainActivity: MainActivity
protected lateinit var wordEt: EditText
protected lateinit var translationEt: EditText
private lateinit var wordPojo: WordPojo
private var edit: Boolean = false
override fun onAttach(context: Context?) {
super.onAttach(context)
instances++
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.submit, container, false)
......@@ -34,55 +31,21 @@ class SubmitFragment : Fragment() {
mainActivity = activity as MainActivity
btnSubmitWord.setOnClickListener { submit() }
btnCancelWord.setOnClickListener { cancel() }
if (edit) {
mainActivity.vocabularyAdapter.showPopupMenu = false
clickableView?.let { gone(it) }
wordEt.requestFocus()
showKeyboard(mainActivity)
}
init()
}
private fun submit() {
wordPojo.word = wordEt.text.toString()
wordPojo.translation = translationEt.text.toString()
mainActivity.vocabularyAdapter.updateWord(wordPojo)
finish()
}
private fun cancel() {
wordEt.setText(wordPojo.word)
translationEt.setText(wordPojo.translation)
finish()
}
private fun finish() {
if (edit) {
wordEt.clearFocus()
translationEt.clearFocus()
clickableView?.let { visible(it) }
mainActivity.hideKeyboardNotFromActivity(mainActivity)
mainActivity.vocabularyAdapter.showPopupMenu = true
} else
wordEt.requestFocus()
removeFragment(mainActivity.supportFragmentManager, this)
return
}
abstract fun finish()
abstract fun init()
abstract fun submit()
abstract fun cancel()
fun setData(wordEt: EditText,
translationEt: EditText,
edit: Boolean,
wordPojo: WordPojo? = null,
clickableView: View? = null) {
this.wordEt = wordEt
this.translationEt = translationEt
this.edit = edit
this.wordPojo = wordPojo ?: WordPojo("", "")
this.clickableView = clickableView
override fun onDetach() {
super.onDetach()
instances--
}
companion object {
private val TAG = "VN/" + SubmitFragment::class.java.simpleName
var instances: Int = 0
}
}
\ No newline at end of file
......@@ -9,10 +9,10 @@ import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.fragments.SubmitFragment
import com.paktalin.vocabularynotebook.ui.fragments.SubmitEditedFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.addFragment
import com.paktalin.vocabularynotebook.utils.addSubmitFragment
import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.word_item.view.*
......@@ -104,9 +104,9 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
@SuppressLint("ResourceType")
private fun editWord(container: View, wordPojo: WordPojo) {
addFragment(mainActivity.supportFragmentManager,
SubmitFragment().apply {
setData(container.word, container.translation, true, wordPojo, container.clickable_view)
addSubmitFragment(mainActivity.supportFragmentManager,
SubmitEditedFragment().apply {
setData(container.word, container.translation, wordPojo, container.clickable_view)
},
R.id.main_activity_container)
}
......
......@@ -11,6 +11,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import com.paktalin.vocabularynotebook.ui.fragments.SubmitFragment
import com.paktalin.vocabularynotebook.ui.fragments.TagFragment
import org.apache.commons.lang3.StringUtils
......@@ -36,6 +37,10 @@ fun addTagFragment(fragmentManager: FragmentManager, containerId: Int) {
addFragment(fragmentManager, tagFragment, containerId)
}
fun addSubmitFragment(fragmentManager: FragmentManager, fragment: SubmitFragment, containerId: Int) {
if (SubmitFragment.instances == 0) addFragment(fragmentManager, fragment, containerId)
}
fun removeTagFragment(fragmentManager: FragmentManager) {
removeFragment(fragmentManager, tagFragment)
}
......
......@@ -12,9 +12,8 @@
android:layout_margin="@dimen/small_margin"
android:orientation="vertical">
<fragment
android:id="@+id/fragmentAddWord"
android:name="com.paktalin.vocabularynotebook.ui.fragments.AddWordFragment"
<include
layout="@layout/word_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/small_margin"
......@@ -42,6 +41,4 @@
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
<!--<include layout="@layout/submit" />-->
</RelativeLayout>
\ No newline at end of file
......@@ -72,7 +72,7 @@
android:id="@+id/clickable_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:background="@android:color/holo_green_dark"
android:clickable="true"
android:focusable="true"
android:visibility="gone" />
......
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