Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Gson library with Moshi #4309

Merged
merged 20 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
156c84b
convert API entities from Gson to Moshi
cbeyls Mar 4, 2024
02f0e5c
declare missing optional fields in Instance
cbeyls Mar 5, 2024
48615d5
update DB entities code to use Moshi instead of Gson and remove all G…
cbeyls Mar 5, 2024
800558c
fix missing optional fields Card.image, Attachment.Size.aspect, Insta…
cbeyls Mar 5, 2024
acaf360
Merge branch 'develop' into refactor/gson_to_moshi
cbeyls Mar 5, 2024
4a349ad
replace commented Gson annotations with Moshi annotations
cbeyls Mar 5, 2024
3320dbd
fix KtLint errors and add missing @JsonClass declaration for Instance…
cbeyls Mar 5, 2024
87dfb07
fix KtLint errors in Account.kt
cbeyls Mar 5, 2024
43e63d8
fix KtLint errors in NetworkModule.kt
cbeyls Mar 5, 2024
a475312
add changes suggested by @Goooler
cbeyls Mar 6, 2024
ae477b8
changed more ?: emptyList() to .orEmpty()
cbeyls Mar 6, 2024
25b0606
improve GuardedAdapter code
cbeyls Mar 6, 2024
4db5543
changes after review from @connyduck
cbeyls Mar 29, 2024
d2735de
Merge branch 'develop' into refactor/gson_to_moshi
cbeyls Mar 29, 2024
fc39093
Merge branch 'develop' into refactor/gson_to_moshi
cbeyls Mar 29, 2024
9fc32b2
fix nullable fields declared as non-null
cbeyls Mar 30, 2024
1c6c0b6
comment out unused fields in FilterResult
cbeyls Mar 31, 2024
fb318df
update optional fields according to API documentation + rename fields…
cbeyls Mar 31, 2024
6be3f07
remove unused import after code simplification
cbeyls Mar 31, 2024
01d27dc
regression fix: set the keywords field back to optional, even if the …
cbeyls Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ dependencies {

implementation libs.android.material

implementation libs.gson
implementation libs.bundles.moshi
ksp libs.moshi.kotlin.codegen

implementation libs.bundles.retrofit
implementation libs.networkresult.calladapter
Expand Down
28 changes: 0 additions & 28 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,6 @@

# TUSKY SPECIFIC OPTIONS

# keep members of our model classes, they are used in json de/serialization
-keepclassmembers class com.keylesspalace.tusky.entity.* { *; }

-keep public enum com.keylesspalace.tusky.entity.*$** {
**[] $VALUES;
public *;
}

-keepclassmembers class com.keylesspalace.tusky.components.conversation.ConversationAccountEntity { *; }
-keepclassmembers class com.keylesspalace.tusky.db.DraftAttachment { *; }

-keep enum com.keylesspalace.tusky.db.DraftAttachment$Type {
public *;
}

# https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

# Retain generic signatures of classes used in MastodonApi so Retrofit works
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-keep,allowobfuscation,allowshrinking class kotlin.collections.List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EmojiAdapter(
private val animate: Boolean
) : RecyclerView.Adapter<BindingHolder<ItemEmojiButtonBinding>>() {

private val emojiList: List<Emoji> = emojiList.filter { emoji -> emoji.visibleInPicker == null || emoji.visibleInPicker }
private val emojiList: List<Emoji> = emojiList.filter { emoji -> emoji.visibleInPicker }
.sortedBy { it.shortcode.lowercase(Locale.ROOT) }

override fun getItemCount() = emojiList.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ReportNotificationViewHolder(

binding.notificationTopText.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
binding.notificationTopText.text = itemView.context.getString(R.string.notification_header_report_format, reporterName, reporteeName)
binding.notificationSummary.text = itemView.context.getString(R.string.notification_summary_report_format, getRelativeTimeSpanString(itemView.context, report.createdAt.time, Date().time), report.status_ids?.size ?: 0)
binding.notificationSummary.text = itemView.context.getString(R.string.notification_summary_report_format, getRelativeTimeSpanString(itemView.context, report.createdAt.time, Date().time), report.status_ids.size)
binding.notificationCategory.text = getTranslatedCategory(itemView.context, report.category)

// Fancy avatar inset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public void setupWithStatus(@NonNull StatusViewData.Concrete status,

setupButtons(listener, actionable.getAccount().getId(), status.getContent().toString(),
statusDisplayOptions);
setRebloggingEnabled(actionable.rebloggingAllowed(), actionable.getVisibility());
setRebloggingEnabled(actionable.isRebloggingAllowed(), actionable.getVisibility());

setSpoilerAndContent(status, statusDisplayOptions, listener);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.keylesspalace.tusky.appstore

import com.google.gson.Gson
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.AppDatabase
import javax.inject.Inject
Expand All @@ -13,8 +12,7 @@ import kotlinx.coroutines.launch
class CacheUpdater @Inject constructor(
eventHub: EventHub,
accountManager: AccountManager,
appDatabase: AppDatabase,
gson: Gson
appDatabase: AppDatabase
) {

private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
Expand All @@ -30,17 +28,15 @@ class CacheUpdater @Inject constructor(
val status = event.status
timelineDao.update(
accountId = accountId,
status = status,
gson = gson
status = status
)
}
is UnfollowEvent ->
timelineDao.removeAllByUser(accountId, event.accountId)
is StatusDeletedEvent ->
timelineDao.delete(accountId, event.statusId)
is PollVoteEvent -> {
val pollString = gson.toJson(event.poll)
timelineDao.setVoted(accountId, event.statusId, pollString)
timelineDao.setVoted(accountId, event.statusId, event.poll)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
)
setClickableText(binding.accountNoteTextView, emojifiedNote, emptyList(), null, this)

accountFieldAdapter.fields = account.fields.orEmpty()
accountFieldAdapter.emojis = account.emojis.orEmpty()
accountFieldAdapter.fields = account.fields
accountFieldAdapter.emojis = account.emojis
accountFieldAdapter.notifyDataSetChanged()

binding.accountLockedImageView.visible(account.locked)
Expand Down Expand Up @@ -660,7 +660,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
*/
private fun updateRemoteAccount() {
loadedAccount?.let { account ->
if (account.isRemote()) {
if (account.isRemote) {
binding.accountRemoveView.show()
binding.accountRemoveView.setOnClickListener {
openLink(account.url)
Expand Down Expand Up @@ -1088,7 +1088,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
}

private fun getFullUsername(account: Account): String {
return if (account.isRemote()) {
return if (account.isRemote) {
"@" + account.username
} else {
val localUsername = account.localUsername
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class ComposeViewModel @Inject constructor(
val tootToSend = StatusToSend(
text = content,
warningText = spoilerText,
visibility = statusVisibility.value.serverString(),
visibility = statusVisibility.value.serverString,
sensitive = attachedMedia.isNotEmpty() && (markMediaAsSensitive.value || showContentWarning.value),
media = attachedMedia,
scheduledAt = scheduledAt.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.entity.TimelineAccount
import com.keylesspalace.tusky.viewdata.StatusViewData
import com.squareup.moshi.JsonClass
import java.util.Date

@Entity(primaryKeys = ["id", "accountId"])
Expand All @@ -50,6 +51,7 @@ data class ConversationEntity(
}
}

@JsonClass(generateAdapter = true)
data class ConversationAccountEntity(
val id: String,
val localUsername: String,
Expand Down Expand Up @@ -131,7 +133,7 @@ data class ConversationStatusEntity(
poll = poll,
card = null,
language = language,
filtered = null
filtered = emptyList()
),
isExpanded = expanded,
isShowingContent = showingHiddenContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ class DraftHelper @Inject constructor(
}
}

val attachments: MutableList<DraftAttachment> = mutableListOf()
for (i in mediaUris.indices) {
attachments.add(
DraftAttachment(
uriString = uris[i].toString(),
description = mediaDescriptions[i],
focus = mediaFocus[i],
type = types[i]
val attachments: List<DraftAttachment> = buildList {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val attachments: List<DraftAttachment> = buildList {
val attachments: List<DraftAttachment> = buildList(mediaUris.size) {

for (i in mediaUris.indices) {
add(
DraftAttachment(
uriString = uris[i].toString(),
description = mediaDescriptions[i],
focus = mediaFocus[i],
type = types[i]
)
)
)
}
}

val draft = DraftEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LoginWebViewViewModel @Inject constructor(
viewModelScope.launch {
api.getInstance().fold(
{ instance ->
instanceRules.value = instance.rules.orEmpty().map { rule -> rule.text }
instanceRules.value = instance.rules.map { rule -> rule.text }
},
{ throwable ->
if (throwable.isHttpNotFound()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
key = PrefKeys.DEFAULT_POST_PRIVACY
setSummaryProvider { entry }
val visibility = accountManager.activeAccount?.defaultPostPrivacy ?: Status.Visibility.PUBLIC
value = visibility.serverString()
value = visibility.serverString
setIcon(getIconForVisibility(visibility))
setOnPreferenceChangeListener { _, newValue ->
setIcon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class SearchStatusesFragment : SearchFragment<StatusViewData.Concrete>(), Status
Status.Visibility.PUBLIC, Status.Visibility.UNLISTED -> {
val textId =
getString(
if (status.isPinned()) R.string.unpin_action else R.string.pin_action
if (status.pinned) R.string.unpin_action else R.string.pin_action
)
menu.add(0, R.id.pin, 1, textId)
}
Expand Down Expand Up @@ -391,7 +391,7 @@ class SearchStatusesFragment : SearchFragment<StatusViewData.Concrete>(), Status
return@setOnMenuItemClickListener true
}
R.id.pin -> {
viewModel.pinAccount(status, !status.isPinned())
viewModel.pinAccount(status, !status.pinned)
return@setOnMenuItemClickListener true
}
}
Expand Down Expand Up @@ -498,7 +498,7 @@ class SearchStatusesFragment : SearchFragment<StatusViewData.Concrete>(), Status
{ deletedStatus ->
removeItem(position)

val redraftStatus = if (deletedStatus.isEmpty()) {
val redraftStatus = if (deletedStatus.isEmpty) {
status.toDeletedStatus()
} else {
deletedStatus
Expand Down
Loading