Commit 27412ac6 authored by Filip Wiesner's avatar Filip Wiesner

Teacher name in Event detail (-> TeacherDao, teachers download on login)

parent 8bb07859
......@@ -29,4 +29,8 @@ class RoomModule(context: Context) {
@Singleton
@Provides
internal fun providesCourseDao(db: BlackBirdDB) = db.courseDao()
@Singleton
@Provides
internal fun providesTeacherDao(db: BlackBirdDB) = db.teacherDao()
}
\ No newline at end of file
......@@ -15,6 +15,7 @@ import com.cvut.blackbird.model.Success
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.EventType
import com.cvut.blackbird.support.glue.bind
import com.cvut.blackbird.support.glue.observe
import com.cvut.blackbird.support.glue.toPassValueTo
import kotlinx.android.synthetic.main.fragment_timetable_detail.*
......@@ -23,34 +24,29 @@ class TimetableDetailFragment : Fragment() {
lateinit var event: Event
private lateinit var viewModel: TimetableViewModel
private lateinit var revealSettings: CircularRevealSettings
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(activity!!).get(TimetableViewModel::class.java)
this bind viewModel.eventDetail toPassValueTo ::setupEvent
bind(viewModel.course) toPassValueTo {
if (it is Success) detail_fullName.text = it.value.name
}
// detailExitBtn.setOnClickListener { _ ->
// view?.startCircularExit(revealSettings) {
// findMainNav()?.navigateUp()
// }
// }
observe(viewModel.course) {
if (it is Success && it.value != null)
detail_fullName.text = it.value.name
}
observe(viewModel.teacher) {
if (it is Success && it.value != null)
detail_teachersContent.text = "${it.value.firstName} ${it.value.lastName}"
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_timetable_detail, container, false)
// revealSettings = TimetableDetailFragmentArgs.fromBundle(arguments).revealSettings
// view.registerCircularReveal(revealSettings)
return view
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
= inflater.inflate(R.layout.fragment_timetable_detail, container, false)
private fun setupEvent(event: Event) {
viewModel.requestCourse(event.linked?.course ?: "")
viewModel.requestTeacher(event.linked?.teachers?.firstOrNull() ?: "")
detail_abbr.background = when {
......@@ -69,8 +65,6 @@ class TimetableDetailFragment : Fragment() {
detail_parallelContent.text = if (event.parallel.isNotBlank()) event.parallel else "-"
detail_roomContent.text = event.linked?.room ?: "None"
detail_noteContent.text = event.note?.cs ?: "None"
//TODO: paralelka, ucitel, ucebna, note
}
val String.courseAbbr get() =this.substring(
......
......@@ -96,14 +96,6 @@ class TimetableFragment : Fragment() {
private fun onLectureClick(element: View) {
if (element is TimetableBall) {
val directions = NavigationFragmentDirections.toEventDetail(
CircularRevealSettings(
element.globalCenterX,
element.globalCenterY,
2000,2000,
getColor(R.color.colorAccent),
Color.WHITE
))
viewModel.eventDetail.value = element.payload
findMainNav()!!.navigate(R.id.toEventDetail)
}
......
......@@ -10,6 +10,7 @@ import com.cvut.blackbird.model.Result
import com.cvut.blackbird.model.Success
import com.cvut.blackbird.model.entities.Course
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.Teacher
import com.cvut.blackbird.model.flows.DateSpan
import com.cvut.blackbird.model.flows.TimetableModel
import kotlinx.coroutines.experimental.Job
......@@ -25,13 +26,15 @@ class TimetableViewModel : ViewModel() {
private val _timetableUpdateRequestResult = MutableLiveData<Result<List<Event>>>() withDefault NotYet()
private val _timetable = MutableLiveData<Map<Int,List<Event>>>() withDefault mapOf()
private val _displayedWeek = MutableLiveData<DateTime>() withDefault DateTime.now()
private val _course = MutableLiveData<Result<Course>>() withDefault NotYet()
private val _course = MutableLiveData<Result<Course?>>() withDefault NotYet()
private val _teacher = MutableLiveData<Result<Teacher?>>() withDefault NotYet()
val loadingStatus: LiveData<Boolean> get() = _loadingStatus
val timetableUpdateResult: LiveData<Result<List<Event>>> get() = _timetableUpdateRequestResult
val timetable: LiveData<Map<Int,List<Event>>> get() = _timetable
val displayedWeek: LiveData<DateTime> get() = _displayedWeek
val course: LiveData<Result<Course>> get() = _course
val course: LiveData<Result<Course?>> get() = _course
val teacher: LiveData<Result<Teacher?>> get() = _teacher
val eventDetail = MutableLiveData<Event>() withDefault Event.empty
......@@ -69,9 +72,13 @@ class TimetableViewModel : ViewModel() {
}
}
fun requestCourse(code: String) {
launch { _course.postValue(Success(model.getCourse(code))) }
}
fun requestCourse(code: String) { launch {
_course.postValue(Success(model.getCourse(code)))
} }
fun requestTeacher(username: String) { launch {
_teacher.postValue(Success(model.getTeacher(username)))
} }
private var refreshJob: Job? = null
fun refresh() {
......
......@@ -6,17 +6,19 @@ import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.cvut.blackbird.model.entities.Course
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.Teacher
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat
@Database(entities = [Event::class, Course::class], version = 4)
@Database(entities = [Event::class, Course::class, Teacher::class], version = 6)
@TypeConverters(DBTypeConverters::class)
abstract class BlackBirdDB: RoomDatabase() {
abstract fun eventDao(): EventDao
abstract fun courseDao(): CourseDao
abstract fun teacherDao(): TeacherDao
}
class DBTypeConverters {
......
......@@ -10,7 +10,7 @@ import com.cvut.blackbird.model.entities.Course
interface CourseDao {
@Query("SELECT * FROM Course WHERE code = :code")
fun getCourse(code: String): Course
fun getCourse(code: String): Course?
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(courses: List<Course>)
......
......@@ -17,9 +17,22 @@ interface EventDao {
@Query("SELECT * FROM Event Where startsAt BETWEEN :from AND :to ORDER BY startsAt")
fun timeSpan(from: Long, to: Long): LiveData<List<Event>>
@Query("SELECT DISTINCT teachers FROM Event")
fun getRawTeachers(): List<String>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(events: List<Event>)
@Query("DELETE FROM Event")
fun deleteAll()
}
fun EventDao.getTeachers(): List<String> {
val raw = getRawTeachers()
val convertor = DBTypeConverters()
val result = ArrayList<String>()
for (json in raw) {
result.addAll(convertor.restoreList(json))
}
return result.distinct()
}
\ No newline at end of file
package com.cvut.blackbird.model.database
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.cvut.blackbird.model.entities.Teacher
@Dao
interface TeacherDao {
@Query("SELECT * FROM Teacher WHERE username = :username")
fun getTeacher(username: String): Teacher?
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(courses: List<Teacher>)
}
\ No newline at end of file
package com.cvut.blackbird.model.entities
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.tickaroo.tikxml.annotation.Path
import com.tickaroo.tikxml.annotation.PropertyElement
import com.tickaroo.tikxml.annotation.Xml
@Xml
@Entity
data class Teacher(
@Path("atom:content")
@PropertyElement val firstName: String,
@Path("atom:content")
@PropertyElement val lastName: String,
@Path("atom:content")
@PropertyElement val personalNumber: Int,
@Path("atom:content")
@PropertyElement val titlesPost: String?,
@Path("atom:content")
@PropertyElement val titlesPre: String?,
@PrimaryKey
@Path("atom:content")
@PropertyElement val username: String,
@Path("atom:content")
@PropertyElement val division: String,
@Path("atom:content")
@PropertyElement val email: String,
@Path("atom:content")
@PropertyElement val extern: Boolean,
@Path("atom:content")
@PropertyElement val phone: String?
)
\ No newline at end of file
......@@ -4,8 +4,11 @@ import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.model.*
import com.cvut.blackbird.model.database.CourseDao
import com.cvut.blackbird.model.database.EventDao
import com.cvut.blackbird.model.database.TeacherDao
import com.cvut.blackbird.model.database.getTeachers
import com.cvut.blackbird.model.entities.Course
import com.cvut.blackbird.model.entities.Student
import com.cvut.blackbird.model.entities.Teacher
import com.cvut.blackbird.model.services.AuthInfo
import com.cvut.blackbird.model.services.KosService
import com.cvut.blackbird.model.services.SiriusService
......@@ -19,6 +22,7 @@ class AuthModel: BlackBirdModel() {
@Inject lateinit var siriusService: SiriusService
@Inject lateinit var eventDao: EventDao
@Inject lateinit var courseDao: CourseDao
@Inject lateinit var teacherDao: TeacherDao
init {
......@@ -84,27 +88,47 @@ class AuthModel: BlackBirdModel() {
suspend fun initLoad(): Result<Unit> {
val events = async { refreshEvents(siriusService, eventDao) }
val teachers = async {
val deferredTeacherList = ArrayList<Deferred<Result<Teacher>>>()
val completedTeacherList = ArrayList<Result<Teacher>>()
events.await()
val teachers = eventDao.getTeachers()
for (teacher in teachers)
deferredTeacherList.add(async { fetch(kosService.getTeacher(teacher)) })
deferredTeacherList.forEach { completedTeacherList.add(it.await()) }
completedTeacherList
}
val courses = async {
val deferredCourseList = ArrayList<Deferred<Result<Course>>>()
val completedCoarses = ArrayList<Result<Course>>()
val completedCourses = ArrayList<Result<Course>>()
val courses = fetch(kosService.getStudentsCourses())
if (courses is Success) {
for (link in courses.value.courses)
deferredCourseList.add(async { fetch(kosService.getCourse(link.name)) })
deferredCourseList.forEach { completedCoarses.add(it.await()) }
deferredCourseList.forEach { completedCourses.add(it.await()) }
}
completedCoarses
completedCourses
}
val resultCourses = courses.await()
val resultEvents = events.await()
return if (resultCourses.all { it is Success } && resultEvents is Success) {
val resultTeachers = teachers.await()
return if (
resultCourses.all { it is Success } &&
resultTeachers.all { it is Success } &&
resultEvents is Success)
{
courseDao.insertAll(resultCourses.map { (it as Success).value })
teacherDao.insertAll(resultTeachers.map { (it as Success).value })
Success(Unit)
} else Failure("Failed initializing account.\n" +
"Error message: " +
when (resultEvents) {
is Failure -> resultEvents.message
when {
resultEvents is Failure -> resultEvents.message
resultTeachers.any { it is Failure } -> (resultTeachers.firstOrNull { it is Failure } as? Failure)
?.message ?: "Unknow error"
else -> (resultCourses.firstOrNull { it is Failure } as? Failure)
?.message ?: "Unknow error"
}
......
......@@ -4,6 +4,7 @@ import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.model.*
import com.cvut.blackbird.model.database.CourseDao
import com.cvut.blackbird.model.database.EventDao
import com.cvut.blackbird.model.database.TeacherDao
import com.cvut.blackbird.model.services.SiriusService
import javax.inject.Inject
......@@ -13,6 +14,7 @@ class TimetableModel: BlackBirdModel() {
@Inject lateinit var siriusService: SiriusService
@Inject lateinit var eventDao: EventDao
@Inject lateinit var courseDao: CourseDao
@Inject lateinit var teacherDao: TeacherDao
init { BlackBirdAC.graph.inject(this) }
......@@ -21,4 +23,6 @@ class TimetableModel: BlackBirdModel() {
suspend fun refreshEvents() = refreshEvents(siriusService, eventDao)
suspend fun getCourse(code: String) = courseDao.getCourse(code)
suspend fun getTeacher(username: String) = teacherDao.getTeacher(username)
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package com.cvut.blackbird.model.services
import com.cvut.blackbird.model.entities.Course
import com.cvut.blackbird.model.entities.EnrolledCourseRoot
import com.cvut.blackbird.model.entities.Student
import com.cvut.blackbird.model.entities.Teacher
import retrofit2.Call
import retrofit2.http.*
......@@ -12,6 +13,12 @@ interface KosService {
private val accessToken = "Bearer ${AuthInfo.accessToken}"
}
/**
* Student
*/
@GET("students/{user}")
fun getStudent(
@Path("user") user: String,
......@@ -28,11 +35,32 @@ interface KosService {
@Query("multilang") multilang: Boolean = false
): Call<EnrolledCourseRoot>
/**
* Course
*/
@GET("courses/{course}")
fun getCourse(
@Path("course") course: String,
@Header("Authorization") token: String = accessToken,
@Query("lang") lang: String = "en"
@Query("lang") lang: String = "en",
@Query("multilang") multilang: Boolean = false
): Call<Course>
/**
* Teacher
*/
@GET("teachers/{teacher}")
fun getTeacher(
@Path("teacher") teacherUsername: String,
@Header("Authorization") token: String = accessToken,
@Query("lang") lang: String = "en",
@Query("multilang") multilang: Boolean = false
): Call<Teacher>
}
......@@ -13,7 +13,9 @@
<action
android:id="@+id/toEventDetail"
app:destination="@id/timetableDetailFragment"
/>
app:enterAnim="@anim/nav_default_enter_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/authFragment"
......@@ -31,9 +33,6 @@
android:label="fragment_timetable_detail"
tools:layout="@layout/fragment_timetable_detail" >
<argument
android:name="revealSettings"
app:argType="com.cvut.blackbird.extensions.CircularRevealSettings" />
</fragment>
<action
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment