Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

courses: safer transactions (fixes #3731) #3802

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import android.widget.AdapterView
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Spinner
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.TextInputLayout
import com.google.gson.Gson
import com.google.gson.JsonObject
import org.ole.planet.myplanet.R
Expand Down Expand Up @@ -43,11 +47,18 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
private lateinit var orderByDate: Button
private lateinit var orderByTitle: Button
private lateinit var selectAll: CheckBox
var userModel: RealmUserModel ?= null
lateinit var spnGrade: Spinner
lateinit var spnSubject: Spinner
lateinit var searchTags: MutableList<RealmTag>
private var userModel: RealmUserModel ?= null
private lateinit var spnGrade: Spinner
private lateinit var spnSubject: Spinner
private lateinit var searchTags: MutableList<RealmTag>
private lateinit var confirmation: AlertDialog
private lateinit var filter: ImageButton
private lateinit var collections: Button
private lateinit var parentLayout: CoordinatorLayout
private lateinit var bottomSheet: CardView
private lateinit var tags: TextInputLayout
private lateinit var clearTags: Button

override fun getLayout(): Int {
return R.layout.fragment_my_course
}
Expand Down Expand Up @@ -78,7 +89,8 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
super.onViewCreated(view, savedInstanceState)
userModel = UserProfileDbHandler(requireContext()).userModel
searchTags = ArrayList()
initializeView()

initializeView(view)
if (isMyCourseLib) {
tvDelete?.setText(R.string.archive)
btnRemove.visibility = View.VISIBLE
Expand Down Expand Up @@ -113,54 +125,62 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
.setNegativeButton(R.string.no, null).show()
}

requireView().findViewById<View>(R.id.btn_collections).setOnClickListener {
collections.setOnClickListener {
val f = CollectionsFragment.getInstance(searchTags, "courses")
f.setListener(this)
f.show(childFragmentManager, "")
}

clearTags()
showNoData(tvMessage, adapterCourses.itemCount, "courses")
setupUI(requireView().findViewById(R.id.my_course_parent_layout), requireActivity())
setupUI(parentLayout, requireActivity())
changeButtonStatus()
if (!isMyCourseLib) tvFragmentInfo.setText(R.string.our_courses)
additionalSetup()
}

private fun additionalSetup() {
val bottomSheet = requireView().findViewById<View>(R.id.card_filter)
requireView().findViewById<View>(R.id.filter).setOnClickListener {
filter.setOnClickListener {
bottomSheet.visibility = if (bottomSheet.visibility == View.VISIBLE) View.GONE else View.VISIBLE
}
orderByDate = requireView().findViewById(R.id.order_by_date_button)
orderByTitle = requireView().findViewById(R.id.order_by_title_button)
orderByDate.setOnClickListener { adapterCourses.toggleSortOrder() }
orderByTitle.setOnClickListener { adapterCourses.toggleTitleSortOrder() }
}

private fun initializeView() {
tvAddToLib = requireView().findViewById(R.id.tv_add)
private fun initializeView(view: View) {
tvAddToLib = view.findViewById(R.id.tv_add)
etSearch = view.findViewById(R.id.et_search)
tvSelected = view.findViewById(R.id.tv_selected)
btnRemove = view.findViewById(R.id.btn_remove)
spnGrade = view.findViewById(R.id.spn_grade)
spnSubject = view.findViewById(R.id.spn_subject)
tvMessage = view.findViewById(R.id.tv_message)
tvFragmentInfo = view.findViewById(R.id.tv_fragment_info)
selectAll = view.findViewById(R.id.selectAll)
filter = view.findViewById(R.id.filter)
collections = view.findViewById(R.id.btn_collections)
parentLayout = view.findViewById(R.id.my_course_parent_layout)
bottomSheet = view.findViewById(R.id.card_filter)
orderByDate = view.findViewById(R.id.order_by_date_button)
orderByTitle = view.findViewById(R.id.order_by_title_button)
tags = view.findViewById(R.id.tl_tags)
clearTags = view.findViewById(R.id.btn_clear_tags)

tvAddToLib.setOnClickListener {
if ((selectedItems?.size ?: 0) > 0) {
confirmation = createAlertDialog()
confirmation.show()
addToMyList()
selectedItems?.clear()
tvAddToLib.isEnabled = false // selectedItems will always have a size of 0
tvAddToLib.isEnabled = false
checkList()
}
}
etSearch = requireView().findViewById(R.id.et_search)
tvSelected = requireView().findViewById(R.id.tv_selected)
btnRemove = requireView().findViewById(R.id.btn_remove)
spnGrade = requireView().findViewById(R.id.spn_grade)
spnSubject = requireView().findViewById(R.id.spn_subject)
tvMessage = requireView().findViewById(R.id.tv_message)
requireView().findViewById<View>(R.id.tl_tags).visibility = View.GONE
tvFragmentInfo = requireView().findViewById(R.id.tv_fragment_info)

tags.visibility = View.GONE

spnGrade.onItemSelectedListener = itemSelectedListener
spnSubject.onItemSelectedListener = itemSelectedListener
selectAll = requireView().findViewById(R.id.selectAll)
if(userModel?.isGuest() == true){
tvAddToLib.visibility = View.GONE
btnRemove.visibility = View.GONE
Expand All @@ -186,14 +206,13 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
selectAll.visibility = View.GONE
etSearch.visibility = View.GONE
tvAddToLib.visibility = View.GONE
requireView().findViewById<View>(R.id.filter).visibility = View.GONE
filter.visibility = View.GONE
btnRemove.visibility = View.GONE
tvSelected.visibility = View.GONE
tvDelete?.visibility = View.GONE
}
}


private val itemSelectedListener: AdapterView.OnItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) {
gradeLevel = if (spnGrade.selectedItem.toString() == "All") "" else spnGrade.selectedItem.toString()
Expand All @@ -206,7 +225,7 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
}

private fun clearTags() {
requireView().findViewById<View>(R.id.btn_clear_tags).setOnClickListener {
clearTags.setOnClickListener {
searchTags.clear()
etSearch.setText("")
tvSelected.text = ""
Expand Down Expand Up @@ -318,18 +337,16 @@ class CoursesFragment : BaseRecyclerFragment<RealmMyCourse?>(), OnCourseItemSele
}

private fun recreateFragment(fragment: Fragment) {
if (isMyCourseLib) {
val args = Bundle()
args.putBoolean("isMyCourseLib", true)
args.putString("courseLib", courseLib)
args.putSerializable("resources", resources?.let { ArrayList(it) })
fragment.arguments = args
val transaction = parentFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.addToBackStack(null)
transaction.commitAllowingStateLoss()
} else {
if (isAdded && activity != null && !requireActivity().isFinishing) {
val transaction = parentFragmentManager.beginTransaction()
if (isMyCourseLib) {
val args = Bundle().apply {
putBoolean("isMyCourseLib", true)
putString("courseLib", courseLib)
putSerializable("resources", resources?.let { ArrayList(it) })
}
fragment.arguments = args
}
transaction.replace(R.id.fragment_container, fragment)
transaction.addToBackStack(null)
transaction.commitAllowingStateLoss()
Expand Down