Start work on integrating Retrofit - Update Account entity to be fetched with Retrofit and parsed with GSON
parent
348d2c8b4f
commit
3120fbed4c
@ -1,92 +0,0 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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; |
||||
|
||||
import android.text.Spanned; |
||||
|
||||
import org.json.JSONArray; |
||||
import org.json.JSONException; |
||||
import org.json.JSONObject; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
class Account { |
||||
String id; |
||||
String username; |
||||
String displayName; |
||||
Spanned note; |
||||
String url; |
||||
String avatar; |
||||
String header; |
||||
String followersCount; |
||||
String followingCount; |
||||
String statusesCount; |
||||
|
||||
public static Account parse(JSONObject object) throws JSONException { |
||||
Account account = new Account(); |
||||
account.id = object.getString("id"); |
||||
account.username = object.getString("acct"); |
||||
account.displayName = object.getString("display_name"); |
||||
if (account.displayName.isEmpty()) { |
||||
account.displayName = object.getString("username"); |
||||
} |
||||
account.note = HtmlUtils.fromHtml(object.getString("note")); |
||||
account.url = object.getString("url"); |
||||
String avatarUrl = object.getString("avatar"); |
||||
if (!avatarUrl.equals("/avatars/original/missing.png")) { |
||||
account.avatar = avatarUrl; |
||||
} else { |
||||
account.avatar = null; |
||||
} |
||||
String headerUrl = object.getString("header"); |
||||
if (!headerUrl.equals("/headers/original/missing.png")) { |
||||
account.header = headerUrl; |
||||
} else { |
||||
account.header = null; |
||||
} |
||||
account.followersCount = object.getString("followers_count"); |
||||
account.followingCount = object.getString("following_count"); |
||||
account.statusesCount = object.getString("statuses_count"); |
||||
return account; |
||||
} |
||||
|
||||
public static List<Account> parse(JSONArray array) throws JSONException { |
||||
List<Account> accounts = new ArrayList<>(); |
||||
for (int i = 0; i < array.length(); i++) { |
||||
JSONObject object = array.getJSONObject(i); |
||||
Account account = parse(object); |
||||
accounts.add(account); |
||||
} |
||||
return accounts; |
||||
} |
||||
|
||||
@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); |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
package com.keylesspalace.tusky; |
||||
|
||||
import android.text.Spanned; |
||||
|
||||
import com.google.gson.JsonDeserializationContext; |
||||
import com.google.gson.JsonDeserializer; |
||||
import com.google.gson.JsonElement; |
||||
import com.google.gson.JsonParseException; |
||||
|
||||
import java.lang.reflect.Type; |
||||
|
||||
class SpannedTypeAdapter implements JsonDeserializer<Spanned> { |
||||
@Override |
||||
public Spanned deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
||||
return HtmlUtils.fromHtml(json.getAsString()); |
||||
} |
||||
} |
@ -0,0 +1,63 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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; |
||||
|
||||
public class Account { |
||||
public String id; |
||||
|
||||
@SerializedName("acct") |
||||
public String username; |
||||
|
||||
@SerializedName("display_name") |
||||
public String displayName; |
||||
|
||||
public Spanned note; |
||||
|
||||
public String url; |
||||
|
||||
public String avatar; |
||||
|
||||
public String header; |
||||
|
||||
@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); |
||||
} |
||||
} |
Loading…
Reference in new issue