Skip to content

Commit

Permalink
fix: avoid using nullable types as @InjectedParams (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkesiSeli authored Dec 8, 2024
1 parent e761587 commit 386b762
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class BanUserScreen(
userId,
communityId,
newValue,
postId,
commentId,
postId ?: 0L,
commentId ?: 0L,
)
}
val uiState by model.uiState.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class BanUserViewModel(
@InjectedParam private val userId: Long,
@InjectedParam private val communityId: Long,
@InjectedParam private val newValue: Boolean,
@InjectedParam private val postId: Long?,
@InjectedParam private val commentId: Long?,
@InjectedParam private val postId: Long,
@InjectedParam private val commentId: Long,
private val identityRepository: IdentityRepository,
private val communityRepository: CommunityRepository,
private val notificationCenter: NotificationCenter,
) : BanUserMviModel,
DefaultMviModel<BanUserMviModel.Intent, BanUserMviModel.UiState, BanUserMviModel.Effect>(
) : DefaultMviModel<BanUserMviModel.Intent, BanUserMviModel.UiState, BanUserMviModel.Effect>(
initialState = BanUserMviModel.UiState(),
) {
),
BanUserMviModel {
init {
screenModelScope.launch {
updateState {
Expand Down Expand Up @@ -92,15 +92,15 @@ class BanUserViewModel(
removeData = removeData,
)
if (newUser != null) {
postId?.also {
postId.takeIf { it != 0L }?.also {
val evt =
NotificationCenterEvent.UserBannedPost(
postId = it,
user = newUser,
)
notificationCenter.send(evt)
}
commentId?.also {
commentId.takeIf { it != 0L }?.also {
val evt =
NotificationCenterEvent.UserBannedComment(
commentId = it,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.livefast.eattrash.raccoonforlemmy.unit.createcomment

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -84,16 +83,16 @@ class CreateCommentScreen(
private val editedCommentId: Long? = null,
private val initialText: String? = null,
) : Screen {
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Content() {
val model =
getScreenModel<CreateCommentMviModel> {
parametersOf(
originalPostId,
originalCommentId,
editedCommentId,
draftId,
originalPostId ?: 0L,
originalCommentId ?: 0L,
editedCommentId ?: 0L,
draftId ?: 0L,
)
}
val uiState by model.uiState.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import org.koin.core.annotation.InjectedParam

@Factory(binds = [CreateCommentMviModel::class])
class CreateCommentViewModel(
@InjectedParam private val postId: Long?,
@InjectedParam private val parentId: Long?,
@InjectedParam private val editedCommentId: Long?,
@InjectedParam private val draftId: Long?,
@InjectedParam private val postId: Long,
@InjectedParam private val parentId: Long,
@InjectedParam private val editedCommentId: Long,
@InjectedParam private val draftId: Long,
private val identityRepository: IdentityRepository,
private val commentRepository: CommentRepository,
private val postRepository: PostRepository,
Expand Down Expand Up @@ -188,7 +188,7 @@ class CreateCommentViewModel(
try {
val auth = identityRepository.authToken.value.orEmpty()
when {
editedCommentId != null -> {
editedCommentId != 0L -> {
commentRepository.edit(
commentId = editedCommentId,
text = text,
Expand All @@ -197,7 +197,7 @@ class CreateCommentViewModel(
)
}

postId != null -> {
postId != 0L -> {
commentRepository.create(
postId = postId,
parentId = parentId,
Expand All @@ -209,10 +209,10 @@ class CreateCommentViewModel(
}
// the comment count has changed, emits update
emitPostUpdateNotification()
if (draftId != null) {
if (draftId != 0L) {
deleteDraft()
}
emitEffect(CreateCommentMviModel.Effect.Success(new = editedCommentId == null))
emitEffect(CreateCommentMviModel.Effect.Success(new = editedCommentId == 0L))
} catch (e: Throwable) {
val message = e.message
emitEffect(CreateCommentMviModel.Effect.Failure(message))
Expand All @@ -223,7 +223,7 @@ class CreateCommentViewModel(
}

private suspend fun emitPostUpdateNotification() {
val postId = postId ?: return
val postId = postId.takeIf { it != 0L } ?: return
val auth = identityRepository.authToken.value
val newPost = postRepository.get(postId, auth)
if (newPost != null) {
Expand Down Expand Up @@ -287,7 +287,7 @@ class CreateCommentViewModel(
currentState.originalPost?.title
},
)
if (draftId == null) {
if (draftId == 0L) {
draftRepository.create(
model = draft,
accountId = accountId,
Expand All @@ -301,9 +301,9 @@ class CreateCommentViewModel(
}

private suspend fun deleteDraft() {
draftId?.also { id ->
draftRepository.delete(id)
notificationCenter.send(NotificationCenterEvent.DraftDeleted)
if (draftId != 0L) {
draftRepository.delete(draftId)
}
notificationCenter.send(NotificationCenterEvent.DraftDeleted)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ class CreatePostScreen(
override fun Content() {
val model =
getScreenModel<CreatePostMviModel> {
parametersOf(editedPostId, crossPostId, draftId)
parametersOf(
editedPostId ?: 0L,
crossPostId ?: 0L,
draftId ?: 0L,
)
}
val uiState by model.uiState.collectAsState()
val snackbarHostState = remember { SnackbarHostState() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import org.koin.core.annotation.InjectedParam

@Factory(binds = [CreatePostMviModel::class])
class CreatePostViewModel(
@InjectedParam private val editedPostId: Long?,
@InjectedParam private val crossPostId: Long?,
@InjectedParam private val draftId: Long?,
@InjectedParam private val editedPostId: Long,
@InjectedParam private val crossPostId: Long,
@InjectedParam private val draftId: Long,
private val identityRepository: IdentityRepository,
private val postRepository: PostRepository,
private val mediaRepository: MediaRepository,
Expand All @@ -55,11 +55,11 @@ class CreatePostViewModel(
init {
screenModelScope.launch {
val editedPost =
editedPostId?.let {
editedPostId.takeIf { it != 0L }?.let {
itemCache.getPost(it)
}
val crossPost =
crossPostId?.let {
crossPostId.takeIf { it != 0L }?.let {
itemCache.getPost(it)
}
updateState { it.copy(editedPost = editedPost, crossPost = crossPost) }
Expand Down Expand Up @@ -304,7 +304,7 @@ class CreatePostViewModel(
try {
val auth = identityRepository.authToken.value.orEmpty()
when {
editedPostId != null -> {
editedPostId != 0L -> {
postRepository.edit(
postId = editedPostId,
title = title,
Expand All @@ -328,7 +328,7 @@ class CreatePostViewModel(
)
}
}
if (draftId != null) {
if (draftId != 0L) {
deleteDraft()
}
emitEffect(CreatePostMviModel.Effect.Success)
Expand Down Expand Up @@ -374,7 +374,7 @@ class CreatePostViewModel(
date = epochMillis(),
reference = community?.name,
)
if (draftId == null) {
if (draftId == 0L) {
draftRepository.create(
model = draft,
accountId = accountId,
Expand All @@ -388,8 +388,8 @@ class CreatePostViewModel(
}

private suspend fun deleteDraft() {
draftId?.also { id ->
draftRepository.delete(id)
if (draftId != 0L) {
draftRepository.delete(draftId)
notificationCenter.send(NotificationCenterEvent.DraftDeleted)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ModlogScreen(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Content() {
val model = getScreenModel<ModlogMviModel> { parametersOf(communityId) }
val model = getScreenModel<ModlogMviModel> { parametersOf(communityId ?: 0L) }
val uiState by model.uiState.collectAsState()
val topAppBarState = rememberTopAppBarState()
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(topAppBarState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.koin.core.annotation.InjectedParam

@Factory(binds = [ModlogMviModel::class])
class ModlogViewModel(
@InjectedParam private val communityId: Long?,
@InjectedParam private val communityId: Long,
private val themeRepository: ThemeRepository,
private val identityRepository: IdentityRepository,
private val modlogRepository: ModlogRepository,
Expand Down Expand Up @@ -87,7 +87,7 @@ class ModlogViewModel(
val itemList =
modlogRepository.getItems(
auth = auth,
communityId = communityId,
communityId = communityId.takeIf { it != 0L },
page = currentPage,
)
val itemsToAdd = itemList.orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class PostDetailScreen(
parametersOf(
postId,
otherInstance,
highlightCommentId,
highlightCommentId ?: 0L,
isMod,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import org.koin.core.annotation.InjectedParam
class PostDetailViewModel(
@InjectedParam postId: Long,
@InjectedParam private val otherInstance: String,
@InjectedParam private val highlightCommentId: Long?,
@InjectedParam private val highlightCommentId: Long,
@InjectedParam private val isModerator: Boolean,
private val identityRepository: IdentityRepository,
private val apiConfigurationRepository: ApiConfigurationRepository,
Expand Down Expand Up @@ -241,7 +241,7 @@ class PostDetailViewModel(
)
}

if (highlightCommentId != null) {
if (highlightCommentId != 0L) {
val comment =
commentRepository.getBy(
id = highlightCommentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ReportListScreen(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Content() {
val model = getScreenModel<ReportListMviModel> { parametersOf(communityId) }
val model = getScreenModel<ReportListMviModel> { parametersOf(communityId ?: 0L) }
val uiState by model.uiState.collectAsState()
val topAppBarState = rememberTopAppBarState()
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(topAppBarState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.koin.core.annotation.InjectedParam

@Factory(binds = [ReportListMviModel::class])
class ReportListViewModel(
@InjectedParam private val communityId: Long?,
@InjectedParam private val communityId: Long,
private val identityRepository: IdentityRepository,
private val postRepository: PostRepository,
private val commentRepository: CommentRepository,
Expand Down Expand Up @@ -151,7 +151,7 @@ class ReportListViewModel(
async {
postRepository.getReports(
auth = auth,
communityId = communityId,
communityId = communityId.takeIf { it != 0L },
page = page,
unresolvedOnly = unresolvedOnly,
)
Expand All @@ -164,7 +164,7 @@ class ReportListViewModel(
commentRepository
.getReports(
auth = auth,
communityId = communityId,
communityId = communityId.takeIf { it != 0L },
page = 1,
unresolvedOnly = unresolvedOnly,
).orEmpty()
Expand Down Expand Up @@ -197,7 +197,7 @@ class ReportListViewModel(
val itemList =
commentRepository.getReports(
auth = auth,
communityId = communityId,
communityId = communityId.takeIf { it != 0L },
page = page,
unresolvedOnly = unresolvedOnly,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ class UserDetailScreen(
val model =
getScreenModel<UserDetailMviModel>(
tag = userId.toString(),
parameters = { parametersOf(userId, otherInstance) },
parameters = {
parametersOf(
userId,
otherInstance,
)
},
)
val uiState by model.uiState.collectAsState()
val lazyListState = rememberLazyListState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import org.koin.core.annotation.InjectedParam
@Factory(binds = [UserDetailMviModel::class])
class UserDetailViewModel(
@InjectedParam private val userId: Long,
@InjectedParam private val otherInstance: String = "",
@InjectedParam private val otherInstance: String,
private val identityRepository: IdentityRepository,
private val apiConfigurationRepository: ApiConfigurationRepository,
private val postPaginationManager: PostPaginationManager,
Expand Down

0 comments on commit 386b762

Please sign in to comment.