Skip to content

Commit d26590d

Browse files
committed
Ability to follow and unfollow a user
1 parent 4b2c2ed commit d26590d

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.view.View
2424
import android.widget.FrameLayout
2525
import android.widget.ImageView
2626
import android.widget.ScrollView
27+
import android.widget.Toast
2728
import androidx.appcompat.app.AppCompatActivity
2829
import androidx.appcompat.widget.AppCompatImageView
2930
import com.google.android.material.card.MaterialCardView
@@ -51,7 +52,11 @@ import org.quantumbadger.redreader.common.time.TimestampUTC.Companion.fromUtcSec
5152
import org.quantumbadger.redreader.common.time.TimestampUTC.Companion.now
5253
import org.quantumbadger.redreader.reddit.APIResponseHandler.UserResponseHandler
5354
import org.quantumbadger.redreader.reddit.RedditAPI
55+
import org.quantumbadger.redreader.reddit.api.RedditSubredditSubscriptionManager
56+
import org.quantumbadger.redreader.reddit.api.SubredditSubscriptionState
57+
import org.quantumbadger.redreader.reddit.things.InvalidSubredditNameException
5458
import org.quantumbadger.redreader.reddit.things.RedditUser
59+
import org.quantumbadger.redreader.reddit.things.SubredditCanonicalId
5560
import org.quantumbadger.redreader.reddit.url.UserPostListingURL
5661
import org.quantumbadger.redreader.views.LoadingSpinnerView
5762
import org.quantumbadger.redreader.views.liststatus.ErrorView
@@ -87,6 +92,9 @@ object UserProfileDialog {
8792
val postsKarma = dialog.findViewById<MaterialTextView>(R.id.user_profile_posts_karma)!!
8893
val commentsKarma = dialog.findViewById<MaterialTextView>(R.id.user_profile_comments_karma)!!
8994
val chipMoreInfo = dialog.findViewById<Chip>(R.id.user_profile_chip_more_info)!!
95+
val chipFollow = dialog.findViewById<Chip>(R.id.user_profile_chip_follow)!!
96+
val chipFollowed = dialog.findViewById<Chip>(R.id.user_profile_chip_followed)!!
97+
val chipUnfollow = dialog.findViewById<Chip>(R.id.user_profile_chip_unfollow)!!
9098

9199
val cm = CacheManager.getInstance(activity)
92100
val accountManager = RedditAccountManager.getInstance(activity)
@@ -147,6 +155,19 @@ object UserProfileDialog {
147155
chipGold.visibility = View.GONE
148156
}
149157

158+
val usernameToSubreddit = "u_"+username
159+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
160+
if ((getSubMan(activity).getSubscriptionState(userSubredditCanonicalId)
161+
== SubredditSubscriptionState.NOT_SUBSCRIBED)
162+
) {
163+
chipFollowed.visibility = View.GONE
164+
chipFollow.visibility = View.VISIBLE
165+
chipUnfollow.visibility = View.GONE
166+
}else{
167+
chipFollow.visibility = View.GONE
168+
chipUnfollow.visibility = View.VISIBLE
169+
}
170+
150171
if (PrefsUtility.appearance_user_show_avatars()) {
151172
val iconUrl = user.iconUrl
152173
if (!iconUrl.isNullOrEmpty()) {
@@ -220,6 +241,12 @@ object UserProfileDialog {
220241
UserPropertiesDialog.newInstance(user)
221242
.show(activity.supportFragmentManager, null)
222243
}
244+
chipFollow.setOnClickListener {
245+
subscribeToUser(activity, username)
246+
}
247+
chipUnfollow.setOnClickListener {
248+
unsubscribeToUser(activity, username)
249+
}
223250
}
224251
}
225252

@@ -244,6 +271,70 @@ object UserProfileDialog {
244271
)
245272
}
246273

274+
private fun subscribeToUser(activity: AppCompatActivity, username: String) {
275+
try {
276+
//Every user has a user-subreddit that you can follow
277+
val usernameToSubreddit = "u_"+username //subreddit of spez is u_spez
278+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
279+
280+
val subMan = getSubMan(activity)
281+
if ((subMan.getSubscriptionState(userSubredditCanonicalId)
282+
== SubredditSubscriptionState.NOT_SUBSCRIBED)
283+
) {
284+
subMan.subscribe(userSubredditCanonicalId, activity)
285+
Toast.makeText(
286+
activity,
287+
R.string.userprofile_toast_follow_loading,
288+
Toast.LENGTH_SHORT
289+
).show()
290+
} else {
291+
Toast.makeText(
292+
activity,
293+
R.string.userprofile_toast_followed,
294+
Toast.LENGTH_SHORT
295+
).show()
296+
}
297+
} catch (e: InvalidSubredditNameException) {
298+
throw RuntimeException(e)
299+
}
300+
}
301+
302+
private fun unsubscribeToUser(activity: AppCompatActivity, username: String) {
303+
try {
304+
//Every user has a user-subreddit that you can follow
305+
val usernameToSubreddit = "u_"+username //subreddit of spez is u_spez
306+
val userSubredditCanonicalId = SubredditCanonicalId(usernameToSubreddit)
307+
308+
val subMan = getSubMan(activity)
309+
if ((subMan.getSubscriptionState(userSubredditCanonicalId)
310+
== SubredditSubscriptionState.SUBSCRIBED)
311+
) {
312+
subMan.unsubscribe(userSubredditCanonicalId, activity)
313+
Toast.makeText(
314+
activity,
315+
R.string.userprofile_toast_unfollow_loading,
316+
Toast.LENGTH_SHORT
317+
).show()
318+
} else {
319+
Toast.makeText(
320+
activity,
321+
R.string.userprofile_toast_not_following,
322+
Toast.LENGTH_SHORT
323+
).show()
324+
}
325+
} catch (e: InvalidSubredditNameException) {
326+
throw RuntimeException(e)
327+
}
328+
}
329+
330+
private fun getSubMan(activity: AppCompatActivity): RedditSubredditSubscriptionManager {
331+
val subMan = RedditSubredditSubscriptionManager.getSingleton(
332+
activity,
333+
RedditAccountManager.getInstance(activity).defaultAccount
334+
)
335+
return subMan
336+
}
337+
247338
@Throws(URISyntaxException::class)
248339
private fun assignUserAvatar(
249340
url: String,

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

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

108+
if (user.is_followed != null) {
109+
items.addView(propView(
110+
context,
111+
R.string.userprofile_tag_followed,
112+
user.is_followed ? R.string.general_true : R.string.general_false,
113+
false));
114+
}
115+
108116
if (user.is_employee != null) {
109117
items.addView(propView(
110118
context,
@@ -121,6 +129,14 @@ protected void prepare(
121129
false));
122130
}
123131

132+
if (user.is_followed != null) {
133+
items.addView(propView(
134+
context,
135+
R.string.userprofile_tag_followed,
136+
user.is_followed ? R.string.general_true : R.string.general_false,
137+
false));
138+
}
139+
124140
if (user.icon_img != null) {
125141
items.addView(propView(
126142
context,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class RedditUser implements Parcelable, JsonObject.JsonDeserializable {
4040
@Nullable public Boolean is_mod;
4141
@Nullable public Boolean is_suspended;
4242
@Nullable public Boolean over_18;
43+
@Nullable public Boolean is_followed;
4344

4445
@Nullable public String id;
4546
@NonNull public String name;

src/main/res/layout/user_profile_dialog.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@
149149
android:checkable="false"
150150
app:chipMinTouchTargetSize="0dp"/>
151151

152+
<com.google.android.material.chip.Chip
153+
android:id="@+id/user_profile_chip_followed"
154+
android:layout_width="wrap_content"
155+
android:layout_height="wrap_content"
156+
android:text="@string/userprofile_tag_followed"
157+
style="@style/Widget.Material3.Chip.Assist"
158+
android:clickable="false"
159+
android:checkable="false"
160+
app:chipMinTouchTargetSize="0dp"/>
161+
152162
</com.google.android.material.chip.ChipGroup>
153163

154164
<com.google.android.material.card.MaterialCardView
@@ -322,6 +332,26 @@
322332
style="@style/Widget.Material3.Chip.Assist.Elevated"
323333
android:checkable="false"/>
324334

335+
<com.google.android.material.chip.Chip
336+
android:id="@+id/user_profile_chip_follow"
337+
android:layout_width="wrap_content"
338+
android:layout_height="wrap_content"
339+
app:chipIcon="?rrIconPerson"
340+
app:chipIconSize="16sp"
341+
android:text="@string/userprofile_button_follow"
342+
style="@style/Widget.Material3.Chip.Assist.Elevated"
343+
android:checkable="false"/>
344+
345+
<com.google.android.material.chip.Chip
346+
android:id="@+id/user_profile_chip_unfollow"
347+
android:layout_width="wrap_content"
348+
android:layout_height="wrap_content"
349+
app:chipIcon="?rrIconCross"
350+
app:chipIconSize="16sp"
351+
android:text="@string/userprofile_button_unfollow"
352+
style="@style/Widget.Material3.Chip.Assist.Elevated"
353+
android:checkable="false"/>
354+
325355
</com.google.android.material.chip.ChipGroup>
326356

327357
</androidx.appcompat.widget.LinearLayoutCompat>

src/main/res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,4 +1829,12 @@
18291829
<string name="pref_album_skip_to_first_key" translatable="false">pref_album_skip_to_first_key</string>
18301830
<string name="pref_album_skip_to_first_title">Automatically open first album image</string>
18311831

1832+
<!-- 2024-01-09 -->
1833+
<string name="userprofile_button_follow" translatable="true">Follow</string>
1834+
<string name="userprofile_toast_follow_loading" translatable="true">Following…</string>
1835+
<string name="userprofile_tag_followed" translatable="true">Followed</string>
1836+
<string name="userprofile_toast_followed" translatable="true">You are already following this user!</string>
1837+
<string name="userprofile_button_unfollow" translatable="true">Unfollow</string>
1838+
<string name="userprofile_toast_unfollow_loading" translatable="true">Unfollowing…</string>
1839+
<string name="userprofile_toast_not_following" translatable="true">You are not following this user!</string>
18321840
</resources>

0 commit comments

Comments
 (0)