Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalbuddha committed Dec 21, 2022
1 parent 00e7618 commit 06dff8e
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ internal class MastodonApiKtor(
}

override suspend fun getHomeFeed(domain: String, accessToken: String): Result<List<Status>> {
return runCatchingIgnoreCancelled<List<Status>> {
httpClient.get("https://$domain/api/v1/timelines/home") {
return runCatchingIgnoreCancelled {
httpClient.get{
url {
host = domain
path("/api/v1/timelines/home")
}
headers {
append(HttpHeaders.Authorization, "Bearer $accessToken")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CREATE TABLE StatusDB (

insertFeedItem:
INSERT OR REPLACE INTO StatusDB
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);
VALUES ?;

selectHomeItems:
SELECT * FROM StatusDB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ fun StatusDB.toLocal(
remoteId = remoteId,
feedType = key,
createdAt = createdAt,
repliesCount = repliesCount,
reblogsCount = favouritesCount,
favoritesCount = favouritesCount,
repliesCount = repliesCount?:0,
reblogsCount = favouritesCount?:0,
favoritesCount = favouritesCount?:0,
content = content,
sensitive = sensitive ?: false,
spoilerText = spoilerText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,7 @@ private fun TimelineQueries.homeItemsAsLocal(key: FeedType) = selectHomeItems()

fun TimelineDatabase.tryWriteItem(it: StatusDB, type: FeedType): Boolean = try {
timelineQueries.insertFeedItem(
type = type.type,
remoteId = it.remoteId,
uri = it.uri,
createdAt = it.createdAt,
content = it.content,
accountId = it.accountId,
visibility = it.visibility,
sensitive = it.sensitive,
spoilerText = it.spoilerText,
applicationName = it.applicationName,
repliesCount = it.repliesCount,
favouritesCount = it.favouritesCount,
reblogsCount = it.reblogsCount,
avatarUrl = it.avatarUrl,
accountAddress = it.accountAddress,
userName = it.userName
it.copy(type = type.type)
)
true
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ data class StatusLocal(
val remoteId:String,
val feedType: FeedType,
val createdAt: String,
val repliesCount: Long?,
val reblogsCount: Long?,
val favoritesCount: Long?,
val repliesCount: Long=0,
val reblogsCount: Long=0,
val favoritesCount: Long=0,
val content: String,
val account: Account?=null,
val sensitive: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.StateFlow
import org.mobilenativefoundation.store.store5.StoreResponse
import social.androiddev.domain.timeline.model.StatusLocal
import social.androiddev.signedin.navigation.SignedInRootComponent
import social.androiddev.timeline.FeedItemState
import social.androiddev.timeline.TimelineContent

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ fun SignedInRootContent(

@Composable
private fun TimelineTab(
state: StateFlow<StoreResponse<List<StatusLocal>>>
state: StateFlow<StoreResponse<List<FeedItemState>>>
) {
TimelineContent(
state = state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -33,33 +34,18 @@ import social.androiddev.timeline.navigation.TimelineComponent
*/
@Composable
fun TimelineContent(
state: StateFlow<StoreResponse<List<StatusLocal>>>,
state: StateFlow<StoreResponse<List<FeedItemState>>>,
modifier: Modifier = Modifier,
) {
val items = state.collectAsState()

val feedItems = when(val value = items.value){
is StoreResponse.Data -> {
val feedItems = value.value.map {
FeedItemState(
id = it.remoteId,
userAvatarUrl = it.avatarUrl,
date = it.createdAt,
username = it.userName,
acctAddress = it.accountAddress,
message = it.content,
images = emptyList(),
videoUrl = null,
)
}

when (val items = state.collectAsState().value) {
is StoreResponse.Data -> {
TimelineContent(
items = feedItems,
items = items.value,
modifier = modifier,
)
}
else ->{

else -> {
//handle error/loading
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import kotlinx.coroutines.flow.StateFlow
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.mobilenativefoundation.store.store5.StoreResponse
import social.androiddev.domain.timeline.FeedType
import social.androiddev.domain.timeline.HomeTimelineRepository
import social.androiddev.domain.timeline.model.StatusLocal
import social.androiddev.timeline.FeedItemState
import kotlin.coroutines.CoroutineContext


Expand All @@ -31,11 +32,12 @@ class DefaultTimelineComponent(
private val viewModel = instanceKeeper.getOrCreate {
TimelineViewModel(
mainContext = mainContext,
homeTimelineRepository
homeTimelineRepository,
FeedType.Home
)
}

override val state: StateFlow<StoreResponse<List<StatusLocal>>> = viewModel.state
override val state: StateFlow<StoreResponse<List<FeedItemState>>> = viewModel.state

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package social.androiddev.timeline.navigation

import kotlinx.coroutines.flow.StateFlow
import org.mobilenativefoundation.store.store5.StoreResponse
import social.androiddev.domain.timeline.model.StatusLocal
import social.androiddev.timeline.FeedItemState

/**
* The base component describing all business logic needed for the timeline view
*/
interface TimelineComponent {
val state: StateFlow<StoreResponse<List<StatusLocal>>>
val state: StateFlow<StoreResponse<List<FeedItemState>>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,47 @@ import org.mobilenativefoundation.store.store5.StoreResponse
import social.androiddev.domain.timeline.FeedType
import social.androiddev.domain.timeline.HomeTimelineRepository
import social.androiddev.domain.timeline.model.StatusLocal
import social.androiddev.timeline.FeedItemState
import kotlin.coroutines.CoroutineContext

class TimelineViewModel(
private val mainContext: CoroutineContext,
private val homeTimelineRepository: HomeTimelineRepository
private val homeTimelineRepository: HomeTimelineRepository,
private val feedType: FeedType
) : InstanceKeeper.Instance {
private val scope = CoroutineScope(mainContext + SupervisorJob())
private val _state = MutableStateFlow<StoreResponse<List<StatusLocal>>>(StoreResponse.Loading(ResponseOrigin.SourceOfTruth))
val state: StateFlow<StoreResponse<List<StatusLocal>>> = _state.asStateFlow()
private val _state =
MutableStateFlow<StoreResponse<List<FeedItemState>>>(StoreResponse.Loading(ResponseOrigin.SourceOfTruth))
val state: StateFlow<StoreResponse<List<FeedItemState>>> = _state.asStateFlow()

init {
scope.launch {
try {
homeTimelineRepository.read(FeedType.Home, refresh = true).collect{
_state.value = it
}
} catch (e:Exception){
e.message
}
homeTimelineRepository.read(refresh = true).collect {
when (val response: StoreResponse<List<StatusLocal>> = it) {
is StoreResponse.Data -> {
val result = StoreResponse.Data(response.value.map {
FeedItemState(
id = it.remoteId,
userAvatarUrl = it.avatarUrl,
date = it.createdAt,
username = it.userName,
acctAddress = it.accountAddress,
message = it.content,
images = emptyList(),
videoUrl = null,
)
}, response.origin)
_state.value = result
}

else -> {
//TODO display error/loading
}
}
}
}
}

override fun onDestroy() {
scope.cancel() // Cancel the scope when the instance is destroyed
}
Expand Down

0 comments on commit 06dff8e

Please sign in to comment.