Skip to content
Permalink
Browse files

Retrofit

  • Loading branch information...
imuhao committed Nov 7, 2017
1 parent e20510f commit 40d48484d400c173beae458864e62f828a87e243
@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.caimuhao.kedditbysteps">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@@ -0,0 +1,26 @@
package com.caimuhao.kedditbysteps.api

/**
* @author Smile
* @time 2017/11/7 10:41
* @desc ${TODD}
*/

class RedditNewsResponse(val data: RedditDataResponse)

class RedditDataResponse(
val children: List<RedditChildrenResponse>,
val after: String?,
val before: String?
)

class RedditChildrenResponse(val data: RedditNewsDataResponse)

class RedditNewsDataResponse(
val author: String,
val title: String,
val num_comments: Int,
val created: Long,
val thumbnail: String,
val url: String
)
@@ -0,0 +1,17 @@
package com.caimuhao.kedditbysteps.api

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

/**
* @author Smile
* @time 2017/11/7 10:45
* @desc ${TODD}
*/

interface RedditApi {
@GET("/top.json")
fun getTop(@Query("after") after: String, @Query("limit") limit: Int)
: Call<RedditNewsResponse>
}
@@ -0,0 +1,11 @@
package com.caimuhao.kedditbysteps.api

import com.caimuhao.kedditbysteps.bean.RedditNewsItem

/**
* @author Smile
* @time 2017/11/7 11:26
* @desc ${TODD}
*/
data class RedditNews(var after: String, var before: String, var news: List<RedditNewsItem>) {
}
@@ -0,0 +1,28 @@
package com.caimuhao.kedditbysteps.api

import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

/**
* @author Smile
* @time 2017/11/7 10:55
* @desc ${TODD}
*/

class RestApi {
private val redditApi: RedditApi

init {
val retrofit = Retrofit.Builder()
.baseUrl("https://www.reddit.com")
.addConverterFactory(MoshiConverterFactory.create())
.build()
redditApi = retrofit.create(RedditApi::class.java)
}

fun getNews(after: String, limit: Int): Call<RedditNewsResponse> {
return redditApi.getTop(after, limit)
}

}
@@ -8,6 +8,8 @@ import com.caimuhao.kedditbysteps.commons.adapter.ViewType
* @time 2017/11/5 17:34
* @desc ${TODD}
*/
data class RedditNewsItem(var author: String, var title: String) : ViewType {
data class RedditNewsItem(var author: String, var title: String, var num_comments: Int,
var created: Long, var thumbnail: String, var url: String
) : ViewType {
override fun getViewType() = AdapterConstants.NEWS
}
@@ -2,11 +2,13 @@

package com.caimuhao.kedditbysteps.commons.extensions

import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import com.caimuhao.kedditbysteps.R
import com.squareup.picasso.Picasso

@@ -26,4 +28,8 @@ fun ImageView.loadImg(imageUrl: String) {
} else {
Picasso.with(context).load(imageUrl).into(this)
}
}

fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(context, message, duration)
}
@@ -8,8 +8,11 @@ import android.view.ViewGroup
import com.caimuhao.kedditbysteps.R
import com.caimuhao.kedditbysteps.commons.BaseFragment
import com.caimuhao.kedditbysteps.commons.extensions.inflate
import com.caimuhao.kedditbysteps.commons.extensions.showToast
import com.caimuhao.kedditbysteps.fatures.news.adapter.NewsAdapter
import kotlinx.android.synthetic.main.fragment_news.*
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
* @author Smile
@@ -36,5 +39,19 @@ class NewsFragment : BaseFragment() {
super.onActivityCreated(savedInstanceState)
var adapter = NewsAdapter()
newsList.adapter = adapter

refreshData()
}

fun refreshData() {
newsManager.getNews("", 10)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ retrievedNews ->
var redditNews = retrievedNews
(newsList.adapter as NewsAdapter).addNews(redditNews.news)
}, { error ->
showToast(error.message ?: "")
})
}
}
@@ -1,5 +1,7 @@
package com.caimuhao.kedditbysteps.fatures.news

import com.caimuhao.kedditbysteps.api.RedditNews
import com.caimuhao.kedditbysteps.api.RestApi
import com.caimuhao.kedditbysteps.bean.RedditNewsItem
import rx.Observable

@@ -8,15 +10,24 @@ import rx.Observable
* @time 2017/11/6 14:45
* @desc ${TODD}
*/
class NewsManager {
fun getNews(): Observable<List<RedditNewsItem>> {
class NewsManager(private val api: RestApi = RestApi()) {

fun getNews(after: String, limit: Int): Observable<RedditNews> {
return Observable.create { subscriber ->
var news = mutableListOf<RedditNewsItem>()
for (i in 1..10) {
news.add(RedditNewsItem("smile" + i, "title" + i))
val execute = api.getNews(after, limit).execute()
var response = execute.body().data
if (execute.isSuccessful) {
var items = response.children.map {
var item = it.data
RedditNewsItem(item.author, item.title, item.num_comments,
item.created, item.thumbnail, item.url)
}
var news = RedditNews(response.after ?: "", response.before ?: "", items)
subscriber.onNext(news)
subscriber.onCompleted()
} else {
subscriber.onError(Throwable(execute.message()))
}
subscriber.onNext(news)
subscriber.onCompleted()
}
}
}
@@ -28,6 +28,12 @@ class NewsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
items = ArrayList()
}

fun addNews(news: List<RedditNewsItem>) {
items.addAll(news)

notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return delegateAdapters.get(viewType).onCreateViewHolder(parent)
}
@@ -6,6 +6,7 @@ import com.caimuhao.kedditbysteps.R
import com.caimuhao.kedditbysteps.bean.RedditNewsItem
import com.caimuhao.kedditbysteps.commons.adapter.ViewType
import com.caimuhao.kedditbysteps.commons.adapter.ViewTypeDelegateAdapter
import com.caimuhao.kedditbysteps.commons.extensions.getFriendlyTime
import com.caimuhao.kedditbysteps.commons.extensions.inflate
import com.caimuhao.kedditbysteps.commons.extensions.loadImg
import kotlinx.android.synthetic.main.news_item.view.*
@@ -35,7 +36,10 @@ class NewsDelegateAdapter : ViewTypeDelegateAdapter {
fun bind(item: RedditNewsItem) {
description.text = item.title
author.text = item.author
img_thumbnail.loadImg("")
img_thumbnail.loadImg(item.thumbnail)
comments.text = "${item.num_comments} comments"
time.text = item.created.getFriendlyTime()

}
}
}

0 comments on commit 40d4848

Please sign in to comment.
You can’t perform that action at this time.