Skip to content

Commit

Permalink
Merge pull request #2007 from Hafizzle/Fiz/tags-order-update
Browse files Browse the repository at this point in the history
Match web implementation for tags / order tags
  • Loading branch information
phillipthelen committed Jul 7, 2023
2 parents 057ecd7 + e139b37 commit c93fbca
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions Habitica/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
<string name="remove_tasks">Remove</string>
<string name="keep_tasks">Keep</string>
<string name="my_challenges">My Challenges</string>
<string name="challenges">Challenges</string>
<string name="official">Official</string>
<string name="challenge">Challenge</string>
<string name="not_part_of_a_challenge">You’re not part of any Challenges right now!</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open class Tag : RealmObject(), BaseObject {

var userId: String? = null
var name: String = ""
var group: String? = null
internal var challenge: Boolean = false

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.CheckBox
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.widget.AppCompatCheckBox
Expand Down Expand Up @@ -217,6 +218,7 @@ class TaskFormActivity : BaseActivity() {
.map { tagRepository.getUnmanagedCopy(it) }
.collect {
tags = it
sortTagPositions()
setTagViews()
}
}
Expand Down Expand Up @@ -525,18 +527,65 @@ class TaskFormActivity : BaseActivity() {
}
}

private fun sortTagPositions() {
val sortedTagList = arrayListOf<Tag>()
val challengeTagList = arrayListOf<Tag>()
val groupTagList = arrayListOf<Tag>()
val otherTagList = arrayListOf<Tag>()
tags.forEach {
if (it.challenge) {
challengeTagList.add(it)
} else if (it.group != null) {
groupTagList.add(it)
} else {
otherTagList.add(it)
}
}
val challengesTagTitle = Tag().apply {
name = getString(R.string.challenges)
}
val groupsTagTitle = Tag().apply {
name = getString(R.string.groups)
}
val otherTagTitle = Tag().apply {
name = getString(R.string.tags)
}
if (challengeTagList.isNotEmpty()) {
sortedTagList.add(challengesTagTitle)
sortedTagList.addAll(challengeTagList)
}
if (groupTagList.isNotEmpty()) {
sortedTagList.add(groupsTagTitle)
sortedTagList.addAll(groupTagList)
}
if (otherTagList.isNotEmpty()) {
sortedTagList.add(otherTagTitle)
sortedTagList.addAll(otherTagList)
}
tags = sortedTagList
}

private fun setTagViews() {
binding.tagsWrapper.removeAllViews()
val padding = 20.dpToPx(this)
for (tag in tags) {
val view = CheckBox(this)
view.setPadding(padding, view.paddingTop, view.paddingRight, view.paddingBottom)
view.text = tag.name
view.setTextColor(getThemeColor(R.attr.textColorTintedPrimary))
if (preselectedTags?.contains(tag.id) == true) {
view.isChecked = true
tags.forEach { tag ->
if (tag.id.isBlank()) {
// This is a title for a tag group
val view = TextView(this)
view.setPadding(0, view.paddingTop, view.paddingRight, view.paddingBottom)
view.text = tag.name
view.setTextColor(getThemeColor(R.attr.textColorTintedPrimary))
binding.tagsWrapper.addView(view)
} else {
val view = CheckBox(this)
view.setPadding(padding, view.paddingTop, view.paddingRight, view.paddingBottom)
view.text = tag.name
view.setTextColor(getThemeColor(R.attr.textColorTintedPrimary))
if (preselectedTags?.contains(tag.id) == true) {
view.isChecked = true
}
binding.tagsWrapper.addView(view)
}
binding.tagsWrapper.addView(view)
}
setAllTagSelections()
updateTagViewsColors()
Expand Down Expand Up @@ -678,7 +727,7 @@ class TaskFormActivity : BaseActivity() {
thisTask.tags = RealmList()
binding.tagsWrapper.forEachIndexed { index, view ->
val tagView = view as? CheckBox
if (tagView?.isChecked == true) {
if (tagView?.isChecked == true && tags[index].id.isNotBlank()) {
thisTask.tags?.add(tags[index])
}
}
Expand Down

0 comments on commit c93fbca

Please sign in to comment.