Skip to content

Commit

Permalink
Controversial posts and comments (#1106)
Browse files Browse the repository at this point in the history
* Added Controversial sort options for posts and comments

* Added Controversial sort options for posts and comments

* Added supported versions for sort types, siteVersion() in SiteViewModel, and showing sort type dialogs dynamically according to supported sort types

* Fixed lint errors

* Added short form and long form to SortType enum, removed getLocalizedSortType utils, moved getSupportedSortTypes to the sort type enums, used them in Account settings as well

* Lint errors

* Removed dialog string for Controversial and made everything use the short form which is the same, removed the text field from the SortType enum and used the long form in the Dialogs forms

* lint

* Fixed SortType and supported sort type indexes not lining up

* unused imports

* Fixing lints.

---------

Co-authored-by: Dessalines <tyhou13@gmx.com>
  • Loading branch information
iByteABit256 and dessalines authored Aug 24, 2023
1 parent ef9b94c commit 8da2465
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 172 deletions.
64 changes: 1 addition & 63 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1041,69 +1041,6 @@ fun convertSpToPx(sp: TextUnit, ctx: Context): Int {
return (sp.value * ctx.resources.displayMetrics.scaledDensity).toInt()
}

/**
* Returns localized Strings for SortingType Enum
*/

fun getLocalizedSortingTypeShortName(ctx: Context, sortingType: SortType): String {
return ctx.getString(
MAP_SORT_TYPE_SHORT_FORM[sortingType]
?: throw IllegalStateException("Someone forgot to update the MAP_SORT_TYPE_SHORT_FORM"),
)
}

// ORDER MUST BE THE SAME AS THE ENUM
val MAP_SORT_TYPE_SHORT_FORM = mapOf(
SortType.Active to R.string.sorttype_active,
SortType.Hot to R.string.sorttype_hot,
SortType.New to R.string.sorttype_new,
SortType.Old to R.string.sorttype_old,
SortType.TopDay to R.string.sorttype_topday,
SortType.TopWeek to R.string.sorttype_topweek,
SortType.TopMonth to R.string.sorttype_topmonth,
SortType.TopYear to R.string.sorttype_topyear,
SortType.TopAll to R.string.sorttype_topall,
SortType.MostComments to R.string.sorttype_mostcomments,
SortType.NewComments to R.string.sorttype_newcomments,
SortType.TopHour to R.string.sorttype_tophour,
SortType.TopSixHour to R.string.sorttype_topsixhour,
SortType.TopTwelveHour to R.string.sorttype_toptwelvehour,
SortType.TopThreeMonths to R.string.sorttype_topthreemonths,
SortType.TopSixMonths to R.string.sorttype_topsixmonths,
SortType.TopNineMonths to R.string.sorttype_topninemonths,
)

/**
* Returns localized Strings for SortingType Enum
*/

fun getLocalizedSortingTypeLongName(ctx: Context, sortingType: SortType): String {
return ctx.getString(
MAP_SORT_TYPE_LONG_FORM[sortingType]
?: throw IllegalStateException("Someone forgot to update the MAP_SORT_TYPE_LONG_FORM"),
)
}

val MAP_SORT_TYPE_LONG_FORM = mapOf(
SortType.Active to R.string.sorttype_active,
SortType.Hot to R.string.sorttype_hot,
SortType.New to R.string.sorttype_new,
SortType.Old to R.string.sorttype_old,
SortType.TopDay to R.string.dialogs_top_day,
SortType.TopWeek to R.string.dialogs_top_week,
SortType.TopMonth to R.string.dialogs_top_month,
SortType.TopYear to R.string.dialogs_top_year,
SortType.TopAll to R.string.dialogs_top_all,
SortType.MostComments to R.string.dialogs_most_comments,
SortType.NewComments to R.string.dialogs_new_comments,
SortType.TopHour to R.string.dialogs_top_hour,
SortType.TopSixHour to R.string.dialogs_top_six_hour,
SortType.TopTwelveHour to R.string.dialogs_top_twelve_hour,
SortType.TopThreeMonths to R.string.dialogs_top_three_month,
SortType.TopSixMonths to R.string.dialogs_top_six_month,
SortType.TopNineMonths to R.string.dialogs_top_nine_month,
)

/**
* Returns localized Strings for UserTab Enum
*/
Expand Down Expand Up @@ -1137,6 +1074,7 @@ fun getLocalizedCommentSortTypeName(ctx: Context, commentSortType: CommentSortTy
CommentSortType.New -> ctx.getString(R.string.sorttype_new)
CommentSortType.Old -> ctx.getString(R.string.sorttype_old)
CommentSortType.Top -> ctx.getString(R.string.dialogs_top)
CommentSortType.Controversial -> ctx.getString(R.string.sorttype_controversial)
}
return returnString
}
Expand Down
167 changes: 143 additions & 24 deletions app/src/main/java/com/jerboa/datatypes/types/Others.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package com.jerboa.datatypes.types

import android.os.Parcelable
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.BarChart
import androidx.compose.material.icons.outlined.BrightnessLow
import androidx.compose.material.icons.outlined.FormatListNumbered
import androidx.compose.material.icons.outlined.History
import androidx.compose.material.icons.outlined.LocalFireDepartment
import androidx.compose.material.icons.outlined.Moving
import androidx.compose.material.icons.outlined.NewReleases
import androidx.compose.material.icons.outlined.ThumbsUpDown
import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.vector.ImageVector
import com.google.gson.annotations.SerializedName
import com.jerboa.R
import com.jerboa.api.MINIMUM_API_VERSION
import com.jerboa.compareVersions
import kotlinx.parcelize.Parcelize

const val MINIMUM_CONTROVERSIAL_SORT_API_VERSION: String = "0.19"
const val MINIMUM_TOP_X_MONTHLY_SORT_API_VERSION: String = "0.18.1"

enum class RegistrationMode {
@SerializedName("Closed")
Closed,
Expand All @@ -19,135 +36,237 @@ enum class RegistrationMode {
/**
* Different sort types used in lemmy.
*/
enum class SortType {
enum class SortType(
@StringRes val shortForm: Int,
@StringRes val longForm: Int,
val icon: ImageVector? = null,
val version: String = MINIMUM_API_VERSION,
) {
/**
* Posts sorted by the most recent comment.
*/
@SerializedName("Active")
Active,
Active(
R.string.sorttype_active,
R.string.sorttype_active,
Icons.Outlined.Moving,
),

/**
* Posts sorted by the published time.
*/
@SerializedName("Hot")
Hot,
Hot(
R.string.sorttype_hot,
R.string.sorttype_hot,
Icons.Outlined.LocalFireDepartment,
),

@SerializedName("New")
New,
New(
R.string.sorttype_new,
R.string.sorttype_new,
Icons.Outlined.BrightnessLow,
),

/**
* Posts sorted by the published time ascending
*/
@SerializedName("Old")
Old,
Old(
R.string.sorttype_old,
R.string.sorttype_old,
Icons.Outlined.History,
),

/**
* Posts sorted by controversy rank.
*/
@SerializedName("Controversial")
Controversial(
R.string.sorttype_controversial,
R.string.sorttype_controversial,
Icons.Outlined.ThumbsUpDown,
MINIMUM_CONTROVERSIAL_SORT_API_VERSION,
),

/**
* The top posts for this last day.
*/
@SerializedName("TopDay")
TopDay,
TopDay(
R.string.sorttype_topday,
R.string.dialogs_top_day,
Icons.Outlined.BarChart,
),

/**
* The top posts for this last week.
*/
@SerializedName("TopWeek")
TopWeek,
TopWeek(
R.string.sorttype_topweek,
R.string.dialogs_top_week,
Icons.Outlined.BarChart,
),

/**
* The top posts for this last month.
*/
@SerializedName("TopMonth")
TopMonth,
TopMonth(
R.string.sorttype_topmonth,
R.string.dialogs_top_month,
Icons.Outlined.BarChart,
),

/**
* The top posts for this last year.
*/
@SerializedName("TopYear")
TopYear,
TopYear(
R.string.sorttype_topyear,
R.string.dialogs_top_year,
Icons.Outlined.BarChart,
),

/**
* The top posts of all time.
*/
@SerializedName("TopAll")
TopAll,
TopAll(
R.string.sorttype_topall,
R.string.dialogs_top_all,
Icons.Outlined.BarChart,
),

/**
* Posts sorted by the most comments.
*/
@SerializedName("MostComments")
MostComments,
MostComments(
R.string.sorttype_mostcomments,
R.string.sorttype_mostcomments,
Icons.Outlined.FormatListNumbered,
),

/**
* Posts sorted by the newest comments, with no necrobumping. IE a forum sort.
*/
@SerializedName("NewComments")
NewComments,
NewComments(
R.string.sorttype_newcomments,
R.string.sorttype_newcomments,
Icons.Outlined.NewReleases,
),

/**
* Posts sorted by the top hour.
*/
@SerializedName("TopHour")
TopHour,
TopHour(
R.string.sorttype_tophour,
R.string.dialogs_top_hour,
Icons.Outlined.BarChart,
),

/**
* Posts sorted by the top six hour.
*/
@SerializedName("TopSixHour")
TopSixHour,
TopSixHour(
R.string.sorttype_topsixhour,
R.string.dialogs_top_six_hour,
Icons.Outlined.BarChart,
),

/**
* Posts sorted by the top twelve hour.
*/
@SerializedName("TopTwelveHour")
TopTwelveHour,
TopTwelveHour(
R.string.sorttype_toptwelvehour,
R.string.dialogs_top_twelve_hour,
Icons.Outlined.BarChart,
),

/**
* Posts sorted by the top three months.
*/
@SerializedName("TopThreeMonths")
TopThreeMonths,
TopThreeMonths(
R.string.sorttype_topthreemonths,
R.string.dialogs_top_three_month,
Icons.Outlined.BarChart,
MINIMUM_TOP_X_MONTHLY_SORT_API_VERSION,
),

/**
* Posts sorted by the top six months.
*/
@SerializedName("TopSixMonths")
TopSixMonths,
TopSixMonths(
R.string.sorttype_topsixmonths,
R.string.dialogs_top_six_month,
Icons.Outlined.BarChart,
MINIMUM_TOP_X_MONTHLY_SORT_API_VERSION,
),

/**
* Posts sorted by the top nine months.
*/
@SerializedName("TopNineMonths")
TopNineMonths,
TopNineMonths(
R.string.sorttype_topninemonths,
R.string.dialogs_top_nine_month,
Icons.Outlined.BarChart,
MINIMUM_TOP_X_MONTHLY_SORT_API_VERSION,
),
;

companion object {
val getSupportedSortTypes = { siteVersion: String -> values().filter { compareVersions(siteVersion, it.version) >= 0 } }
}
}
// When updating this enum, don't forget to update MAP_SORT_TYPE_SHORT_FORM and MAP_SORT_TYPE_LONG_FORM

/**
* Different comment sort types used in lemmy.
*/
enum class CommentSortType {
enum class CommentSortType(val text: Int, val icon: ImageVector? = null, val version: String = MINIMUM_API_VERSION) {
/**
* Comments sorted by a decaying rank.
*/
@SerializedName("Hot")
Hot,
Hot(R.string.dialogs_hot, Icons.Outlined.LocalFireDepartment),

/**
* Comments sorted by top score.
*/
@SerializedName("Top")
Top,
Top(R.string.dialogs_top, Icons.Outlined.BarChart),

/**
* Comments sorted by new.
*/
@SerializedName("New")
New,
New(R.string.dialogs_new, Icons.Outlined.BrightnessLow),

/**
* Comments sorted by old.
*/
@SerializedName("Old")
Old,
Old(R.string.dialogs_old, Icons.Outlined.History),

/**
* Posts sorted by controversy rank.
*/
@SerializedName("Controversial")
Controversial(R.string.sorttype_controversial, Icons.Outlined.ThumbsUpDown, MINIMUM_CONTROVERSIAL_SORT_API_VERSION),
;

companion object {
val getSupportedSortTypes = { siteVersion: String -> values().filter { compareVersions(siteVersion, it.version) >= 0 } }
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/jerboa/model/SiteViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory
import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.DEFAULT_INSTANCE
import com.jerboa.api.MINIMUM_API_VERSION
import com.jerboa.api.apiWrapper
import com.jerboa.datatypes.types.GetSite
import com.jerboa.datatypes.types.GetSiteResponse
Expand Down Expand Up @@ -150,6 +151,13 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode
}
}

fun siteVersion(): String {
return when (val res = siteRes) {
is ApiState.Success -> res.data.version
else -> MINIMUM_API_VERSION
}
}

companion object {
val Factory = viewModelFactory {
initializer {
Expand Down
Loading

0 comments on commit 8da2465

Please sign in to comment.