From 68b3f71b7b0a70c9328fa0ca327b06f65d5a9bd2 Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Thu, 5 Dec 2024 23:16:32 -0600 Subject: [PATCH] Convert PageProperties to kotlin --- .../wikidata/model/page/GeoMarshaller.java | 28 ---- .../wikidata/model/page/GeoUnmarshaller.java | 39 ----- .../wikidata/model/page/PageProperties.java | 156 ------------------ .../wikidata/model/page/PageProperties.kt | 156 ++++++++++++++++++ 4 files changed, 156 insertions(+), 223 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoMarshaller.java delete mode 100644 app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoUnmarshaller.java delete mode 100644 app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.java create mode 100644 app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.kt diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoMarshaller.java b/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoMarshaller.java deleted file mode 100644 index 427833512..000000000 --- a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoMarshaller.java +++ /dev/null @@ -1,28 +0,0 @@ -package fr.free.nrw.commons.wikidata.model.page; - -import android.location.Location; - -import androidx.annotation.Nullable; - -import org.json.JSONException; -import org.json.JSONObject; - -public final class GeoMarshaller { - @Nullable - public static String marshal(@Nullable Location object) { - if (object == null) { - return null; - } - - JSONObject jsonObj = new JSONObject(); - try { - jsonObj.put(GeoUnmarshaller.LATITUDE, object.getLatitude()); - jsonObj.put(GeoUnmarshaller.LONGITUDE, object.getLongitude()); - } catch (JSONException e) { - throw new RuntimeException(e); - } - return jsonObj.toString(); - } - - private GeoMarshaller() { } -} diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoUnmarshaller.java b/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoUnmarshaller.java deleted file mode 100644 index aa5952964..000000000 --- a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/GeoUnmarshaller.java +++ /dev/null @@ -1,39 +0,0 @@ -package fr.free.nrw.commons.wikidata.model.page; - -import android.location.Location; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.json.JSONException; -import org.json.JSONObject; - -public final class GeoUnmarshaller { - static final String LATITUDE = "latitude"; - static final String LONGITUDE = "longitude"; - - @Nullable - public static Location unmarshal(@Nullable String json) { - if (json == null) { - return null; - } - - JSONObject jsonObj; - try { - jsonObj = new JSONObject(json); - } catch (JSONException e) { - return null; - } - return unmarshal(jsonObj); - } - - @Nullable - public static Location unmarshal(@NonNull JSONObject jsonObj) { - Location ret = new Location((String) null); - ret.setLatitude(jsonObj.optDouble(LATITUDE)); - ret.setLongitude(jsonObj.optDouble(LONGITUDE)); - return ret; - } - - private GeoUnmarshaller() { } -} diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.java b/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.java deleted file mode 100644 index 8b32252d8..000000000 --- a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.java +++ /dev/null @@ -1,156 +0,0 @@ -package fr.free.nrw.commons.wikidata.model.page; - -import android.location.Location; -import android.os.Parcel; -import android.os.Parcelable; -import android.text.TextUtils; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.util.Date; - -/** - * Immutable class that contains metadata associated with a PageTitle. - */ -public class PageProperties implements Parcelable { - private final int pageId; - @NonNull private final Namespace namespace; - private final long revisionId; - private final Date lastModified; - private final String displayTitleText; - private final String editProtectionStatus; - private final int languageCount; - private final boolean isMainPage; - private final boolean isDisambiguationPage; - /** Nullable URL with no scheme. For example, foo.bar.com/ instead of http://foo.bar.com/. */ - @Nullable private final String leadImageUrl; - @Nullable private final String leadImageName; - @Nullable private final String titlePronunciationUrl; - @Nullable private final Location geo; - @Nullable private final String wikiBaseItem; - @Nullable private final String descriptionSource; - - /** - * True if the user who first requested this page can edit this page - * FIXME: This is not a true page property, since it depends on current user. - */ - private final boolean canEdit; - - public int getPageId() { - return pageId; - } - - public boolean isMainPage() { - return isMainPage; - } - - public boolean isDisambiguationPage() { - return isDisambiguationPage; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int flags) { - parcel.writeInt(pageId); - parcel.writeInt(namespace.code()); - parcel.writeLong(revisionId); - parcel.writeLong(lastModified.getTime()); - parcel.writeString(displayTitleText); - parcel.writeString(titlePronunciationUrl); - parcel.writeString(GeoMarshaller.marshal(geo)); - parcel.writeString(editProtectionStatus); - parcel.writeInt(languageCount); - parcel.writeInt(canEdit ? 1 : 0); - parcel.writeInt(isMainPage ? 1 : 0); - parcel.writeInt(isDisambiguationPage ? 1 : 0); - parcel.writeString(leadImageUrl); - parcel.writeString(leadImageName); - parcel.writeString(wikiBaseItem); - parcel.writeString(descriptionSource); - } - - private PageProperties(Parcel in) { - pageId = in.readInt(); - namespace = Namespace.of(in.readInt()); - revisionId = in.readLong(); - lastModified = new Date(in.readLong()); - displayTitleText = in.readString(); - titlePronunciationUrl = in.readString(); - geo = GeoUnmarshaller.unmarshal(in.readString()); - editProtectionStatus = in.readString(); - languageCount = in.readInt(); - canEdit = in.readInt() == 1; - isMainPage = in.readInt() == 1; - isDisambiguationPage = in.readInt() == 1; - leadImageUrl = in.readString(); - leadImageName = in.readString(); - wikiBaseItem = in.readString(); - descriptionSource = in.readString(); - } - - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { - @Override - public PageProperties createFromParcel(Parcel in) { - return new PageProperties(in); - } - - @Override - public PageProperties[] newArray(int size) { - return new PageProperties[size]; - } - }; - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - PageProperties that = (PageProperties) o; - - return pageId == that.pageId - && namespace == that.namespace - && revisionId == that.revisionId - && lastModified.equals(that.lastModified) - && displayTitleText.equals(that.displayTitleText) - && TextUtils.equals(titlePronunciationUrl, that.titlePronunciationUrl) - && (geo == that.geo || geo != null && geo.equals(that.geo)) - && languageCount == that.languageCount - && canEdit == that.canEdit - && isMainPage == that.isMainPage - && isDisambiguationPage == that.isDisambiguationPage - && TextUtils.equals(editProtectionStatus, that.editProtectionStatus) - && TextUtils.equals(leadImageUrl, that.leadImageUrl) - && TextUtils.equals(leadImageName, that.leadImageName) - && TextUtils.equals(wikiBaseItem, that.wikiBaseItem); - } - - @Override - public int hashCode() { - int result = lastModified.hashCode(); - result = 31 * result + displayTitleText.hashCode(); - result = 31 * result + (titlePronunciationUrl != null ? titlePronunciationUrl.hashCode() : 0); - result = 31 * result + (geo != null ? geo.hashCode() : 0); - result = 31 * result + (editProtectionStatus != null ? editProtectionStatus.hashCode() : 0); - result = 31 * result + languageCount; - result = 31 * result + (isMainPage ? 1 : 0); - result = 31 * result + (isDisambiguationPage ? 1 : 0); - result = 31 * result + (leadImageUrl != null ? leadImageUrl.hashCode() : 0); - result = 31 * result + (leadImageName != null ? leadImageName.hashCode() : 0); - result = 31 * result + (wikiBaseItem != null ? wikiBaseItem.hashCode() : 0); - result = 31 * result + (canEdit ? 1 : 0); - result = 31 * result + pageId; - result = 31 * result + namespace.code(); - result = 31 * result + (int) revisionId; - return result; - } -} diff --git a/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.kt b/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.kt new file mode 100644 index 000000000..ce8873e8c --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/wikidata/model/page/PageProperties.kt @@ -0,0 +1,156 @@ +package fr.free.nrw.commons.wikidata.model.page + +import android.location.Location +import android.os.Parcel +import android.os.Parcelable +import android.text.TextUtils +import org.json.JSONException +import org.json.JSONObject +import java.util.Date + +/** + * Immutable class that contains metadata associated with a PageTitle. + */ +class PageProperties private constructor(parcel: Parcel) : Parcelable { + val pageId: Int = parcel.readInt() + private val namespace = Namespace.of(parcel.readInt()) + private val revisionId = parcel.readLong() + private val lastModified = Date(parcel.readLong()) + private val displayTitleText = parcel.readString() + private val editProtectionStatus = parcel.readString() + private val languageCount = parcel.readInt() + val isMainPage: Boolean = parcel.readInt() == 1 + val isDisambiguationPage: Boolean = parcel.readInt() == 1 + + /** Nullable URL with no scheme. For example, foo.bar.com/ instead of http://foo.bar.com/. */ + private val leadImageUrl = parcel.readString() + private val leadImageName = parcel.readString() + private val titlePronunciationUrl = parcel.readString() + private val geo = unmarshal(parcel.readString()) + private val wikiBaseItem = parcel.readString() + private val descriptionSource = parcel.readString() + + /** + * True if the user who first requested this page can edit this page + * FIXME: This is not a true page property, since it depends on current user. + */ + private val canEdit = parcel.readInt() == 1 + + override fun describeContents(): Int { + return 0 + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeInt(pageId) + parcel.writeInt(namespace.code()) + parcel.writeLong(revisionId) + parcel.writeLong(lastModified.time) + parcel.writeString(displayTitleText) + parcel.writeString(titlePronunciationUrl) + parcel.writeString(marshal(geo)) + parcel.writeString(editProtectionStatus) + parcel.writeInt(languageCount) + parcel.writeInt(if (canEdit) 1 else 0) + parcel.writeInt(if (isMainPage) 1 else 0) + parcel.writeInt(if (isDisambiguationPage) 1 else 0) + parcel.writeString(leadImageUrl) + parcel.writeString(leadImageName) + parcel.writeString(wikiBaseItem) + parcel.writeString(descriptionSource) + } + + override fun equals(o: Any?): Boolean { + if (this === o) { + return true + } + if (o == null || javaClass != o.javaClass) { + return false + } + + val that = o as PageProperties + + return pageId == that.pageId && + namespace === that.namespace && + revisionId == that.revisionId && + lastModified == that.lastModified && + displayTitleText == that.displayTitleText && + TextUtils.equals(titlePronunciationUrl, that.titlePronunciationUrl) && + (geo === that.geo || geo != null && geo == that.geo) && + languageCount == that.languageCount && + canEdit == that.canEdit && + isMainPage == that.isMainPage && + isDisambiguationPage == that.isDisambiguationPage && + TextUtils.equals(editProtectionStatus, that.editProtectionStatus) && + TextUtils.equals(leadImageUrl, that.leadImageUrl) && + TextUtils.equals(leadImageName, that.leadImageName) && + TextUtils.equals(wikiBaseItem, that.wikiBaseItem) + } + + override fun hashCode(): Int { + var result = lastModified.hashCode() + result = 31 * result + displayTitleText.hashCode() + result = 31 * result + (titlePronunciationUrl?.hashCode() ?: 0) + result = 31 * result + (geo?.hashCode() ?: 0) + result = 31 * result + (editProtectionStatus?.hashCode() ?: 0) + result = 31 * result + languageCount + result = 31 * result + (if (isMainPage) 1 else 0) + result = 31 * result + (if (isDisambiguationPage) 1 else 0) + result = 31 * result + (leadImageUrl?.hashCode() ?: 0) + result = 31 * result + (leadImageName?.hashCode() ?: 0) + result = 31 * result + (wikiBaseItem?.hashCode() ?: 0) + result = 31 * result + (if (canEdit) 1 else 0) + result = 31 * result + pageId + result = 31 * result + namespace.code() + result = 31 * result + revisionId.toInt() + return result + } + + companion object { + @JvmField + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): PageProperties { + return PageProperties(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } +} + +private const val LATITUDE: String = "latitude" +private const val LONGITUDE: String = "longitude" + +private fun marshal(location: Location?): String? { + if (location == null) { + return null + } + + val jsonObj = JSONObject().apply { + try { + put(LATITUDE, location.latitude) + put(LONGITUDE, location.longitude) + } catch (e: JSONException) { + throw RuntimeException(e) + } + } + + return jsonObj.toString() +} + +private fun unmarshal(json: String?): Location? { + if (json == null) { + return null + } + + return try { + val jsonObject = JSONObject(json) + Location(null as String?).apply { + latitude = jsonObject.optDouble(LATITUDE) + longitude = jsonObject.optDouble(LONGITUDE) + } + } catch (e: JSONException) { + null + } +} \ No newline at end of file