diff --git a/app/src/main/java/com/jerboa/Utils.kt b/app/src/main/java/com/jerboa/Utils.kt index 67f02ea07..7425ffe7d 100644 --- a/app/src/main/java/com/jerboa/Utils.kt +++ b/app/src/main/java/com/jerboa/Utils.kt @@ -40,7 +40,7 @@ val prettyTime = PrettyTime(Locale.getDefault()) val gson = Gson() -const val LAUNCH_DELAY = 1500L +const val LAUNCH_DELAY = 300L const val MAX_POST_TITLE_LENGTH = 200 val DEFAULT_LEMMY_INSTANCES = listOf( diff --git a/app/src/main/java/com/jerboa/api/Http.kt b/app/src/main/java/com/jerboa/api/Http.kt index 2cd4af89a..e9337e7aa 100644 --- a/app/src/main/java/com/jerboa/api/Http.kt +++ b/app/src/main/java/com/jerboa/api/Http.kt @@ -208,7 +208,7 @@ interface API { return api!! } - private fun buildApi(): API? { + private fun buildApi(): API { val interceptor = HttpLoggingInterceptor() interceptor.level = HttpLoggingInterceptor.Level.BODY val client: OkHttpClient = OkHttpClient.Builder().addInterceptor(interceptor).build() @@ -226,7 +226,7 @@ interface API { suspend fun followCommunityWrapper( communityView: CommunityView, auth: String, -): CommunityResponse { +): CommunityResponse? { var communityRes: CommunityResponse? = null val api = API.getInstance() @@ -249,10 +249,10 @@ suspend fun followCommunityWrapper( e.toString() ) } - return communityRes!! + return communityRes } -suspend fun getSiteWrapper(auth: String?): GetSiteResponse { +suspend fun getSiteWrapper(auth: String?): GetSiteResponse? { var siteRes: GetSiteResponse? = null val api = API.getInstance() @@ -271,10 +271,10 @@ suspend fun getSiteWrapper(auth: String?): GetSiteResponse { e.toString() ) } - return siteRes!! + return siteRes } -suspend fun getSiteMetadataWrapper(url: String): SiteMetadata { +suspend fun getSiteMetadataWrapper(url: String): SiteMetadata? { var res: SiteMetadata? = null val api = API.getInstance() @@ -293,7 +293,7 @@ suspend fun getSiteMetadataWrapper(url: String): SiteMetadata { e.toString() ) } - return res!! + return res } suspend fun fetchPostsWrapper( @@ -338,7 +338,7 @@ suspend fun searchWrapper( page: Int? = null, query: String, creatorId: Int? = null, -): SearchResponse { +): SearchResponse? { var res: SearchResponse? = null val api = API.getInstance() @@ -362,7 +362,7 @@ suspend fun searchWrapper( toastException(ctx = ctx, error = e) } - return res!! + return res } suspend fun createPostWrapper( @@ -372,7 +372,7 @@ suspend fun createPostWrapper( body: String?, url: String?, name: String, -): PostView { +): PostView? { var createdPostView: PostView? = null val api = API.getInstance() @@ -392,7 +392,7 @@ suspend fun createPostWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return createdPostView!! + return createdPostView } suspend fun editPostWrapper( @@ -402,7 +402,7 @@ suspend fun editPostWrapper( body: String?, url: String?, name: String, -): PostView { +): PostView? { var editedPostView: PostView? = null val api = API.getInstance() @@ -422,7 +422,7 @@ suspend fun editPostWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return editedPostView!! + return editedPostView } suspend fun likePostWrapper( @@ -430,7 +430,7 @@ suspend fun likePostWrapper( voteType: VoteType, account: Account, ctx: Context, -): PostResponse { +): PostResponse? { var updatedPost: PostResponse? = null val api = API.getInstance() try { @@ -442,7 +442,7 @@ suspend fun likePostWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedPost!! + return updatedPost } suspend fun likeCommentWrapper( @@ -450,7 +450,7 @@ suspend fun likeCommentWrapper( voteType: VoteType, account: Account, ctx: Context, -): CommentResponse { +): CommentResponse? { var updatedComment: CommentResponse? = null val api = API.getInstance() try { @@ -462,14 +462,14 @@ suspend fun likeCommentWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedComment!! + return updatedComment } suspend fun savePostWrapper( pv: PostView, account: Account, ctx: Context, -): PostResponse { +): PostResponse? { var updatedPost: PostResponse? = null val api = API.getInstance() try { @@ -480,14 +480,14 @@ suspend fun savePostWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedPost!! + return updatedPost } suspend fun saveCommentWrapper( cv: CommentView, account: Account, ctx: Context, -): CommentResponse { +): CommentResponse? { var updatedComment: CommentResponse? = null val api = API.getInstance() try { @@ -498,14 +498,14 @@ suspend fun saveCommentWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedComment!! + return updatedComment } suspend fun markCommentAsReadWrapper( cv: CommentView, account: Account, ctx: Context, -): CommentResponse { +): CommentResponse? { var updatedComment: CommentResponse? = null val api = API.getInstance() try { @@ -516,14 +516,14 @@ suspend fun markCommentAsReadWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedComment!! + return updatedComment } suspend fun markPersonMentionAsReadWrapper( personMentionView: PersonMentionView, account: Account, ctx: Context, -): PersonMentionResponse { +): PersonMentionResponse? { var updatedPm: PersonMentionResponse? = null val api = API.getInstance() try { @@ -537,14 +537,14 @@ suspend fun markPersonMentionAsReadWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedPm!! + return updatedPm } suspend fun markPrivateMessageAsReadWrapper( pm: PrivateMessageView, account: Account, ctx: Context, -): PrivateMessageResponse { +): PrivateMessageResponse? { var updatedPm: PrivateMessageResponse? = null val api = API.getInstance() try { @@ -557,13 +557,13 @@ suspend fun markPrivateMessageAsReadWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return updatedPm!! + return updatedPm } suspend fun createCommentWrapper( form: CreateComment, ctx: Context, -): CommentResponse { +): CommentResponse? { var createdComment: CommentResponse? = null val api = API.getInstance() @@ -573,13 +573,13 @@ suspend fun createCommentWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return createdComment!! + return createdComment } suspend fun editCommentWrapper( form: EditComment, ctx: Context, -): CommentResponse { +): CommentResponse? { var editedComment: CommentResponse? = null val api = API.getInstance() @@ -589,13 +589,13 @@ suspend fun editCommentWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return editedComment!! + return editedComment } suspend fun createPrivateMessageWrapper( form: CreatePrivateMessage, ctx: Context, -): PrivateMessageResponse { +): PrivateMessageResponse? { var createdPrivateMessage: PrivateMessageResponse? = null val api = API.getInstance() @@ -605,10 +605,10 @@ suspend fun createPrivateMessageWrapper( } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return createdPrivateMessage!! + return createdPrivateMessage } -suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Context): String { +suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Context): String? { var imageUrl: String? = null val api = API.getInstance() try { @@ -622,11 +622,11 @@ suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Conte val cookie = "jwt=${account.jwt}" val images = api.uploadImage(url, cookie, part) Log.d("jerboa", "Uploading done.") - imageUrl = "$url/${images.files!![0].file}" + imageUrl = "$url/${images.files?.get(0)?.file}" } catch (e: Exception) { toastException(ctx = ctx, error = e) } - return imageUrl!! + return imageUrl } // diff --git a/app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt b/app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt index 8de81d36a..1d7cb1223 100644 --- a/app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt +++ b/app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt @@ -34,7 +34,7 @@ fun likeCommentRoutine( val updatedCommentView = likeCommentWrapper( cv, voteType, account, ctx - ).comment_view + )?.comment_view commentView.value = updatedCommentView comments?.also { findAndUpdateComment(comments, updatedCommentView) @@ -56,7 +56,7 @@ fun saveCommentRoutine( cv, account, ctx, - ).comment_view + )?.comment_view commentView.value = updatedCommentView comments?.also { findAndUpdateComment(comments, updatedCommentView) @@ -78,7 +78,7 @@ fun markCommentAsReadRoutine( cv, account, ctx, - ).comment_view + )?.comment_view commentView.value = updatedCommentView comments?.also { findAndUpdateComment(comments, updatedCommentView) @@ -100,7 +100,7 @@ fun markPersonMentionAsReadRoutine( pmv, account, ctx, - ).person_mention_view + )?.person_mention_view personMentionView.value = updatedPmv mentions?.also { findAndUpdateMention(mentions, updatedPmv) @@ -122,7 +122,7 @@ fun markPrivateMessageAsReadRoutine( pmv, account, ctx, - ).private_message_view + )?.private_message_view privateMessageView.value = updatedPmv messages?.also { findAndUpdatePrivateMessage(messages, updatedPmv) @@ -153,17 +153,19 @@ fun createCommentRoutine( post_id = postId, auth = account.jwt ) - val commentView = createCommentWrapper(form, ctx).comment_view + val commentView = createCommentWrapper(form, ctx)?.comment_view loading.value = false focusManager.clearFocus() // Add to all the views which might have your comment - addCommentToMutableList(postViewModel.comments, commentView) + if (commentView != null) { + addCommentToMutableList(postViewModel.comments, commentView) - // Maybe a back button would view this page. - if (account.id == personProfileViewModel.personId.value) { - addCommentToMutableList(personProfileViewModel.comments, commentView) + // Maybe a back button would view this page. + if (account.id == personProfileViewModel.personId.value) { + addCommentToMutableList(personProfileViewModel.comments, commentView) + } } // Mark as read if you replied to it, and the grandparent is you @@ -199,7 +201,7 @@ fun editCommentRoutine( comment_id = cv.comment.id, auth = account.jwt ) - commentView.value = editCommentWrapper(form, ctx).comment_view + commentView.value = editCommentWrapper(form, ctx)?.comment_view loading.value = false focusManager.clearFocus() @@ -224,8 +226,8 @@ fun createPrivateMessageRoutine( ) { scope.launch { loading.value = true - val pmView = createPrivateMessageWrapper(form, ctx).private_message_view - messages?.add(0, pmView) + val pmView = createPrivateMessageWrapper(form, ctx)?.private_message_view + pmView?.let { messages?.add(0, it) } loading.value = false focusManager.clearFocus() navController.navigateUp() diff --git a/app/src/main/java/com/jerboa/ui/components/comment/edit/CommentEditActivity.kt b/app/src/main/java/com/jerboa/ui/components/comment/edit/CommentEditActivity.kt index 1831ad5f3..5288363ea 100644 --- a/app/src/main/java/com/jerboa/ui/components/comment/edit/CommentEditActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/comment/edit/CommentEditActivity.kt @@ -69,7 +69,9 @@ fun CommentEditActivity( scope.launch { account?.also { acct -> val url = uploadPictrsImage(acct, imageIs, ctx) - content = appendMarkdownImage(content, url) + url?.also { + content = appendMarkdownImage(content, it) + } } } }, diff --git a/app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt b/app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt index 0df1dd034..2a9c64e8d 100644 --- a/app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt @@ -89,7 +89,9 @@ fun CommentReplyActivity( scope.launch { account?.also { acct -> val url = uploadPictrsImage(acct, imageIs, ctx) - reply = appendMarkdownImage(reply, url) + url?.also { + reply = appendMarkdownImage(reply, it) + } } } }, @@ -115,7 +117,9 @@ fun CommentReplyActivity( scope.launch { account?.also { acct -> val url = uploadPictrsImage(acct, imageIs, ctx) - reply = appendMarkdownImage(reply, url) + url?.also { + reply = appendMarkdownImage(reply, it) + } } } }, diff --git a/app/src/main/java/com/jerboa/ui/components/community/CommunityViewModel.kt b/app/src/main/java/com/jerboa/ui/components/community/CommunityViewModel.kt index dfaf82e22..37e55cf8f 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/CommunityViewModel.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/CommunityViewModel.kt @@ -77,7 +77,7 @@ class CommunityViewModel : ViewModel() { viewModelScope.launch { account?.also { acct -> communityView = - followCommunityWrapper(communityView = cv, auth = acct.jwt).community_view + followCommunityWrapper(communityView = cv, auth = acct.jwt)?.community_view } } } diff --git a/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListViewModel.kt b/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListViewModel.kt index 31c70a121..7bb95e293 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListViewModel.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/list/CommunityListViewModel.kt @@ -34,9 +34,9 @@ class CommunityListViewModel : ViewModel() { listingType = ListingType.All, query = query, searchType = SearchType.Communities, - ).communities + )?.communities communityList.clear() - communityList.addAll(communities) + communityList.addAll(communities.orEmpty()) } } diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostRoutines.kt b/app/src/main/java/com/jerboa/ui/components/post/PostRoutines.kt index 7ea6cef3d..18a4f5226 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostRoutines.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostRoutines.kt @@ -84,7 +84,7 @@ fun likePostRoutine( val updatedPostView = likePostWrapper( pv, voteType, account, ctx - ).post_view + )?.post_view postView.value = updatedPostView posts?.also { findAndUpdatePost(posts, updatedPostView) @@ -108,7 +108,7 @@ fun savePostRoutine( pv, account, ctx, - ).post_view + )?.post_view postView.value = updatedPostView posts?.also { findAndUpdatePost(posts, updatedPostView) diff --git a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt index 2ebe8caec..20ea25536 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt @@ -96,7 +96,7 @@ fun CreatePostActivity( val imageIs = imageInputStreamFromUri(ctx, uri) scope.launch { account?.also { acct -> - url = uploadPictrsImage(acct, imageIs, ctx) + url = uploadPictrsImage(acct, imageIs, ctx).orEmpty() } } } diff --git a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostViewModel.kt b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostViewModel.kt index efdff7d26..5fcfb8758 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostViewModel.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostViewModel.kt @@ -38,8 +38,9 @@ class CreatePostViewModel : ViewModel() { name = name, ctx = ctx, ) + // TODO not sure here postViewModel.fetchPost( - id = postOut.post.id, clear = true, account = account, + id = postOut!!.post.id, clear = true, account = account, ctx = ctx ) loading = false @@ -50,7 +51,7 @@ class CreatePostViewModel : ViewModel() { fun fetchSuggestedTitle(url: String) { viewModelScope.launch { - suggestedTitle = getSiteMetadataWrapper(url).title + suggestedTitle = getSiteMetadataWrapper(url)?.title } } } diff --git a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt index b9f14ffa0..1af50e015 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt @@ -93,7 +93,7 @@ fun PostEditActivity( val imageIs = imageInputStreamFromUri(ctx, uri) scope.launch { account?.also { acct -> - url = uploadPictrsImage(acct, imageIs, ctx) + url = uploadPictrsImage(acct, imageIs, ctx).orEmpty() } } }