mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-03 00:03:53 +01:00
Changed to data classes, and added immutability
This commit is contained in:
parent
bc065c8792
commit
fd0141a5b4
3 changed files with 104 additions and 153 deletions
|
|
@ -1,104 +1,45 @@
|
||||||
package fr.free.nrw.commons.profile.achievements
|
package fr.free.nrw.commons.profile.achievements
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents Achievements class and stores all the parameters
|
* Represents Achievements data class and stores all the parameters.
|
||||||
|
* Immutable version with default values for optional properties.
|
||||||
*/
|
*/
|
||||||
class Achievements {
|
data class Achievements(
|
||||||
|
val uniqueUsedImages: Int = 0,
|
||||||
|
val articlesUsingImages: Int = 0,
|
||||||
|
val thanksReceived: Int = 0,
|
||||||
|
val featuredImages: Int = 0,
|
||||||
|
val qualityImages: Int = 0,
|
||||||
|
val imagesUploaded: Int = 0,
|
||||||
|
val revertCount: Int = 0
|
||||||
|
) {
|
||||||
/**
|
/**
|
||||||
* The count of unique images used by the wiki.
|
* Used to calculate the percentages of images that haven't been reverted.
|
||||||
* @return The count of unique images used.
|
* Returns 100 if imagesUploaded is 0 to avoid division by zero.
|
||||||
* @param uniqueUsedImages The count to set for unique images used.
|
|
||||||
*/
|
|
||||||
var uniqueUsedImages = 0
|
|
||||||
private var articlesUsingImages = 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The count of thanks received.
|
|
||||||
* @return The count of thanks received.
|
|
||||||
* @param thanksReceived The count to set for thanks received.
|
|
||||||
*/
|
|
||||||
var thanksReceived = 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The count of featured images.
|
|
||||||
* @return The count of featured images.
|
|
||||||
* @param featuredImages The count to set for featured images.
|
|
||||||
*/
|
|
||||||
var featuredImages = 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The count of quality images.
|
|
||||||
* @return The count of quality images.
|
|
||||||
* @param qualityImages The count to set for quality images.
|
|
||||||
*/
|
|
||||||
var qualityImages = 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The count of images uploaded.
|
|
||||||
* @return The count of images uploaded.
|
|
||||||
* @param imagesUploaded The count to set for images uploaded.
|
|
||||||
*/
|
|
||||||
var imagesUploaded = 0
|
|
||||||
private var revertCount = 0
|
|
||||||
|
|
||||||
constructor() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* constructor for achievements class to set its data members
|
|
||||||
* @param uniqueUsedImages
|
|
||||||
* @param articlesUsingImages
|
|
||||||
* @param thanksReceived
|
|
||||||
* @param featuredImages
|
|
||||||
* @param imagesUploaded
|
|
||||||
* @param revertCount
|
|
||||||
*/
|
|
||||||
constructor(
|
|
||||||
uniqueUsedImages: Int,
|
|
||||||
articlesUsingImages: Int,
|
|
||||||
thanksReceived: Int,
|
|
||||||
featuredImages: Int,
|
|
||||||
qualityImages: Int,
|
|
||||||
imagesUploaded: Int,
|
|
||||||
revertCount: Int,
|
|
||||||
) {
|
|
||||||
this.uniqueUsedImages = uniqueUsedImages
|
|
||||||
this.articlesUsingImages = articlesUsingImages
|
|
||||||
this.thanksReceived = thanksReceived
|
|
||||||
this.featuredImages = featuredImages
|
|
||||||
this.qualityImages = qualityImages
|
|
||||||
this.imagesUploaded = imagesUploaded
|
|
||||||
this.revertCount = revertCount
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* used to calculate the percentages of images that haven't been reverted
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
val notRevertPercentage: Int
|
val notRevertPercentage: Int
|
||||||
get() =
|
get() = if (imagesUploaded > 0) {
|
||||||
try {
|
|
||||||
(imagesUploaded - revertCount) * 100 / imagesUploaded
|
(imagesUploaded - revertCount) * 100 / imagesUploaded
|
||||||
} catch (divideByZero: ArithmeticException) {
|
} else {
|
||||||
100
|
100
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Get Achievements object from FeedbackResponse
|
* Get Achievements object from FeedbackResponse.
|
||||||
*
|
*
|
||||||
* @param response
|
* @param response The feedback response to convert.
|
||||||
* @return
|
* @return An Achievements object with values from the response.
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun from(response: FeedbackResponse): Achievements =
|
fun from(response: FeedbackResponse): Achievements = Achievements(
|
||||||
Achievements(
|
uniqueUsedImages = response.uniqueUsedImages,
|
||||||
response.uniqueUsedImages,
|
articlesUsingImages = response.articlesUsingImages,
|
||||||
response.articlesUsingImages,
|
thanksReceived = response.thanksReceived,
|
||||||
response.thanksReceived,
|
featuredImages = response.featuredImages.featuredPicturesOnWikimediaCommons,
|
||||||
response.featuredImages.featuredPicturesOnWikimediaCommons,
|
qualityImages = response.featuredImages.qualityImages,
|
||||||
response.featuredImages.qualityImages,
|
imagesUploaded = 0, // Assuming imagesUploaded should be 0
|
||||||
0,
|
revertCount = response.deletedUploads
|
||||||
response.deletedUploads,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -294,8 +294,18 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
|
||||||
* @param uploadCount
|
* @param uploadCount
|
||||||
*/
|
*/
|
||||||
private void setAchievementsUploadCount(Achievements achievements, int uploadCount) {
|
private void setAchievementsUploadCount(Achievements achievements, int uploadCount) {
|
||||||
achievements.setImagesUploaded(uploadCount);
|
// Create a new instance of Achievements with updated imagesUploaded
|
||||||
hideProgressBar(achievements);
|
Achievements updatedAchievements = new Achievements(
|
||||||
|
achievements.getUniqueUsedImages(),
|
||||||
|
achievements.getArticlesUsingImages(),
|
||||||
|
achievements.getThanksReceived(),
|
||||||
|
achievements.getFeaturedImages(),
|
||||||
|
achievements.getQualityImages(),
|
||||||
|
uploadCount, // Update imagesUploaded with new value
|
||||||
|
achievements.getRevertCount()
|
||||||
|
);
|
||||||
|
|
||||||
|
hideProgressBar(updatedAchievements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import com.google.gson.annotations.SerializedName
|
||||||
* Represents Featured Images on WikiMedia Commons platform
|
* Represents Featured Images on WikiMedia Commons platform
|
||||||
* Used by Achievements and FeedbackResponse (objects) of the user
|
* Used by Achievements and FeedbackResponse (objects) of the user
|
||||||
*/
|
*/
|
||||||
class FeaturedImages(
|
data class FeaturedImages(
|
||||||
@field:SerializedName("Quality_images") val qualityImages: Int,
|
@field:SerializedName("Quality_images") val qualityImages: Int,
|
||||||
@field:SerializedName("Featured_pictures_on_Wikimedia_Commons") val featuredPicturesOnWikimediaCommons: Int,
|
@field:SerializedName("Featured_pictures_on_Wikimedia_Commons") val featuredPicturesOnWikimediaCommons: Int,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue