-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A lot of repackaging, renaming, and altering models for better clarity.
- Loading branch information
Showing
23 changed files
with
234 additions
and
239 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...emergetools/hackernews/data/ChromeTabs.kt → .../com/emergetools/hackernews/ChromeTabs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
android/app/src/main/java/com/emergetools/hackernews/data/HackerNewsBaseDataSource.kt
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
android/app/src/main/java/com/emergetools/hackernews/data/ItemRepository.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
android/app/src/main/java/com/emergetools/hackernews/data/remote/HackerNewsBaseApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.emergetools.hackernews.data.remote | ||
|
||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
|
||
interface HackerNewsBaseApi { | ||
@GET("topstories.json") | ||
suspend fun getTopStoryIds(): List<Long> | ||
|
||
@GET("newstories.json") | ||
suspend fun getNewStoryIds(): List<Long> | ||
|
||
@GET("item/{id}.json") | ||
suspend fun getItem(@Path("id") itemId: Long): ItemResponse | ||
} |
118 changes: 118 additions & 0 deletions
118
android/app/src/main/java/com/emergetools/hackernews/data/remote/HackerNewsBaseClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.emergetools.hackernews.data.remote | ||
|
||
import android.util.Log | ||
import com.emergetools.hackernews.data.remote.ItemResponse.Item | ||
import com.emergetools.hackernews.features.stories.FeedType | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonContentPolymorphicSerializer | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonNull | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
|
||
typealias ItemId = Long | ||
typealias Page = List<ItemId> | ||
|
||
private const val BASE_FIREBASE_URL = "https://hacker-news.firebaseio.com/v0/" | ||
|
||
@Serializable(with = ItemResponseSerializer::class) | ||
sealed interface ItemResponse { | ||
@Serializable | ||
data class Item( | ||
val id: Long, | ||
val type: String, | ||
val time: Long, | ||
val by: String? = null, | ||
val title: String? = null, | ||
val score: Int? = null, | ||
val url: String? = null, | ||
val descendants: Int? = null, | ||
val kids: List<Long>? = null, | ||
val text: String? = null | ||
) : ItemResponse | ||
|
||
@Serializable | ||
data object NullResponse : ItemResponse | ||
} | ||
|
||
private object ItemResponseSerializer : | ||
JsonContentPolymorphicSerializer<ItemResponse>(ItemResponse::class) { | ||
override fun selectDeserializer(element: JsonElement) = when { | ||
element is JsonNull -> ItemResponse.NullResponse.serializer() | ||
else -> Item.serializer() | ||
} | ||
} | ||
|
||
sealed class FeedIdResponse(val page: Page) { | ||
class Success(page: Page) : FeedIdResponse(page) | ||
data class Error(val message: String) : FeedIdResponse(emptyList()) | ||
} | ||
|
||
class HackerNewsBaseClient( | ||
json: Json, | ||
client: OkHttpClient, | ||
) { | ||
private val retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_FIREBASE_URL) | ||
.addConverterFactory(json.asConverterFactory("application/json; charset=UTF8".toMediaType())) | ||
.client(client) | ||
.build() | ||
|
||
private val api = retrofit.create(HackerNewsBaseApi::class.java) | ||
|
||
suspend fun getFeedIds(type: FeedType): FeedIdResponse { | ||
return withContext(Dispatchers.IO) { | ||
when (type) { | ||
FeedType.Top -> { | ||
try { | ||
val result = api.getTopStoryIds() | ||
FeedIdResponse.Success(result) | ||
} catch (error: Exception) { | ||
FeedIdResponse.Error(error.message.orEmpty()) | ||
} | ||
} | ||
|
||
FeedType.New -> { | ||
try { | ||
val result = api.getNewStoryIds() | ||
FeedIdResponse.Success(result) | ||
} catch (error: Exception) { | ||
FeedIdResponse.Error(error.message.orEmpty()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun getPage(page: Page): List<Item> { | ||
Log.d("Feed", "Loading Page: $page") | ||
return withContext(Dispatchers.IO) { | ||
val result = mutableListOf<Item>() | ||
page.forEach { itemId -> | ||
try { | ||
val response = api.getItem(itemId) | ||
if (response is Item) { | ||
result.add(response) | ||
} | ||
} catch (_: Exception) { | ||
} | ||
} | ||
result.toList() | ||
} | ||
} | ||
|
||
suspend fun getItem(itemId: Long): ItemResponse { | ||
return withContext(Dispatchers.IO) { | ||
api.getItem(itemId) | ||
} | ||
} | ||
} | ||
|
||
fun MutableList<Page>.next() = removeAt(0) | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...hackernews/data/HackerNewsSearchClient.kt → ...ews/data/remote/HackerNewsSearchClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.