Commit f08675b2 by Paktalin

Split SubmitFragment into Add and Edit Fragments

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