-
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.
Add ability to Ban and Ban from Community for posts and comments. (#1325
) * Adding ability to remove and restore comments. #1182 * Fix weird formatting miss. * Use ImmutableList instead of List. * Surrounding canMod with remember. * Adding ability to remove and restore posts. #1182 * Fixing jerboaappstate. * Removing extra RemoveViewModel. * Changing remove icon to gavel. * Change a few strings to non-contextual. * Add remove comment to commentmentionnode. * Remove non-optional body from compiler warning. * Adding ban and ban from community. - Adding appropriate viewModels and activities. - Adds functions to search for and update lists of posts and comments - Context: #1182 * Fixing some formatting issues. * Adding admin key to canMod remember. * Simplifying futureDaysToUnixTime function.
- Loading branch information
1 parent
39be12c
commit d1b348e
Showing
34 changed files
with
1,104 additions
and
62 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
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
95 changes: 95 additions & 0 deletions
95
app/src/main/java/com/jerboa/model/BanFromCommunityViewModel.kt
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,95 @@ | ||
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.communityNameShown | ||
import com.jerboa.datatypes.BanFromCommunityData | ||
import com.jerboa.futureDaysToUnixTime | ||
import com.jerboa.personNameShown | ||
import com.jerboa.ui.components.common.apiErrorToast | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.BanFromCommunity | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.BanFromCommunityResponse | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.Community | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.PersonId | ||
import kotlinx.coroutines.launch | ||
|
||
class BanFromCommunityViewModel : ViewModel() { | ||
var banFromCommunityRes: ApiState<BanFromCommunityResponse> by mutableStateOf(ApiState.Empty) | ||
private set | ||
|
||
fun banOrUnbanFromCommunity( | ||
personId: PersonId, | ||
community: Community, | ||
ban: Boolean, | ||
removeData: Boolean? = null, | ||
reason: String, | ||
expireDays: Int? = null, | ||
ctx: Context, | ||
focusManager: FocusManager, | ||
onSuccess: (BanFromCommunityData) -> Unit, | ||
) { | ||
viewModelScope.launch { | ||
val form = | ||
BanFromCommunity( | ||
person_id = personId, | ||
community_id = community.id, | ||
ban = ban, | ||
remove_data = removeData, | ||
reason = reason, | ||
expires = futureDaysToUnixTime(expireDays), | ||
) | ||
|
||
banFromCommunityRes = ApiState.Loading | ||
banFromCommunityRes = API.getInstance().banFromCommunity(form).toApiState() | ||
|
||
when (val res = banFromCommunityRes) { | ||
is ApiState.Failure -> { | ||
Log.d("banFromCommunity", "failed", res.msg) | ||
apiErrorToast(msg = res.msg, ctx = ctx) | ||
} | ||
|
||
is ApiState.Success -> { | ||
val personNameShown = personNameShown(res.data.person_view.person, true) | ||
val communityNameShown = communityNameShown(community) | ||
val message = | ||
if (ban) { | ||
if (expireDays !== null) { | ||
ctx.getString( | ||
R.string.person_banned_from_community_for_x_days, | ||
personNameShown, | ||
communityNameShown, | ||
expireDays, | ||
) | ||
} else { | ||
ctx.getString(R.string.person_banned_from_community, personNameShown, communityNameShown) | ||
} | ||
} else { | ||
ctx.getString(R.string.person_unbanned_from_community, personNameShown, communityNameShown) | ||
} | ||
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show() | ||
|
||
focusManager.clearFocus() | ||
onSuccess( | ||
BanFromCommunityData( | ||
person = res.data.person_view.person, | ||
community = community, | ||
banned = res.data.banned, | ||
), | ||
) | ||
} | ||
else -> {} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.