Skip to content

Commit

Permalink
courses: enable collections view (fixes #3195) (#3196)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Mar 6, 2024
1 parent 4ae4588 commit 54a9341
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 144 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1394
versionName "0.13.94"
versionCode 1395
versionName "0.13.95"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
21 changes: 8 additions & 13 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ open class RealmTag : RealmObject() {
private fun setAttachedTo(attachedTo: JsonArray) {
this.attachedTo = RealmList()
for (i in 0 until attachedTo.size()) {
this.attachedTo!!.add(JsonUtils.getString(attachedTo, i))
this.attachedTo?.add(JsonUtils.getString(attachedTo, i))
}
isAttached = this.attachedTo!!.size > 0
isAttached = (this.attachedTo?.size ?: 0) > 0
}

override fun toString(): String {
Expand All @@ -56,6 +56,7 @@ open class RealmTag : RealmObject() {
return map
}

@JvmStatic
fun insert(mRealm: Realm, act: JsonObject) {
var tag =
mRealm.where(RealmTag::class.java).equalTo("_id", JsonUtils.getString("_id", act)).findFirst()
Expand All @@ -73,23 +74,17 @@ open class RealmTag : RealmObject() {
if (el != null && el.isJsonArray) {
tag.setAttachedTo(JsonUtils.getJsonArray("attachedTo", act))
} else {
tag.attachedTo!!.add(JsonUtils.getString("attachedTo", act))
tag.attachedTo?.add(JsonUtils.getString("attachedTo", act))
}
tag.isAttached = tag.attachedTo!!.size > 0
tag.isAttached = (tag.attachedTo?.size ?: 0) > 0
}


}

@JvmStatic
fun getTagsArray(list: List<RealmTag?>?): JsonArray {
fun getTagsArray(list: List<RealmTag>): JsonArray {
val array = JsonArray()
if (list != null) {
for (t in list) {
if (t != null) {
array.add(t._id)
}
}
for (t in list) {
array.add(t._id)
}
return array
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ole.planet.myplanet.ui.library

import android.R
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
Expand All @@ -9,43 +8,39 @@ import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.fragment.app.DialogFragment
import io.realm.Realm
import io.realm.RealmResults
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.callback.TagClickListener
import org.ole.planet.myplanet.databinding.FragmentCollectionsBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmTag
import org.ole.planet.myplanet.ui.library.TagExpandableAdapter.OnClickTagItem
import org.ole.planet.myplanet.utilities.KeyboardUtils.hideSoftKeyboard
import java.util.Locale
import org.ole.planet.myplanet.utilities.KeyboardUtils
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.List
import io.realm.Realm

class CollectionsFragment : DialogFragment(), OnClickTagItem, CompoundButton.OnCheckedChangeListener {
private var fragmentCollectionsBinding: FragmentCollectionsBinding? = null
var mRealm: Realm? = null
var list: List<RealmTag>? = null
private var filteredList: MutableList<RealmTag>? = null
var adapter: TagExpandableAdapter? = null
class CollectionsFragment : DialogFragment(), TagExpandableAdapter.OnClickTagItem, CompoundButton.OnCheckedChangeListener {
private lateinit var fragmentCollectionsBinding: FragmentCollectionsBinding
private lateinit var mRealm: Realm
private lateinit var list: List<RealmTag>
private var filteredList: ArrayList<RealmTag> = ArrayList()
private lateinit var adapter: TagExpandableAdapter
private var dbType: String? = null
private var tagListener: TagClickListener? = null
private var selectedItemsList: MutableList<RealmTag> = ArrayList()

fun setListener(listener: TagClickListener?) {
this.tagListener = listener
}
private var listener: TagClickListener? = null
private var selectedItemsList: ArrayList<RealmTag> = ArrayList()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth)
if (arguments != null) dbType = requireArguments().getString("dbType")
setStyle(STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth)
dbType = arguments?.getString("dbType")
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentCollectionsBinding = FragmentCollectionsBinding.inflate(inflater, container, false)
mRealm = DatabaseService(requireActivity()).realmInstance
filteredList = ArrayList()
hideSoftKeyboard(requireActivity())
return fragmentCollectionsBinding!!.root
KeyboardUtils.hideSoftKeyboard(requireActivity())
return fragmentCollectionsBinding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -55,65 +50,61 @@ class CollectionsFragment : DialogFragment(), OnClickTagItem, CompoundButton.OnC
}

private fun setListeners() {
fragmentCollectionsBinding!!.btnOk.setOnClickListener {
if (tagListener != null) {
tagListener!!.onOkClicked(selectedItemsList)
dismiss()
}
fragmentCollectionsBinding.btnOk.setOnClickListener {
listener?.onOkClicked(selectedItemsList)
dismiss()
}
fragmentCollectionsBinding!!.etFilter.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
filterTags(charSequence.toString())
fragmentCollectionsBinding.etFilter.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(charSequence: CharSequence?, start: Int, before: Int, count: Int) {
charSequence?.let { filterTags(it.toString()) }
}

override fun afterTextChanged(editable: Editable) {}
override fun afterTextChanged(editable: Editable?) {}
})
}

private fun filterTags(charSequence: String) {
filteredList!!.clear()
filteredList.clear()
if (charSequence.isEmpty()) {
adapter!!.setTagList(list!!)
adapter.setTagList(list)
return
}
for (t in list!!) {
if (t.name!!.lowercase(Locale.getDefault()).contains(charSequence.lowercase(Locale.getDefault()))) {
filteredList!!.add(t)
list.forEach { t ->
if (t.name?.lowercase(Locale.ROOT)?.contains(charSequence.lowercase(Locale.ROOT)) == true) {
filteredList.add(t)
}
}
adapter!!.setTagList(filteredList!!)
adapter.setTagList(filteredList)
}

private fun setListAdapter() {
list = mRealm!!.where(RealmTag::class.java).equalTo("db", dbType).isNotEmpty("name").equalTo("isAttached", false).findAll()
selectedItemsList = recentList
val allTags: List<RealmTag> = mRealm!!.where(RealmTag::class.java).findAll()
val childMap = HashMap<String?, MutableList<RealmTag>>()
for (t in allTags) {
createChildMap(childMap, t)
}
fragmentCollectionsBinding!!.listTags.setGroupIndicator(null)
adapter = TagExpandableAdapter(list as RealmResults<RealmTag>, childMap, selectedItemsList)
adapter!!.setSelectMultiple(true)
adapter!!.setClickListener(this)
fragmentCollectionsBinding!!.listTags.setAdapter(adapter)
fragmentCollectionsBinding!!.btnOk.visibility = View.VISIBLE
list = mRealm.where(RealmTag::class.java).equalTo("db", dbType).isNotEmpty("name").equalTo("isAttached", false).findAll()

selectedItemsList = ArrayList(recentList)
val allTags = mRealm.where(RealmTag::class.java).findAll()
val childMap = HashMap<String, List<RealmTag>>()
allTags.forEach { t -> createChildMap(childMap, t) }
fragmentCollectionsBinding.listTags.setGroupIndicator(null)
adapter = TagExpandableAdapter(list, childMap, selectedItemsList)
adapter.setSelectMultiple(true)
adapter.setClickListener(this)
fragmentCollectionsBinding.listTags.setAdapter(adapter)
fragmentCollectionsBinding.btnOk.visibility = View.VISIBLE
}

private fun createChildMap(childMap: HashMap<String?, MutableList<RealmTag>>, t: RealmTag) {
for (s in t.attachedTo!!) {
var l: MutableList<RealmTag> = ArrayList()
private fun createChildMap(childMap: HashMap<String, List<RealmTag>>, t: RealmTag) {
t.attachedTo?.forEach { s ->
val l: MutableList<RealmTag> = ArrayList()
if (childMap.containsKey(s)) {
l = childMap[s]!!
l.addAll(childMap[s]!!)
}
if (!l.contains(t)) l.add(t)
childMap[s] = l
}
}

override fun onTagClicked(tag: RealmTag) {
if (tagListener != null) tagListener!!.onTagSelected(tag)
listener?.onTagSelected(tag)
dismiss()
}

Expand All @@ -127,15 +118,16 @@ class CollectionsFragment : DialogFragment(), OnClickTagItem, CompoundButton.OnC

override fun onCheckedChanged(compoundButton: CompoundButton, b: Boolean) {
MainApplication.isCollectionSwitchOn = b
adapter!!.setSelectMultiple(b)
adapter!!.setTagList(list!!)
fragmentCollectionsBinding!!.listTags.setAdapter(adapter)
fragmentCollectionsBinding!!.btnOk.visibility = if (b) View.VISIBLE else View.GONE
adapter.setSelectMultiple(b)
adapter.setTagList(list)
fragmentCollectionsBinding.listTags.setAdapter(adapter)
fragmentCollectionsBinding.btnOk.visibility = if (b) View.VISIBLE else View.GONE
}

companion object {
lateinit var recentList: MutableList<RealmTag>
fun getInstance(l: MutableList<RealmTag>, dbType: String?): CollectionsFragment {
private lateinit var recentList: MutableList<RealmTag>
@JvmStatic
fun getInstance(l: MutableList<RealmTag>, dbType: String): CollectionsFragment {
recentList = l
val f = CollectionsFragment()
val b = Bundle()
Expand All @@ -144,4 +136,8 @@ class CollectionsFragment : DialogFragment(), OnClickTagItem, CompoundButton.OnC
return f
}
}
}

fun setListener(listener: TagClickListener) {
this.listener = listener
}
}
Loading

0 comments on commit 54a9341

Please sign in to comment.