Commit c3e193bc authored by Filip Wiesner's avatar Filip Wiesner

Live PinnedTask note

parent e95bdd14
......@@ -27,7 +27,10 @@ infix fun<T> SuperGlue<Result<T>>.toNotifyOnSuccess(action: () -> Unit) = this.a
infix fun<T> MutableLiveData<T>.withDefault(init: T) = apply { value = init }
fun <T> LiveData<T>.haveValue() = value != null
fun <T> LiveData<T>.dontHaveValue() = value == null
fun <T> LiveData<T>.dontHaveValue(): Boolean {
return value == null
}
inline fun MutableLiveData<Boolean>.asBoolProgressStatus(job: () -> Unit) {
......@@ -57,7 +60,7 @@ inline infix fun<T> MutableLiveData<Result<T>>.fetchUsingJob(crossinline job: su
postValue(job.invoke())
}
infix fun <T> LiveData<T>.passTo(data: MutableLiveData<T>) = observeForever { data.value = it }
infix fun <T> LiveData<T>.passTo(data: MutableLiveData<T>) = observeForever { data.postValue(it) }
/**
......
......@@ -34,7 +34,7 @@ class EventDetailFragment : Fragment() {
private fun setupBinding() {
bind(viewModel.detailedEvent) toPassValueTo ::setupEvent
bind(detail_pin) clickToInvoke ::onPinnedStateChange
bindText(detail_notePersonalContent) changeTo viewModel::changeEventNote
// bindText(detail_notePersonalContent) changeTo viewModel::changeEventNote
}
private fun setupEvent(event: DetailedEvent) {
......@@ -94,4 +94,9 @@ class EventDetailFragment : Fragment() {
if (detailedEvent.event.isPinned) ColorStateList.valueOf(getColor(R.color.colorTextLight))
else ColorStateList.valueOf(getColor(R.color.colorTextDark))
}
override fun onPause() {
super.onPause()
viewModel.changeEventNote(detail_notePersonalContent.text.toString())
}
}
\ No newline at end of file
......@@ -7,8 +7,6 @@ import com.cvut.blackbird.extensions.withDefault
import com.cvut.blackbird.flows.BlackBirdVM
import com.cvut.blackbird.model.entities.DetailedEvent
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.EventNote
import com.cvut.blackbird.model.entities.Note
import com.cvut.blackbird.model.flows.EventDetailModel
interface EventDetailVM {
......
......@@ -26,6 +26,7 @@ import androidx.viewpager.widget.ViewPager
import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.R
import com.cvut.blackbird.extensions.*
import com.cvut.blackbird.model.entities.DetailedEvent
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.EventType
import com.cvut.blackbird.model.entities.News
......@@ -41,7 +42,7 @@ import org.joda.time.Minutes
sealed class Task(val id: Int)
data class ExamTask(val event: Event): Task(event.id)
data class PinnedTask(val event: Event): Task(event.id)
data class PinnedTask(val event: DetailedEvent): Task(event.event.id)
data class NewsTask(val news: List<News>): Task(news.hashCode()) {
public var lastClickedStory: News? = null
}
......@@ -140,7 +141,7 @@ class PinnedViewHolder(view: View, context: Context) : TaskViewHolder(view, cont
override fun bind(task: Task, clickListener: (Task) -> Unit) {
val pinnedEvent = (task as? PinnedTask)?.event ?: throw TypeCastException("You have to pass pinnedTask to PinnedViewHolder dumbass")
view.pin_eventAbbr.background = when(pinnedEvent.eventType) {
view.pin_eventAbbr.background = when(pinnedEvent.event.eventType) {
EventType.LECTURE ->
context.resources.getDrawable(R.drawable.ic_timetable_lecture, context.theme)
EventType.TUTORIAL ->
......@@ -149,15 +150,21 @@ class PinnedViewHolder(view: View, context: Context) : TaskViewHolder(view, cont
context.resources.getDrawable(R.drawable.ic_timetable_info, context.theme)
}
view.pin_eventAbbr.text = pinnedEvent.linked?.course?.courseAbbr ?: "-"
view.pin_timeLeftTxt.text = "Due in ${Days.daysBetween(DateTime.now(),pinnedEvent.startsAt).days} days"
view.pin_eventAbbr.text = pinnedEvent.event.linked?.course?.courseAbbr ?: "-"
view.pin_timeLeftTxt.text = "Due in ${Days.daysBetween(DateTime.now(),pinnedEvent.event.startsAt).days} days"
if (pinnedEvent.note?.cs != null) {
if (pinnedEvent.event.note?.cs != null || pinnedEvent.userNote.note.isNotBlank())
view.pin_description.setVisible()
view.pin_description.text = "${pinnedEvent.note.cs}\n\n"//TODO pass user note here
pinnedEvent.event.note?.let { view.pin_description.text = it.cs }
view.pin_description.let {
it.text = it.text.toString() +
if (pinnedEvent.userNote.note.isNotBlank())
pinnedEvent.userNote.note
else ""
}
view.pin_unpinBtn.setOnClickListener { pinnedEvent.isPinned = !pinnedEvent.isPinned }
view.pin_unpinBtn.setOnClickListener { pinnedEvent.event.isPinned = !pinnedEvent.event.isPinned }
view.setOnClickListener{clickListener(task)}
}
......
......@@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.*
import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.BlackBirdAC.Companion.log
import com.cvut.blackbird.R
import com.cvut.blackbird.flows.detail.EventDetailViewModel
......@@ -45,7 +46,7 @@ class TasksFragment : Fragment() {
private fun setupUi() {
taskAdapter = TasksListAdapter {
when (it) {
is PinnedTask -> goToEventDetail(it.event)
is PinnedTask -> goToEventDetail(it.event.event)
is ExamTask -> goToEventDetail(it.event)
is NewsTask -> goToNews(it.lastClickedStory)
}
......@@ -57,6 +58,10 @@ class TasksFragment : Fragment() {
private fun setupBinding() {
bind(viewModel.tasks) toPassValueTo taskAdapter::submitList
observe(viewModel.tasks) {
log(it.toString())
}
observe(viewModel.refreshResult) {
Log.d(BlackBirdAC.LOG_TAG, (it as? Failure)?.message ?: it.toString())
}
......
......@@ -4,6 +4,7 @@ import androidx.lifecycle.*
import com.cvut.blackbird.extensions.*
import com.cvut.blackbird.flows.BlackBirdVM
import com.cvut.blackbird.model.*
import com.cvut.blackbird.model.entities.DetailedEvent
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.News
import com.cvut.blackbird.model.flows.TasksModel
......@@ -42,7 +43,7 @@ class TasksViewModel : BlackBirdVM() {
private fun configTasks() { launch {
val combined = combineLiveData(model.getPinnedEvents(), model.getExams(), _news) {
pinned: List<Event>?, exams: List<Event>?, news: List<News>? ->
pinned: List<DetailedEvent>?, exams: List<Event>?, news: List<News>? ->
val resultTasks = ArrayList<Task>()
if (pinned != null) resultTasks.addAll(pinned.map { PinnedTask(it) })
......
......@@ -2,6 +2,7 @@ package com.cvut.blackbird.model.database
import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.OnConflictStrategy.REPLACE
import com.cvut.blackbird.model.entities.DetailedEvent
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.EventNote
......@@ -35,7 +36,7 @@ interface EventDao {
@Update
fun updateEvent(event: Event)
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = REPLACE)
fun insertAllEvents(events: List<Event>)
@Query("DELETE FROM Event")
......@@ -48,12 +49,16 @@ interface EventDao {
@Query("SELECT * FROM Event WHERE id = :id")
fun getDetailedEvent(id: Int): DetailedEvent
@Transaction
@Query("SELECT * FROM Event WHERE id IN (:ids)")
fun getDetailedEvents(ids: List<Int>): LiveData<List<DetailedEvent>>
/** EVENT - NOTE*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = REPLACE)
fun insertAllNotes(notes: List<EventNote>)
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = REPLACE)
fun saveEventNote(eventNote: EventNote)
@Query("DELETE FROM EventNote")
......@@ -61,8 +66,8 @@ interface EventDao {
}
fun EventDao.insertAll(events: List<Event>) {
insertAllEvents(events)
insertAllNotes(events.map { EventNote(it.id) })
insertAllEvents(events)
}
fun EventDao.getTeachers(): List<String> {
......
......@@ -17,7 +17,8 @@ data class DetailedEvent (
var course: Set<Course> = setOf()
@Relation(parentColumn = "id", entityColumn = "eventId")
var userNotes: List<EventNote> = listOf(EventNote(event.id))
var userNotes: List<EventNote> = listOf()
val userNote get() = userNotes.first()
val courseEnt: Course? get() = course.firstOrNull()
......@@ -29,5 +30,27 @@ data class DetailedEvent (
teacherList = teacherDao.getTeachers(event.linked!!.teachers!!)
}
val userNote get() = userNotes.first()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as DetailedEvent
if (event != other.event) return false
if (course != other.course) return false
if (userNotes != other.userNotes) return false
if (teacherList != other.teacherList) return false
return true
}
override fun hashCode(): Int {
var result = event.hashCode()
result = 31 * result + course.hashCode()
result = 31 * result + userNotes.hashCode()
result = 31 * result + teacherList.hashCode()
return result
}
}
\ No newline at end of file
......@@ -5,15 +5,21 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chibatching.kotpref.livedata.asLiveData
import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.extensions.passTo
import com.cvut.blackbird.model.*
import com.cvut.blackbird.model.database.EventDao
import com.cvut.blackbird.model.entities.DetailedEvent
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.News
import com.cvut.blackbird.model.entities.NewsRoot
import com.cvut.blackbird.model.services.*
import com.tickaroo.tikxml.TikXml
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.android.Main
import kotlinx.coroutines.android.UI
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception
......@@ -25,7 +31,8 @@ class TasksModel: BlackBirdModel() {
@Inject lateinit var eventDao: EventDao
private val pinnedPref: LiveData<Set<String>>
private val pinnedEvents = MutableLiveData<List<Event>>()
private val pinnedEvents = MutableLiveData<List<DetailedEvent>>()
private lateinit var detailedEvents: LiveData<List<DetailedEvent>>
init {
BlackBirdAC.graph.inject(this)
......@@ -36,11 +43,11 @@ class TasksModel: BlackBirdModel() {
}
suspend fun getExams() = eventDao.futureExams()
fun getPinnedEvents(): LiveData<List<Event>> = pinnedEvents
fun getPinnedEvents(): LiveData<List<DetailedEvent>> = pinnedEvents
private suspend fun onPinnedChange(set: Set<String>) {
pinnedEvents.postValue(
eventDao.getEvents( set.map { it.toInt() }))
detailedEvents = eventDao.getDetailedEvents( set.map { it.toInt() })
withContext(Dispatchers.Main) { detailedEvents passTo pinnedEvents }
}
suspend fun refreshEvents() = refreshEvents(siriusService, eventDao)
......
......@@ -33,7 +33,7 @@
android:paddingEnd="6dp"
android:paddingBottom="2dp"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
......@@ -341,24 +341,27 @@
android:gravity="end"
android:textColor="@android:color/white"
android:textSize="22sp"
android:textStyle="bold"
tools:text="Posledni termin" />
<View
android:id="@+id/detail_personalNoteDivider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="12dp"
android:background="?android:attr/listDivider" />
<EditText
android:id="@+id/detail_notePersonalContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="6dp"
android:layout_marginTop="12dp"
android:background="@android:color/transparent"
android:fontFamily="sans-serif-condensed"
android:gravity="end"
android:textColor="@android:color/white"
android:textSize="22sp"
android:textStyle="bold"
android:hint="Your note"
tools:text="Posledni termin personal" />
android:textColor="@android:color/white"
android:textSize="24sp"
tools:text="My note that I adeed to this event" />
</LinearLayout>
</ScrollView>
......
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