@@ -24,6 +24,7 @@ import android.view.View
24
24
import android.widget.FrameLayout
25
25
import android.widget.ImageView
26
26
import android.widget.ScrollView
27
+ import android.widget.Toast
27
28
import androidx.appcompat.app.AppCompatActivity
28
29
import androidx.appcompat.widget.AppCompatImageView
29
30
import com.google.android.material.card.MaterialCardView
@@ -51,7 +52,11 @@ import org.quantumbadger.redreader.common.time.TimestampUTC.Companion.fromUtcSec
51
52
import org.quantumbadger.redreader.common.time.TimestampUTC.Companion.now
52
53
import org.quantumbadger.redreader.reddit.APIResponseHandler.UserResponseHandler
53
54
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
54
58
import org.quantumbadger.redreader.reddit.things.RedditUser
59
+ import org.quantumbadger.redreader.reddit.things.SubredditCanonicalId
55
60
import org.quantumbadger.redreader.reddit.url.UserPostListingURL
56
61
import org.quantumbadger.redreader.views.LoadingSpinnerView
57
62
import org.quantumbadger.redreader.views.liststatus.ErrorView
@@ -87,6 +92,9 @@ object UserProfileDialog {
87
92
val postsKarma = dialog.findViewById<MaterialTextView >(R .id.user_profile_posts_karma)!!
88
93
val commentsKarma = dialog.findViewById<MaterialTextView >(R .id.user_profile_comments_karma)!!
89
94
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)!!
90
98
91
99
val cm = CacheManager .getInstance(activity)
92
100
val accountManager = RedditAccountManager .getInstance(activity)
@@ -147,6 +155,19 @@ object UserProfileDialog {
147
155
chipGold.visibility = View .GONE
148
156
}
149
157
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
+
150
171
if (PrefsUtility .appearance_user_show_avatars()) {
151
172
val iconUrl = user.iconUrl
152
173
if (! iconUrl.isNullOrEmpty()) {
@@ -220,6 +241,12 @@ object UserProfileDialog {
220
241
UserPropertiesDialog .newInstance(user)
221
242
.show(activity.supportFragmentManager, null )
222
243
}
244
+ chipFollow.setOnClickListener {
245
+ subscribeToUser(activity, username)
246
+ }
247
+ chipUnfollow.setOnClickListener {
248
+ unsubscribeToUser(activity, username)
249
+ }
223
250
}
224
251
}
225
252
@@ -244,6 +271,70 @@ object UserProfileDialog {
244
271
)
245
272
}
246
273
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
+
247
338
@Throws(URISyntaxException ::class )
248
339
private fun assignUserAvatar (
249
340
url : String ,
0 commit comments