PixelDroid-App-Android/app/src/main/java/com/h/pixeldroid/objects/Status.kt

126 lines
3.7 KiB
Kotlin

package com.h.pixeldroid.objects
import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.h.pixeldroid.R
import com.h.pixeldroid.utils.ImageConverter
import java.io.Serializable
/*
Represents a status posted by an account.
https://docs.joinmastodon.org/entities/status/
*/
data class Status(
//Base attributes
val id: String,
val uri: String,
val created_at: String, //ISO 8601 Datetime (maybe can use a date type)
val account: Account,
val content: String, //HTML
val visibility: Visibility,
val sensitive: Boolean,
val spoiler_text: String,
val media_attachments: List<Attachment>,
val application: Application,
//Rendering attributes
val mentions: List<Mention>,
val tags: List<Tag>,
val emojis: List<Emoji>,
//Informational attributes
val reblogs_count: Int,
val favourites_count: Int,
val replies_count: Int,
//Nullable attributes
val url: String?, //URL
val in_reply_to_id: String?,
val in_reply_to_account: String?,
val reblog: Status?,
val poll: Poll?,
val card: Card?,
val language: String?, //ISO 639 Part 1 two-letter language code
val text: String?,
//Authorized user attributes
val favourited: Boolean,
val reblogged: Boolean,
val muted: Boolean,
val bookmarked: Boolean,
val pinned: Boolean
) : Serializable
{
companion object {
const val POST_TAG = "postTag"
const val POST_FRAG_TAG = "postFragTag"
}
fun getPostUrl() : String? = media_attachments?.getOrNull(0)?.url
fun getProfilePicUrl() : String? = account?.avatar
fun getDescription() : CharSequence {
val description = content as CharSequence
if(description.isEmpty()) {
return "No description"
}
return description
}
fun getUsername() : CharSequence {
var name = account?.username
if (name.isNullOrEmpty()) {
name = account?.display_name
}
return name!!
}
fun getUsernameDescription() : CharSequence {
return account?.display_name ?: ""
}
fun getNLikes() : CharSequence {
val nLikes : Int = favourites_count ?: 0
return "$nLikes Likes"
}
fun getNShares() : CharSequence {
val nShares : Int = reblogs_count ?: 0
return "$nShares Shares"
}
fun setupPost(fragment: Fragment, rootView : View) {
//Setup username as a button that opens the profile
val username = rootView.findViewById<TextView>(R.id.username)
username.text = this.getUsername()
username.setTypeface(null, Typeface.BOLD)
val usernameDesc = rootView.findViewById<TextView>(R.id.usernameDesc)
usernameDesc.text = this.getUsernameDescription()
usernameDesc.setTypeface(null, Typeface.BOLD)
val description = rootView.findViewById<TextView>(R.id.description)
description.text = this.getDescription()
val nlikes = rootView.findViewById<TextView>(R.id.nlikes)
nlikes.text = this.getNLikes()
nlikes.setTypeface(null, Typeface.BOLD)
val nshares = rootView.findViewById<TextView>(R.id.nshares)
nshares.text = this.getNShares()
nshares.setTypeface(null, Typeface.BOLD)
//Setup post and profile images
ImageConverter.setImageViewFromURL(
fragment,
getPostUrl(),
rootView.findViewById(R.id.postPicture)
)
ImageConverter.setImageViewFromURL(
fragment,
getProfilePicUrl(),
rootView.findViewById(R.id.profilePic)
)
}
enum class Visibility : Serializable {
public, unlisted, private, direct
}
}