Skip to content

Commit

Permalink
[MBL-15076][Student] App crashes when accessing module items
Browse files Browse the repository at this point in the history
refs: MBL-15076
affects: Student
release note: Fixed a crash when trying to open a module in a course which has lots of modules.
  • Loading branch information
tamaskozmer authored Jan 13, 2021
1 parent 2634331 commit c81accb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.instructure.student.R
import com.instructure.student.events.ModuleUpdatedEvent
import com.instructure.student.events.post
import com.instructure.student.util.Const
import com.instructure.student.util.CourseModulesStore
import com.instructure.student.util.ModuleProgressionUtility
import com.instructure.student.util.ModuleUtility
import kotlinx.android.synthetic.main.course_module_progression.*
Expand Down Expand Up @@ -751,22 +752,30 @@ class CourseModuleProgressionFragment : ParentFragment(), Bookmarkable {
else -> !moduleItem.title.equals(context.getString(R.string.loading), ignoreCase = true)
}

fun makeRoute(moduleObjects: ArrayList<ModuleObject>, itemList: ArrayList<ArrayList<ModuleItem>>, canvasContext: CanvasContext, groupPos: Int, childPos: Int): Route {
fun makeRoute(canvasContext: CanvasContext, groupPos: Int, childPos: Int): Route {
return Route(null, CourseModuleProgressionFragment::class.java, canvasContext, canvasContext.makeBundle(Bundle().apply {
putParcelableArrayList(MODULE_OBJECTS, moduleObjects)
putSerializable(MODULE_ITEMS, itemList)
putInt(GROUP_POSITION, groupPos)
putInt(CHILD_POSITION, childPos)
}))
}

fun newInstance(route: Route): CourseModuleProgressionFragment? = if (validRoute(route)) CourseModuleProgressionFragment().apply {
arguments = route.arguments
moduleItemId = route.queryParamsHash[RouterParams.MODULE_ITEM_ID] ?: ""
} else null
fun newInstance(route: Route): CourseModuleProgressionFragment? {
val fragment = if (validRoute(route)) CourseModuleProgressionFragment().apply {
arguments = route.arguments.apply {
putSerializable(MODULE_ITEMS, CourseModulesStore.moduleListItems)
putParcelableArrayList(MODULE_OBJECTS, CourseModulesStore.moduleObjects)
}
moduleItemId = route.queryParamsHash[RouterParams.MODULE_ITEM_ID] ?: ""
} else null

CourseModulesStore.moduleListItems = null
CourseModulesStore.moduleObjects = null

return fragment
}

private fun validRoute(route: Route): Boolean = route.canvasContext != null
&& (route.arguments.containsKey(MODULE_OBJECTS) && route.arguments.containsKey(MODULE_ITEMS))
|| route.queryParamsHash.keys.any { it == RouterParams.MODULE_ITEM_ID }
&& (CourseModulesStore.moduleObjects != null && CourseModulesStore.moduleListItems != null)
|| route.queryParamsHash.keys.any { it == RouterParams.MODULE_ITEM_ID }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.instructure.student.adapter.ModuleListRecyclerAdapter
import com.instructure.student.events.ModuleUpdatedEvent
import com.instructure.student.interfaces.ModuleAdapterToFragmentCallback
import com.instructure.student.router.RouteMatcher
import com.instructure.student.util.CourseModulesStore
import com.instructure.student.util.ModuleProgressionUtility
import com.instructure.student.util.ModuleUtility
import kotlinx.android.synthetic.main.fragment_module_list.*
Expand Down Expand Up @@ -145,8 +146,9 @@ class ModuleListFragment : ParentFragment(), Bookmarkable {
val moduleItemsArray = groups.indices.mapTo(ArrayList<ArrayList<ModuleItem>>()) { recyclerAdapter.getItems(groups[it]) }
val moduleHelper = ModuleProgressionUtility.prepareModulesForCourseProgression(requireContext(), moduleItem.id, groups, moduleItemsArray)

RouteMatcher.route(requireContext(), CourseModuleProgressionFragment.makeRoute(groups,
moduleHelper.strippedModuleItems,
CourseModulesStore.moduleListItems = moduleHelper.strippedModuleItems
CourseModulesStore.moduleObjects = groups
RouteMatcher.route(requireContext(), CourseModuleProgressionFragment.makeRoute(
canvasContext,
moduleHelper.newGroupPosition,
moduleHelper.newChildPosition))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2021 - present Instructure, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package com.instructure.student.util

import com.instructure.canvasapi2.models.ModuleItem
import com.instructure.canvasapi2.models.ModuleObject
import java.util.ArrayList

object CourseModulesStore {

var moduleListItems: ArrayList<ArrayList<ModuleItem>>? = null
var moduleObjects: ArrayList<ModuleObject>? = null
}

0 comments on commit c81accb

Please sign in to comment.