Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Follow hashtag button #346

Merged
merged 8 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ object AnalyticsIdentifiers {
const val SEARCH_HASHTAG_CLICKED = "search.hashtag.clicked"
const val SEARCH_ACCOUNT_FOLLOW = "search.account.follow"
const val SEARCH_HASHTAG_FOLLOW = "search.hashtag.follow"

const val HASHTAG_FOLLOW = "hashtag.follow"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.mozilla.social.core.database.dao

import androidx.room.Dao
import androidx.room.Query
import kotlinx.coroutines.flow.Flow
import org.mozilla.social.core.database.model.entities.DatabaseHashTagEntity

@Dao
Expand All @@ -16,4 +17,10 @@ interface HashTagsDao : BaseDao<DatabaseHashTagEntity> {
hashTag: String,
isFollowing: Boolean,
)

@Query(
"SELECT * FROM hashtags " +
"WHERE name = :name",
)
fun getHashTagFlow(name: String): Flow<DatabaseHashTagEntity>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mozilla.social.core.network.mastodon

import org.mozilla.social.core.network.mastodon.model.NetworkHashTag
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

Expand All @@ -15,4 +16,9 @@ interface TagsApi {
suspend fun unfollowHashTag(
@Path("hashTag") hashTag: String,
): NetworkHashTag

@GET("api/v1/tags/{hashTag}")
suspend fun getHashTag(
@Path("hashTag") hashTag: String,
): NetworkHashTag
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mozilla.social.core.repository.mastodon

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.mozilla.social.common.annotations.PreferUseCase
import org.mozilla.social.core.database.dao.HashTagsDao
import org.mozilla.social.core.model.HashTag
Expand All @@ -20,6 +22,12 @@ class HashtagRepository(
hashTag: HashTag,
) = dao.upsert(hashTag.toDatabaseModel())

fun getHashTagFlow(
hashTag: String
): Flow<HashTag> = dao.getHashTagFlow(hashTag).map {
it.toExternalModel()
}

@PreferUseCase
suspend fun updateFollowing(
hashTag: String,
Expand All @@ -29,11 +37,18 @@ class HashtagRepository(
isFollowing,
)

@PreferUseCase
suspend fun followHashTag(
hashTag: String,
): HashTag = api.followHashTag(hashTag).toExternalModel()

@PreferUseCase
suspend fun unfollowHashTag(
hashTag: String,
): HashTag = api.unfollowHashTag(hashTag).toExternalModel()

@PreferUseCase
suspend fun getHashTag(
hashTag: String
): HashTag = api.getHashTag(hashTag).toExternalModel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import org.mozilla.social.core.ui.common.text.SmallTextLabel
fun FollowingButton(
onButtonClicked: () -> Unit,
isFollowing: Boolean,
modifier: Modifier = Modifier,
) {
MoSoToggleButton(
modifier = Modifier.height(32.dp),
modifier = modifier.height(32.dp),
onClick = { onButtonClicked() },
toggleState = if (isFollowing) {
ToggleButtonState.Secondary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.mozilla.social.core.usecase.mastodon.auth.IsSignedInFlow
import org.mozilla.social.core.usecase.mastodon.auth.Login
import org.mozilla.social.core.usecase.mastodon.auth.Logout
import org.mozilla.social.core.usecase.mastodon.hashtag.FollowHashTag
import org.mozilla.social.core.usecase.mastodon.hashtag.GetHashTag
import org.mozilla.social.core.usecase.mastodon.hashtag.UnfollowHashTag
import org.mozilla.social.core.usecase.mastodon.report.Report
import org.mozilla.social.core.usecase.mastodon.search.SearchAll
Expand Down Expand Up @@ -209,4 +210,10 @@ val mastodonUsecaseModule =
singleOf(::GetDomain)
singleOf(::SearchAll)
singleOf(::GetInReplyToAccountNames)

single {
GetHashTag(
hashtagRepository = get(),
)
}
Comment on lines +214 to +218
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
single {
GetHashTag(
hashtagRepository = get(),
)
}
singleOf(::GetHashTag)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetHashTag has another parameter for the coroutine dispatcher with a default value, so we have to use the normal constructor pattern

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.mozilla.social.core.usecase.mastodon.hashtag

import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.mozilla.social.common.Resource
import org.mozilla.social.common.annotations.PreferUseCase
import org.mozilla.social.core.model.HashTag
import org.mozilla.social.core.repository.mastodon.HashtagRepository
import timber.log.Timber

class GetHashTag(
private val hashtagRepository: HashtagRepository,
private val dispatcherIo: CoroutineDispatcher = Dispatchers.IO,
) {

@OptIn(PreferUseCase::class)
suspend operator fun invoke(
name: String,
coroutineScope: CoroutineScope,
): Flow<Resource<HashTag>> =
flow {
emit(Resource.Loading())
val deferred = CompletableDeferred<Resource<Unit>>()
// the hashtag from the server might be lower case, so we need to assign this
// to whatever we get back from the server
var realName: String = name
coroutineScope.launch(dispatcherIo) {
try {
val hashtag = hashtagRepository.getHashTag(realName)
realName = hashtag.name
hashtagRepository.insert(hashtag)
deferred.complete(
Resource.Loaded(
Unit
)
)
} catch (e: Exception) {
Timber.e(e)
deferred.complete(Resource.Error(e))
}
}
when (val deferredResult = deferred.await()) {
is Resource.Error -> emit(Resource.Error(deferredResult.exception))
else -> {
try {
emitAll(hashtagRepository.getHashTagFlow(realName).map { Resource.Loaded(it) })
} catch (e: Exception) {
Timber.e(e)
emit(Resource.Error(e))
}
}
}
}
}
2 changes: 2 additions & 0 deletions feature/hashtag/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ dependencies {
implementation(libs.protobuf.kotlin.lite)

implementation(libs.androidx.room)

implementation(libs.jakewharton.timber)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package org.mozilla.social.feature.hashtag

interface HashTagInteractions {
fun onScreenViewed() = Unit
fun onFollowClicked(name: String, isFollowing: Boolean) = Unit
fun onRetryClicked() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ val hashTagModule =
analytics = get(),
userAccountId = get(),
timelineRepository = get(),
unfollowHashTag = get(),
followHashTag = get(),
getHashTag = get(),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package org.mozilla.social.feature.hashtag

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf
import org.mozilla.social.common.Resource
import org.mozilla.social.core.model.HashTag
import org.mozilla.social.core.ui.common.MoSoSurface
import org.mozilla.social.core.ui.common.appbar.MoSoCloseableTopAppBar
import org.mozilla.social.core.ui.common.error.GenericError
import org.mozilla.social.core.ui.common.following.FollowingButton
import org.mozilla.social.core.ui.common.loading.MaxSizeLoading
import org.mozilla.social.core.ui.postcard.PostCardInteractions
import org.mozilla.social.core.ui.postcard.PostCardList
import org.mozilla.social.core.ui.postcard.PostCardUiState
Expand All @@ -20,10 +30,15 @@ internal fun HashTagScreen(
hashTag: String,
viewModel: HashTagViewModel = koinViewModel(parameters = { parametersOf(hashTag) }),
) {

val uiState: Resource<HashTag> by viewModel.uiState.collectAsStateWithLifecycle()

HashTagScreen(
uiState = uiState,
hashTag = hashTag,
feed = viewModel.feed,
postCardInteractions = viewModel.postCardDelegate,
hashTagInteractions = viewModel,
)

LaunchedEffect(Unit) {
Expand All @@ -33,24 +48,54 @@ internal fun HashTagScreen(

@Composable
private fun HashTagScreen(
uiState: Resource<HashTag>,
hashTag: String,
feed: Flow<PagingData<PostCardUiState>>,
postCardInteractions: PostCardInteractions,
hashTagInteractions: HashTagInteractions,
) {
MoSoSurface {
Column(
modifier = Modifier.systemBarsPadding(),
) {
MoSoCloseableTopAppBar(
title = "#$hashTag",
actions = {
if (uiState is Resource.Loaded) {
FollowingButton(
modifier = Modifier
.padding(end = 8.dp),
onButtonClicked = {
hashTagInteractions.onFollowClicked(
hashTag,
uiState.data.following
)
},
isFollowing = uiState.data.following
)
}
}
)

PostCardList(
feed = feed,
postCardInteractions = postCardInteractions,
pullToRefreshEnabled = true,
isFullScreenLoading = true,
)
when (uiState) {
is Resource.Loading -> {
MaxSizeLoading()
}
is Resource.Loaded -> {
PostCardList(
feed = feed,
postCardInteractions = postCardInteractions,
pullToRefreshEnabled = true,
isFullScreenLoading = true,
)
}
is Resource.Error -> {
GenericError(
modifier = Modifier.fillMaxSize(),
onRetryClicked = { hashTagInteractions.onRetryClicked() }
)
}
}
}
}
}
Loading