diff --git a/lib/src/main/java/com/kirkbushman/araw/RedditApi.kt b/lib/src/main/java/com/kirkbushman/araw/RedditApi.kt index 949065a5..3a0088be 100644 --- a/lib/src/main/java/com/kirkbushman/araw/RedditApi.kt +++ b/lib/src/main/java/com/kirkbushman/araw/RedditApi.kt @@ -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 @@ -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 @@ -302,12 +304,6 @@ interface RedditApi { @HeaderMap header: HashMap ): Call - @GET("/r/{subreddit}/wiki/.json") - fun wiki( - @Path("subreddit") subreddit: String, - @HeaderMap header: HashMap - ): Call - @FormUrlEncoded @POST("/api/subscribe") fun subscribe( @@ -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 + ): Call + + @GET("/r/{subreddit}/wiki/pages/.json") + fun wikiPages( + @Path("subreddit") subreddit: String, + @HeaderMap header: HashMap + ): Call + + @GET("/r/{subreddit}/wiki/revisions/.json") + fun wikiRevisions( + @Path("subreddit") subreddit: String, + @HeaderMap header: HashMap + ): Call + + // --- Wiki section: END --- + @GET("/search") fun search( @Query("type") type: String? = null, diff --git a/lib/src/main/java/com/kirkbushman/araw/RedditClient.kt b/lib/src/main/java/com/kirkbushman/araw/RedditClient.kt index 5efc77d8..231426bb 100644 --- a/lib/src/main/java/com/kirkbushman/araw/RedditClient.kt +++ b/lib/src/main/java/com/kirkbushman/araw/RedditClient.kt @@ -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 @@ -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 diff --git a/lib/src/main/java/com/kirkbushman/araw/clients/SubredditsClient.kt b/lib/src/main/java/com/kirkbushman/araw/clients/SubredditsClient.kt index 5eeea917..8091d332 100644 --- a/lib/src/main/java/com/kirkbushman/araw/clients/SubredditsClient.kt +++ b/lib/src/main/java/com/kirkbushman/araw/clients/SubredditsClient.kt @@ -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 @@ -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, diff --git a/lib/src/main/java/com/kirkbushman/araw/clients/WikisClient.kt b/lib/src/main/java/com/kirkbushman/araw/clients/WikisClient.kt new file mode 100644 index 00000000..718b66dd --- /dev/null +++ b/lib/src/main/java/com/kirkbushman/araw/clients/WikisClient.kt @@ -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 + +) : 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? { + return wikiPages(subreddit.displayName) + } + + fun wikiPages(subreddit: String): List? { + + 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() + } +} diff --git a/lib/src/main/java/com/kirkbushman/araw/http/EnvelopedWikiRevisionListing.kt b/lib/src/main/java/com/kirkbushman/araw/http/EnvelopedWikiRevisionListing.kt new file mode 100644 index 00000000..d2e7c47a --- /dev/null +++ b/lib/src/main/java/com/kirkbushman/araw/http/EnvelopedWikiRevisionListing.kt @@ -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, Parcelable diff --git a/lib/src/main/java/com/kirkbushman/araw/http/base/EnvelopeKind.kt b/lib/src/main/java/com/kirkbushman/araw/http/base/EnvelopeKind.kt index 5ca64c11..a7044b48 100644 --- a/lib/src/main/java/com/kirkbushman/araw/http/base/EnvelopeKind.kt +++ b/lib/src/main/java/com/kirkbushman/araw/http/base/EnvelopeKind.kt @@ -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"), diff --git a/lib/src/main/java/com/kirkbushman/araw/http/listings/WikiRevisionListing.kt b/lib/src/main/java/com/kirkbushman/araw/http/listings/WikiRevisionListing.kt new file mode 100644 index 00000000..debafcca --- /dev/null +++ b/lib/src/main/java/com/kirkbushman/araw/http/listings/WikiRevisionListing.kt @@ -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, + + @Json(name = "after") + override val after: String?, + @Json(name = "before") + override val before: String? + +) : Listing, Parcelable diff --git a/lib/src/main/java/com/kirkbushman/araw/models/WikiPageList.kt b/lib/src/main/java/com/kirkbushman/araw/models/WikiPageList.kt new file mode 100644 index 00000000..a9a8c530 --- /dev/null +++ b/lib/src/main/java/com/kirkbushman/araw/models/WikiPageList.kt @@ -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 + +) : Parcelable diff --git a/lib/src/main/java/com/kirkbushman/araw/models/WikiRevision.kt b/lib/src/main/java/com/kirkbushman/araw/models/WikiRevision.kt new file mode 100644 index 00000000..7a1b2429 --- /dev/null +++ b/lib/src/main/java/com/kirkbushman/araw/models/WikiRevision.kt @@ -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 diff --git a/lib/src/main/java/com/kirkbushman/araw/utils/Extensions.kt b/lib/src/main/java/com/kirkbushman/araw/utils/Extensions.kt index e46c321a..f10aea24 100644 --- a/lib/src/main/java/com/kirkbushman/araw/utils/Extensions.kt +++ b/lib/src/main/java/com/kirkbushman/araw/utils/Extensions.kt @@ -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 @@ -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.toCommentSequence(): ArrayList { val sequence = ArrayList() diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApiDetailActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApiDetailActivity.kt index 77dfe1db..d7b946c2 100644 --- a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApiDetailActivity.kt +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApiDetailActivity.kt @@ -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) @@ -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) @@ -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!" diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApisActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApisActivity.kt index 09d0e552..6ae94ae5 100644 --- a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApisActivity.kt +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/ApisActivity.kt @@ -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) + } } } diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/WikiPageActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/WikiPageActivity.kt index e534bd38..72b9cc35 100644 --- a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/WikiPageActivity.kt +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/WikiPageActivity.kt @@ -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() diff --git a/sampleapp/src/main/res/layout/activity_apis.xml b/sampleapp/src/main/res/layout/activity_apis.xml index b3f6705b..0f99d98e 100644 --- a/sampleapp/src/main/res/layout/activity_apis.xml +++ b/sampleapp/src/main/res/layout/activity_apis.xml @@ -491,6 +491,55 @@ app:layout_constraintEnd_toStartOf="@id/bttn_user_gilded" /> + + + + + + + + \ No newline at end of file diff --git a/sampleapp/src/main/res/values/strings.xml b/sampleapp/src/main/res/values/strings.xml index 1bad789d..c757c963 100644 --- a/sampleapp/src/main/res/values/strings.xml +++ b/sampleapp/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ Message: Subreddit: Users: + Wiki: APIs Me @@ -54,6 +55,8 @@ Comment Comments Wiki + Wiki Pages + Wiki Revisions Rules Insert user: Insert username: