Skip to content

Commit

Permalink
経過コミット
Browse files Browse the repository at this point in the history
  • Loading branch information
jageishi committed May 12, 2020
1 parent 786a5d7 commit 4036c8c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6'

def okhttp_version = '4.2.0'
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

def retrofit_version = '2.6.1'
def retrofit_version = '2.8.1'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.ageage.eggplant.common.enums.SortType
import org.ageage.eggplant.common.model.Bookmark
Expand All @@ -30,11 +31,12 @@ class BookmarksViewModel(
return
}


viewModelScope.launch {
_isLoading.value = true
try {
_bookmarks.value =
repository.fetchBookmarks(url).let {
repository.fetchBookmarks(url).collect {
_bookmarks.value =
when (sortType) {
SortType.POPULAR -> {
it.filter { bookmark -> bookmark.comment.isNotEmpty() }
Expand All @@ -46,7 +48,7 @@ class BookmarksViewModel(
.sortedByDescending { bookmark -> bookmark.timeStamp }
}
}
}
}
isAlreadyLoaded = true
} catch (e: Throwable) {
// TODO エラー処理を記述する
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.ageage.eggplant.common.api

import kotlinx.coroutines.flow.Flow
import org.ageage.eggplant.common.api.response.BookmarkEntryResponse
import org.ageage.eggplant.common.api.response.BookmarkStarResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface BookmarkService {
@GET("/entry/jsonlite/")
suspend fun bookmarkEntry(@Query("url") url: String): BookmarkEntryResponse
fun bookmarkEntry(@Query("url") url: String): Flow<BookmarkEntryResponse>

@GET("/entry.json")
suspend fun startCount(@Query("uri") url: String): BookmarkStarResponse
fun startCount(@Query("uri") url: String): Flow<BookmarkStarResponse>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ageage.eggplant.common.repository

import android.text.format.DateFormat
import kotlinx.coroutines.flow.*
import org.ageage.eggplant.common.api.BookmarkService
import org.ageage.eggplant.common.api.Client
import org.ageage.eggplant.common.api.response.mapper.toBookmarks
Expand All @@ -11,18 +12,19 @@ import java.util.*

class BookmarkRepository {

suspend fun fetchBookmarks(url: String): List<Bookmark> {
fun fetchBookmarks(url: String): Flow<List<Bookmark>> {
val service =
Client.retrofitClient(Endpoint.HATENA_BOOKMARK)
.create(BookmarkService::class.java)

return service.bookmarkEntry(url)
.let { bookmarkEntry ->
.flatMapConcat { bookmarkEntry ->
bookmarkEntry.bookmarkResponses
.asFlow()
.filter {
it.comment.isNotEmpty()
}
.map { bookmark ->
.flatMapConcat { bookmark ->
val timestamp =
DateFormat.format(
"yyyyMMdd",
Expand All @@ -36,17 +38,17 @@ class BookmarkRepository {
.create(BookmarkService::class.java)
.startCount("${Endpoint.HATENA_BOOKMARK.url}/${bookmark.user}/${timestamp}#bookmark-${bookmarkEntry.eid}")
}
.let { responses ->
.map { responses ->
bookmarkEntry.bookmarkResponses
.filter {
it.comment.isNotEmpty()
}
.forEachIndexed { index, bookmarkResponse ->

bookmarkResponse.entry = responses[index].entries.elementAtOrNull(0)
}
bookmarkEntry.bookmarkResponses.toBookmarks()
}
}
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.31'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
Expand Down

0 comments on commit 4036c8c

Please sign in to comment.