Skip to content

Commit

Permalink
Merge pull request #1311 from cemrich/bugfix/flickering-toolbar-1291
Browse files Browse the repository at this point in the history
Fix flickering toolbar
  • Loading branch information
David-Development authored Nov 1, 2023
2 parents 77c202a + 5e5243e commit c230bad
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 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,9 @@

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
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 +158,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 @@ -256,9 +262,6 @@ protected void onCreate(Bundle savedInstanceState) {

checkNotificationPermissions();

binding.toolbarLayout.avatar.setVisibility(View.VISIBLE);
binding.toolbarLayout.avatar.setOnClickListener((v) -> startActivityForResult(new Intent(this, LoginDialogActivity.class), RESULT_LOGIN));

// Init config --> if nothing is configured start the login dialog.
if (!isUserLoggedIn()) {
startLoginActivity();
Expand Down Expand Up @@ -645,24 +648,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";

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 @@ -852,6 +840,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 @@ -928,6 +924,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
} else if (itemId == R.id.menu_update) {
startSync();
}
else if (itemId == R.id.menu_account) {
startLoginActivity();
}
else if (itemId == R.id.menu_toggleShowOnlyUnread) {
boolean newValue = !mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false);
mPrefs.edit().putBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, newValue).commit();
Expand Down Expand Up @@ -1253,6 +1252,37 @@ 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()
.into(new CustomTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
@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;
}
}
}
35 changes: 6 additions & 29 deletions News-Android-App/src/main/res/layout/toolbar_layout.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.Material3.Toolbar.Surface"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1" />

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">

<ImageButton
android:id="@+id/avatar"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/spacer_1x"
android:background="@null"
android:contentDescription="@string/switch_account"
android:padding="@dimen/spacer_1x"
android:visibility="gone"
app:srcCompat="@drawable/ic_baseline_account_circle_24"
tools:visibility="visible" />
</FrameLayout>
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
</com.google.android.material.appbar.AppBarLayout>
7 changes: 7 additions & 0 deletions News-Android-App/src/main/res/menu/news_reader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always|collapseActionView" />

<item
android:id="@+id/menu_account"
android:enabled="true"
android:icon="@drawable/ic_baseline_account_circle_24"
android:title="@string/switch_account"
app:showAsAction="ifRoom" />

<item
android:id="@+id/menu_download_web_archive"
android:enabled="true"
Expand Down

0 comments on commit c230bad

Please sign in to comment.