convert entity classes to Kotlin data classes (#526)

* convert entity classes to Kotlin data classes

* more data classes, code style
This commit is contained in:
Konrad Pozniak 2018-03-03 13:24:03 +01:00 committed by GitHub
commit 71954a277e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 604 additions and 785 deletions

View file

@ -13,11 +13,10 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.SerializedName
public class AccessToken {
@SerializedName("access_token")
public String accessToken;
}
data class AccessToken(
@SerializedName("access_token") val accessToken: String
)

View file

@ -1,130 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Spanned;
import com.google.gson.annotations.SerializedName;
import com.keylesspalace.tusky.util.HtmlUtils;
public class Account implements Parcelable {
public String id;
@SerializedName("username")
public String localUsername;
@SerializedName("acct")
public String username;
@SerializedName("display_name")
public String displayName;
public Spanned note;
public String url;
public String avatar;
public String header;
public boolean locked;
@SerializedName("followers_count")
public String followersCount;
@SerializedName("following_count")
public String followingCount;
@SerializedName("statuses_count")
public String statusesCount;
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object other) {
if (this.id == null) {
return this == other;
} else if (!(other instanceof Account)) {
return false;
}
Account account = (Account) other;
return account.id.equals(this.id);
}
public String getDisplayName() {
if (displayName.length() == 0) {
return localUsername;
}
return displayName;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(localUsername);
dest.writeString(username);
dest.writeString(displayName);
dest.writeString(HtmlUtils.toHtml(note));
dest.writeString(url);
dest.writeString(avatar);
dest.writeString(header);
dest.writeBooleanArray(new boolean[] { locked });
dest.writeString(followersCount);
dest.writeString(followingCount);
dest.writeString(statusesCount);
}
public Account() {}
protected Account(Parcel in) {
id = in.readString();
localUsername = in.readString();
username = in.readString();
displayName = in.readString();
note = HtmlUtils.fromHtml(in.readString());
url = in.readString();
avatar = in.readString();
header = in.readString();
boolean[] lockedArray = new boolean[1];
in.readBooleanArray(lockedArray);
locked = lockedArray[0];
followersCount = in.readString();
followingCount = in.readString();
statusesCount = in.readString();
}
public static final Creator<Account> CREATOR = new Creator<Account>() {
@Override
public Account createFromParcel(Parcel source) {
return new Account(source);
}
@Override
public Account[] newArray(int size) {
return new Account[size];
}
};
}

View file

@ -0,0 +1,69 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity
import android.os.Parcel
import android.os.Parcelable
import android.text.Spanned
import com.google.gson.annotations.SerializedName
import com.keylesspalace.tusky.util.HtmlUtils
import kotlinx.android.parcel.Parceler
import kotlinx.android.parcel.Parcelize
import kotlinx.android.parcel.WriteWith
@Parcelize
data class Account(
val id: String,
@SerializedName("username") val localUsername: String,
@SerializedName("acct") val username: String,
@SerializedName("display_name") val displayName: String,
val note: @WriteWith<SpannedParceler>() Spanned,
val url: String,
val avatar: String,
val header: String,
val locked: Boolean = false,
@SerializedName("followers_count") val followersCount: Int,
@SerializedName("following_count") val followingCount: Int,
@SerializedName("statuses_count") val statusesCount: Int
) : Parcelable {
val name: String
get() = if (displayName.isEmpty()) {
localUsername
} else displayName
override fun hashCode(): Int {
return id.hashCode()
}
override fun equals(other: Any?): Boolean {
if (other !is Account) {
return false
}
val account = other as Account?
return account?.id == this.id
}
object SpannedParceler : Parceler<Spanned> {
override fun create(parcel: Parcel) = HtmlUtils.fromHtml(parcel.readString())
override fun Spanned.write(parcel: Parcel, flags: Int) {
parcel.writeString(HtmlUtils.toHtml(this))
}
}
}

View file

@ -13,14 +13,11 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.SerializedName
public class AppCredentials {
@SerializedName("client_id")
public String clientId;
@SerializedName("client_secret")
public String clientSecret;
}
data class AppCredentials(
@SerializedName("client_id") val clientId: String,
@SerializedName("client_secret") val clientSecret: String
)

View file

@ -1,79 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
public class Attachment {
public String id;
public String url;
@SerializedName("preview_url")
public String previewUrl;
@SerializedName("text_url")
public String textUrl;
public Type type;
public String description;
public static class Meta {
public MediaProperties original;
public MediaProperties small;
}
public static class MediaProperties {
public int width;
public int height;
public float aspect;
}
@JsonAdapter(MediaTypeDeserializer.class)
public enum Type {
@SerializedName("image")
IMAGE,
@SerializedName("gifv")
GIFV,
@SerializedName("video")
VIDEO,
@SerializedName("unknown")
UNKNOWN
}
static class MediaTypeDeserializer implements JsonDeserializer<Type> {
@Override
public Type deserialize(JsonElement json, java.lang.reflect.Type classOfT, JsonDeserializationContext context)
throws JsonParseException {
switch(json.toString()) {
case "\"image\"":
return Type.IMAGE;
case "\"gifv\"":
return Type.GIFV;
case "\"video\"":
return Type.VIDEO;
default:
return Type.UNKNOWN;
}
}
}
}

View file

@ -0,0 +1,57 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
data class Attachment(
var id: String,
var url: String,
@SerializedName("preview_url") val previewUrl: String,
@SerializedName("text_url") val textUrl: String?,
var type: Type,
var description: String?
) {
@JsonAdapter(MediaTypeDeserializer::class)
enum class Type {
@SerializedName("image")
IMAGE,
@SerializedName("gifv")
GIFV,
@SerializedName("video")
VIDEO,
@SerializedName("unknown")
UNKNOWN
}
class MediaTypeDeserializer : JsonDeserializer<Type> {
@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, classOfT: java.lang.reflect.Type, context: JsonDeserializationContext): Type {
return when (json.toString()) {
"\"image\"" -> Type.IMAGE
"\"gifv\"" -> Type.GIFV
"\"video\"" -> Type.VIDEO
else -> Type.UNKNOWN
}
}
}
}

View file

@ -1,92 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
import android.os.Parcel;
import android.os.Parcelable;
public class Card implements Parcelable {
public String url;
public String title;
public String description;
public String image;
public String type;
public int width;
public int height;
@Override
public int hashCode() {
return url.hashCode();
}
@Override
public boolean equals(Object other) {
if (this.url == null) {
return this == other;
} else if (!(other instanceof Card)) {
return false;
}
Card account = (Card) other;
return account.url.equals(this.url);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(url);
dest.writeString(title);
dest.writeString(description);
dest.writeString(image);
dest.writeString(type);
dest.writeInt(width);
dest.writeInt(height);
}
public Card() {}
private Card(Parcel in) {
url = in.readString();
title = in.readString();
description = in.readString();
image = in.readString();
type = in.readString();
width = in.readInt();
height = in.readInt();
}
public static final Creator<Card> CREATOR = new Creator<Card>() {
@Override
public Card createFromParcel(Parcel source) {
return new Card(source);
}
@Override
public Card[] newArray(int size) {
return new Card[size];
}
};
}

View file

@ -13,21 +13,32 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName;
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
public class Relationship {
public String id;
@Parcelize
data class Card(
val url: String,
val title: String,
val description: String,
val image: String,
val type: String,
val width: Int,
val height: Int
) : Parcelable {
public boolean following;
override fun hashCode(): Int {
return url.hashCode()
}
@SerializedName("followed_by")
public boolean followedBy;
override fun equals(other: Any?): Boolean {
if (other !is Card) {
return false
}
val account = other as Card?
return account?.url == this.url
}
public boolean blocking;
public boolean muting;
public boolean requested;
}

View file

@ -13,12 +13,17 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.SerializedName
public class Notification {
public enum Type {
data class Notification(
val type: Type,
val id: String,
val account: Account,
val status: Status?) {
enum class Type {
@SerializedName("mention")
MENTION,
@SerializedName("reblog")
@ -26,30 +31,18 @@ public class Notification {
@SerializedName("favourite")
FAVOURITE,
@SerializedName("follow")
FOLLOW,
FOLLOW
}
public Type type;
public String id;
public Account account;
public Status status;
@Override
public int hashCode() {
return id.hashCode();
override fun hashCode(): Int {
return id.hashCode()
}
@Override
public boolean equals(Object other) {
if (this.id == null) {
return this == other;
} else if (!(other instanceof Notification)) {
return false;
override fun equals(other: Any?): Boolean {
if (other !is Notification) {
return false
}
Notification notification = (Notification) other;
return notification.id.equals(this.id);
val notification = other as Notification?
return notification?.id == this.id
}
}

View file

@ -0,0 +1,11 @@
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName
data class Profile(
@SerializedName("display_name") val displayName: String?,
val note: String?,
val avatar: String?,
val header: String? = null
)

View file

@ -0,0 +1,27 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity
import com.google.gson.annotations.SerializedName
data class Relationship (
val id: String,
val following: Boolean,
@SerializedName("followed_by") val followedBy: Boolean,
val blocking: Boolean,
val muting: Boolean,
val requested: Boolean
)

View file

@ -13,11 +13,10 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
import java.util.List;
public class StatusContext {
public List<Status> ancestors;
public List<Status> descendants;
}
data class SearchResults (
val accounts: List<Account>,
val statuses: List<Status>,
val hashtags: List<String>
)

View file

@ -1,189 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
import android.text.Spanned;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
import java.util.List;
public class Status {
public String url;
@SerializedName("reblogs_count")
public String reblogsCount;
@SerializedName("favourites_count")
public String favouritesCount;
@SerializedName("in_reply_to_id")
public String inReplyToId;
@SerializedName("in_reply_to_account_id")
public String inReplyToAccountId;
public String getActionableId() {
return reblog == null ? id : reblog.id;
}
public Status getActionableStatus() {
return reblog == null ? this : reblog;
}
public enum Visibility {
UNKNOWN(0),
@SerializedName("public")
PUBLIC(1),
@SerializedName("unlisted")
UNLISTED(2),
@SerializedName("private")
PRIVATE(3),
@SerializedName("direct")
DIRECT(4);
private final int num;
Visibility(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public static Visibility byNum(int num) {
switch (num) {
case 4: return DIRECT;
case 3: return PRIVATE;
case 2: return UNLISTED;
case 1: return PUBLIC;
case 0: default: return UNKNOWN;
}
}
public static Visibility byString(String s) {
switch (s) {
case "public": return PUBLIC;
case "unlisted": return UNLISTED;
case "private": return PRIVATE;
case "direct": return DIRECT;
case "unknown": default: return UNKNOWN;
}
}
public String serverString() {
switch (this) {
case PUBLIC: return "public";
case UNLISTED: return "unlisted";
case PRIVATE: return "private";
case DIRECT: return "direct";
case UNKNOWN: default: return "unknown";
}
}
}
public String id;
public Account account;
public Spanned content;
public Status reblog;
@SerializedName("created_at")
public Date createdAt;
public boolean reblogged;
public boolean favourited;
public boolean sensitive;
public List<Emoji> emojis;
@SerializedName("spoiler_text")
public String spoilerText;
public Visibility visibility;
public Visibility getVisibility() {
return visibility == null ? Visibility.UNLISTED : visibility;
}
public boolean rebloggingAllowed() {
return visibility != null
&& visibility != Visibility.PRIVATE
&& visibility != Visibility.DIRECT
&& visibility != Visibility.UNKNOWN;
}
@SerializedName("media_attachments")
public Attachment[] attachments;
public Mention[] mentions;
public Application application;
public static final int MAX_MEDIA_ATTACHMENTS = 4;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Status status = (Status) o;
return id != null ? id.equals(status.id) : status.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
public static final class Mention {
public String id;
public String url;
@SerializedName("acct")
public String username;
@SerializedName("username")
public String localUsername;
}
public static class Application {
public String name;
public String website;
}
@SuppressWarnings("unused")
public static class Emoji {
private String shortcode;
private String url;
public String getShortcode() {
return shortcode;
}
public String getUrl() {
return url;
}
}
}

View file

@ -0,0 +1,142 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity
import android.text.Spanned
import com.google.gson.annotations.SerializedName
import java.util.*
data class Status(
var id: String,
var url: String,
val account: Account,
@SerializedName("in_reply_to_id") var inReplyToId: String?,
@SerializedName("in_reply_to_account_id") val inReplyToAccountId: String?,
val reblog: Status?,
val content: Spanned,
@SerializedName("created_at") val createdAt: Date,
val emojis: List<Emoji>,
@SerializedName("reblogs_count") val reblogsCount: Int,
@SerializedName("favourites_count") val favouritesCount: Int,
var reblogged: Boolean?,
var favourited: Boolean?,
var sensitive: Boolean,
@SerializedName("spoiler_text") val spoilerText: String,
val visibility: Visibility,
@SerializedName("media_attachments") var attachments: Array<Attachment>,
val mentions: Array<Mention>,
val application: Application?
) {
val actionableId: String?
get() = reblog?.id ?: id
val actionableStatus: Status
get() = reblog ?: this
enum class Visibility(val num: Int) {
UNKNOWN(0),
@SerializedName("public")
PUBLIC(1),
@SerializedName("unlisted")
UNLISTED(2),
@SerializedName("private")
PRIVATE(3),
@SerializedName("direct")
DIRECT(4);
fun serverString(): String {
return when (this) {
PUBLIC -> "public"
UNLISTED -> "unlisted"
PRIVATE -> "private"
DIRECT -> "direct"
UNKNOWN -> "unknown"
}
}
companion object {
@JvmStatic
fun byNum(num: Int): Visibility {
return when (num) {
4 -> DIRECT
3 -> PRIVATE
2 -> UNLISTED
1 -> PUBLIC
0 -> UNKNOWN
else -> UNKNOWN
}
}
@JvmStatic
fun byString(s: String): Visibility {
return when (s) {
"public" -> PUBLIC
"unlisted" -> UNLISTED
"private" -> PRIVATE
"direct" -> DIRECT
"unknown" -> UNKNOWN
else -> UNKNOWN
}
}
}
}
fun rebloggingAllowed(): Boolean {
return (visibility != Visibility.PRIVATE && visibility != Visibility.DIRECT && visibility != Visibility.UNKNOWN)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val status = other as Status?
return id == status?.id
}
override fun hashCode(): Int {
return id.hashCode()
}
class Mention {
var id: String? = null
var url: String? = null
@SerializedName("acct")
var username: String? = null
@SerializedName("username")
var localUsername: String? = null
}
class Application {
var name: String? = null
var website: String? = null
}
class Emoji {
val shortcode: String? = null
val url: String? = null
}
companion object {
const val MAX_MEDIA_ATTACHMENTS = 4
}
}

View file

@ -13,10 +13,9 @@
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity;
package com.keylesspalace.tusky.entity
public class SearchResults {
public Account[] accounts;
public Status[] statuses;
public String[] hashtags;
}
data class StatusContext (
val ancestors: List<Status>,
val descendants: List<Status>
)