Skip to content

Commit

Permalink
WIPˆ
Browse files Browse the repository at this point in the history
  • Loading branch information
jageishi committed Apr 6, 2020
1 parent 10b6315 commit a6520fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ package org.ageage.eggplant.bookmarks
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.ageage.eggplant.common.enums.SortType
import org.ageage.eggplant.common.model.Bookmark
import org.ageage.eggplant.common.repository.BookmarkRepository
import org.ageage.eggplant.common.schedulerprovider.BaseSchedulerProvider

class BookmarksViewModel(
private val repository: BookmarkRepository,
private val schedulerProvider: BaseSchedulerProvider
private val repository: BookmarkRepository
) : ViewModel() {

private val disposable = CompositeDisposable()

private val _isLoading = MutableLiveData<Boolean>()
val isLoading: MutableLiveData<Boolean>
get() = _isLoading
Expand All @@ -27,43 +23,32 @@ class BookmarksViewModel(

private var isAlreadyLoaded = false

override fun onCleared() {
super.onCleared()
disposable.clear()
}

fun loadBookmarks(url: String, sortType: SortType, forceLoad: Boolean = false) {
if (isAlreadyLoaded && !forceLoad) {
return
}

_isLoading.value = true
repository
.fetchBookmarks(url)
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe({ bookmarkList ->
val sortedBookmarks = when (sortType) {
SortType.POPULAR -> {
bookmarkList
.filter { bookmark -> bookmark.comment.isNotEmpty() }
.sortedByDescending { bookmark -> bookmark.yellowStarNumber }
.take(10)
}
SortType.RECENT -> {
bookmarkList
.filter { bookmark -> bookmark.comment.isNotEmpty() }
.sortedByDescending { bookmark -> bookmark.timeStamp }
viewModelScope.launch {
_isLoading.value = true
repository
.fetchBookmarks(url).let{ bookmarkList ->
val sortedBookmarks = when (sortType) {
SortType.POPULAR -> {
bookmarkList
.filter { bookmark -> bookmark.comment.isNotEmpty() }
.sortedByDescending { bookmark -> bookmark.yellowStarNumber }
.take(10)
}
SortType.RECENT -> {
bookmarkList
.filter { bookmark -> bookmark.comment.isNotEmpty() }
.sortedByDescending { bookmark -> bookmark.timeStamp }
}
}
_bookmarks.value = sortedBookmarks
_isLoading.value = false
isAlreadyLoaded = true
}

_bookmarks.value = sortedBookmarks
_isLoading.value = false
isAlreadyLoaded = true

}, {
_isLoading.value = false
})
.addTo(disposable)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.ageage.eggplant.bookmarks
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import org.ageage.eggplant.common.repository.BookmarkRepository
import org.ageage.eggplant.common.schedulerprovider.SchedulerProvider

class BookmarksViewModelFactory : ViewModelProvider.Factory {

Expand All @@ -14,8 +13,7 @@ class BookmarksViewModelFactory : ViewModelProvider.Factory {
}

return BookmarksViewModel(
BookmarkRepository(),
SchedulerProvider()
BookmarkRepository()
) as T
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.ageage.eggplant.common.repository

import android.text.format.DateFormat
import io.reactivex.Observable
import io.reactivex.rxkotlin.toObservable
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 @@ -13,14 +11,13 @@ import java.util.*

class BookmarkRepository {

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

return service.bookmarkEntry(url)
.let { bookmarkEntry ->
bookmarkEntry.bookmarkResponses
service.bookmarkEntry(url).let { bookmarkEntry ->
return bookmarkEntry.bookmarkResponses
.filter {
it.comment.isNotEmpty()
}
Expand All @@ -36,8 +33,7 @@ class BookmarkRepository {
Client.retrofitClient(Endpoint.HATENA_STAR)
.create(BookmarkService::class.java)
.startCount("${Endpoint.HATENA_BOOKMARK.url}/${bookmark.user}/${timestamp}#bookmark-${bookmarkEntry.eid}")
}
.map { responses ->
}.let { responses ->
bookmarkEntry.bookmarkResponses
.filter {
it.comment.isNotEmpty()
Expand All @@ -47,7 +43,6 @@ class BookmarkRepository {
}
bookmarkEntry.bookmarkResponses.toBookmarks()
}
.toObservable()
}
}
}

0 comments on commit a6520fa

Please sign in to comment.