Skip to content

Commit

Permalink
익명 여부를 로컬에 저장 (#79)
Browse files Browse the repository at this point in the history
* SikshaPrefObjects에 communityIsAnonymous 추가

* CommunityRepository에 isAnonymous flow 추가

* PostDetailViewModel애 isAnonymous StateFlow 추가

* 익명 여부 get/set 시 preference에 접근하도록 수정
  • Loading branch information
eastshine2741 committed Aug 30, 2024
1 parent 862bacd commit 33ac85f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ fun PostDetailRoute(
val post by postDetailViewModel.post.collectAsState()
val board by postListViewModel.selectedBoard.collectAsState()
val comments = postDetailViewModel.commentPagingData.collectAsLazyPagingItems()
val isAnonymous by postDetailViewModel.isAnonymous.collectAsState()

PostDetailScreen(
post = post,
board = board,
comments = comments,
postDetailEvent = postDetailViewModel.postDetailEvent,
isAnonymous = isAnonymous,
onNavigateUp = onNavigateUp,
refreshComments = { comments.refresh() },
togglePostLike = postDetailViewModel::togglePostLike,
toggleCommentLike = postDetailViewModel::toggleCommentLike,
updateListWithLikedPost = postListViewModel::updateListWithLikedPost,
updateListWithCommentAddedPost = postListViewModel::updateListWithCommentAddedPost,
addComment = postDetailViewModel::addComment,
onIsAnonymousChanged = postDetailViewModel::setIsAnonymous,
modifier = modifier
)
}
Expand All @@ -114,17 +117,18 @@ fun PostDetailScreen(
board: Board,
comments: LazyPagingItems<Comment>,
postDetailEvent: SharedFlow<PostDetailEvent>,
isAnonymous: Boolean,
onNavigateUp: () -> Unit,
togglePostLike: () -> Unit,
refreshComments: () -> Unit,
toggleCommentLike: (Comment) -> Unit,
updateListWithLikedPost: (Post) -> Unit,
updateListWithCommentAddedPost: (Post) -> Unit,
addComment: (String, Boolean) -> Unit,
onIsAnonymousChanged: (Boolean) -> Unit,
modifier: Modifier = Modifier
) {
var commentInput by remember { mutableStateOf("") }
var isAnonymousInput by remember { mutableStateOf(true) }
val commentListState = rememberLazyListState()

PostDetailViewEventEffect(
Expand Down Expand Up @@ -187,10 +191,10 @@ fun PostDetailScreen(
CommentInputRow(
commentInput = commentInput,
onCommentInputChanged = { commentInput = it },
isAnonymous = isAnonymousInput,
onIsAnonymousChanged = { isAnonymousInput = it },
isAnonymous = isAnonymous,
onIsAnonymousChanged = onIsAnonymousChanged,
addComment = { ->
addComment(commentInput, isAnonymousInput)
addComment(commentInput, isAnonymous)
updateListWithCommentAddedPost(post)
},
modifier = Modifier
Expand Down Expand Up @@ -583,13 +587,15 @@ fun PostDetailScreenPreview() {
board = Board(),
comments = flowOf(PagingData.empty<Comment>()).collectAsLazyPagingItems(),
postDetailEvent = MutableSharedFlow(),
isAnonymous = true,
onNavigateUp = {},
togglePostLike = {},
refreshComments = {},
updateListWithLikedPost = {},
toggleCommentLike = {},
addComment = { _, _ -> },
updateListWithCommentAddedPost = {},
onIsAnonymousChanged = {},
modifier = Modifier.fillMaxSize()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ class SikshaPrefObjects @Inject constructor(
serializer,
OAuthProvider::class.java
)

val communityIsAnonymous: Preference<Boolean> =
Preference(
"communityIsAnonymous",
true,
sharedPreferences,
serializer,
Boolean::class.java
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import com.wafflestudio.siksha2.models.Board
import com.wafflestudio.siksha2.models.Post
import com.wafflestudio.siksha2.network.SikshaApi
import com.wafflestudio.siksha2.network.dto.PostCommentRequestBody
import com.wafflestudio.siksha2.preferences.SikshaPrefObjects
import com.wafflestudio.siksha2.repositories.pagingsource.CommentPagingSource
import com.wafflestudio.siksha2.repositories.pagingsource.PostPagingSource
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class CommunityRepository @Inject constructor(
private val api: SikshaApi
private val api: SikshaApi,
private val sikshaPrefObjects: SikshaPrefObjects
) {
val isAnonymous = sikshaPrefObjects.communityIsAnonymous.asFlow()

suspend fun getBoards(): List<Board> {
return api.getBoards().map { it.toBoard() }
}
Expand Down Expand Up @@ -44,4 +48,8 @@ class CommunityRepository @Inject constructor(
suspend fun unlikeComment(commentId: Long) {
api.postUnlikeComment(commentId)
}

fun setIsAnonymous(isAnonymous: Boolean) {
sikshaPrefObjects.communityIsAnonymous.setValue(isAnonymous)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class PostDetailViewModel @Inject constructor(
private val _postDetailEvent = MutableSharedFlow<PostDetailEvent>()
val postDetailEvent = _postDetailEvent.asSharedFlow()

val isAnonymous = communityRepository.isAnonymous.stateIn(viewModelScope, SharingStarted.Eagerly, true)

init {
refreshPost(PostDetailFragmentArgs.fromSavedStateHandle(savedStateHandle).postId)
}
Expand Down Expand Up @@ -99,6 +101,10 @@ class PostDetailViewModel @Inject constructor(
}
}
}

fun setIsAnonymous(isAnonymous: Boolean) {
communityRepository.setIsAnonymous(isAnonymous)
}
}

sealed interface PostDetailEvent {
Expand Down

0 comments on commit 33ac85f

Please sign in to comment.