Commit af51e854 by Paktalin

added submit and cancel functionality to TagFragment

parent ef2ddec9
......@@ -32,7 +32,7 @@ class AddWordFragment : WordFragment() {
translationEmpty = translation.text.isEmpty() })
}
override fun cancelEditing() {
override fun cancel() {
clearFields()
gone(mainActivity.btnSubmitLayout)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
......
......@@ -20,7 +20,7 @@ class EditWordFragment : WordFragment() {
private lateinit var wordPojo: WordPojo
private var container: ViewGroup? = null
override fun cancelEditing() {
override fun cancel() {
stop()
showPreviousViews()
}
......@@ -73,7 +73,7 @@ class EditWordFragment : WordFragment() {
private fun stop() {
// set onClickListener from AddWordFragment
mainActivity.btnSubmitWord.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() }
mainActivity.btnSubmitWord.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submit() }
removeFragment(mainActivity.supportFragmentManager, this)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout)
......
......@@ -6,10 +6,41 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.removeTagFragment
import com.paktalin.vocabularynotebook.utils.shortToast
import kotlinx.android.synthetic.main.fragment_tag.*
class TagFragment : Fragment() {
private lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_tag, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
btnSubmitTag.setOnClickListener { submit() }
btnCancelTag.setOnClickListener { cancel() }
}
private fun submit() {
tag_et.text.toString().let {
if (it.isBlank())
shortToast(mainActivity, mainActivity.getString(R.string.toast_tag_cannot_be_empty))
else
mainActivity.vocabularyAdapter.addTagToSelected(it)
}
}
private fun cancel() {
mainActivity.vocabularyAdapter.selectionTracker.clearSelection()
removeTagFragment(mainActivity.supportFragmentManager)
}
companion object {
private val TAG = "VN/" + TagFragment::class.simpleName
}
}
\ No newline at end of file
......@@ -21,13 +21,13 @@ abstract class WordFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.btnSubmitWord.setOnClickListener { submitWord() }
mainActivity.btnCancelWord.setOnClickListener { cancelEditing() }
mainActivity.btnSubmitWord.setOnClickListener { submit() }
mainActivity.btnCancelWord.setOnClickListener { cancel() }
btnClear.setOnClickListener { clearFields() }
}
fun submitWord() {
fun submit() {
gone(mainActivity.btnSubmitLayout)
save(word.text.toString(), translation.text.toString())
return
......@@ -38,6 +38,6 @@ abstract class WordFragment : Fragment() {
translation.text.clear()
}
protected abstract fun cancelEditing()
protected abstract fun cancel()
protected abstract fun save(word:String, translation:String)
}
\ No newline at end of file
......@@ -132,6 +132,10 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
return vocabulary.getModified()
}
fun addTagToSelected(tag: String) {
}
companion object {
private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
}
......
......@@ -37,6 +37,10 @@ class MySelectionTracker {
return false
}
fun clearSelection() {
tracker?.clearSelection()
}
companion object {
private val TAG = "VN/" + MySelectionTracker::class.simpleName
}
......
......@@ -15,12 +15,11 @@ import org.apache.commons.lang3.StringUtils
val progressFragment = ProgressFragment()
val tagFragment = TagFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle? = null, tag: String? = null) {
fragment.arguments = arguments
// remove progressFragment if it exists
if(fragmentManager.findFragmentById(fragment.id) != null)
removeFragment(fragmentManager, fragment)
fragmentManager.beginTransaction().add(containerId, fragment, tag).commitAllowingStateLoss()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arg: Bundle? = null, tag: String? = null) {
if (!fragment.isAdded)
fragmentManager.beginTransaction()
.add(containerId, fragment.apply { arguments = arg }, tag)
.commitAllowingStateLoss()
}
fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) {
......@@ -32,8 +31,7 @@ fun addProgressBar(fragmentManager: FragmentManager, containerId: Int) {
}
fun addTagFragment(fragmentManager: FragmentManager, containerId: Int) {
if (fragmentManager.findFragmentById(tagFragment.id) == null)
addFragment(fragmentManager, tagFragment, containerId)
addFragment(fragmentManager, tagFragment, containerId)
}
fun removeTagFragment(fragmentManager: FragmentManager) {
......@@ -53,7 +51,7 @@ fun fieldsNotEmpty(username: String, password: String, context: Context): Boolea
}
fun validUsername(username: String, context: Context): Boolean {
return if(isEmail(username) ||
return if (isEmail(username) ||
StringUtils.containsOnly(username.toLowerCase(), "abcdefghijklmnopqrstuvwxyz0123456789-_."))
true
else {
......
......@@ -20,7 +20,7 @@
tools:ignore="ContentDescription" />
<AutoCompleteTextView
android:id="@+id/tag"
android:id="@+id/tag_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
......
......@@ -20,6 +20,7 @@
<string name="toast_successful_sign_up">Поздравляем! Теперь вы можете использовать словарь с любого устройства</string>
<string name="toast_enter_username_password">Пожалуйста, введите логин и пароль</string>
<string name="toast_invalid_username">Пожалуйста, придумайте логин, используя символы a–z, цифры 0–9 и символы -_.</string>
<string name="add_tag">Добавить тег</string>
<string name="enter_tag">Добавить тег</string>
<string name="toast_select_words_to_tag">Выберите слова для тега</string>
<string name="toast_tag_cannot_be_empty">Пожалуйста, введите тег</string>
</resources>
\ No newline at end of file
......@@ -13,7 +13,7 @@
<string name="log_out">Log out</string>
<string name="sign_up">Sign Up</string>
<string name="log_in">Log In</string>
<string name="add_tag">Add tag</string>
<string name="enter_tag">Add tag</string>
<!--Toast messages-->
<string name="toast_empty_vocabulary">Your vocabulary is empty. Add your fist word!</string>
......@@ -22,5 +22,5 @@
<string name="toast_enter_username_password">Please, enter username and password</string>
<string name="toast_invalid_username">Please, create a username using letters a–z, numbers 0–9 and characters -_.</string>
<string name="toast_select_words_to_tag">Select words to tag</string>
<string name="enter_tag">Enter tag</string>
<string name="toast_tag_cannot_be_empty">Please, enter the tag</string>
</resources>
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