feat(tabs/notifications): add unread badge

This commit is contained in:
FineFindus 2023-03-01 21:44:11 +01:00
parent e7cd1cfda2
commit 4ecd525f13
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
3 changed files with 55 additions and 0 deletions

View File

@ -47,6 +47,7 @@ public class GlobalUserPreferences{
public static boolean collapseLongPosts; public static boolean collapseLongPosts;
public static boolean spectatorMode; public static boolean spectatorMode;
public static boolean autoHideFab; public static boolean autoHideFab;
public static long lastNotificationOpenedTime;
public static String publishButtonText; public static String publishButtonText;
public static ThemePreference theme; public static ThemePreference theme;
public static ColorPreference color; public static ColorPreference color;
@ -101,6 +102,7 @@ public class GlobalUserPreferences{
collapseLongPosts=prefs.getBoolean("collapseLongPosts", true); collapseLongPosts=prefs.getBoolean("collapseLongPosts", true);
spectatorMode=prefs.getBoolean("spectatorMode", false); spectatorMode=prefs.getBoolean("spectatorMode", false);
autoHideFab=prefs.getBoolean("autoHideFab", true); autoHideFab=prefs.getBoolean("autoHideFab", true);
lastNotificationOpenedTime=prefs.getLong("lastNotificationOpenedTime", 0);
publishButtonText=prefs.getString("publishButtonText", ""); publishButtonText=prefs.getString("publishButtonText", "");
theme=ThemePreference.values()[prefs.getInt("theme", 0)]; theme=ThemePreference.values()[prefs.getInt("theme", 0)];
recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>()); recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>());
@ -150,6 +152,7 @@ public class GlobalUserPreferences{
.putBoolean("collapseLongPosts", collapseLongPosts) .putBoolean("collapseLongPosts", collapseLongPosts)
.putBoolean("spectatorMode", spectatorMode) .putBoolean("spectatorMode", spectatorMode)
.putBoolean("autoHideFab", autoHideFab) .putBoolean("autoHideFab", autoHideFab)
.putLong("lastNotificationOpenedTime", lastNotificationOpenedTime)
.putString("publishButtonText", publishButtonText) .putString("publishButtonText", publishButtonText)
.putBoolean("bottomEncoding", bottomEncoding) .putBoolean("bottomEncoding", bottomEncoding)
.putInt("theme", theme.ordinal()) .putInt("theme", theme.ordinal())

View File

@ -16,21 +16,32 @@ import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.R; import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.markers.SaveMarkers;
import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSession;
import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.fragments.discover.DiscoverFragment; import org.joinmastodon.android.fragments.discover.DiscoverFragment;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Notification;
import org.joinmastodon.android.model.PaginatedResponse;
import org.joinmastodon.android.ui.AccountSwitcherSheet; import org.joinmastodon.android.ui.AccountSwitcherSheet;
import org.joinmastodon.android.ui.utils.UiUtils; import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.views.TabBar; import org.joinmastodon.android.ui.views.TabBar;
import org.parceler.Parcels; import org.parceler.Parcels;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import androidx.annotation.IdRes; import androidx.annotation.IdRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import me.grishka.appkit.FragmentStackActivity; import me.grishka.appkit.FragmentStackActivity;
import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
import me.grishka.appkit.api.SimpleCallback;
import me.grishka.appkit.fragments.AppKitFragment; import me.grishka.appkit.fragments.AppKitFragment;
import me.grishka.appkit.fragments.LoaderFragment; import me.grishka.appkit.fragments.LoaderFragment;
import me.grishka.appkit.fragments.OnBackPressedListener; import me.grishka.appkit.fragments.OnBackPressedListener;
@ -52,6 +63,8 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
private TabBar tabBar; private TabBar tabBar;
private View tabBarWrap; private View tabBarWrap;
private ImageView tabBarAvatar; private ImageView tabBarAvatar;
private ImageView notificationTabIcon;
private boolean notificationBadged = false;
@IdRes @IdRes
private int currentTab=R.id.tab_home; private int currentTab=R.id.tab_home;
@ -117,6 +130,23 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
Account self=AccountSessionManager.getInstance().getAccount(accountID).self; Account self=AccountSessionManager.getInstance().getAccount(accountID).self;
ViewImageLoader.load(tabBarAvatar, null, new UrlImageLoaderRequest(self.avatar, V.dp(28), V.dp(28))); ViewImageLoader.load(tabBarAvatar, null, new UrlImageLoaderRequest(self.avatar, V.dp(28), V.dp(28)));
notificationTabIcon=content.findViewById(R.id.tab_notifications);
AccountSessionManager.getInstance()
.getAccount(accountID).getCacheController()
.getNotifications(null, 1, false, false, true, new Callback<>() {
@Override
public void onSuccess(PaginatedResponse<List<Notification>> result) {
notificationBadged = result.items.get(0).createdAt.isAfter(Instant.ofEpochMilli(GlobalUserPreferences.lastNotificationOpenedTime));
setNotificationBadge();
}
@Override
public void onError(ErrorResponse error) {
error.showToast(getContext());
}
});
if(savedInstanceState==null){ if(savedInstanceState==null){
getChildFragmentManager().beginTransaction() getChildFragmentManager().beginTransaction()
.add(R.id.fragment_wrap, homeTabFragment) .add(R.id.fragment_wrap, homeTabFragment)
@ -255,6 +285,14 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
scrollable.scrollToTop(); scrollable.scrollToTop();
return; return;
} }
if(tab == R.id.tab_notifications){
notificationBadged=false;
GlobalUserPreferences.lastNotificationOpenedTime = System.currentTimeMillis();
GlobalUserPreferences.save();
setNotificationBadge();
}
getChildFragmentManager().beginTransaction().hide(fragmentForTab(currentTab)).show(newFragment).commit(); getChildFragmentManager().beginTransaction().hide(fragmentForTab(currentTab)).show(newFragment).commit();
maybeTriggerLoading(newFragment); maybeTriggerLoading(newFragment);
currentTab=tab; currentTab=tab;
@ -327,4 +365,8 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
// getChildFragmentManager().putFragment(outState, "notificationsFragment", notificationsFragment); // getChildFragmentManager().putFragment(outState, "notificationsFragment", notificationsFragment);
// getChildFragmentManager().putFragment(outState, "profileFragment", profileFragment); // getChildFragmentManager().putFragment(outState, "profileFragment", profileFragment);
} }
private void setNotificationBadge() {
notificationTabIcon.setImageDrawable(getContext().getDrawable(notificationBadged ? R.drawable.ic_notifications_tab_badged : R.drawable.ic_fluent_alert_28_selector));
}
} }

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_fluent_alert_28_selector" android:left="2dp" android:right="2dp" android:top="2dp" android:bottom="2dp"/>
<item android:width="14dp" android:height="14dp" android:gravity="top|right">
<shape android:shape="oval">
<stroke android:color="?android:colorPrimary" android:width="2dp"/>
<solid android:color="?android:colorAccent"/>
</shape>
</item>
</layer-list>