Skip to content

Commit

Permalink
Merge pull request #361 from stakwork/tt/feature/feed-content-last-date
Browse files Browse the repository at this point in the history
Content seen date logic
  • Loading branch information
tomastiminskas authored Dec 3, 2021
2 parents 2a81f31 + 6983c50 commit 75b79c8
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chat.sphinx.activitymain.navigation.navigators.detail
import chat.sphinx.activitymain.navigation.drivers.DetailNavigationDriver
import chat.sphinx.newsletter_detail.navigation.NewsletterDetailNavigator
import chat.sphinx.web_view.navigation.ToWebViewDetail
import chat.sphinx.wrapper_common.dashboard.ChatId
import chat.sphinx.wrapper_common.feed.FeedUrl
import javax.inject.Inject

Expand All @@ -14,9 +15,9 @@ internal class NewsletterDetailNavigatorImpl @Inject constructor(
(navigationDriver as DetailNavigationDriver).closeDetailScreen()
}

override suspend fun toWebViewDetail(title: String, url: FeedUrl) {
override suspend fun toWebViewDetail(chatId: ChatId?, title: String, url: FeedUrl) {
detailDriver.submitNavigationRequest(
ToWebViewDetail(title, url, true)
ToWebViewDetail(chatId, title, url, true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ internal class DashboardNavigatorImpl @Inject constructor(
)
}

override suspend fun toWebViewDetail(title: String, url: FeedUrl) {
override suspend fun toWebViewDetail(chatId: ChatId?, title: String, url: FeedUrl) {
detailDriver.submitNavigationRequest(
ToWebViewDetail(title, url, false)
ToWebViewDetail(chatId, title, url, false)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ UPDATE chatDbo
SET is_muted = :muted
WHERE id = ?;

chatUpdateContentSeenAt:
UPDATE chatDbo
SET content_seen_at = :content_seen_at
WHERE id = ?;

chatGetAll:
SELECT *
FROM chatDbo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ interface ChatRepository {
* */
suspend fun toggleChatMuted(chat: Chat): Response<Boolean, ResponseError>

suspend fun updateChatContentSeenAt(chatId: ChatId)

fun joinTribe(
tribeDto: TribeDto,
): Flow<LoadResponse<ChatDto, ResponseError>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package chat.sphinx.concept_repository_media

import chat.sphinx.wrapper_chat.Chat
import chat.sphinx.wrapper_chat.ChatMetaData
import chat.sphinx.wrapper_common.dashboard.ChatId
import chat.sphinx.wrapper_message.Message
import chat.sphinx.wrapper_podcast.PodcastDestination
import kotlinx.coroutines.flow.Flow

interface RepositoryMedia {
fun updateChatMetaData(chatId: ChatId, metaData: ChatMetaData, shouldSync: Boolean = true)

suspend fun updateChatContentSeenAt(chatId: ChatId)

fun downloadMediaIfApplicable(
message: Message,
sent: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ import okio.base64.encodeBase64
import java.io.File
import java.io.InputStream
import java.text.ParseException
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.LinkedHashMap
import kotlin.math.absoluteValue


Expand Down Expand Up @@ -3144,6 +3147,19 @@ abstract class SphinxRepository(
return response
}

override suspend fun updateChatContentSeenAt(chatId: ChatId) {
val queries = coreDB.getSphinxDatabaseQueries()

chatLock.withLock {
withContext(io) {
queries.chatUpdateContentSeenAt(
DateTime(Date()),
chatId
)
}
}
}

override fun joinTribe(
tribeDto: TribeDto
): Flow<LoadResponse<ChatDto, ResponseError>> = flow {
Expand Down Expand Up @@ -3410,6 +3426,7 @@ abstract class SphinxRepository(

override fun getAllFeedsOfType(feedType: FeedType): Flow<List<Feed>> = flow {
val queries = coreDB.getSphinxDatabaseQueries()

emitAll(
queries.feedGetAllByFeedType(feedType)
.asFlow()
Expand Down Expand Up @@ -3481,7 +3498,7 @@ abstract class SphinxRepository(
}
}

return listFeedDbo.map {
val list = listFeedDbo.map {
mapFeedDbo(
feedDbo = it,
items = itemsMap[it.id] ?: listOf(),
Expand All @@ -3490,6 +3507,14 @@ abstract class SphinxRepository(
chat = chatsMap[it.chat_id]
)
}

var sortedList: List<Feed>? = null

withContext(dispatchers.default) {
sortedList = list.sortedByDescending { it.chat?.contentSeenAt?.time ?: it.lastItem?.datePublished?.time ?: 0 }
}

return sortedList ?: listOf()
}

private suspend fun mapFeedDbo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chat.sphinx.newsletter_detail.navigation

import androidx.navigation.NavController
import chat.sphinx.wrapper_common.dashboard.ChatId
import chat.sphinx.wrapper_common.feed.FeedUrl
import io.matthewnelson.android_feature_navigation.requests.PopBackStack
import io.matthewnelson.concept_navigation.BaseNavigationDriver
Expand All @@ -12,7 +13,7 @@ abstract class NewsletterDetailNavigator(

abstract suspend fun closeDetailScreen()

abstract suspend fun toWebViewDetail(title: String, url: FeedUrl)
abstract suspend fun toWebViewDetail(chatId: ChatId?, title: String, url: FeedUrl)

suspend fun popBackStack() {
navigationDriver.submitNavigationRequest(PopBackStack())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ internal class NewsletterDetailViewModel @Inject constructor(
fun newsletterItemSelected(item: FeedItem) {
viewModelScope.launch(mainImmediate) {
navigator.toWebViewDetail(
item?.feed?.chat?.id ?: item?.feed?.chatId,
app.getString(R.string.newsletter_article),
item.enclosureUrl
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ internal class VideoFeedWatchScreenViewModel @Inject constructor(

init {
subscribeToViewStateFlow()

viewModelScope.launch(mainImmediate) {
chatRepository.updateChatContentSeenAt(
getArgChatId()
)
}
}

open val playingVideoStateContainer: ViewStateContainer<PlayingVideoViewState> by lazy {
Expand Down
1 change: 1 addition & 0 deletions sphinx/screens-detail/web-view/web-view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
api project(path: ':sphinx:activity:insetter-activity')
api project(path: ':sphinx:application:common:wrappers:wrapper-common')

implementation project(path: ':sphinx:application:data:concepts:repositories:concept-repository-chat')
implementation project(path: ':sphinx:screens-detail:common:detail-resources')
implementation project(path: ':sphinx:application:common:logger')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import androidx.navigation.NavController
import chat.sphinx.detail_resources.DetailNavOptions
import chat.sphinx.web_view.R
import chat.sphinx.web_view.ui.WebViewFragmentArgs
import chat.sphinx.wrapper_common.dashboard.ChatId
import chat.sphinx.wrapper_common.feed.FeedUrl
import io.matthewnelson.concept_navigation.NavigationRequest
import io.matthewnelson.android_feature_navigation.R as nav_R

class ToWebViewDetail(
private val chatId: ChatId?,
private val title: String,
private val url: FeedUrl,
private val fromList: Boolean,
Expand All @@ -26,7 +28,12 @@ class ToWebViewDetail(

controller.navigate(
R.id.web_view_nav_graph,
WebViewFragmentArgs.Builder(title, url.value, fromList)
WebViewFragmentArgs.Builder(
chatId?.value ?: ChatId.NULL_CHAT_ID.toLong(),
title,
url.value,
fromList
)
.build()
.toBundle(),
navOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
package chat.sphinx.web_view.ui

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import chat.sphinx.concept_repository_chat.ChatRepository
import chat.sphinx.web_view.navigation.WebViewNavigator
import chat.sphinx.wrapper_common.dashboard.ChatId
import dagger.hilt.android.lifecycle.HiltViewModel
import io.matthewnelson.android_feature_navigation.util.navArgs
import io.matthewnelson.android_feature_viewmodel.BaseViewModel
import io.matthewnelson.concept_coroutines.CoroutineDispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

internal inline val WebViewFragmentArgs.chatId: ChatId?
get() = if (argChatId == ChatId.NULL_CHAT_ID.toLong()) {
null
} else {
ChatId(argChatId)
}

@HiltViewModel
internal class WebViewViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
dispatchers: CoroutineDispatchers,
val navigator: WebViewNavigator
val navigator: WebViewNavigator,
private val chatRepository: ChatRepository,
): BaseViewModel<WebViewViewState>(dispatchers, WebViewViewState.Idle)
{
private val args: WebViewFragmentArgs by savedStateHandle.navArgs()

init {
args.chatId?.let { chatId ->
viewModelScope.launch(mainImmediate) {
chatRepository.updateChatContentSeenAt(chatId)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
android:name="chat.sphinx.web_view.ui.WebViewFragment"
tools:layout="@layout/fragment_web_view" >

<argument
android:name="argChatId"
app:argType="long" />

<argument
android:name="argTitle"
app:argType="string" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class DashboardNavigator(

abstract suspend fun toVideoWatchScreen(chatId: ChatId, feedUrl: FeedUrl)

abstract suspend fun toWebViewDetail(title: String, url: FeedUrl)
abstract suspend fun toWebViewDetail(chatId: ChatId?, title: String, url: FeedUrl)

abstract suspend fun toNewsletterDetail(chatId: ChatId, feedUrl: FeedUrl)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ sealed class DashboardChat {
open val owner: Contact? = null

override val sortBy: Long
get() = message?.date?.time ?: chat.createdAt.time
get() {
val lastContentSeenDate = chat.contentSeenAt?.time
val lastMessageActionDate = message?.date?.time ?: chat.createdAt.time

if (lastContentSeenDate != null && lastContentSeenDate > lastMessageActionDate) {
return lastContentSeenDate
}
return lastMessageActionDate
}

override fun getDisplayTime(today00: DateTime): String {
return message?.date?.chatTimeFormat(today00) ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.content.Context
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import chat.sphinx.concept_repository_chat.ChatRepository
import chat.sphinx.concept_repository_dashboard_android.RepositoryDashboardAndroid
import chat.sphinx.dashboard.R
import chat.sphinx.dashboard.navigation.DashboardNavigator
Expand Down Expand Up @@ -48,11 +49,12 @@ class FeedReadViewModel @Inject constructor(
}
}

fun newsletterItemSelected(episode: FeedItem) {
fun newsletterItemSelected(item: FeedItem) {
viewModelScope.launch(mainImmediate) {
dashboardNavigator.toWebViewDetail(
item?.feed?.chat?.id ?: item?.feed?.chatId,
app.getString(R.string.newsletter_article),
episode.enclosureUrl
item.enclosureUrl
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import chat.sphinx.feature_service_media_player_android.util.toIntent
import io.matthewnelson.concept_coroutines.CoroutineDispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ internal abstract class MediaPlayerService: SphinxService() {
}
is UserAction.ServiceAction.Play -> {

serviceLifecycleScope.launch {
repositoryMedia.updateChatContentSeenAt(userAction.chatId)
}

podData?.let { nnData ->
if (nnData.chatId != userAction.chatId) {
//Podcast has changed. Payments Destinations needs to be set again
Expand Down

0 comments on commit 75b79c8

Please sign in to comment.