Skip to content

Commit

Permalink
Load user avatar and display name into account menu item
Browse files Browse the repository at this point in the history
Signed-off-by: Christine Coenen <[email protected]>
  • Loading branch information
cemrich committed Oct 31, 2023
1 parent cc1c055 commit df94416
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
Expand All @@ -65,6 +66,8 @@

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.api.NextcloudAPI;
Expand Down Expand Up @@ -154,6 +157,8 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements

//private ServiceConnection mConnection = null;

private OcsUser currentUser = null;

private ActionBarDrawerToggle drawerToggle;
private SearchView mSearchView;
private String mSearchString;
Expand Down Expand Up @@ -642,25 +647,9 @@ public void onTopItemLongClicked(long idFeed, boolean isFolder) {

@Override
public void onUserInfoUpdated(OcsUser userInfo) {
final Drawable placeHolder = getDrawable(R.drawable.ic_baseline_account_circle_24);

if (userInfo.getId() != null) {
String mOc_root_path = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null);
String avatarUrl = mOc_root_path + "/index.php/avatar/" + Uri.encode(userInfo.getId()) + "/64";

// TODO: invalidate options menu and hack avatar and user name into the menu
/*Glide.with(this)
.load(avatarUrl)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.placeholder(placeHolder)
.error(placeHolder)
.circleCrop()
.into(binding.toolbarLayout.avatar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
binding.toolbarLayout.avatar.setTooltipText(userInfo.getDisplayName());
}*/
}
currentUser = userInfo;

invalidateOptionsMenu();
}

@Override
Expand Down Expand Up @@ -850,6 +839,14 @@ public MenuItem getMenuItemDownloadMoreItems() {
return menuItemDownloadMoreItems;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem accountItem = menu.findItem(R.id.menu_account);
prepareAccountMenuItem(accountItem);

return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down Expand Up @@ -1254,6 +1251,38 @@ private void openRssItemInExternalBrowser(Uri currentUrl) {
startActivity(browserIntent);
}

private void prepareAccountMenuItem(MenuItem accountMenuItem) {
if (currentUser == null || currentUser.getId() == null) {
// the default menu item is fine if no user info is present
return;
}

accountMenuItem.setTitle(currentUser.getDisplayName());

String ownCloudRootPath = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null);
String avatarUrl = currentUser.getAvatarUrl(ownCloudRootPath);

Glide.with(this)
.asDrawable()
.load(avatarUrl)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.placeholder(R.drawable.ic_baseline_account_circle_24)
.error(R.drawable.ic_baseline_account_circle_24)
.circleCrop()
// TODO: Which are the correct dimensions in pixels?
.into(new CustomTarget<Drawable>(300, 300) {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
accountMenuItem.setIcon(resource);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
accountMenuItem.setIcon(R.drawable.ic_baseline_account_circle_24);
}
});
}

// private void openRssItemInInternalBrowser(Uri currentUrl) {
// getNewsReaderDetailFragment().binding.webview.loadUrl(currentUrl.toString());
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.luhmer.owncloudnewsreader.model;

import android.net.Uri;

import androidx.annotation.Nullable;

import java.io.Serializable;

/**
Expand Down Expand Up @@ -35,6 +39,14 @@ public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public @Nullable String getAvatarUrl(@Nullable String ownCloudRootPath) {
if (id == null || ownCloudRootPath == null) {
return null;
}

return ownCloudRootPath + "/index.php/avatar/" + Uri.encode(id) + "/64";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -52,4 +64,4 @@ public int hashCode() {
result = 31 * result + (displayName != null ? displayName.hashCode() : 0);
return result;
}
}
}

0 comments on commit df94416

Please sign in to comment.