2018-07-30 23:43:27 +10:00
|
|
|
/* Copyright 2018 Conny Duck
|
|
|
|
*
|
|
|
|
* 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.db
|
|
|
|
|
2019-02-13 05:22:37 +11:00
|
|
|
import android.text.Spanned
|
2018-12-18 01:25:35 +11:00
|
|
|
import androidx.room.TypeConverter
|
2019-02-13 05:22:37 +11:00
|
|
|
import com.google.gson.GsonBuilder
|
2018-07-30 23:43:27 +10:00
|
|
|
import com.google.gson.reflect.TypeToken
|
2019-02-13 05:22:37 +11:00
|
|
|
import com.keylesspalace.tusky.TabData
|
|
|
|
import com.keylesspalace.tusky.components.conversation.ConversationAccountEntity
|
|
|
|
import com.keylesspalace.tusky.createTabDataFromId
|
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
2018-07-30 23:43:27 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Emoji
|
2018-11-13 07:09:39 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Status
|
2019-02-13 05:22:37 +11:00
|
|
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter
|
|
|
|
import com.keylesspalace.tusky.util.HtmlUtils
|
|
|
|
import java.util.*
|
2018-07-30 23:43:27 +10:00
|
|
|
|
|
|
|
class Converters {
|
|
|
|
|
2019-02-13 05:22:37 +11:00
|
|
|
private val gson = GsonBuilder()
|
|
|
|
.registerTypeAdapter(Spanned::class.java, SpannedTypeAdapter())
|
|
|
|
.create()
|
2018-07-30 23:43:27 +10:00
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun jsonToEmojiList(emojiListJson: String?): List<Emoji>? {
|
|
|
|
return gson.fromJson(emojiListJson, object : TypeToken<List<Emoji>>() {}.type)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun emojiListToJson(emojiList: List<Emoji>?): String {
|
|
|
|
return gson.toJson(emojiList)
|
|
|
|
}
|
2018-11-13 07:09:39 +11:00
|
|
|
|
|
|
|
@TypeConverter
|
2019-02-13 05:22:37 +11:00
|
|
|
fun visibilityToInt(visibility: Status.Visibility?): Int {
|
|
|
|
return visibility?.num ?: Status.Visibility.UNKNOWN.num
|
2018-11-13 07:09:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun intToVisibility(visibility: Int): Status.Visibility {
|
|
|
|
return Status.Visibility.byNum(visibility)
|
|
|
|
}
|
2019-02-13 05:22:37 +11:00
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun stringToTabData(str: String?): List<TabData>? {
|
|
|
|
return str?.split(";")
|
2019-03-24 18:59:55 +11:00
|
|
|
?.map {
|
|
|
|
val data = it.split(":")
|
|
|
|
createTabDataFromId(data[0], data.drop(1))
|
|
|
|
}
|
2019-02-13 05:22:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun tabDataToString(tabData: List<TabData>?): String? {
|
2019-03-24 18:59:55 +11:00
|
|
|
return tabData?.joinToString(";") { it.id + ":" + it.arguments.joinToString(":") }
|
2019-02-13 05:22:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun accountToJson(account: ConversationAccountEntity?): String {
|
|
|
|
return gson.toJson(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun jsonToAccount(accountJson: String?): ConversationAccountEntity? {
|
|
|
|
return gson.fromJson(accountJson, ConversationAccountEntity::class.java)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun accountListToJson(accountList: List<ConversationAccountEntity>?): String {
|
|
|
|
return gson.toJson(accountList)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun jsonToAccountList(accountListJson: String?): List<ConversationAccountEntity>? {
|
|
|
|
return gson.fromJson(accountListJson, object : TypeToken<List<ConversationAccountEntity>>() {}.type)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun attachmentListToJson(attachmentList: List<Attachment>?): String {
|
|
|
|
return gson.toJson(attachmentList)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
2019-04-21 23:16:39 +10:00
|
|
|
fun jsonToAttachmentList(attachmentListJson: String?): ArrayList<Attachment>? {
|
|
|
|
return gson.fromJson(attachmentListJson, object : TypeToken<ArrayList<Attachment>>() {}.type)
|
2019-02-13 05:22:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun mentionArrayToJson(mentionArray: Array<Status.Mention>?): String? {
|
|
|
|
return gson.toJson(mentionArray)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun jsonToMentionArray(mentionListJson: String?): Array<Status.Mention>? {
|
|
|
|
return gson.fromJson(mentionListJson, object : TypeToken<Array<Status.Mention>>() {}.type)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun dateToLong(date: Date): Long {
|
|
|
|
return date.time
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun longToDate(date: Long): Date {
|
|
|
|
return Date(date)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun spannedToString(spanned: Spanned?): String? {
|
|
|
|
if(spanned == null) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return HtmlUtils.toHtml(spanned)
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
fun stringToSpanned(spannedString: String?): Spanned? {
|
|
|
|
if(spannedString == null) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return HtmlUtils.fromHtml(spannedString)
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:43:27 +10:00
|
|
|
}
|