更新
This commit is contained in:
@@ -2,6 +2,8 @@ package com.aiosman.riderpro.ui.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -29,14 +31,17 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import coil.compose.AsyncImage
|
||||
import com.aiosman.riderpro.data.AccountProfileEntity
|
||||
import com.aiosman.riderpro.data.AccountService
|
||||
import com.aiosman.riderpro.data.TestAccountServiceImpl
|
||||
import com.aiosman.riderpro.data.TestUserServiceImpl
|
||||
import com.aiosman.riderpro.data.UploadImage
|
||||
import com.aiosman.riderpro.data.UserService
|
||||
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
|
||||
import com.aiosman.riderpro.ui.post.NewPostViewModel.uriToFile
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -46,12 +51,14 @@ fun AccountEditScreen() {
|
||||
val accountService: AccountService = TestAccountServiceImpl()
|
||||
var name by remember { mutableStateOf("") }
|
||||
var bio by remember { mutableStateOf("") }
|
||||
var imageUrl by remember { mutableStateOf<Uri?>(null) }
|
||||
var profile by remember {
|
||||
mutableStateOf<AccountProfileEntity?>(
|
||||
null
|
||||
)
|
||||
}
|
||||
val scope = rememberCoroutineScope()
|
||||
val context = LocalContext.current
|
||||
suspend fun reloadProfile() {
|
||||
|
||||
accountService.getMyAccountProfile().let {
|
||||
@@ -72,7 +79,25 @@ fun AccountEditScreen() {
|
||||
}
|
||||
fun updateUserProfile() {
|
||||
scope.launch {
|
||||
accountService.updateProfile(name, bio)
|
||||
val newAvatar = imageUrl?.let {
|
||||
val cursor = context.contentResolver.query(it, null, null, null, null)
|
||||
var newAvatar: UploadImage? = null
|
||||
cursor?.use {cur ->
|
||||
if (cur.moveToFirst()) {
|
||||
val displayName = cur.getString(cur.getColumnIndex("_display_name"))
|
||||
val extension = displayName.substringAfterLast(".")
|
||||
Log.d("NewPost", "File name: $displayName, extension: $extension")
|
||||
// read as file
|
||||
val file = uriToFile(context, it)
|
||||
Log.d("NewPost", "File size: ${file.length()}")
|
||||
newAvatar = UploadImage(file, displayName, it.toString(), extension)
|
||||
}
|
||||
}
|
||||
newAvatar
|
||||
}
|
||||
val newName = if (name == profile?.nickName) null else name
|
||||
|
||||
accountService.updateProfile(newAvatar, newName, bio)
|
||||
reloadProfile()
|
||||
}
|
||||
}
|
||||
@@ -83,7 +108,7 @@ fun AccountEditScreen() {
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val uri = result.data?.data
|
||||
uri?.let {
|
||||
updateUserAvatar(it.toString())
|
||||
imageUrl = it
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,7 +145,11 @@ fun AccountEditScreen() {
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
AsyncImage(
|
||||
it.avatar,
|
||||
if (imageUrl != null) {
|
||||
imageUrl.toString()
|
||||
} else {
|
||||
it.avatar
|
||||
},
|
||||
contentDescription = null,
|
||||
modifier = Modifier
|
||||
.size(100.dp)
|
||||
|
||||
@@ -297,7 +297,7 @@ fun MomentTopRowGroup(momentEntity: MomentEntity) {
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
MomentName(momentEntity.nickname)
|
||||
MomentFollowBtn()
|
||||
// MomentFollowBtn()
|
||||
}
|
||||
Row(
|
||||
modifier = Modifier
|
||||
|
||||
@@ -84,6 +84,8 @@ import com.aiosman.riderpro.data.TestCommentServiceImpl
|
||||
import com.aiosman.riderpro.data.MomentService
|
||||
import com.aiosman.riderpro.data.TestAccountServiceImpl
|
||||
import com.aiosman.riderpro.data.TestMomentServiceImpl
|
||||
import com.aiosman.riderpro.data.TestUserServiceImpl
|
||||
import com.aiosman.riderpro.data.UserService
|
||||
import com.aiosman.riderpro.model.MomentEntity
|
||||
import com.aiosman.riderpro.ui.NavigationRoute
|
||||
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
|
||||
@@ -103,6 +105,7 @@ class PostViewModel(
|
||||
) : ViewModel() {
|
||||
var service: MomentService = TestMomentServiceImpl()
|
||||
var commentService: CommentService = TestCommentServiceImpl()
|
||||
var userService : UserService = TestUserServiceImpl()
|
||||
private var _commentsFlow = MutableStateFlow<PagingData<CommentEntity>>(PagingData.empty())
|
||||
val commentsFlow = _commentsFlow.asStateFlow()
|
||||
|
||||
@@ -138,7 +141,7 @@ class PostViewModel(
|
||||
val currentPagingData = commentsFlow.value
|
||||
val updatedPagingData = currentPagingData.map { comment ->
|
||||
if (comment.id == commentId) {
|
||||
comment.copy(liked = !comment.liked)
|
||||
comment.copy(liked = !comment.liked, likes = comment.likes + 1)
|
||||
} else {
|
||||
comment
|
||||
}
|
||||
@@ -151,7 +154,7 @@ class PostViewModel(
|
||||
val currentPagingData = commentsFlow.value
|
||||
val updatedPagingData = currentPagingData.map { comment ->
|
||||
if (comment.id == commentId) {
|
||||
comment.copy(liked = !comment.liked)
|
||||
comment.copy(liked = !comment.liked, likes = comment.likes - 1)
|
||||
} else {
|
||||
comment
|
||||
}
|
||||
@@ -180,6 +183,39 @@ class PostViewModel(
|
||||
MomentViewModel.updateDislikeMomentById(it.id)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun favoriteMoment() {
|
||||
moment?.let {
|
||||
service.favoriteMoment(it.id)
|
||||
moment =
|
||||
moment?.copy(favoriteCount = moment?.favoriteCount?.plus(1) ?: 0, isFavorite = true)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun unfavoriteMoment() {
|
||||
moment?.let {
|
||||
service.unfavoriteMoment(it.id)
|
||||
moment = moment?.copy(
|
||||
favoriteCount = moment?.favoriteCount?.minus(1) ?: 0,
|
||||
isFavorite = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun followUser() {
|
||||
accountProfileEntity?.let {
|
||||
userService.followUser(it.id.toString())
|
||||
accountProfileEntity = accountProfileEntity?.copy(isFollowing = true)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun unfollowUser() {
|
||||
accountProfileEntity?.let {
|
||||
userService.unFollowUser(it.id.toString())
|
||||
accountProfileEntity = accountProfileEntity?.copy(isFollowing = false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -224,6 +260,15 @@ fun PostScreen(
|
||||
commentsPagging.refresh()
|
||||
}
|
||||
},
|
||||
onFavoriteClick = {
|
||||
scope.launch {
|
||||
if (viewModel.moment?.isFavorite == true) {
|
||||
viewModel.unfavoriteMoment()
|
||||
} else {
|
||||
viewModel.favoriteMoment()
|
||||
}
|
||||
}
|
||||
},
|
||||
momentEntity = viewModel.moment
|
||||
)
|
||||
}
|
||||
@@ -236,7 +281,17 @@ fun PostScreen(
|
||||
Header(
|
||||
avatar = viewModel.moment?.avatar,
|
||||
nickname = viewModel.moment?.nickname,
|
||||
userId = viewModel.moment?.authorId
|
||||
userId = viewModel.moment?.authorId,
|
||||
isFollowing = viewModel.accountProfileEntity?.isFollowing ?: false,
|
||||
onFollowClick = {
|
||||
scope.launch {
|
||||
if (viewModel.accountProfileEntity?.isFollowing == true) {
|
||||
viewModel.unfollowUser()
|
||||
} else {
|
||||
viewModel.followUser()
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
Column(modifier = Modifier.animateContentSize()) {
|
||||
AnimatedVisibility(visible = showCollapseContent) {
|
||||
@@ -290,7 +345,13 @@ fun PostScreen(
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Header(avatar: String?, nickname: String?, userId: Int?) {
|
||||
fun Header(
|
||||
avatar: String?,
|
||||
nickname: String?,
|
||||
userId: Int?,
|
||||
isFollowing: Boolean,
|
||||
onFollowClick: () -> Unit
|
||||
) {
|
||||
val navController = LocalNavController.current
|
||||
Row(
|
||||
modifier = Modifier
|
||||
@@ -336,7 +397,9 @@ fun Header(avatar: String?, nickname: String?, userId: Int?) {
|
||||
modifier = Modifier
|
||||
.height(20.dp)
|
||||
.wrapContentWidth()
|
||||
.padding(start = 6.dp),
|
||||
.padding(start = 6.dp).noRippleClickable {
|
||||
onFollowClick()
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Image(
|
||||
@@ -345,7 +408,7 @@ fun Header(avatar: String?, nickname: String?, userId: Int?) {
|
||||
contentDescription = ""
|
||||
)
|
||||
Text(
|
||||
text = "FOLLOW",
|
||||
text = if (isFollowing) "Following" else "Follow",
|
||||
fontSize = 12.sp,
|
||||
color = Color.White,
|
||||
style = TextStyle(fontWeight = FontWeight.Bold)
|
||||
@@ -530,6 +593,7 @@ fun CommentItem(commentEntity: CommentEntity, onLike: () -> Unit = {}) {
|
||||
fun BottomNavigationBar(
|
||||
onCreateComment: (String) -> Unit = {},
|
||||
onLikeClick: () -> Unit = {},
|
||||
onFavoriteClick: () -> Unit = {},
|
||||
momentEntity: MomentEntity?
|
||||
) {
|
||||
val systemUiController = rememberSystemUiController()
|
||||
@@ -593,15 +657,22 @@ fun BottomNavigationBar(
|
||||
}
|
||||
Text(text = momentEntity?.likeCount.toString())
|
||||
IconButton(
|
||||
onClick = { /*TODO*/ }) {
|
||||
Icon(Icons.Filled.Star, contentDescription = "Send")
|
||||
onClick = {
|
||||
onFavoriteClick()
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
Icons.Filled.Star,
|
||||
contentDescription = "Favourite",
|
||||
tint = if (momentEntity?.isFavorite == true) Color.Red else Color.Gray
|
||||
)
|
||||
}
|
||||
Text(text = "2077")
|
||||
IconButton(
|
||||
onClick = { /*TODO*/ }) {
|
||||
Icon(Icons.Filled.CheckCircle, contentDescription = "Send")
|
||||
}
|
||||
Text(text = "2077")
|
||||
Text(text = momentEntity?.favoriteCount.toString())
|
||||
// IconButton(
|
||||
// onClick = { /*TODO*/ }) {
|
||||
// Icon(Icons.Filled.CheckCircle, contentDescription = "Send")
|
||||
// }
|
||||
// Text(text = "2077")
|
||||
|
||||
}
|
||||
BottomNavigationPlaceholder(
|
||||
|
||||
Reference in New Issue
Block a user