Skip to content

Commit

Permalink
moved wiki functions to sub-client
Browse files Browse the repository at this point in the history
  • Loading branch information
luca committed Nov 1, 2019
1 parent 867d29f commit f9e0fad
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 25 deletions.
30 changes: 24 additions & 6 deletions lib/src/main/java/com/kirkbushman/araw/RedditApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.kirkbushman.araw.http.EnvelopedSubreddit
import com.kirkbushman.araw.http.EnvelopedSubredditListing
import com.kirkbushman.araw.http.EnvelopedWikiPage
import com.kirkbushman.araw.http.base.Listing
import com.kirkbushman.araw.http.listings.WikiRevisionListing
import com.kirkbushman.araw.models.FriendList
import com.kirkbushman.araw.models.KarmaList
import com.kirkbushman.araw.models.Me
Expand All @@ -22,6 +23,7 @@ import com.kirkbushman.araw.models.SubredditSearchResult
import com.kirkbushman.araw.models.TrendingSubreddits
import com.kirkbushman.araw.models.TrophyList
import com.kirkbushman.araw.models.UserList
import com.kirkbushman.araw.models.WikiPageList
import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
Expand Down Expand Up @@ -302,12 +304,6 @@ interface RedditApi {
@HeaderMap header: HashMap<String, String>
): Call<SubredditRules>

@GET("/r/{subreddit}/wiki/.json")
fun wiki(
@Path("subreddit") subreddit: String,
@HeaderMap header: HashMap<String, String>
): Call<EnvelopedWikiPage>

@FormUrlEncoded
@POST("/api/subscribe")
fun subscribe(
Expand Down Expand Up @@ -408,6 +404,28 @@ interface RedditApi {

// --- Redditor sections: END ---

// --- Wiki section: BEGIN ---

@GET("/r/{subreddit}/wiki/.json")
fun wiki(
@Path("subreddit") subreddit: String,
@HeaderMap header: HashMap<String, String>
): Call<EnvelopedWikiPage>

@GET("/r/{subreddit}/wiki/pages/.json")
fun wikiPages(
@Path("subreddit") subreddit: String,
@HeaderMap header: HashMap<String, String>
): Call<WikiPageList>

@GET("/r/{subreddit}/wiki/revisions/.json")
fun wikiRevisions(
@Path("subreddit") subreddit: String,
@HeaderMap header: HashMap<String, String>
): Call<Any>

// --- Wiki section: END ---

@GET("/search")
fun search(
@Query("type") type: String? = null,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/RedditClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.kirkbushman.araw.clients.ContributionsClient
import com.kirkbushman.araw.clients.MessagesClient
import com.kirkbushman.araw.clients.SubredditsClient
import com.kirkbushman.araw.clients.RedditorsClient
import com.kirkbushman.araw.clients.WikisClient
import com.kirkbushman.araw.models.Me
import com.kirkbushman.araw.models.SubredditSearchResult
import com.kirkbushman.araw.utils.Utils.getRetrofit
Expand All @@ -20,6 +21,7 @@ class RedditClient(private val bearer: TokenBearer, logging: Boolean) {
val messagesClient by lazy { MessagesClient(api, ::getHeaderMap) }
val subredditsClient by lazy { SubredditsClient(api, ::getHeaderMap) }
val redditorsClient by lazy { RedditorsClient(api, ::getHeaderMap) }
val wikisClient by lazy { WikisClient(api, ::getHeaderMap) }

private var currentUser: Me? = null
fun getCurrentUser(): Me? = currentUser
Expand Down
18 changes: 0 additions & 18 deletions lib/src/main/java/com/kirkbushman/araw/clients/SubredditsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.kirkbushman.araw.fetcher.SubredditsSearchFetcher
import com.kirkbushman.araw.models.Subreddit
import com.kirkbushman.araw.models.SubredditRule
import com.kirkbushman.araw.models.User
import com.kirkbushman.araw.models.WikiPage
import com.kirkbushman.araw.models.general.SubmissionKind
import com.kirkbushman.araw.models.general.SubmissionsSorting
import com.kirkbushman.araw.models.general.SubredditSearchSorting
Expand Down Expand Up @@ -152,23 +151,6 @@ class SubredditsClient(
return res.body()?.rules
}

fun wiki(subreddit: Subreddit): WikiPage? {
return wiki(subreddit.displayName)
}

fun wiki(subreddit: String): WikiPage? {

val authMap = getHeaderMap()
val req = api.wiki(subreddit = subreddit, header = authMap)
val res = req.execute()

if (!res.isSuccessful) {
return null
}

return res.body()?.data
}

fun frontpage(

limit: Int = Fetcher.DEFAULT_LIMIT,
Expand Down
65 changes: 65 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/clients/WikisClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kirkbushman.araw.clients

import com.kirkbushman.araw.RedditApi
import com.kirkbushman.araw.models.Subreddit
import com.kirkbushman.araw.models.WikiPage
import com.kirkbushman.araw.models.WikiRevision

class WikisClient(

private val api: RedditApi,
private inline val getHeaderMap: () -> HashMap<String, String>

) : BaseRedditClient(api, getHeaderMap) {

fun wiki(subreddit: Subreddit): WikiPage? {
return wiki(subreddit.displayName)
}

fun wiki(subreddit: String): WikiPage? {

val authMap = getHeaderMap()
val req = api.wiki(subreddit = subreddit, header = authMap)
val res = req.execute()

if (!res.isSuccessful) {
return null
}

return res.body()?.data
}

fun wikiPages(subreddit: Subreddit): List<String>? {
return wikiPages(subreddit.displayName)
}

fun wikiPages(subreddit: String): List<String>? {

val authMap = getHeaderMap()
val req = api.wikiPages(subreddit = subreddit, header = authMap)
val res = req.execute()

if (!res.isSuccessful) {
return null
}

return res.body()?.data
}

fun wikiRevisions(subreddit: Subreddit): Any? {
return wikiRevisions(subreddit.displayName)
}

fun wikiRevisions(subreddit: String): Any? {

val authMap = getHeaderMap()
val req = api.wikiRevisions(subreddit = subreddit, header = authMap)
val res = req.execute()

if (!res.isSuccessful) {
return null
}

return res.body()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kirkbushman.araw.http

import android.os.Parcelable
import com.kirkbushman.araw.http.base.Envelope
import com.kirkbushman.araw.http.base.EnvelopeKind
import com.kirkbushman.araw.http.listings.WikiRevisionListing
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.android.parcel.Parcelize

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

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

@Json(name = "data")
override val data: WikiRevisionListing

) : Envelope<WikiRevisionListing>, Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class EnvelopeKind(val value: String) {

@Json(name = "Listing") Listing("Listing"),
@Json(name = "wikipage") Wikipage("wikipage"),
@Json(name = "wikipagelisting") WikipageListing("wikipagelisting"),
@Json(name = "more") More("more"),
@Json(name = "TrophyList") TrophyList("TrophyList"),
@Json(name = "UserList") UserList("UserList"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kirkbushman.araw.http.listings

import android.os.Parcelable
import com.kirkbushman.araw.http.base.Listing
import com.kirkbushman.araw.models.WikiRevision
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.android.parcel.Parcelize

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

@Json(name = "modhash")
override val modhash: String?,
@Json(name = "dist")
override val dist: Int?,

@Json(name = "children")
override val children: List<WikiRevision>,

@Json(name = "after")
override val after: String?,
@Json(name = "before")
override val before: String?

) : Listing<WikiRevision>, Parcelable
19 changes: 19 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/models/WikiPageList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kirkbushman.araw.models

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

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

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

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

) : Parcelable
23 changes: 23 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/models/WikiRevision.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.kirkbushman.araw.models

import android.os.Parcelable
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.android.parcel.Parcelize

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

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

@Json(name = "revision_hidden")
val revisionHidden: Boolean,

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

val timestamp: Long

) : Parcelable
7 changes: 7 additions & 0 deletions lib/src/main/java/com/kirkbushman/araw/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kirkbushman.araw.utils
import com.kirkbushman.araw.models.Comment
import com.kirkbushman.araw.models.Friend
import com.kirkbushman.araw.models.User
import com.kirkbushman.araw.models.WikiRevision
import com.kirkbushman.araw.models.general.Distinguished
import com.kirkbushman.araw.models.general.Vote
import com.kirkbushman.araw.models.mixins.CommentData
Expand Down Expand Up @@ -81,6 +82,12 @@ val User.userDate: Date
return Date(milliseconds)
}

val WikiRevision.timestampDate: Date
get() {
val milliseconds = timestamp * 1000L
return Date(milliseconds)
}

fun List<CommentData>.toCommentSequence(): ArrayList<CommentData> {

val sequence = ArrayList<CommentData>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ApiDetailActivity : AppCompatActivity() {
private const val API_USER_COMMENTS = "param_user_call_comments"
private const val API_USER_GILDED = "param_user_call_gilded"
private const val API_USER_TROPHIES = "param_user_call_trophies"
private const val API_WIKI = "param_wiki_call_wiki"
private const val API_WIKI_PAGES = "param_wiki_call_wiki_pages"
private const val API_WIKI_REVISIONS = "param_wiki_call_wiki_revisions"

fun startApiMe(context: Context) {
start(context, API_ME)
Expand Down Expand Up @@ -201,6 +204,18 @@ class ApiDetailActivity : AppCompatActivity() {
start(context, API_USER_TROPHIES)
}

fun startApiWiki(context: Context) {
start(context, API_WIKI)
}

fun startApiWikiPages(context: Context) {
start(context, API_WIKI_PAGES)
}

fun startApiWikiRevisions(context: Context) {
start(context, API_WIKI_REVISIONS)
}

fun start(context: Context, call: String) {

val intent = Intent(context, ApiDetailActivity::class.java)
Expand Down Expand Up @@ -445,6 +460,24 @@ class ApiDetailActivity : AppCompatActivity() {
val trophies = client?.redditorsClient?.trophies(userName)
return trophies.toString()
}

API_WIKI -> {
val subredditName = getRandomSubredditName()
val wiki = client?.wikisClient?.wiki(subredditName)
return wiki.toString()
}

API_WIKI_PAGES -> {
val subredditName = getRandomSubredditName()
val wikiPages = client?.wikisClient?.wikiPages(subredditName)
return wikiPages.toString()
}

API_WIKI_REVISIONS -> {
val subredditName = getRandomSubredditName()
val wikiRevisions = client?.wikisClient?.wikiRevisions(subredditName)
return wikiRevisions.toString()
}
}

return "Api call not registered!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,17 @@ class ApisActivity : AppCompatActivity() {
bttn_user_trophies.setOnClickListener {
ApiDetailActivity.startApiUserTrophies(this)
}

bttn_wiki.setOnClickListener {
ApiDetailActivity.startApiWiki(this)
}

bttn_wiki_pages.setOnClickListener {
ApiDetailActivity.startApiWikiPages(this)
}

bttn_wiki_revisions.setOnClickListener {
ApiDetailActivity.startApiWikiRevisions(this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class WikiPageActivity : AppCompatActivity() {
var wiki: WikiPage? = null
doAsync(doWork = {

wiki = client?.subredditsClient?.wiki(subreddit)
wiki = client?.wikisClient?.wiki(subreddit)
}, onPost = {

wiki_text.text = wiki.toString()
Expand Down
Loading

0 comments on commit f9e0fad

Please sign in to comment.