From 0d9787618cd290457d87ec5c8719f9f1e3c3ece9 Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 4 Apr 2020 22:39:36 +0200 Subject: [PATCH] Use database magic to make it easier to display conferences names and progress for events --- .../entities/recording/persistence/Event.kt | 3 ++ .../recording/persistence/EventDao.kt | 11 ++++++ .../entities/progress/PlaybackProgress.kt | 2 +- .../entities/progress/PlaybackProgressDao.kt | 7 ++++ .../entities/progress/ProgressEventView.kt | 13 +++++++ .../entities/watchlist/WatchlistItemDao.kt | 4 ++- .../common/viewmodel/BrowseViewModel.kt | 22 ++++++++---- .../chaosflix/touch/BindingAdapters.kt | 15 ++++++++ .../adapters/EventRecyclerViewAdapter.kt | 6 ++++ .../browse/eventslist/EventsListFragment.java | 2 ++ .../layout-large/item_conference_cardview.xml | 2 +- .../res/layout-large/item_event_cardview.xml | 2 +- .../layout-large/item_liveevent_cardview.xml | 2 +- .../res/layout-large/item_offline_event.xml | 2 +- .../res/layout/item_conference_cardview.xml | 2 +- .../main/res/layout/item_event_cardview.xml | 34 ++++++++++++------- .../res/layout/item_liveevent_cardview.xml | 2 +- .../main/res/layout/item_offline_event.xml | 2 +- .../layout/related_event_cardview_layout.xml | 2 +- 19 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/ProgressEventView.kt diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/Event.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/Event.kt index 0ef4abdb..073945c2 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/Event.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/Event.kt @@ -55,6 +55,9 @@ data class Event( var recordings: List? = null ) : Parcelable, Comparable { + @Ignore + var progress: Long = 0 + override fun compareTo(other: Event): Int = title.compareTo(other.title) constructor(parcel: Parcel) : this( diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/EventDao.kt b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/EventDao.kt index dc0b348b..b59fe59e 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/EventDao.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/mediadata/entities/recording/persistence/EventDao.kt @@ -67,6 +67,17 @@ abstract class EventDao : BaseDao() { @Query("SELECT * FROM event WHERE title LIKE :title LIMIT 1") abstract fun findSingleEventByTitle(title: String): LiveData +// @Query("SELECT * FROM event JOIN conference ON event.conferenceId=conference.id") +// abstract suspend fun getEventWithConference(eventId: Long): List +// +// @Query("SELECT * FROM event JOIN conference ON event.conferenceId=conference.id WHERE event.id = :eventId") +// abstract suspend fun getAllEventsWithConference(eventId: Long): List + + @Query("""SELECT event.*, conference.title as conference + FROM event JOIN conference ON event.conferenceId=conference.id + WHERE conference.id = :confernceId""") + abstract fun getEventsWithConferenceForConfernce(confernceId: Long): LiveData> + override suspend fun updateOrInsertInternal(item: Event) { if (item.id != 0L) { update(item) diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt index 5e29fb00..cc1f24ed 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgress.kt @@ -7,7 +7,7 @@ import androidx.room.PrimaryKey import java.util.Date @Entity(tableName = "playback_progress", - indices = arrayOf(Index(value = ["event_guid"], unique = true))) + indices = [Index(value = ["event_guid"], unique = true)]) data class PlaybackProgress( @PrimaryKey(autoGenerate = true) val id: Long = 0, diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgressDao.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgressDao.kt index 05b574ec..15018e86 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgressDao.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/PlaybackProgressDao.kt @@ -8,6 +8,13 @@ import androidx.room.Query @Dao interface PlaybackProgressDao { + + @Query("""SELECT event.*, progress.progress, progress.watch_date, conference.title as conference + FROM playback_progress as progress + JOIN event ON event_guid = event.guid + JOIN conference ON event.conferenceId = conference.id""") + fun getInProgessEvents(): LiveData> + @Query("SELECT * from playback_progress") fun getAll(): LiveData> diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/ProgressEventView.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/ProgressEventView.kt new file mode 100644 index 00000000..7775b3ee --- /dev/null +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/progress/ProgressEventView.kt @@ -0,0 +1,13 @@ +package de.nicidienase.chaosflix.common.userdata.entities.progress + +import androidx.room.ColumnInfo +import androidx.room.Embedded +import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event + +class ProgressEventView( + val progress: Long, + @ColumnInfo(name = "watch_date") + val watchDate: Long, + @Embedded + val event: Event +) diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItemDao.kt b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItemDao.kt index 186a51a7..84ef5d3f 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItemDao.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/userdata/entities/watchlist/WatchlistItemDao.kt @@ -18,7 +18,9 @@ abstract class WatchlistItemDao : BaseDao() { @Query("SELECT * FROM watchlist_item") abstract fun getAllSync(): List - @Query("SELECT * FROM watchlist_item JOIN event ON watchlist_item.event_guid=event.guid") + @Query("""SELECT event.*, conference.title as conference FROM watchlist_item + JOIN event ON watchlist_item.event_guid=event.guid + JOIN conference ON event.conferenceId = conference.id""") abstract fun getWatchlistEvents(): LiveData> @Query("SELECT * FROM watchlist_item WHERE event_guid = :guid LIMIT 1") diff --git a/common/src/main/java/de/nicidienase/chaosflix/common/viewmodel/BrowseViewModel.kt b/common/src/main/java/de/nicidienase/chaosflix/common/viewmodel/BrowseViewModel.kt index f25687e0..289f280f 100644 --- a/common/src/main/java/de/nicidienase/chaosflix/common/viewmodel/BrowseViewModel.kt +++ b/common/src/main/java/de/nicidienase/chaosflix/common/viewmodel/BrowseViewModel.kt @@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.common.viewmodel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Transformations import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import de.nicidienase.chaosflix.R @@ -49,8 +50,8 @@ class BrowseViewModel( fun getConferencesByGroup(groupId: Long) = database.conferenceDao().findConferenceByGroup(groupId) - fun getEventsforConference(conference: Conference) = - database.eventDao().findEventsByConference(conference.id) + fun getEventsforConference(conference: Conference): LiveData> = + database.eventDao().getEventsWithConferenceForConfernce(conference.id) fun getUpdateState() = mediaRepository.updateConferencesAndGroups() @@ -74,11 +75,18 @@ class BrowseViewModel( return itemDao.getWatchlistEvents() } - fun getInProgressEvents(): LiveData> = updateAndGetEventsForGuids { - database - .playbackProgressDao() - .getAllSync() - .map { it.eventGuid } } + fun getInProgressEvents(): LiveData> { + val dao = database.playbackProgressDao() + viewModelScope.launch(Dispatchers.IO) { + dao.getAllSync().forEach { + mediaRepository.updateSingleEvent(it.eventGuid) + } + } + return Transformations.map(dao.getInProgessEvents()) { list -> + list.forEach { it.event.progress = it.progress } + list.map { it.event } + } + } fun getPromotedEvents(): LiveData> = database.eventDao().findPromotedEvents() diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/BindingAdapters.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/BindingAdapters.kt index 88e79051..52bbafb0 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/BindingAdapters.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/BindingAdapters.kt @@ -1,10 +1,13 @@ package de.nicidienase.chaosflix.touch +import android.view.View import android.widget.ImageView +import android.widget.ProgressBar import android.widget.TextView import androidx.databinding.BindingAdapter import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions +import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event @BindingAdapter("imageUrl") fun loadImage(imageView: ImageView, url: String?) { @@ -19,3 +22,15 @@ fun loadImage(imageView: ImageView, url: String?) { fun setDuration(textView: TextView, duration: Long) { textView.text = String.format("%d:%02d:%02d", duration / 3600, (duration % 3600) / 60, duration % 60) } + +@BindingAdapter("progress") +fun ProgressBar.eventProgress(event: Event) { + val progress = event.progress + if (progress > 0) { + this.visibility = View.VISIBLE + this.max = event.length.toInt() + this.progress = (progress / 1000).toInt() + } else { + this.visibility = View.GONE + } +} diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/adapters/EventRecyclerViewAdapter.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/adapters/EventRecyclerViewAdapter.kt index 087e17ee..17ce2ee9 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/adapters/EventRecyclerViewAdapter.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/adapters/EventRecyclerViewAdapter.kt @@ -1,6 +1,7 @@ package de.nicidienase.chaosflix.touch.browse.adapters import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import androidx.core.view.ViewCompat import androidx.recyclerview.widget.RecyclerView @@ -10,6 +11,8 @@ import de.nicidienase.chaosflix.touch.databinding.ItemEventCardviewBinding open class EventRecyclerViewAdapter(val listener: (Event) -> Unit) : ItemRecyclerViewAdapter() { + var showConferenceName: Boolean = false + override fun getComparator(): Comparator? { return Comparator { o1, o2 -> o1.title.compareTo(o2.title) } } @@ -37,6 +40,9 @@ open class EventRecyclerViewAdapter(val listener: (Event) -> Unit) : holder.binding.root.setOnClickListener { listener(event) } + if (showConferenceName) { + holder.binding.conferenceNameText.visibility = View.VISIBLE + } ViewCompat.setTransitionName(holder.binding.titleText, "title_${event.guid}") ViewCompat.setTransitionName(holder.binding.subtitleText, "subtitle_${event.guid}") diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java index f3b483c5..e6387576 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/eventslist/EventsListFragment.java @@ -118,9 +118,11 @@ public class EventsListFragment extends BrowseFragment implements SearchView.OnQ if (type == TYPE_BOOKMARKS) { setupToolbar(binding.incToolbar.toolbar, R.string.bookmarks); + eventAdapter.setShowConferenceName(true); getViewModel().getBookmarkedEvents().observe(this, listObserver); } else if (type == TYPE_IN_PROGRESS) { setupToolbar(binding.incToolbar.toolbar, R.string.continue_watching); + eventAdapter.setShowConferenceName(true); getViewModel().getInProgressEvents().observe(this, listObserver); } else if (type == TYPE_EVENTS) { { diff --git a/touch/src/main/res/layout-large/item_conference_cardview.xml b/touch/src/main/res/layout-large/item_conference_cardview.xml index 30e71d13..4c13dfb8 100644 --- a/touch/src/main/res/layout-large/item_conference_cardview.xml +++ b/touch/src/main/res/layout-large/item_conference_cardview.xml @@ -69,7 +69,7 @@ app:layout_constraintTop_toBottomOf="@+id/title_text"/> + app:layout_constraintTop_toBottomOf="@+id/conference_name_text" + tools:text="This is a very long title text that propably won't fit in one line, maybe even need three." /> + app:layout_constraintEnd_toEndOf="@+id/title_text" + app:layout_constraintStart_toStartOf="@+id/title_text" + app:layout_constraintTop_toTopOf="parent" + tools:text="42c3: Don't panic" + tools:visibility="visible" /> + + diff --git a/touch/src/main/res/layout/item_liveevent_cardview.xml b/touch/src/main/res/layout/item_liveevent_cardview.xml index 8773dfb1..a6032356 100644 --- a/touch/src/main/res/layout/item_liveevent_cardview.xml +++ b/touch/src/main/res/layout/item_liveevent_cardview.xml @@ -63,7 +63,7 @@ tools:text="Subtitle"/>