Skip to content

Commit

Permalink
added the ability to get a redditor's moderated subs
Browse files Browse the repository at this point in the history
  • Loading branch information
KirkBushman committed Jun 14, 2020
1 parent b787525 commit db567f6
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/RedditApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.kirkbushman.araw.http.base.Listing
import com.kirkbushman.araw.models.FriendList
import com.kirkbushman.araw.models.KarmaList
import com.kirkbushman.araw.models.Me
import com.kirkbushman.araw.models.ModeratedList
import com.kirkbushman.araw.models.MoreChildrenResponse
import com.kirkbushman.araw.models.Prefs
import com.kirkbushman.araw.models.Reply
Expand Down Expand Up @@ -463,6 +464,12 @@ interface RedditApi {
@HeaderMap header: HashMap<String, String>
): Call<EnvelopedRedditorListing>

@GET("/user/{username}/moderated_subreddits/.json")
fun redditorModeratedSubreddits(
@Path("username") username: String,
@HeaderMap header: HashMap<String, String>
): Call<ModeratedList>

@GET("/api/v1/user/{username}/trophies/.json")
fun redditorTrophies(
@Path("username") username: String,
Expand Down
14 changes: 14 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/clients/RedditorsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.kirkbushman.araw.RedditApi
import com.kirkbushman.araw.fetcher.ContributionsFetcher
import com.kirkbushman.araw.fetcher.Fetcher
import com.kirkbushman.araw.fetcher.RedditorSearchFetcher
import com.kirkbushman.araw.models.ModeratedSub
import com.kirkbushman.araw.models.Redditor
import com.kirkbushman.araw.models.Trophy
import com.kirkbushman.araw.models.general.ContributionsSorting
Expand Down Expand Up @@ -163,6 +164,19 @@ class RedditorsClient(
)
}

fun moderatedSubreddits(username: String): List<ModeratedSub>? {

val authMap = getHeaderMap()
val req = api.redditorModeratedSubreddits(username, authMap)
val res = req.execute()

if (!res.isSuccessful) {
return null
}

return res.body()?.data
}

fun trophies(username: String, disableLegacyEncoding: Boolean = false): List<Trophy>? {

val authMap = getHeaderMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ enum class EnvelopeKind(val value: String) {
@Json(name = "more") More("more"),
@Json(name = "TrophyList") TrophyList("TrophyList"),
@Json(name = "UserList") UserList("UserList"),
@Json(name = "KarmaList") KarmaList("KarmaList")
@Json(name = "KarmaList") KarmaList("KarmaList"),
@Json(name = "ModeratedList") ModeratedList("ModeratedList")
}
71 changes: 71 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/models/ModeratedList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.kirkbushman.araw.models

import android.os.Parcelable
import com.kirkbushman.araw.http.base.EnvelopeKind
import com.kirkbushman.araw.models.mixins.Created
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.android.parcel.Parcelize

@JsonClass(generateAdapter = true)
@Parcelize
data class ModeratedList(

@Json(name = "kind")
val kind: EnvelopeKind?,

@Json(name = "data")
val data: List<ModeratedSub>?

) : Parcelable

@JsonClass(generateAdapter = true)
@Parcelize
data class ModeratedSub(

@Json(name = "name")
val fullname: String,

@Json(name = "banner_img")
val bannerImg: String,

@Json(name = "banner_size")
val bannerImgSize: List<Int>?,

@Json(name = "created")
override val created: Long,

@Json(name = "created_utc")
override val createdUtc: Long,

@Json(name = "icon_img")
val iconImg: String,

@Json(name = "icon_size")
val iconImgSize: List<Int>?,

@Json(name = "user_is_subscriber")
val isSubscriber: Boolean,

@Json(name = "sr")
val displayName: String,

@Json(name = "sr_display_name_prefixed")
val displayNamePrefixed: String,

@Json(name = "over_18")
val over18: Boolean,

@Json(name = "subreddit_type")
val subredditType: String,

@Json(name = "subscribers")
val subscribers: Int,

@Json(name = "title")
val title: String,

@Json(name = "url")
val url: String

) : Parcelable, Created
1 change: 1 addition & 0 deletions sampleapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<activity android:name=".activities.InboxActivity"/>
<activity android:name=".activities.RedditorActivity"/>
<activity android:name=".activities.RedditorInfoActivity"/>
<activity android:name=".activities.RedditorModeratedSubs" />
<activity android:name=".activities.SubredditActivity"/>
<activity android:name=".activities.SubmissionActivity"/>
<activity android:name=".activities.SubmissionsActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class MainActivity : AppCompatActivity() {
UserSearchActivity.start(this)
}

bttn_user_moderated_subs.setOnClickListener {

RedditorModeratedSubs.start(this)
}

bttn_subreddit.setOnClickListener {

SubredditActivity.start(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.kirkbushman.sampleapp.activities

import android.content.Context
import android.content.Intent
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.widget.Toolbar
import com.airbnb.epoxy.EpoxyRecyclerView
import com.kirkbushman.araw.RedditClient
import com.kirkbushman.araw.models.ModeratedSub
import com.kirkbushman.sampleapp.R
import com.kirkbushman.sampleapp.activities.base.BaseSearchControllerActivity
import com.kirkbushman.sampleapp.controllers.BaseController
import com.kirkbushman.sampleapp.controllers.ModeratedSubsController
import com.kirkbushman.sampleapp.controllers.SubredditController
import kotlinx.android.synthetic.main.activity_wiki_pages.*

class RedditorModeratedSubs : BaseSearchControllerActivity<ModeratedSub, SubredditController.SubredditCallback>(R.layout.activity_moderated_subs) {

companion object {

fun start(context: Context) {

val intent = Intent(context, RedditorModeratedSubs::class.java)
context.startActivity(intent)
}
}

override val actionBar: Toolbar
get() = toolbar

override val bttnSearch: Button
get() = search_bttn

override val editSearch: EditText
get() = search

override val recyclerView: EpoxyRecyclerView
get() = list

override val callback: SubredditController.SubredditCallback?
get() = object : SubredditController.SubredditCallback {

override fun subscribeClick(index: Int) {}
}

override val controller: BaseController<ModeratedSub, SubredditController.SubredditCallback> = ModeratedSubsController(callback!!)

override fun fetchItem(client: RedditClient?, query: String): Collection<ModeratedSub>? {

return client?.redditorsClient?.moderatedSubreddits(query)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kirkbushman.sampleapp.controllers

import android.view.View
import com.kirkbushman.araw.models.ModeratedSub
import com.kirkbushman.sampleapp.models.subreddit

class ModeratedSubsController(callback: SubredditController.SubredditCallback) : BaseController<ModeratedSub, SubredditController.SubredditCallback>(callback) {

override fun itemModel(index: Int, it: ModeratedSub, callback: SubredditController.SubredditCallback?) {

subreddit {
id(it.fullname)
subreddit(it.displayNamePrefixed)
subscribed(it.isSubscriber)
subscribeClick(View.OnClickListener { callback?.subscribeClick(index) })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.kirkbushman.sampleapp.models.subreddit

class SubredditController(private val callback: SubredditCallback) : EpoxyController() {

interface SubredditCallback {
interface SubredditCallback : BaseCallback {

fun subscribeClick(index: Int)
}
Expand Down
7 changes: 7 additions & 0 deletions sampleapp/src/main/res/layout-v21/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
android:layout_margin="8dp"
android:text="@string/bttn_user_search" />

<com.google.android.material.button.MaterialButton
android:id="@+id/bttn_user_moderated_subs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/bttn_user_moderated_subs" />

<com.google.android.material.button.MaterialButton
android:id="@+id/bttn_subreddit"
android:layout_width="match_parent"
Expand Down
7 changes: 7 additions & 0 deletions sampleapp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
android:layout_margin="8dp"
android:text="@string/bttn_user_search" />

<com.google.android.material.button.MaterialButton
android:id="@+id/bttn_user_moderated_subs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/bttn_user_moderated_subs" />

<com.google.android.material.button.MaterialButton
android:id="@+id/bttn_subreddit"
android:layout_width="match_parent"
Expand Down
58 changes: 58 additions & 0 deletions sampleapp/src/main/res/layout/activity_moderated_subs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="@string/edit_insert_query"
android:inputType="text"
android:importantForAutofill="no"
/>

<com.google.android.material.button.MaterialButton
android:id="@+id/search_bttn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/bttn_ok"
/>

</LinearLayout>

</androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:scrollbars="vertical"
android:fadeScrollbars="true"
app:itemSpacing="16dp"
app:layoutManager="@string/linear_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
1 change: 1 addition & 0 deletions sampleapp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@
<string name="timeperiod_month">Month</string>
<string name="timeperiod_year">Year</string>
<string name="timeperiod_all">All</string>
<string name="bttn_user_moderated_subs">User Moderated Subs</string>

</resources>

0 comments on commit db567f6

Please sign in to comment.