refactor: Integrate user auth for post interactions and enable post creation
This commit enhances the post detail and creation flows by integrating user authentication and activating the post creation functionality.
**Key Changes:**
* **refactor(PostDetailViewModel):**
* The `loadPostDetail` function no longer requires `userId` as a parameter. It now retrieves the current user's ID directly from the JWT using `TokenManager`.
* When creating a new comment (`CreateCommentDto`), the current `commentDateTime` (using `java.util.Date`) is now included.
* **feat(PostViewModel):**
* The previously stubbed `createPost` function has been fully implemented.
* It now makes sequential repository calls: first to `createPostMetadata` to get a new `postId`, and then to `createPostBody` with that ID.
* The UI state is updated to reflect success or failure throughout the creation process.
This commit is contained in:
parent
4325757404
commit
c5ec5b1a0b
@ -7,6 +7,7 @@ import com.qingshuige.tangyuan.model.CommentCard
|
||||
import com.qingshuige.tangyuan.model.CreateCommentDto
|
||||
import com.qingshuige.tangyuan.model.PostCard
|
||||
import com.qingshuige.tangyuan.model.PostDetailState
|
||||
import com.qingshuige.tangyuan.network.TokenManager
|
||||
import com.qingshuige.tangyuan.repository.PostDetailRepository
|
||||
import com.qingshuige.tangyuan.utils.ImageSaveUtils
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@ -16,6 +17,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.Date
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@ -24,6 +26,8 @@ class PostDetailViewModel @Inject constructor(
|
||||
@ApplicationContext private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
private val tokenManager = TokenManager()
|
||||
|
||||
private val _state = MutableStateFlow(PostDetailState())
|
||||
val state: StateFlow<PostDetailState> = _state.asStateFlow()
|
||||
|
||||
@ -33,9 +37,9 @@ class PostDetailViewModel @Inject constructor(
|
||||
/**
|
||||
* 加载帖子详情和评论 - 分离加载,先加载帖子再加载评论
|
||||
*/
|
||||
fun loadPostDetail(postId: Int, userId: Int = 0) {
|
||||
fun loadPostDetail(postId: Int) {
|
||||
currentPostId = postId
|
||||
currentUserId = userId
|
||||
currentUserId = tokenManager.getUserIdFromToken() ?: 0
|
||||
|
||||
viewModelScope.launch {
|
||||
_state.value = _state.value.copy(isLoading = true, error = null)
|
||||
@ -57,7 +61,7 @@ class PostDetailViewModel @Inject constructor(
|
||||
)
|
||||
|
||||
// 然后异步加载评论
|
||||
loadComments(postId, userId)
|
||||
loadComments(postId, currentUserId)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_state.value = _state.value.copy(
|
||||
@ -133,6 +137,7 @@ class PostDetailViewModel @Inject constructor(
|
||||
|
||||
val createCommentDto = CreateCommentDto(
|
||||
postId = currentPostId.toLong(),
|
||||
commentDateTime = Date(),
|
||||
content = content,
|
||||
parentCommentId = if (parentCommentId == 0) null else parentCommentId.toLong(),
|
||||
userId = currentUserId.toLong()
|
||||
|
||||
@ -161,13 +161,26 @@ class PostViewModel @Inject constructor(
|
||||
viewModelScope.launch {
|
||||
_postUiState.value = _postUiState.value.copy(isCreating = true, error = null)
|
||||
try {
|
||||
// TODO: Call repository createPost method
|
||||
// val postId = postRepository.createPostMetadata(metadata)
|
||||
// postRepository.createPostBody(body.copy(postId = postId))
|
||||
// _postUiState.value = _postUiState.value.copy(
|
||||
// isCreating = false,
|
||||
// createSuccess = true
|
||||
// )
|
||||
var postId: Int? = null
|
||||
|
||||
postRepository.createPostMetadata(metadata)
|
||||
.catch { e -> throw e }
|
||||
.collect { id ->
|
||||
postId = id
|
||||
|
||||
postRepository.createPostBody(body.copy(postId = id))
|
||||
.catch { e -> throw e }
|
||||
.collect { success ->
|
||||
if (success) {
|
||||
_postUiState.value = _postUiState.value.copy(
|
||||
isCreating = false,
|
||||
createSuccess = true
|
||||
)
|
||||
} else {
|
||||
throw Exception("Failed to create post body")
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_postUiState.value = _postUiState.value.copy(
|
||||
isCreating = false,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user