Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #143 from schul-cloud/15-add-submission-support
Browse files Browse the repository at this point in the history
15 add submission support
  • Loading branch information
JonasWanke authored Sep 11, 2018
2 parents e8aaf09 + c642d80 commit c0be275
Show file tree
Hide file tree
Showing 65 changed files with 1,416 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import org.schulcloud.mobile.viewmodels.CourseViewModel
import org.schulcloud.mobile.viewmodels.IdViewModelFactory
import org.schulcloud.mobile.views.DividerItemDecoration

class CourseFragment : MainFragment() {
class CourseFragment : MainFragment<CourseViewModel>() {
companion object {
val TAG: String = CourseFragment::class.java.simpleName
}

private lateinit var viewModel: CourseViewModel
private val topicsAdapter: TopicListAdapter by lazy {
org.schulcloud.mobile.controllers.course.TopicListAdapter {
navController.navigate(R.id.action_global_fragment_topic,
Expand All @@ -39,7 +38,7 @@ class CourseFragment : MainFragment() {


override var url: String? = null
get() = viewModel.course.value?.url
get() = "/courses/${viewModel.course.value?.id}"

override fun provideConfig() = viewModel.course.map { course ->
MainFragmentConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import org.schulcloud.mobile.utils.asLiveData
import org.schulcloud.mobile.viewmodels.CourseListViewModel
import org.schulcloud.mobile.views.ItemOffsetDecoration

class CourseListFragment : MainFragment() {
class CourseListFragment : MainFragment<CourseListViewModel>() {
companion object {
val TAG: String = CourseListFragment::class.java.simpleName
}

private lateinit var viewModel: CourseListViewModel
private val courseAdapter: CourseAdapter by lazy {
CourseAdapter {
navController.navigate(R.id.action_global_fragment_course,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import org.schulcloud.mobile.R
import org.schulcloud.mobile.controllers.main.FragmentType
import org.schulcloud.mobile.controllers.main.MainFragment
import org.schulcloud.mobile.controllers.main.MainFragmentConfig
import org.schulcloud.mobile.utils.asLiveData

class DashboardFragment : MainFragment() {
class DashboardFragment : MainFragment<ViewModel>() {
companion object {
val TAG: String = DashboardFragment::class.java.simpleName
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.schulcloud.mobile.controllers.dashboard

import androidx.fragment.app.Fragment
import org.schulcloud.mobile.controllers.main.Refreshable
import org.schulcloud.mobile.controllers.main.RefreshableImpl

open class Widget : Fragment() {
open suspend fun refresh() {}
abstract class Widget(refreshableImpl: RefreshableImpl = RefreshableImpl()) : Fragment(),
Refreshable by refreshableImpl {
init {
refreshableImpl.refresh = { refresh() }
}

abstract suspend fun refresh()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat.startActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
Expand All @@ -17,6 +18,7 @@ import kotlinx.android.synthetic.main.fragment_file.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.schulcloud.mobile.R
import org.schulcloud.mobile.R.id.*
import org.schulcloud.mobile.controllers.course.CourseFragmentArgs
import org.schulcloud.mobile.controllers.main.MainFragment
import org.schulcloud.mobile.controllers.main.MainFragmentConfig
Expand All @@ -34,15 +36,15 @@ import retrofit2.HttpException
import ru.gildor.coroutines.retrofit.await


class FileFragment : MainFragment() {
class FileFragment : MainFragment<FileViewModel>() {
companion object {
val TAG: String = FileFragment::class.java.simpleName
}

private val args: FileFragmentArgs by lazy {
FileFragmentArgs.fromBundle(arguments)
}
private lateinit var viewModel: FileViewModel

private val directoryAdapter: DirectoryAdapter by lazy {
DirectoryAdapter {
navController.navigate(R.id.action_global_fragment_file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import org.schulcloud.mobile.models.file.FileRepository
import org.schulcloud.mobile.utils.asLiveData
import org.schulcloud.mobile.viewmodels.FileOverviewViewModel

class FileOverviewFragment : MainFragment() {
class FileOverviewFragment : MainFragment<FileOverviewViewModel>() {
companion object {
val TAG: String = FileOverviewFragment::class.java.simpleName
}

private lateinit var viewModel: FileOverviewViewModel
private val coursesAdapter: FileOverviewCourseAdapter by lazy {
FileOverviewCourseAdapter {
navController.navigate(R.id.action_global_fragment_file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_homework_list.*
import org.schulcloud.mobile.R
import org.schulcloud.mobile.controllers.homework.detailed.HomeworkFragmentArgs
import org.schulcloud.mobile.controllers.main.FragmentType
import org.schulcloud.mobile.controllers.main.MainFragment
import org.schulcloud.mobile.controllers.main.MainFragmentConfig
import org.schulcloud.mobile.models.homework.HomeworkRepository
import org.schulcloud.mobile.utils.asLiveData
import org.schulcloud.mobile.viewmodels.HomeworkListViewModel

class HomeworkListFragment : MainFragment() {
class HomeworkListFragment : MainFragment<HomeworkListViewModel>() {
companion object {
val TAG: String = HomeworkListFragment::class.java.simpleName
}

private lateinit var viewModel: HomeworkListViewModel
private val homeworkAdapter: HomeworkAdapter by lazy {
HomeworkAdapter({
navController.navigate(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package org.schulcloud.mobile.controllers.homework
package org.schulcloud.mobile.controllers.homework.detailed

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import kotlinx.android.synthetic.main.fragment_homework.*
import org.schulcloud.mobile.R
import org.schulcloud.mobile.controllers.course.CourseFragmentArgs
import org.schulcloud.mobile.controllers.main.MainFragment
import org.schulcloud.mobile.controllers.main.MainFragmentConfig
import org.schulcloud.mobile.controllers.main.ParentFragment
import org.schulcloud.mobile.controllers.main.TabFragment
import org.schulcloud.mobile.databinding.FragmentHomeworkBinding
import org.schulcloud.mobile.models.homework.HomeworkRepository
import org.schulcloud.mobile.models.homework.submission.SubmissionRepository
import org.schulcloud.mobile.utils.map
import org.schulcloud.mobile.utils.visibilityBool
import org.schulcloud.mobile.viewmodels.HomeworkViewModel
import org.schulcloud.mobile.viewmodels.IdViewModelFactory

class HomeworkFragment : MainFragment() {
class HomeworkFragment : MainFragment<HomeworkViewModel>(), ParentFragment {
companion object {
val TAG: String = HomeworkFragment::class.java.simpleName
}

private lateinit var viewModel: HomeworkViewModel

private val pagerAdapter by lazy { HomeworkPagerAdapter(context!!, childFragmentManager) }

override var url: String? = null
get() = "homework/${viewModel.homework.value?.id}"
Expand All @@ -47,11 +52,25 @@ class HomeworkFragment : MainFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return FragmentHomeworkBinding.inflate(layoutInflater).also {
it.viewModel = viewModel
it.setLifecycleOwner(this)
}.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewPager.adapter = pagerAdapter
tabLayout.setupWithViewPager(viewPager)
mainViewModel.toolbarColors.observe(this, Observer {
tabLayout.setTabTextColors(it.textColorSecondary, it.textColor)
tabLayout.setSelectedTabIndicatorColor(it.textColor)
})

viewModel.homework.observe(this, Observer {
pagerAdapter.homework = it
// Hide tabs if only overview is visible
tabLayout.visibilityBool = it?.canSeeSubmissions() == true
})
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.homework_action_gotoCourse -> viewModel.homework.value?.course?.id?.also { id ->
Expand All @@ -64,6 +83,14 @@ class HomeworkFragment : MainFragment() {
}

override suspend fun refresh() {
HomeworkRepository.syncHomework(viewModel.id)
refreshWithChild(false)
}

override suspend fun refreshWithChild(fromChild: Boolean) {
if (fromChild) {
HomeworkRepository.syncHomework(viewModel.id)
SubmissionRepository.syncSubmissionsForHomework(viewModel.id)
} else if (viewPager != null)
(pagerAdapter.getItem(viewPager.currentItem) as? TabFragment<*, *>)?.performRefresh()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.schulcloud.mobile.controllers.homework.detailed

import android.content.Context
import androidx.annotation.IntDef
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import org.schulcloud.mobile.R
import org.schulcloud.mobile.models.homework.Homework
import kotlin.properties.Delegates


class HomeworkPagerAdapter(private val context: Context, fm: FragmentManager) : FragmentPagerAdapter(fm) {
companion object {
private const val TAB_INVALID = 0
private const val TAB_DETAILS = 1
private const val TAB_SUBMISSIONS = 2

@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
@IntDef(TAB_INVALID, TAB_DETAILS, TAB_SUBMISSIONS)
annotation class Tab
}

var homework by Delegates.observable<Homework?>(null) { _, _, _ ->
notifyDataSetChanged()
}

override fun getItem(position: Int): Fragment? {
return when (getTabType(position)) {
TAB_DETAILS -> OverviewFragment()
TAB_SUBMISSIONS -> SubmissionsFragment()

TAB_INVALID -> null
else -> null
}
}

override fun getPageTitle(position: Int): CharSequence? {
val titleId = when (getTabType(position)) {
TAB_DETAILS -> R.string.homework_overview
TAB_SUBMISSIONS -> R.string.homework_submissions

TAB_INVALID -> return null
else -> return null
}
return context.getString(titleId)
}

override fun getCount(): Int {
return if (homework?.canSeeSubmissions() == true) 2
else 1
}

@Tab
private fun getTabType(position: Int): Int {
return when {
position == 0 -> TAB_DETAILS
position == 1 && homework?.canSeeSubmissions() == true -> TAB_SUBMISSIONS
else -> TAB_INVALID
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.schulcloud.mobile.controllers.homework.detailed

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.NavHostFragment
import kotlinx.android.synthetic.main.fragment_homework_overview.*
import org.schulcloud.mobile.R
import org.schulcloud.mobile.controllers.homework.submission.SubmissionFragmentArgs
import org.schulcloud.mobile.controllers.main.ParentFragment
import org.schulcloud.mobile.controllers.main.TabFragment
import org.schulcloud.mobile.databinding.FragmentHomeworkOverviewBinding
import org.schulcloud.mobile.viewmodels.HomeworkViewModel


class OverviewFragment : TabFragment<HomeworkFragment, HomeworkViewModel>() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return FragmentHomeworkOverviewBinding.inflate(layoutInflater).also {
it.viewModel = viewModel
it.setLifecycleOwner(this)
}.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

gotoMySubmission.setOnClickListener {
viewModel.mySubmission.value?.id?.also {
NavHostFragment.findNavController(this).navigate(
R.id.action_global_fragment_submission,
SubmissionFragmentArgs.Builder(it).build().toBundle())
}
}
}

override suspend fun refresh() {
(parentFragment as? ParentFragment)?.refreshWithChild(true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.schulcloud.mobile.controllers.homework.detailed

import android.view.LayoutInflater
import android.view.ViewGroup
import org.schulcloud.mobile.controllers.base.BaseAdapter
import org.schulcloud.mobile.controllers.base.BaseViewHolder
import org.schulcloud.mobile.databinding.ItemSubmissionBinding
import org.schulcloud.mobile.models.homework.submission.Submission
import org.schulcloud.mobile.models.user.User


class SubmissionsAdapter(private val onSelected: (String) -> Unit) :
BaseAdapter<Pair<User, Submission?>, SubmissionsAdapter.SubmissionViewHolder, ItemSubmissionBinding>() {

fun update(submissions: List<Pair<User, Submission?>>) {
items = submissions
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubmissionViewHolder {
val binding = ItemSubmissionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
binding.onSelected = onSelected
return SubmissionViewHolder(binding)
}


inner class SubmissionViewHolder(binding: ItemSubmissionBinding) :
BaseViewHolder<Pair<User, Submission?>, ItemSubmissionBinding>(binding) {
override fun onItemSet() {
binding.student = item.first
binding.submission = item.second
}
}
}
Loading

0 comments on commit c0be275

Please sign in to comment.