Commit e81663b6 by Paktalin

Removed EditWordFragment

parent c4f241ed
package com.paktalin.vocabularynotebook.ui.fragments
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
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.visible
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.submit.*
import kotlinx.android.synthetic.main.word_item.view.*
class EditWordFragment : WordFragment() {
private lateinit var wordPojo: WordPojo
private var container: ViewGroup? = null
override fun cancel() {
stop()
showPreviousViews()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
this.container = container
hidePreviousViews()
wordPojo = arguments!!["wordPojo"] as WordPojo
//(mainActivity.recyclerView.layoutManager as LockableLayoutManager).isScrollEnabled = false
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setWordItemData()
setFocusOnWord()
visible(mainActivity.btnSubmitLayout)
}
private fun setWordItemData() {
word.setText(wordPojo.word)
translation.setText(wordPojo.translation)
}
private fun setFocusOnWord() {
word.requestFocus()
val imm = mainActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
private fun hidePreviousViews() {
gone(container!!.line)
gone(container!!.word)
gone(container!!.translation)
}
private fun showPreviousViews() {
visible(container!!.line)
visible(container!!.word)
visible(container!!.translation)
}
override fun save(word: String, translation: String) {
wordPojo.word = word
wordPojo.translation = translation
mainActivity.vocabularyAdapter.updateWord(wordPojo)
stop()
}
private fun stop() {
// set onClickListener from AddWordFragment
mainActivity.btnSubmitWord.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submit() }
removeFragment(mainActivity.supportFragmentManager, this)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout)
}
companion object {
private val TAG = "VN/" + EditWordFragment::class.java.simpleName
}
}
\ No newline at end of file
......@@ -27,7 +27,7 @@ abstract class WordFragment : Fragment() {
btnClear.setOnClickListener { clearFields() }
}
fun submit() {
private fun submit() {
gone(mainActivity.btnSubmitLayout)
save(word.text.toString(), translation.text.toString())
return
......
package com.paktalin.vocabularynotebook.ui.recycler_view
import android.view.View
import android.widget.TextView
import android.widget.EditText
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
......@@ -9,12 +9,12 @@ import kotlinx.android.synthetic.main.word_item.view.*
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val tvWord: TextView = itemView.word
private val tvTranslation: TextView = itemView.translation
private val tvWord: EditText = itemView.word
private val tvTranslation: EditText = itemView.translation
fun bind(wordPojo: WordPojo, position: Int, isActivated: Boolean, showPopupMenu: (View, Int) -> Unit) {
tvWord.text = wordPojo.word
tvTranslation.text = wordPojo.translation
tvWord.setText(wordPojo.word)
tvTranslation.setText(wordPojo.translation)
itemView.clickable_view.setOnClickListener { showPopupMenu(itemView, position) }
itemView.isActivated = isActivated
}
......
package com.paktalin.vocabularynotebook.ui.recycler_view
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -11,12 +9,15 @@ 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.EditWordFragment
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.gone
import com.paktalin.vocabularynotebook.utils.showKeyboard
import com.paktalin.vocabularynotebook.utils.visible
import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.submit.*
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivity: MainActivity) : RecyclerView.Adapter<ViewHolder>() {
......@@ -66,11 +67,9 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
val popup = PopupMenu(mainActivity, v)
popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) {
deleteWord(vocabulary.displayedAt(position), position)
}
if (it.itemId == R.id.option_edit) {
startEditFragment(v, vocabulary.displayedAt(position))
when (it.itemId) {
R.id.option_delete -> deleteWord(vocabulary.displayedAt(position), position)
R.id.option_edit -> editWord(v, vocabulary.displayedAt(position))
}
true
}
......@@ -95,27 +94,41 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
this.sort()
}
fun updateWord(updatedWord: WordPojo) {
vocabulary.updateWord(updatedWord)
this.sort()
}
private fun sort() {
vocabulary.sort(sort)
this.notifyDataSetChanged()
}
@SuppressLint("ResourceType")
private fun startEditFragment(container: View, wordPojo: WordPojo) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
} else container.id = 18071999
// start EditWordFragment
val arguments = Bundle()
arguments.putSerializable("wordPojo", wordPojo)
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment")
private fun editWord(container: View, wordPojo: WordPojo) {
showPopupMenu = false
gone(container.clickable_view)
container.word.requestFocus()
showKeyboard(mainActivity)
visible(mainActivity.btnSubmitLayout)
mainActivity.btnSubmitWord.setOnClickListener {
vocabulary.updateWord(wordPojo.apply {
wordPojo.word = container.word.text.toString()
wordPojo.translation = container.translation.text.toString()
})
this.sort()
gone(mainActivity.btnSubmitLayout)
container.word.clearFocus()
container.translation.clearFocus()
visible(container.clickable_view)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
showPopupMenu = true
}
mainActivity.btnCancelWord.setOnClickListener {
container.word.setText(wordPojo.word)
container.translation.setText(wordPojo.translation)
gone(mainActivity.btnSubmitLayout)
container.word.clearFocus()
container.translation.clearFocus()
visible(container.clickable_view)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
showPopupMenu = true
}
}
fun filter(query: String) {
......
......@@ -4,7 +4,9 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.paktalin.vocabularynotebook.R
......@@ -78,4 +80,14 @@ fun invisible(view: View) {
fun gone(view: View) {
view.visibility = View.GONE
}
fun showKeyboard(activity: AppCompatActivity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
fun hideKeyboard(activity: AppCompatActivity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, InputMethodManager.RESULT_HIDDEN)
}
\ No newline at end of file
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