Skip to content

Commit

Permalink
Update Gravatar to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGrzybkowski committed Oct 25, 2024
1 parent da5752d commit 926eb61
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ private int getAvatarSize(Context context) {
}
return new AvatarUrl(
new Email(site.getEmail()),
new AvatarQueryOptions(getAvatarSize(context), DefaultAvatarOption.Status404.INSTANCE, null, null)
).url().toString();
new AvatarQueryOptions.Builder()
.setPreferredSize(getAvatarSize(context))
.setDefaultAvatarOption(DefaultAvatarOption.Status404.INSTANCE)
.build()
).url(null).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import androidx.core.widget.NestedScrollView
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.gravatar.services.AvatarService
import com.gravatar.services.Result
import com.gravatar.services.GravatarResult
import com.gravatar.types.Email
import com.yalantis.ucrop.UCrop
import com.yalantis.ucrop.UCropActivity
Expand Down Expand Up @@ -725,12 +725,14 @@ class SignupEpilogueFragment : LoginBaseFormFragment<SignupEpilogueListener?>(),
mAccountStore.accessToken?.let { accessToken ->
startProgress(false)
lifecycleScope.launch {
val result = mAvatarService.upload(
file, Email(mAccountStore.account.email),
accessToken
val result = mAvatarService.uploadCatching(
file,
accessToken,
hash = Email(mAccountStore.account.email).hash(),
selectAvatar = true
)
when (result) {
is Result.Success -> {
is GravatarResult.Success -> {
endProgress()
AnalyticsTracker.track(Stat.ME_GRAVATAR_UPLOADED)
mPhotoUrl = WPAvatarUtils.rewriteAvatarUrl(
Expand All @@ -742,7 +744,7 @@ class SignupEpilogueFragment : LoginBaseFormFragment<SignupEpilogueListener?>(),
mIsAvatarAdded = true
}

is Result.Failure -> {
is GravatarResult.Failure -> {
endProgress()
showErrorDialogWithCloseButton(getString(R.string.signup_epilogue_error_avatar))
val properties: MutableMap<String, Any?> = HashMap()
Expand Down Expand Up @@ -834,16 +836,16 @@ class SignupEpilogueFragment : LoginBaseFormFragment<SignupEpilogueListener?>(),
val uri = MediaUtils.downloadExternalMedia(context, Uri.parse(mUrl))
val file = File(URI(uri.toString()))
lifecycleScope.launch {
when (val result = mAvatarService.upload(file, Email(mEmail), mToken)) {
is Result.Success -> {
when (val result = mAvatarService.uploadCatching(file, mToken, Email(mEmail).hash(), true)) {
is GravatarResult.Success -> {
AppLog.i(
AppLog.T.NUX,
"Google avatar download and Gravatar upload succeeded."
)
AnalyticsTracker.track(Stat.ME_GRAVATAR_UPLOADED)
}

is Result.Failure -> {
is GravatarResult.Failure -> {
AppLog.i(
AppLog.T.NUX,
"Google avatar download and Gravatar upload failed."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ private void showCommentWhenNonNull(
avatarUrl = WPAvatarUtils.rewriteAvatarUrl(comment.getAuthorProfileImageUrl(), avatarSz);
} else if (comment.getAuthorEmail() != null) {
avatarUrl = new AvatarUrl(new Email(comment.getAuthorEmail()),
new AvatarQueryOptions(avatarSz, null, null, null)).url().toString();
new AvatarQueryOptions.Builder().setPreferredSize(avatarSz).build()).url(null).toString();
}
mImageManager.loadIntoCircle(binding.imageAvatar, ImageType.AVATAR_WITH_BACKGROUND, avatarUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class UnifiedCommentViewHolder(
} else if (!TextUtils.isEmpty(comment.authorEmail)) {
AvatarUrl(
Email(comment.authorEmail),
AvatarQueryOptions(preferredSize = resourceProvider.getDimensionPixelSize(R.dimen.avatar_sz_medium))
AvatarQueryOptions {
preferredSize = resourceProvider.getDimensionPixelSize(R.dimen.avatar_sz_medium)
}
).url().toString()
} else {
""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.gravatar.services.AvatarService
import com.gravatar.services.Result
import com.gravatar.services.GravatarResult
import com.gravatar.types.Email
import com.yalantis.ucrop.UCrop
import com.yalantis.ucrop.UCrop.Options
Expand Down Expand Up @@ -684,15 +684,22 @@ class MeFragment : Fragment(R.layout.me_fragment), OnScrollToTopListener {
}
binding?.showGravatarProgressBar(true)
lifecycleScope.launch {
val result =
avatarService.upload(file, Email(accountStore.account.email), accountStore.accessToken.orEmpty())
val result = avatarService.uploadCatching(
file = file,
oauthToken = accountStore.accessToken.orEmpty(),
hash = Email(accountStore.account.email).hash(),
selectAvatar = true,
)
when (result) {
is Result.Failure -> {
AnalyticsTracker.track(ME_GRAVATAR_UPLOAD_EXCEPTION, mapOf("error_type" to result.error.name))
is GravatarResult.Failure -> {
AnalyticsTracker.track(
ME_GRAVATAR_UPLOAD_EXCEPTION,
mapOf("error_type" to result.error.javaClass.name)
)
EventBus.getDefault().post(GravatarUploadFinished(filePath, false))
}

is Result.Success -> {
is GravatarResult.Success -> {
AnalyticsTracker.track(ME_GRAVATAR_UPLOADED)
EventBus.getDefault().post(GravatarUploadFinished(filePath, true))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public static String rewriteAvatarUrl(@NonNull final String imageUrl, int avatar
} else {
try {
return new AvatarUrl(new URL(imageUrl),
new AvatarQueryOptions(avatarSz, defaultImage, null, null)).url().toString();
new AvatarQueryOptions.Builder()
.setPreferredSize(avatarSz)
.setDefaultAvatarOption(defaultImage)
.build()
).url(null).toString();
} catch (MalformedURLException | IllegalArgumentException e) {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ google-play-app-update = '2.1.0'
google-play-review = '2.0.2'
google-play-services-auth = '20.4.1'
google-services = '4.4.2'
gravatar = '1.0.0'
gravatar = '2.0.0'
greenrobot-eventbus = '3.3.1'
gutenberg-kit = 'trunk-a58a46f3fbb892f311b562e3c122d7ef4ebbfe33'
gutenberg-mobile = 'v1.121.0'
Expand Down
2 changes: 1 addition & 1 deletion libs/login/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ext {
// libs
wordpressLintVersion = '2.1.0'
wordpressUtilsVersion = '3.5.0'
gravatarSdkVersion = '0.2.0'
gravatarSdkVersion = '2.0.0'

// main
androidxAppCompatVersion = '1.7.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ object AvatarHelper {
listener: AvatarRequestListener
) {
val avatarSize = fragment.resources.getDimensionPixelSize(R.dimen.avatar_sz_login)
val avatarUrl = email?.let { AvatarUrl(Email(email),
AvatarQueryOptions(preferredSize = avatarSize, defaultAvatarOption = Status404)).url().toString() }
val avatarUrl = email?.let {
AvatarUrl(
email = Email(email),
avatarQueryOptions = AvatarQueryOptions {
preferredSize = avatarSize
defaultAvatarOption = Status404
}
).url().toString()
}
loadAvatarFromUrl(fragment, avatarUrl, avatarView, listener)
}

Expand Down

0 comments on commit 926eb61

Please sign in to comment.