-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Adding ability to remove and restore comments. #1182 * Fix weird formatting miss. * Use ImmutableList instead of List. * Surrounding canMod with remember. * Fixing formatting.
- Loading branch information
1 parent
21e2222
commit 47ee753
Showing
18 changed files
with
424 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.jerboa.model | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.focus.FocusManager | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.jerboa.R | ||
import com.jerboa.api.API | ||
import com.jerboa.api.ApiState | ||
import com.jerboa.api.toApiState | ||
import com.jerboa.ui.components.common.apiErrorToast | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentResponse | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentView | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.RemoveComment | ||
import kotlinx.coroutines.launch | ||
|
||
class RemoveViewModel : ViewModel() { | ||
var commentRemoveRes: ApiState<CommentResponse> by mutableStateOf(ApiState.Empty) | ||
private set | ||
|
||
fun removeOrRestoreComment( | ||
commentId: Int, | ||
removed: Boolean, | ||
reason: String, | ||
ctx: Context, | ||
focusManager: FocusManager, | ||
onSuccess: (CommentView) -> Unit, | ||
) { | ||
viewModelScope.launch { | ||
val form = | ||
RemoveComment( | ||
comment_id = commentId, | ||
removed = removed, | ||
reason = reason, | ||
) | ||
|
||
commentRemoveRes = ApiState.Loading | ||
commentRemoveRes = API.getInstance().removeComment(form).toApiState() | ||
|
||
when (val res = commentRemoveRes) { | ||
is ApiState.Failure -> { | ||
Log.d("removeComment", "failed", res.msg) | ||
apiErrorToast(msg = res.msg, ctx = ctx) | ||
} | ||
|
||
is ApiState.Success -> { | ||
val message = | ||
if (removed) { | ||
ctx.getString(R.string.remove_view_model_comment_removed) | ||
} else { | ||
ctx.getString(R.string.remove_view_model_comment_restored) | ||
} | ||
val commentView = res.data.comment_view | ||
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show() | ||
|
||
focusManager.clearFocus() | ||
onSuccess(commentView) | ||
} | ||
else -> {} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.