summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/client/entities/api/Field.java
blob: 6c24c0ea4a12b258736b0212446caa5641b4b696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package app.fedilab.android.mastodon.client.entities.api;
/* Copyright 2021 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * 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.
 *
 * Fedilab 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 Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.content.Context;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.Date;

import app.fedilab.android.R;
import app.fedilab.android.mastodon.helper.SpannableHelper;

public class Field implements Serializable {
    @SerializedName("name")
    public String name;
    @SerializedName("value")
    public String value;
    @SerializedName("verified_at")
    public Date verified_at;

    //Some extra spannable element - They will be filled automatically when fetching the account
    private transient ForegroundColorSpan value_span;
    private transient ForegroundColorSpan name_span;

    public synchronized Spannable getValueSpan(Context context, Account account, WeakReference<View> viewWeakReference) {

        if (verified_at != null && value != null) {
            value_span = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.verified_text));
        }
        Spannable spannable = SpannableHelper.convert(context, value, null, account, null, viewWeakReference);
        if (value_span != null && spannable != null) {
            spannable.setSpan(value_span, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannable;
    }


    public synchronized Spannable getLabelSpan(Context context, Account account, WeakReference<View> viewWeakReference) {

        Spannable spannable = SpannableHelper.convert(context, name, null, account, null, viewWeakReference);
        if (name_span != null && spannable != null) {
            spannable.setSpan(name_span, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannable;
    }

    public static class FieldParams implements Serializable {
        @SerializedName("name")
        public String name;
        @SerializedName("value")
        public String value;
    }
}