Skip to content

Commit 02f497e

Browse files
folkematQuantumBadger
authored andcommitted
Ability to follow and unfollow a user
1 parent eaa1487 commit 02f497e

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

src/main/java/org/quantumbadger/redreader/fragments/UserProfileDialog.kt

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import android.view.View
2525
import android.widget.FrameLayout
2626
import android.widget.ImageView
2727
import android.widget.ScrollView
28+
import android.widget.Toast
2829
import androidx.appcompat.app.AppCompatActivity
2930
import androidx.appcompat.widget.AppCompatImageView
3031
import com.google.android.material.card.MaterialCardView
@@ -54,8 +55,11 @@ import org.quantumbadger.redreader.common.time.TimestampUTC.Companion.now
5455
import org.quantumbadger.redreader.reddit.APIResponseHandler.ActionResponseHandler
5556
import org.quantumbadger.redreader.reddit.APIResponseHandler.UserResponseHandler
5657
import org.quantumbadger.redreader.reddit.RedditAPI
57-
import org.quantumbadger.redreader.reddit.api.RedditOAuth.completeLogin
58+
import org.quantumbadger.redreader.reddit.api.RedditSubredditSubscriptionManager
59+
import org.quantumbadger.redreader.reddit.api.SubredditSubscriptionState
60+
import org.quantumbadger.redreader.reddit.things.InvalidSubredditNameException
5861
import org.quantumbadger.redreader.reddit.things.RedditUser
62+
import org.quantumbadger.redreader.reddit.things.SubredditCanonicalId
5963
import org.quantumbadger.redreader.reddit.url.UserPostListingURL
6064
import org.quantumbadger.redreader.views.LoadingSpinnerView
6165
import org.quantumbadger.redreader.views.liststatus.ErrorView
@@ -94,6 +98,9 @@ object UserProfileDialog {
9498
val chipMoreInfo = dialog.findViewById<Chip>(R.id.user_profile_chip_more_info)!!
9599
val chipBlock = dialog.findViewById<Chip>(R.id.user_profile_chip_block)!!
96100
val chipUnblock = dialog.findViewById<Chip>(R.id.user_profile_chip_unblock)!!
101+
val chipFollow = dialog.findViewById<Chip>(R.id.user_profile_chip_follow)!!
102+
val chipFollowed = dialog.findViewById<Chip>(R.id.user_profile_chip_followed)!!
103+
val chipUnfollow = dialog.findViewById<Chip>(R.id.user_profile_chip_unfollow)!!
97104

98105
val cm = CacheManager.getInstance(activity)
99106
val accountManager = RedditAccountManager.getInstance(activity)
@@ -170,6 +177,19 @@ object UserProfileDialog {
170177
chipGold.visibility = View.GONE
171178
}
172179

180+
val usernameToSubreddit = "u_"+username
181+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
182+
if ((getSubMan(activity).getSubscriptionState(userSubredditCanonicalId)
183+
== SubredditSubscriptionState.NOT_SUBSCRIBED)
184+
) {
185+
chipFollowed.visibility = View.GONE
186+
chipFollow.visibility = View.VISIBLE
187+
chipUnfollow.visibility = View.GONE
188+
}else{
189+
chipFollow.visibility = View.GONE
190+
chipUnfollow.visibility = View.VISIBLE
191+
}
192+
173193
if (PrefsUtility.appearance_user_show_avatars()) {
174194
val iconUrl = user.iconUrl
175195
if (iconUrl?.value?.isNotEmpty() == true) {
@@ -258,6 +278,12 @@ object UserProfileDialog {
258278
chipUnblock.isEnabled = false // grey out
259279
unblockUser(activity, username, chipBlock, chipBlocked, chipUnblock)
260280
}
281+
chipFollow.setOnClickListener {
282+
subscribeToUser(activity, username)
283+
}
284+
chipUnfollow.setOnClickListener {
285+
unsubscribeToUser(activity, username)
286+
}
261287
}
262288
}
263289

@@ -282,6 +308,70 @@ object UserProfileDialog {
282308
)
283309
}
284310

311+
private fun subscribeToUser(activity: AppCompatActivity, username: String) {
312+
try {
313+
//Every user has a user-subreddit that you can follow
314+
val usernameToSubreddit = "u_"+username //subreddit of spez is u_spez
315+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
316+
317+
val subMan = getSubMan(activity)
318+
if ((subMan.getSubscriptionState(userSubredditCanonicalId)
319+
== SubredditSubscriptionState.NOT_SUBSCRIBED)
320+
) {
321+
subMan.subscribe(userSubredditCanonicalId, activity)
322+
Toast.makeText(
323+
activity,
324+
R.string.userprofile_toast_follow_loading,
325+
Toast.LENGTH_SHORT
326+
).show()
327+
} else {
328+
Toast.makeText(
329+
activity,
330+
R.string.userprofile_toast_followed,
331+
Toast.LENGTH_SHORT
332+
).show()
333+
}
334+
} catch (e: InvalidSubredditNameException) {
335+
throw RuntimeException(e)
336+
}
337+
}
338+
339+
private fun unsubscribeToUser(activity: AppCompatActivity, username: String) {
340+
try {
341+
//Every user has a user-subreddit that you can follow
342+
val usernameToSubreddit = "u_"+username //subreddit of spez is u_spez
343+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
344+
345+
val subMan = getSubMan(activity)
346+
if ((subMan.getSubscriptionState(userSubredditCanonicalId)
347+
== SubredditSubscriptionState.SUBSCRIBED)
348+
) {
349+
subMan.unsubscribe(userSubredditCanonicalId, activity)
350+
Toast.makeText(
351+
activity,
352+
R.string.userprofile_toast_unfollow_loading,
353+
Toast.LENGTH_SHORT
354+
).show()
355+
} else {
356+
Toast.makeText(
357+
activity,
358+
R.string.userprofile_toast_not_following,
359+
Toast.LENGTH_SHORT
360+
).show()
361+
}
362+
} catch (e: InvalidSubredditNameException) {
363+
throw RuntimeException(e)
364+
}
365+
}
366+
367+
private fun getSubMan(activity: AppCompatActivity): RedditSubredditSubscriptionManager {
368+
val subMan = RedditSubredditSubscriptionManager.getSingleton(
369+
activity,
370+
RedditAccountManager.getInstance(activity).defaultAccount
371+
)
372+
return subMan
373+
}
374+
285375
private fun unblockUser(activity: AppCompatActivity, username: String, chipBlock: Chip, chipBlocked: Chip, chipUnblock: Chip) {
286376
val cm = CacheManager.getInstance(activity)
287377
val currentUser = RedditAccountManager.getInstance(activity).defaultAccount

src/main/java/org/quantumbadger/redreader/fragments/UserPropertiesDialog.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ protected void prepare(
107107
false));
108108
}
109109

110+
if (user.is_followed != null) {
111+
items.addView(propView(
112+
context,
113+
R.string.userprofile_tag_followed,
114+
user.is_followed ? R.string.general_true : R.string.general_false,
115+
false));
116+
}
117+
110118
if (user.is_employee != null) {
111119
items.addView(propView(
112120
context,
@@ -131,6 +139,14 @@ protected void prepare(
131139
false));
132140
}
133141

142+
if (user.is_followed != null) {
143+
items.addView(propView(
144+
context,
145+
R.string.userprofile_tag_followed,
146+
user.is_followed ? R.string.general_true : R.string.general_false,
147+
false));
148+
}
149+
134150
if (user.icon_img != null) {
135151
items.addView(propView(
136152
context,

src/main/java/org/quantumbadger/redreader/reddit/things/RedditUser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class RedditUser implements Parcelable, JsonObject.JsonDeserializable {
4444
@Nullable public Boolean is_suspended;
4545
@Nullable public Boolean over_18;
4646
@Nullable public Boolean is_blocked;
47+
@Nullable public Boolean is_followed;
4748

4849
@Nullable public String id;
4950
@NonNull public String name;
@@ -87,6 +88,7 @@ private RedditUser(final Parcel in) {
8788
is_mod = in.readInt() == 1;
8889
over_18 = in.readInt() == 1;
8990
is_blocked = in.readInt() == 1;
91+
is_followed = in.readInt() == 1;
9092

9193
id = in.readString();
9294
name = in.readString();
@@ -121,6 +123,7 @@ public void writeToParcel(final Parcel parcel, final int flags) {
121123
parcel.writeInt(is_mod ? 1 : 0);
122124
parcel.writeInt(over_18 ? 1 : 0);
123125
parcel.writeInt(is_blocked ? 1 : 0);
126+
parcel.writeInt(is_followed ? 1 : 0);
124127

125128
parcel.writeString(id);
126129
parcel.writeString(name);

src/main/res/layout/user_profile_dialog.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@
158158
android:checkable="false"
159159
app:chipMinTouchTargetSize="0dp"/>
160160

161+
<com.google.android.material.chip.Chip
162+
android:id="@+id/user_profile_chip_followed"
163+
android:layout_width="wrap_content"
164+
android:layout_height="wrap_content"
165+
android:text="@string/userprofile_tag_followed"
166+
style="@style/Widget.Material3.Chip.Assist"
167+
android:clickable="false"
168+
android:checkable="false"
169+
app:chipMinTouchTargetSize="0dp"/>
170+
161171
</com.google.android.material.chip.ChipGroup>
162172

163173
<com.google.android.material.card.MaterialCardView
@@ -349,6 +359,26 @@
349359
style="@style/Widget.Material3.Chip.Assist.Elevated"
350360
android:checkable="false"/>
351361

362+
<com.google.android.material.chip.Chip
363+
android:id="@+id/user_profile_chip_follow"
364+
android:layout_width="wrap_content"
365+
android:layout_height="wrap_content"
366+
app:chipIcon="?rrIconPerson"
367+
app:chipIconSize="16sp"
368+
android:text="@string/userprofile_button_follow"
369+
style="@style/Widget.Material3.Chip.Assist.Elevated"
370+
android:checkable="false"/>
371+
372+
<com.google.android.material.chip.Chip
373+
android:id="@+id/user_profile_chip_unfollow"
374+
android:layout_width="wrap_content"
375+
android:layout_height="wrap_content"
376+
app:chipIcon="?rrIconCross"
377+
app:chipIconSize="16sp"
378+
android:text="@string/userprofile_button_unfollow"
379+
style="@style/Widget.Material3.Chip.Assist.Elevated"
380+
android:checkable="false"/>
381+
352382
</com.google.android.material.chip.ChipGroup>
353383

354384
</androidx.appcompat.widget.LinearLayoutCompat>

src/main/res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,4 +1911,12 @@
19111911
<!-- 2025-04-20 -->
19121912
<string name="action_mark_read">Mark as Read</string>
19131913
<string name="action_mark_unread">Mark as Unread</string>
1914+
1915+
<string name="userprofile_button_follow" translatable="true">Follow</string>
1916+
<string name="userprofile_toast_follow_loading" translatable="true">Following…</string>
1917+
<string name="userprofile_tag_followed" translatable="true">Followed</string>
1918+
<string name="userprofile_toast_followed" translatable="true">You are already following this user!</string>
1919+
<string name="userprofile_button_unfollow" translatable="true">Unfollow</string>
1920+
<string name="userprofile_toast_unfollow_loading" translatable="true">Unfollowing…</string>
1921+
<string name="userprofile_toast_not_following" translatable="true">You are not following this user!</string>
19141922
</resources>

0 commit comments

Comments
 (0)