From c7233e05c4ea7be96b2bce0150d0d218d9926b0b Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Tue, 22 Aug 2023 21:49:12 +0200 Subject: [PATCH] Instance Info page --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 10 +- .../toldi/infinityforlemmy/AppComponent.java | 5 + .../activities/InstanceInfoActivity.java | 225 ++++++++++++++++ .../activities/MainActivity.java | 8 +- .../adapters/AdminRecyclerViewAdapter.java | 22 ++ .../customtheme/CustomThemeWrapper.java | 5 + .../toldi/infinityforlemmy/site/SiteInfo.java | 38 ++- .../infinityforlemmy/site/SiteStatistics.java | 55 ++++ app/src/main/res/drawable/ic_groups_24.xml | 10 + .../res/layout/activity_instance_info.xml | 245 ++++++++++++++++++ app/src/main/res/values/strings.xml | 3 + 12 files changed, 619 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/activities/InstanceInfoActivity.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/adapters/AdminRecyclerViewAdapter.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/site/SiteStatistics.java create mode 100644 app/src/main/res/drawable/ic_groups_24.xml create mode 100644 app/src/main/res/layout/activity_instance_info.xml diff --git a/app/build.gradle b/app/build.gradle index 349b7a90..240f6ddb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -108,6 +108,7 @@ dependencies { implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.0" def lifecycleVersion = "2.5.1" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion" @@ -153,6 +154,7 @@ dependencies { implementation "com.google.dagger:dagger:$daggerVersion" annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" + // Binding // NOTE: Deprecated in favor of viewbinding def butterknifeVersion = "10.2.3" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index acacff02..1dd230b6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,8 +22,7 @@ android:maxSdkVersion="28" /> - - + + - admins = siteInfo.getAdmins(); + if (admins != null && !admins.isEmpty()) { + mAdminsCardView.setVisibility(View.VISIBLE); + mAdminAdapter.setUsers(admins); + } + SiteStatistics siteStatistics = siteInfo.getSiteStatistics(); + if (siteStatistics != null) { + mStatisticsCardView.setVisibility(View.VISIBLE); + mUsersTextView.setText(getString(R.string.user_number_detail, siteStatistics.getUsers())); + mCommunitiesTextView.setText(getString(R.string.community_number_detail, siteStatistics.getCommunities())); + mPostsTextView.setText(getString(R.string.post_count_detail, siteStatistics.getPosts())); + mCommentsTextView.setText(getString(R.string.comment_count_detail, siteStatistics.getComments())); + mActiveUsersTextView.setText(getString(R.string.active_users_number_detail, siteStatistics.getUsers_active())); + } + } + + @Override + public void onFetchSiteInfoFailed() { + + } + }); + } + + protected void setUpBindings() { + coordinatorLayout = mInstanceInfoActivityViewBinding.coordinatorLayoutInstanceInfoActivity; + toolbar = mInstanceInfoActivityViewBinding.toolbarInstanceInfoActivity; + appBarLayout = mInstanceInfoActivityViewBinding.appbarLayoutInstanceInfoActivity; + mStatisticsCardView = mInstanceInfoActivityViewBinding.statisticsCardInstanceInfoActivity; + mDescriptionCardView = mInstanceInfoActivityViewBinding.descriptionCardInstanceInfoActivity; + mContentMarkdownView = mInstanceInfoActivityViewBinding.markdownRecyclerViewInstanceInfoActivity; + mAdminsCardView = mInstanceInfoActivityViewBinding.moderatorsCardInstanceInfoActivity; + mLoadingConstraintLayout = mInstanceInfoActivityViewBinding.loadingLayoutInstanceInfoActivity; + mAdminsRecyclerView = mInstanceInfoActivityViewBinding.recyclerViewAdminsInstanceInfoActivity; + mUsersTextView = mInstanceInfoActivityViewBinding.registeredUserCountTextViewInstanceInfoActivity; + mCommunitiesTextView = mInstanceInfoActivityViewBinding.communityCountInstanceInfoActivity; + mPostsTextView = mInstanceInfoActivityViewBinding.postCountTextViewInstanceInfoActivity; + mCommentsTextView = mInstanceInfoActivityViewBinding.commentCountTextViewInstanceInfoActivity; + mActiveUsersTextView = mInstanceInfoActivityViewBinding.activeUserCountTextViewInstanceInfoActivity; + mUsersImageView = mInstanceInfoActivityViewBinding.registeredUserCountImageViewInstanceInfoActivity; + mCommunitiesImageView = mInstanceInfoActivityViewBinding.communitiesIconImageViewInstanceInfoActivity; + mPostsImageView = mInstanceInfoActivityViewBinding.postCountImageViewInstanceInfoActivity; + mCommentsImageView = mInstanceInfoActivityViewBinding.commentCountImageViewInstanceInfoActivity; + mActiveUsersImageView = mInstanceInfoActivityViewBinding.activeUserCountImageViewInstanceInfoActivity; + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, null, toolbar); + mUsersImageView.setColorFilter(mCustomThemeWrapper.getPrimaryTextColor(), PorterDuff.Mode.SRC_IN); + mCommunitiesImageView.setColorFilter(mCustomThemeWrapper.getPrimaryTextColor(), PorterDuff.Mode.SRC_IN); + mPostsImageView.setColorFilter(mCustomThemeWrapper.getPrimaryTextColor(), PorterDuff.Mode.SRC_IN); + mCommentsImageView.setColorFilter(mCustomThemeWrapper.getPrimaryTextColor(), PorterDuff.Mode.SRC_IN); + mActiveUsersImageView.setColorFilter(mCustomThemeWrapper.getPrimaryTextColor(), PorterDuff.Mode.SRC_IN); + mUsersTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + mCommunitiesTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + mPostsTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + mCommentsTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + mActiveUsersTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java index e83cd762..25bb053a 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java @@ -882,9 +882,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb }); } else if (stringId == R.string.anonymous_account_instance) { changeAnonymousAccountInstance(); - } else if (stringId == R.string.blocks) { - intent = new Intent(MainActivity.this, BlockedThingListingActivity.class); - } + } else if (stringId == R.string.blocks) { + intent = new Intent(MainActivity.this, BlockedThingListingActivity.class); + } else if (stringId == R.string.instance_info) { + intent = new Intent(MainActivity.this, InstanceInfoActivity.class); + } if (intent != null) { startActivity(intent); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/AdminRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/AdminRecyclerViewAdapter.java new file mode 100644 index 00000000..237b2fbb --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/AdminRecyclerViewAdapter.java @@ -0,0 +1,22 @@ +package eu.toldi.infinityforlemmy.adapters; + +import eu.toldi.infinityforlemmy.activities.BaseActivity; +import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; +import eu.toldi.infinityforlemmy.user.BasicUserRecyclerViewAdapter; + +public class AdminRecyclerViewAdapter extends BasicUserRecyclerViewAdapter { + + CustomThemeWrapper mCustomThemeWrapper; + + public AdminRecyclerViewAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper) { + super(activity, customThemeWrapper); + mCustomThemeWrapper = customThemeWrapper; + } + + @Override + protected int getUserNameTextColor() { + return mCustomThemeWrapper.getAdmin(); + } + + +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/customtheme/CustomThemeWrapper.java b/app/src/main/java/eu/toldi/infinityforlemmy/customtheme/CustomThemeWrapper.java index 9ad67873..bf4318f9 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/customtheme/CustomThemeWrapper.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/customtheme/CustomThemeWrapper.java @@ -337,6 +337,11 @@ public class CustomThemeWrapper { getDefaultColor("#577F63", "#D67AD2", "#25BE6A")); } + public int getAdmin() { + return getThemeSharedPreferences().getInt(CustomThemeSharedPreferencesUtils.MODERATOR, + getDefaultColor("#a5222f", "#c94f6d", "#EE5396")); + } + public int getCurrentUser() { return getThemeSharedPreferences().getInt(CustomThemeSharedPreferencesUtils.CURRENT_USER, getDefaultColor("#488D93", "#7AD5D6", "#2DC7C4")); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java index c87fffd0..d7ba99b7 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java @@ -1,8 +1,15 @@ package eu.toldi.infinityforlemmy.site; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.util.ArrayList; +import java.util.List; + +import eu.toldi.infinityforlemmy.user.BasicUserInfo; +import eu.toldi.infinityforlemmy.utils.LemmyUtils; + public class SiteInfo { private int id; @@ -13,7 +20,11 @@ public class SiteInfo { private boolean enable_nsfw; private boolean community_creation_admin_only; - public SiteInfo(int id, String name, String sidebar, String description, boolean enable_downvotes, boolean enable_nsfw, boolean community_creation_admin_only) { + private List admins; + + SiteStatistics siteStatistics; + + public SiteInfo(int id, String name, String sidebar, String description, boolean enable_downvotes, boolean enable_nsfw, boolean community_creation_admin_only, List admins, SiteStatistics siteStatistics) { this.id = id; this.name = name; this.sidebar = sidebar; @@ -21,6 +32,8 @@ public class SiteInfo { this.enable_downvotes = enable_downvotes; this.enable_nsfw = enable_nsfw; this.community_creation_admin_only = community_creation_admin_only; + this.admins = admins; + this.siteStatistics = siteStatistics; } public int getId() { @@ -51,6 +64,14 @@ public class SiteInfo { return community_creation_admin_only; } + public List getAdmins() { + return admins; + } + + public SiteStatistics getSiteStatistics() { + return siteStatistics; + } + public static SiteInfo parseSiteInfo(String siteInfoJson) { try { JSONObject siteInfo = new JSONObject(siteInfoJson); @@ -72,9 +93,20 @@ public class SiteInfo { boolean enable_nsfw = localSite.getBoolean("enable_nsfw"); boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only"); - SiteInfo si = new SiteInfo(id, name, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only); + JSONObject counts = siteView.getJSONObject("counts"); + List admins = new ArrayList<>(); + if (siteInfo.has("admins")) { + JSONArray adminsJson = siteInfo.getJSONArray("admins"); + for (int i = 0; i < adminsJson.length(); i++) { + JSONObject adminJson = adminsJson.getJSONObject(i).getJSONObject("person"); + admins.add(new BasicUserInfo(adminJson.getInt("id"), adminJson.getString("name"), + LemmyUtils.actorID2FullName(adminJson.getString("actor_id")), adminJson.optString("avatar ", ""), + adminJson.optString("display_name", adminJson.getString("name"))) + ); + } + } - return si; + return new SiteInfo(id, name, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts)); } catch (JSONException e) { e.printStackTrace(); return null; diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteStatistics.java b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteStatistics.java new file mode 100644 index 00000000..0e581a4c --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteStatistics.java @@ -0,0 +1,55 @@ +package eu.toldi.infinityforlemmy.site; + +import org.json.JSONException; +import org.json.JSONObject; + +public class SiteStatistics { + + private final int users; + private final int posts; + private final int comments; + private final int communities; + private final int users_active; + + public SiteStatistics(int users, int posts, int comments, int communities, int users_active) { + this.users = users; + this.posts = posts; + this.comments = comments; + this.communities = communities; + this.users_active = users_active; + } + + public int getUsers() { + return users; + } + + public int getPosts() { + return posts; + } + + public int getComments() { + return comments; + } + + public int getCommunities() { + return communities; + } + + public int getUsers_active() { + return users_active; + } + + public static SiteStatistics parseSiteStatistics(JSONObject countsJson) { + try { + int users = countsJson.getInt("users"); + int posts = countsJson.getInt("posts"); + int comments = countsJson.getInt("comments"); + int communities = countsJson.getInt("communities"); + int users_active = countsJson.getInt("users_active_month"); + return new SiteStatistics(users, posts, comments, communities, users_active); + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/app/src/main/res/drawable/ic_groups_24.xml b/app/src/main/res/drawable/ic_groups_24.xml new file mode 100644 index 00000000..d173edda --- /dev/null +++ b/app/src/main/res/drawable/ic_groups_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_instance_info.xml b/app/src/main/res/layout/activity_instance_info.xml new file mode 100644 index 00000000..5e4f362f --- /dev/null +++ b/app/src/main/res/layout/activity_instance_info.xml @@ -0,0 +1,245 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a3affca6..790e93d3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -150,6 +150,8 @@ %1$,d Active Users %1$,d Posts %1$,d Comments + %1$,d Users + %1$,d Communities Online: %1$,d Cannot fetch community info Cannot fetch user info @@ -1379,4 +1381,5 @@ Show Statistics Show post and comment scores Moderators + Admins