Commit af51e854 by Paktalin

added submit and cancel functionality to TagFragment

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