mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 13:53:54 +01:00
Convert Notification to kotlin
This commit is contained in:
parent
3e6f5b96f4
commit
e2deda79eb
4 changed files with 128 additions and 194 deletions
|
|
@ -64,8 +64,8 @@ class NotificationClient
|
|||
return Notification(
|
||||
notificationType = notificationType,
|
||||
notificationText = notificationText,
|
||||
date = DateUtil.getMonthOnlyDateString(timestamp),
|
||||
link = contents?.links?.primary?.url ?: "",
|
||||
date = DateUtil.getMonthOnlyDateString(getTimestamp()),
|
||||
link = contents?.links?.getPrimary()?.url ?: "",
|
||||
iconUrl = "",
|
||||
notificationId = id().toString(),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,190 +0,0 @@
|
|||
package fr.free.nrw.commons.wikidata.model.notifications;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import fr.free.nrw.commons.utils.DateUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import fr.free.nrw.commons.wikidata.GsonUtil;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class Notification {
|
||||
@Nullable private String wiki;
|
||||
private long id;
|
||||
@Nullable private String type;
|
||||
@Nullable private String category;
|
||||
|
||||
@Nullable private Title title;
|
||||
@Nullable private Timestamp timestamp;
|
||||
@SerializedName("*") @Nullable private Contents contents;
|
||||
|
||||
@NonNull public String wiki() {
|
||||
return StringUtils.defaultString(wiki);
|
||||
}
|
||||
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long key() {
|
||||
return id + wiki().hashCode();
|
||||
}
|
||||
|
||||
@NonNull public String type() {
|
||||
return StringUtils.defaultString(type);
|
||||
}
|
||||
|
||||
@Nullable public Title title() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Nullable public Contents getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
public void setContents(@Nullable final Contents contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
@NonNull public Date getTimestamp() {
|
||||
return timestamp != null ? timestamp.date() : new Date();
|
||||
}
|
||||
|
||||
public void setTimestamp(@Nullable final Timestamp timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
@NonNull String getUtcIso8601() {
|
||||
return StringUtils.defaultString(timestamp != null ? timestamp.utciso8601 : null);
|
||||
}
|
||||
|
||||
public boolean isFromWikidata() {
|
||||
return wiki().equals("wikidatawiki");
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return Long.toString(id);
|
||||
}
|
||||
|
||||
public static class Title {
|
||||
@Nullable private String full;
|
||||
@Nullable private String text;
|
||||
|
||||
@NonNull public String text() {
|
||||
return StringUtils.defaultString(text);
|
||||
}
|
||||
|
||||
@NonNull public String full() {
|
||||
return StringUtils.defaultString(full);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Timestamp {
|
||||
@Nullable private String utciso8601;
|
||||
|
||||
public void setUtciso8601(@Nullable final String utciso8601) {
|
||||
this.utciso8601 = utciso8601;
|
||||
}
|
||||
|
||||
public Date date() {
|
||||
try {
|
||||
return DateUtil.iso8601DateParse(utciso8601);
|
||||
} catch (ParseException e) {
|
||||
Timber.e(e);
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Link {
|
||||
@Nullable private String url;
|
||||
@Nullable private String label;
|
||||
@Nullable private String tooltip;
|
||||
@Nullable private String description;
|
||||
@Nullable private String icon;
|
||||
|
||||
@NonNull public String getUrl() {
|
||||
return StringUtils.defaultString(url);
|
||||
}
|
||||
|
||||
public void setUrl(@Nullable final String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@NonNull public String getTooltip() {
|
||||
return StringUtils.defaultString(tooltip);
|
||||
}
|
||||
|
||||
@NonNull public String getLabel() {
|
||||
return StringUtils.defaultString(label);
|
||||
}
|
||||
|
||||
@NonNull public String getIcon() {
|
||||
return StringUtils.defaultString(icon);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Links {
|
||||
@Nullable private JsonElement primary;
|
||||
private Link primaryLink;
|
||||
|
||||
public void setPrimary(@Nullable final JsonElement primary) {
|
||||
this.primary = primary;
|
||||
}
|
||||
|
||||
@Nullable public Link getPrimary() {
|
||||
if (primary == null) {
|
||||
return null;
|
||||
}
|
||||
if (primaryLink == null && primary instanceof JsonObject) {
|
||||
primaryLink = GsonUtil.INSTANCE.getDefaultGson().fromJson(primary, Link.class);
|
||||
}
|
||||
return primaryLink;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Contents {
|
||||
@Nullable private String header;
|
||||
@Nullable private String compactHeader;
|
||||
@Nullable private String body;
|
||||
@Nullable private String icon;
|
||||
@Nullable private Links links;
|
||||
|
||||
@NonNull public String getHeader() {
|
||||
return StringUtils.defaultString(header);
|
||||
}
|
||||
|
||||
@NonNull public String getCompactHeader() {
|
||||
return StringUtils.defaultString(compactHeader);
|
||||
}
|
||||
|
||||
public void setCompactHeader(@Nullable final String compactHeader) {
|
||||
this.compactHeader = compactHeader;
|
||||
}
|
||||
|
||||
@NonNull public String getBody() {
|
||||
return StringUtils.defaultString(body);
|
||||
}
|
||||
|
||||
@Nullable public Links getLinks() {
|
||||
return links;
|
||||
}
|
||||
|
||||
public void setLinks(@Nullable final Links links) {
|
||||
this.links = links;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
package fr.free.nrw.commons.wikidata.model.notifications
|
||||
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import fr.free.nrw.commons.utils.DateUtil.iso8601DateParse
|
||||
import fr.free.nrw.commons.wikidata.GsonUtil.defaultGson
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
import timber.log.Timber
|
||||
import java.text.ParseException
|
||||
import java.util.Date
|
||||
|
||||
class Notification {
|
||||
private val wiki: String? = null
|
||||
private var id: Long = 0
|
||||
private val type: String? = null
|
||||
private val category: String? = null
|
||||
|
||||
private val title: Title? = null
|
||||
private var timestamp: Timestamp? = null
|
||||
|
||||
@SerializedName("*")
|
||||
var contents: Contents? = null
|
||||
|
||||
fun wiki(): String = wiki ?: ""
|
||||
|
||||
fun id(): Long = id
|
||||
|
||||
fun setId(id: Long) {
|
||||
this.id = id
|
||||
}
|
||||
|
||||
fun key(): Long =
|
||||
id + wiki().hashCode()
|
||||
|
||||
fun type(): String =
|
||||
type ?: ""
|
||||
|
||||
fun title(): Title? = title
|
||||
|
||||
fun getTimestamp(): Date =
|
||||
timestamp?.date() ?: Date()
|
||||
|
||||
fun setTimestamp(timestamp: Timestamp?) {
|
||||
this.timestamp = timestamp
|
||||
}
|
||||
|
||||
val utcIso8601: String
|
||||
get() = timestamp?.utciso8601 ?: ""
|
||||
|
||||
val isFromWikidata: Boolean
|
||||
get() = wiki() == "wikidatawiki"
|
||||
|
||||
override fun toString(): String =
|
||||
id.toString()
|
||||
|
||||
class Title {
|
||||
private val full: String? = null
|
||||
private val text: String? = null
|
||||
|
||||
fun text(): String = text ?: ""
|
||||
|
||||
fun full(): String = full ?: ""
|
||||
}
|
||||
|
||||
class Timestamp {
|
||||
internal var utciso8601: String? = null
|
||||
|
||||
fun setUtciso8601(utciso8601: String?) {
|
||||
this.utciso8601 = utciso8601
|
||||
}
|
||||
|
||||
fun date(): Date {
|
||||
try {
|
||||
return iso8601DateParse(utciso8601 ?: "")
|
||||
} catch (e: ParseException) {
|
||||
Timber.e(e)
|
||||
return Date()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Link {
|
||||
var url: String? = null
|
||||
get() = field ?: ""
|
||||
val label: String? = null
|
||||
get() = field ?: ""
|
||||
val tooltip: String? = null
|
||||
get() = field ?: ""
|
||||
private val description: String? = null
|
||||
val icon: String? = null
|
||||
get() = field ?: ""
|
||||
}
|
||||
|
||||
class Links {
|
||||
private var primary: JsonElement? = null
|
||||
private var primaryLink: Link? = null
|
||||
|
||||
fun setPrimary(primary: JsonElement?) {
|
||||
this.primary = primary
|
||||
}
|
||||
|
||||
fun getPrimary(): Link? {
|
||||
if (primary == null) {
|
||||
return null
|
||||
}
|
||||
if (primaryLink == null && primary is JsonObject) {
|
||||
primaryLink = defaultGson.fromJson(primary, Link::class.java)
|
||||
}
|
||||
return primaryLink
|
||||
}
|
||||
}
|
||||
|
||||
class Contents {
|
||||
val header: String? = null
|
||||
get() = field ?: ""
|
||||
var compactHeader: String? = null
|
||||
get() = field ?: ""
|
||||
val body: String? = null
|
||||
get() = field ?: ""
|
||||
private val icon: String? = null
|
||||
var links: Links? = null
|
||||
}
|
||||
}
|
||||
|
|
@ -123,11 +123,11 @@ class NotificationClientTest {
|
|||
setTimestamp(Notification.Timestamp().apply { setUtciso8601(timestamp) })
|
||||
|
||||
contents = Notification.Contents().apply {
|
||||
setCompactHeader(compactHeader)
|
||||
this.compactHeader = compactHeader
|
||||
|
||||
links = Notification.Links().apply {
|
||||
setPrimary(GsonUtil.defaultGson.toJsonTree(
|
||||
Notification.Link().apply { setUrl(primaryUrl) }
|
||||
Notification.Link().apply { url = primaryUrl }
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue