Skip to content

Commit

Permalink
Fix null checks and crash. Fixes #60 . Fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Jan 26, 2022
1 parent 2ac1579 commit bf7967c
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
72 changes: 36 additions & 36 deletions app/src/main/java/com/jerboa/api/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -226,7 +226,7 @@ interface API {
suspend fun followCommunityWrapper(
communityView: CommunityView,
auth: String,
): CommunityResponse {
): CommunityResponse? {
var communityRes: CommunityResponse? = null
val api = API.getInstance()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -293,7 +293,7 @@ suspend fun getSiteMetadataWrapper(url: String): SiteMetadata {
e.toString()
)
}
return res!!
return res
}

suspend fun fetchPostsWrapper(
Expand Down Expand Up @@ -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()

Expand All @@ -362,7 +362,7 @@ suspend fun searchWrapper(
toastException(ctx = ctx, error = e)
}

return res!!
return res
}

suspend fun createPostWrapper(
Expand All @@ -372,7 +372,7 @@ suspend fun createPostWrapper(
body: String?,
url: String?,
name: String,
): PostView {
): PostView? {
var createdPostView: PostView? = null
val api = API.getInstance()

Expand All @@ -392,7 +392,7 @@ suspend fun createPostWrapper(
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return createdPostView!!
return createdPostView
}

suspend fun editPostWrapper(
Expand All @@ -402,7 +402,7 @@ suspend fun editPostWrapper(
body: String?,
url: String?,
name: String,
): PostView {
): PostView? {
var editedPostView: PostView? = null
val api = API.getInstance()

Expand All @@ -422,15 +422,15 @@ suspend fun editPostWrapper(
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return editedPostView!!
return editedPostView
}

suspend fun likePostWrapper(
pv: PostView,
voteType: VoteType,
account: Account,
ctx: Context,
): PostResponse {
): PostResponse? {
var updatedPost: PostResponse? = null
val api = API.getInstance()
try {
Expand All @@ -442,15 +442,15 @@ suspend fun likePostWrapper(
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return updatedPost!!
return updatedPost
}

suspend fun likeCommentWrapper(
cv: CommentView,
voteType: VoteType,
account: Account,
ctx: Context,
): CommentResponse {
): CommentResponse? {
var updatedComment: CommentResponse? = null
val api = API.getInstance()
try {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -56,7 +56,7 @@ fun saveCommentRoutine(
cv,
account,
ctx,
).comment_view
)?.comment_view
commentView.value = updatedCommentView
comments?.also {
findAndUpdateComment(comments, updatedCommentView)
Expand All @@ -78,7 +78,7 @@ fun markCommentAsReadRoutine(
cv,
account,
ctx,
).comment_view
)?.comment_view
commentView.value = updatedCommentView
comments?.also {
findAndUpdateComment(comments, updatedCommentView)
Expand All @@ -100,7 +100,7 @@ fun markPersonMentionAsReadRoutine(
pmv,
account,
ctx,
).person_mention_view
)?.person_mention_view
personMentionView.value = updatedPmv
mentions?.also {
findAndUpdateMention(mentions, updatedPmv)
Expand All @@ -122,7 +122,7 @@ fun markPrivateMessageAsReadRoutine(
pmv,
account,
ctx,
).private_message_view
)?.private_message_view
privateMessageView.value = updatedPmv
messages?.also {
findAndUpdatePrivateMessage(messages, updatedPmv)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand Down
Loading

0 comments on commit bf7967c

Please sign in to comment.