Skip to content

Commit efbb0ce

Browse files
Re-factored bunch of code with focus on conciseness, nullability and code re-usability
1 parent a7333d9 commit efbb0ce

18 files changed

Lines changed: 192 additions & 279 deletions

app/src/main/java/com/akshay/newsapp/adapter/NewsArticlesAdapter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import kotlinx.android.synthetic.main.news_article_item.view.*
1414
* @author Akshay Chordiya
1515
* @since 5/23/2017.
1616
*/
17-
class NewsArticlesAdapter(val listener: (NewsArticles) -> Unit) : RecyclerView.Adapter<NewsArticlesAdapter.NewsHolder>() {
17+
class NewsArticlesAdapter(
18+
private val listener: (NewsArticles) -> Unit
19+
) : RecyclerView.Adapter<NewsArticlesAdapter.NewsHolder>() {
1820

1921
/**
2022
* List of news articles
@@ -56,5 +58,6 @@ class NewsArticlesAdapter(val listener: (NewsArticles) -> Unit) : RecyclerView.A
5658
*/
5759
fun replaceItems(items: List<NewsArticles>) {
5860
newsArticles = items
61+
notifyDataSetChanged()
5962
}
6063
}

app/src/main/java/com/akshay/newsapp/api/ApiResponse.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

app/src/main/java/com/akshay/newsapp/api/NewsSourceService.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@ package com.akshay.newsapp.api
33
import android.arch.lifecycle.LiveData
44
import com.akshay.newsapp.BuildConfig
55
import com.akshay.newsapp.model.NewsSource
6+
import com.akshay.newsapp.model.network.Resource
67
import com.akshay.newsapp.utils.LiveDataCallAdapterFactory
78
import retrofit2.Retrofit
89
import retrofit2.converter.gson.GsonConverterFactory
910
import retrofit2.http.GET
1011

1112
/**
12-
* Fetch all the latest news article from Google news
13+
* Fetch all the latest news article from Google news.
1314
*
1415
* @author Akshay Chordiya
1516
* @since 5/23/2017.
1617
*/
1718
interface NewsSourceService {
1819

1920
/**
20-
* Retrieves all the latest news article from Google news
21+
* Retrieves all the latest news article from Google news.
2122
*/
2223
@GET("articles?source=google-news&apiKey=" + BuildConfig.NEWS_API_KEY)
23-
fun getNewsSource(): LiveData<ApiResponse<NewsSource>>
24+
fun getNewsSource(): LiveData<Resource<NewsSource>>
2425

2526
/**
2627
* Kinda like a static block in Java
2728
*/
2829
companion object Factory {
29-
val BASE_URL = "https://newsapi.org/v1/"
30+
private const val BASE_URL = "https://newsapi.org/v1/"
3031

32+
// TODO: Move to DI.
3133
fun getNewsSourceService(): NewsSourceService {
3234
return Retrofit.Builder()
3335
.baseUrl(BASE_URL)

app/src/main/java/com/akshay/newsapp/db/NewsArticlesDao.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.akshay.newsapp.model.NewsArticles
99
/**
1010
* Abstracts access to the news database
1111
*/
12+
//TODO: Use inheritance for code re-usability.
1213
@Dao
1314
interface NewsArticlesDao {
1415

app/src/main/java/com/akshay/newsapp/model/Resource.kt

Lines changed: 0 additions & 42 deletions
This file was deleted.

app/src/main/java/com/akshay/newsapp/model/Status.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.akshay.newsapp.model.network
2+
3+
/**
4+
* A generic class that holds a value with its loading status.
5+
* @param <T>
6+
</T> */
7+
data class Resource<ResultType>(var status: Status, var data: ResultType? = null, var errorMessage: String? = null) {
8+
9+
companion object {
10+
/**
11+
* Creates [Resource] object with `SUCCESS` status and [data].
12+
*/
13+
fun <ResultType> success(data: ResultType): Resource<ResultType> = Resource(Status.SUCCESS, data)
14+
15+
/**
16+
* Creates [Resource] object with `LOADING` status to notify
17+
* the UI to showing loading.
18+
*/
19+
fun <ResultType> loading(): Resource<ResultType> = Resource(Status.LOADING)
20+
21+
/**
22+
* Creates [Resource] object with `ERROR` status and [message].
23+
*/
24+
fun <ResultType> error(message: String?): Resource<ResultType> = Resource(Status.ERROR, errorMessage = message)
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.akshay.newsapp.model.network
2+
3+
/**
4+
* Status of a resource that is provided to the UI.
5+
*
6+
*
7+
* These are usually created by the Repository classes where they return
8+
* `LiveData<Resource<T>>` to pass back the latest data to the UI with its fetch status.
9+
*/
10+
enum class Status {
11+
SUCCESS,
12+
ERROR,
13+
LOADING;
14+
15+
/**
16+
* Returns `true` if the [Status] is success else `false`.
17+
*/
18+
fun isSuccessful() = this == SUCCESS
19+
20+
/**
21+
* Returns `true` if the [Status] is loading else `false`.
22+
*/
23+
fun isLoading() = this == LOADING
24+
}

0 commit comments

Comments
 (0)