mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Convert PageProperties to kotlin
This commit is contained in:
		
							parent
							
								
									e2deda79eb
								
							
						
					
					
						commit
						68b3f71b7b
					
				
					 4 changed files with 156 additions and 223 deletions
				
			
		|  | @ -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() { } | ||||
| } | ||||
|  | @ -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() { } | ||||
| } | ||||
|  | @ -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<PageProperties> CREATOR | ||||
|             = new Parcelable.Creator<PageProperties>() { | ||||
|         @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; | ||||
|     } | ||||
| } | ||||
|  | @ -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<PageProperties> = object : Parcelable.Creator<PageProperties> { | ||||
|                 override fun createFromParcel(parcel: Parcel): PageProperties { | ||||
|                     return PageProperties(parcel) | ||||
|                 } | ||||
| 
 | ||||
|                 override fun newArray(size: Int): Array<PageProperties?> { | ||||
|                     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 | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke