mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
#3847 Convert Media and Contribution to Kotlin Data Classes - convert to data classes - compose a contribution with a media (#3848)
Co-authored-by: Vivek Maskara <maskaravivek@gmail.com>
This commit is contained in:
parent
3361155fad
commit
cc2f14dab8
34 changed files with 334 additions and 741 deletions
|
|
@ -1,220 +0,0 @@
|
|||
package fr.free.nrw.commons.contributions;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.room.Entity;
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.auth.SessionManager;
|
||||
import fr.free.nrw.commons.upload.UploadItem;
|
||||
import fr.free.nrw.commons.upload.UploadMediaDetail;
|
||||
import fr.free.nrw.commons.upload.WikidataPlace;
|
||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity(tableName = "contribution")
|
||||
public class Contribution extends Media {
|
||||
|
||||
// No need to be bitwise - they're mutually exclusive
|
||||
public static final int STATE_COMPLETED = -1;
|
||||
public static final int STATE_FAILED = 1;
|
||||
public static final int STATE_QUEUED = 2;
|
||||
public static final int STATE_IN_PROGRESS = 3;
|
||||
|
||||
private int state;
|
||||
private long transferred;
|
||||
private String decimalCoords;
|
||||
private String dateCreatedSource;
|
||||
private WikidataPlace wikidataPlace;
|
||||
/**
|
||||
* Each depiction loaded in depictions activity is associated with a wikidata entity id, this Id
|
||||
* is in turn used to upload depictions to wikibase
|
||||
*/
|
||||
private List<DepictedItem> depictedItems = new ArrayList<>();
|
||||
private String mimeType;
|
||||
@Nullable
|
||||
private Uri localUri;
|
||||
private long dataLength;
|
||||
private Date dateCreated;
|
||||
|
||||
public Contribution() {
|
||||
}
|
||||
|
||||
public Contribution(final UploadItem item, final SessionManager sessionManager,
|
||||
final List<DepictedItem> depictedItems, final List<String> categories) {
|
||||
super(
|
||||
item.getFileName(),
|
||||
UploadMediaDetail.formatCaptions(item.getUploadMediaDetails()),
|
||||
UploadMediaDetail.formatDescriptions(item.getUploadMediaDetails()),
|
||||
sessionManager.getAuthorName(),
|
||||
categories);
|
||||
localUri = item.getMediaUri();
|
||||
decimalCoords = item.getGpsCoords().getDecimalCoords();
|
||||
dateCreatedSource = "";
|
||||
this.depictedItems = depictedItems;
|
||||
wikidataPlace = WikidataPlace.from(item.getPlace());
|
||||
}
|
||||
|
||||
public void setDateCreatedSource(final String dateCreatedSource) {
|
||||
this.dateCreatedSource = dateCreatedSource;
|
||||
}
|
||||
|
||||
public String getDateCreatedSource() {
|
||||
return dateCreatedSource;
|
||||
}
|
||||
|
||||
public long getTransferred() {
|
||||
return transferred;
|
||||
}
|
||||
|
||||
public void setTransferred(final long transferred) {
|
||||
this.transferred = transferred;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(final int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array list of entityids for the depictions
|
||||
*/
|
||||
public List<DepictedItem> getDepictedItems() {
|
||||
return depictedItems;
|
||||
}
|
||||
|
||||
public void setWikidataPlace(final WikidataPlace wikidataPlace) {
|
||||
this.wikidataPlace = wikidataPlace;
|
||||
}
|
||||
|
||||
public WikidataPlace getWikidataPlace() {
|
||||
return wikidataPlace;
|
||||
}
|
||||
|
||||
public String getDecimalCoords() {
|
||||
return decimalCoords;
|
||||
}
|
||||
|
||||
public void setDecimalCoords(final String decimalCoords) {
|
||||
this.decimalCoords = decimalCoords;
|
||||
}
|
||||
|
||||
public void setDepictedItems(final List<DepictedItem> depictedItems) {
|
||||
this.depictedItems = depictedItems;
|
||||
}
|
||||
|
||||
public String getMimeType() {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
public void setMimeType(final String mimeType) {
|
||||
this.mimeType = mimeType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(final Parcel dest, final int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeInt(state);
|
||||
dest.writeLong(transferred);
|
||||
dest.writeString(decimalCoords);
|
||||
dest.writeString(dateCreatedSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that takes Media object and state as parameters and builds a new Contribution object
|
||||
* @param media
|
||||
* @param state
|
||||
*/
|
||||
public Contribution(Media media, int state) {
|
||||
super(media);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
protected Contribution(final Parcel in) {
|
||||
super(in);
|
||||
state = in.readInt();
|
||||
transferred = in.readLong();
|
||||
decimalCoords = in.readString();
|
||||
dateCreatedSource = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<Contribution> CREATOR = new Creator<Contribution>() {
|
||||
@Override
|
||||
public Contribution createFromParcel(final Parcel source) {
|
||||
return new Contribution(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Contribution[] newArray(final int size) {
|
||||
return new Contribution[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
public Uri getLocalUri() {
|
||||
return localUri;
|
||||
}
|
||||
|
||||
public void setLocalUri(@Nullable Uri localUri) {
|
||||
this.localUri = localUri;
|
||||
}
|
||||
|
||||
public long getDataLength() {
|
||||
return dataLength;
|
||||
}
|
||||
|
||||
public void setDataLength(long dataLength) {
|
||||
this.dataLength = dataLength;
|
||||
}
|
||||
|
||||
public Date getDateCreated() {
|
||||
return dateCreated;
|
||||
}
|
||||
|
||||
public void setDateCreated(Date dateCreated) {
|
||||
this.dateCreated = dateCreated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
final Contribution that = (Contribution) o;
|
||||
return state == that.state &&
|
||||
transferred == that.transferred &&
|
||||
dataLength == that.dataLength &&
|
||||
Objects.equals(decimalCoords, that.decimalCoords) &&
|
||||
Objects.equals(dateCreatedSource, that.dateCreatedSource) &&
|
||||
Objects.equals(wikidataPlace, that.wikidataPlace) &&
|
||||
Objects.equals(depictedItems, that.depictedItems) &&
|
||||
Objects.equals(mimeType, that.mimeType) &&
|
||||
Objects.equals(localUri, that.localUri) &&
|
||||
Objects.equals(dateCreated, that.dateCreated);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects
|
||||
.hash(super.hashCode(), state, transferred, decimalCoords, dateCreatedSource,
|
||||
wikidataPlace,
|
||||
depictedItems, mimeType, localUri, dataLength, dateCreated);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
package fr.free.nrw.commons.contributions
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Parcelable
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.upload.UploadItem
|
||||
import fr.free.nrw.commons.upload.UploadMediaDetail
|
||||
import fr.free.nrw.commons.upload.WikidataPlace
|
||||
import fr.free.nrw.commons.upload.WikidataPlace.Companion.from
|
||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
import java.util.*
|
||||
|
||||
@Entity(tableName = "contribution")
|
||||
@Parcelize
|
||||
data class Contribution constructor(
|
||||
@Embedded(prefix = "media_") val media: Media,
|
||||
@PrimaryKey val pageId: String = media.pageId,
|
||||
var state: Int = 0,
|
||||
var transferred: Long = 0,
|
||||
val decimalCoords: String? = null,
|
||||
var dateCreatedSource: String? = null,
|
||||
var wikidataPlace: WikidataPlace? = null,
|
||||
/**
|
||||
* @return array list of entityids for the depictions
|
||||
*/
|
||||
/**
|
||||
* Each depiction loaded in depictions activity is associated with a wikidata entity id, this Id
|
||||
* is in turn used to upload depictions to wikibase
|
||||
*/
|
||||
val depictedItems: List<DepictedItem> = ArrayList(),
|
||||
var mimeType: String? = null,
|
||||
val localUri: Uri? = null,
|
||||
var dataLength: Long = 0,
|
||||
var dateCreated: Date? = null
|
||||
) : Parcelable {
|
||||
|
||||
fun completeWith(media: Media): Contribution {
|
||||
return copy(pageId = media.pageId, media = media, state = STATE_COMPLETED)
|
||||
}
|
||||
|
||||
constructor(
|
||||
item: UploadItem,
|
||||
sessionManager: SessionManager,
|
||||
depictedItems: List<DepictedItem>,
|
||||
categories: List<String>
|
||||
) : this(
|
||||
Media(
|
||||
formatCaptions(item.uploadMediaDetails),
|
||||
categories,
|
||||
item.fileName,
|
||||
formatDescriptions(item.uploadMediaDetails),
|
||||
sessionManager.authorName
|
||||
),
|
||||
localUri = item.mediaUri,
|
||||
decimalCoords = item.gpsCoords.decimalCoords,
|
||||
dateCreatedSource = "",
|
||||
depictedItems = depictedItems,
|
||||
wikidataPlace = from(item.place)
|
||||
)
|
||||
|
||||
companion object {
|
||||
const val STATE_COMPLETED = -1
|
||||
const val STATE_FAILED = 1
|
||||
const val STATE_QUEUED = 2
|
||||
const val STATE_IN_PROGRESS = 3
|
||||
|
||||
/**
|
||||
* Formatting captions to the Wikibase format for sending labels
|
||||
* @param uploadMediaDetails list of media Details
|
||||
*/
|
||||
fun formatCaptions(uploadMediaDetails: List<UploadMediaDetail>) =
|
||||
uploadMediaDetails.associate { it.languageCode!! to it.captionText }
|
||||
.filter { it.value.isNotBlank() }
|
||||
|
||||
/**
|
||||
* Formats the list of descriptions into the format Commons requires for uploads.
|
||||
*
|
||||
* @param descriptions the list of descriptions, description is ignored if text is null.
|
||||
* @return a string with the pattern of {{en|1=descriptionText}}
|
||||
*/
|
||||
fun formatDescriptions(descriptions: List<UploadMediaDetail>) =
|
||||
descriptions.filter { it.descriptionText.isNotEmpty() }
|
||||
.joinToString { "{{${it.languageCode}|1=${it.descriptionText}}}" }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package fr.free.nrw.commons.contributions
|
||||
|
||||
import androidx.paging.PagedList.BoundaryCallback
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
|
|
@ -53,9 +52,9 @@ class ContributionBoundaryCallback @Inject constructor(
|
|||
fun fetchContributions() {
|
||||
compositeDisposable.add(
|
||||
mediaClient.getMediaListForUser(sessionManager.userName!!)
|
||||
.map { mediaList: List<Media?> ->
|
||||
.map { mediaList ->
|
||||
mediaList.map {
|
||||
Contribution(it, Contribution.STATE_COMPLETED)
|
||||
Contribution(media=it, state=Contribution.STATE_COMPLETED)
|
||||
}
|
||||
}
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import java.util.List;
|
|||
@Dao
|
||||
public abstract class ContributionDao {
|
||||
|
||||
@Query("SELECT * FROM contribution order by dateUploaded DESC")
|
||||
@Query("SELECT * FROM contribution order by media_dateUploaded DESC")
|
||||
abstract DataSource.Factory<Integer, Contribution> fetchContributions();
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
|
|
@ -50,7 +50,7 @@ public abstract class ContributionDao {
|
|||
.fromAction(() -> deleteSynchronous(contribution));
|
||||
}
|
||||
|
||||
@Query("SELECT * from contribution WHERE filename=:fileName")
|
||||
@Query("SELECT * from contribution WHERE media_filename=:fileName")
|
||||
public abstract List<Contribution> getContributionWithTitle(String fileName);
|
||||
|
||||
@Query("SELECT * from contribution WHERE pageId=:pageId")
|
||||
|
|
|
|||
|
|
@ -61,8 +61,8 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
public void init(final int position, final Contribution contribution) {
|
||||
this.contribution = contribution;
|
||||
this.position = position;
|
||||
titleView.setText(contribution.getMostRelevantCaption());
|
||||
final String imageSource = chooseImageSource(contribution.getThumbUrl(),
|
||||
titleView.setText(contribution.getMedia().getMostRelevantCaption());
|
||||
final String imageSource = chooseImageSource(contribution.getMedia().getThumbUrl(),
|
||||
contribution.getLocalUri());
|
||||
if (!TextUtils.isEmpty(imageSource)) {
|
||||
final ImageRequest imageRequest =
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
|||
import fr.free.nrw.commons.nearby.NearbyController;
|
||||
import fr.free.nrw.commons.nearby.NearbyNotificationCardView;
|
||||
import fr.free.nrw.commons.nearby.Place;
|
||||
import fr.free.nrw.commons.settings.Prefs;
|
||||
import fr.free.nrw.commons.upload.UploadService;
|
||||
import fr.free.nrw.commons.utils.ConfigUtils;
|
||||
import fr.free.nrw.commons.utils.DialogUtil;
|
||||
|
|
@ -490,5 +489,10 @@ public class ContributionsFragment
|
|||
public int getTotalMediaCount() {
|
||||
return contributionsListFragment.getTotalMediaCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getContributionStateAt(int position) {
|
||||
return contributionsListFragment.getContributionStateAt(position);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -278,7 +278,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
|
||||
|
||||
public Media getMediaAtPosition(final int i) {
|
||||
return adapter.getContributionForPosition(i);
|
||||
return adapter.getContributionForPosition(i).getMedia();
|
||||
}
|
||||
|
||||
public int getTotalMediaCount() {
|
||||
|
|
@ -303,6 +303,10 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
Utils.handleWebUrl(getContext(), Uri.parse(url));
|
||||
}
|
||||
|
||||
public Integer getContributionStateAt(int position) {
|
||||
return adapter.getContributionForPosition(position).getState();
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
|
||||
void retryUpload(Contribution contribution);
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class WikipediaInstructionsDialogFragment : DialogFragment() {
|
|||
) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
contribution = arguments!!.getParcelable(ARG_CONTRIBUTION)
|
||||
tv_wikicode.setText(contribution?.wikiCode)
|
||||
tv_wikicode.setText(contribution?.media?.wikiCode)
|
||||
instructions_cancel.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
|
|
@ -64,4 +64,4 @@ class WikipediaInstructionsDialogFragment : DialogFragment() {
|
|||
return frag
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue