-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
258 additions
and
38 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
android/app/src/main/java/net/pengcook/android/data/datasource/CategoryFeedPagingSource.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,33 @@ | ||
package net.pengcook.android.data.datasource | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import net.pengcook.android.presentation.core.model.Recipe | ||
|
||
class CategoryFeedPagingSource( | ||
private val initialPageNumber: Int = 0, | ||
private val category: String, | ||
private val fetchFeeds: suspend (pageNumber: Int, size: Int, category: String) -> Result<List<Recipe>>, | ||
) : PagingSource<Int, Recipe>() { | ||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Recipe> { | ||
val pageNumber = params.key ?: initialPageNumber | ||
return runCatching { | ||
val feeds = fetchFeeds(pageNumber, params.loadSize, category) | ||
val pageData = feeds.getOrNull() ?: emptyList() | ||
val nextKey = if (pageData.size < params.loadSize) null else pageNumber + 1 | ||
LoadResult.Page( | ||
data = pageData, | ||
prevKey = if (pageNumber == initialPageNumber) null else pageNumber - 1, | ||
nextKey = nextKey, | ||
) | ||
}.onFailure { throwable -> | ||
LoadResult.Error<Int, Recipe>(throwable) | ||
}.getOrThrow() | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Recipe>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ data class Category( | |
val id: Long, | ||
val title: String, | ||
val imageUrl: String, | ||
val code: String? = null, | ||
) |
2 changes: 1 addition & 1 deletion
2
...oid/app/src/main/java/net/pengcook/android/presentation/category/CategoryEventListener.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package net.pengcook.android.presentation.category | ||
|
||
interface CategoryEventListener { | ||
fun onCategorySelect(categoryId: Long) | ||
fun onCategorySelect(categoryCode: String) | ||
} |
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
14 changes: 13 additions & 1 deletion
14
android/app/src/main/java/net/pengcook/android/presentation/category/CategoryViewModel.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 |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package net.pengcook.android.presentation.category | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import net.pengcook.android.presentation.core.util.Event | ||
|
||
class CategoryViewModel : ViewModel(), CategoryEventListener { | ||
override fun onCategorySelect(categoryId: Long) { | ||
private val _uiEvent: MutableLiveData<Event<CategoryUiEvent>> = MutableLiveData() | ||
val uiEvent: LiveData<Event<CategoryUiEvent>> | ||
get() = _uiEvent | ||
|
||
override fun onCategorySelect(categoryCode: String) { | ||
_uiEvent.value = Event(CategoryUiEvent.NavigateToList(categoryCode)) | ||
} | ||
} | ||
|
||
sealed interface CategoryUiEvent { | ||
data class NavigateToList(val categoryCode: String) : CategoryUiEvent | ||
} |
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
9 changes: 9 additions & 0 deletions
9
.../src/main/java/net/pengcook/android/presentation/category/list/CategoryFeedListUiEvent.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,9 @@ | ||
package net.pengcook.android.presentation.category.list | ||
|
||
import net.pengcook.android.presentation.core.model.Recipe | ||
|
||
sealed interface CategoryFeedListUiEvent { | ||
data object NavigateBack : CategoryFeedListUiEvent | ||
|
||
data class NavigateToDetail(val recipe: Recipe) : CategoryFeedListUiEvent | ||
} |
44 changes: 43 additions & 1 deletion
44
...rc/main/java/net/pengcook/android/presentation/category/list/CategoryFeedListViewModel.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 |
---|---|---|
@@ -1,9 +1,51 @@ | ||
package net.pengcook.android.presentation.category.list | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.cachedIn | ||
import androidx.paging.liveData | ||
import net.pengcook.android.data.datasource.CategoryFeedPagingSource | ||
import net.pengcook.android.data.repository.feed.FeedRepository | ||
import net.pengcook.android.presentation.core.listener.AppbarActionEventListener | ||
import net.pengcook.android.presentation.core.model.Recipe | ||
import net.pengcook.android.presentation.core.util.Event | ||
import net.pengcook.android.presentation.home.listener.FeedItemEventListener | ||
|
||
class CategoryFeedListViewModel( | ||
private val feedRepository: FeedRepository, | ||
private val category: String, | ||
) : ViewModel(), AppbarActionEventListener, FeedItemEventListener { | ||
private val _uiEvent: MutableLiveData<Event<CategoryFeedListUiEvent>> = MutableLiveData() | ||
val uiEvent: LiveData<Event<CategoryFeedListUiEvent>> | ||
get() = _uiEvent | ||
|
||
val feedData: LiveData<PagingData<Recipe>> = | ||
Pager( | ||
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), | ||
pagingSourceFactory = { | ||
CategoryFeedPagingSource( | ||
category = category, | ||
fetchFeeds = feedRepository::fetchRecipesByCategory, | ||
) | ||
}, | ||
) | ||
.liveData | ||
.cachedIn(viewModelScope) | ||
|
||
class CategoryFeedListViewModel : ViewModel(), AppbarActionEventListener { | ||
override fun onNavigateBack() { | ||
_uiEvent.value = Event(CategoryFeedListUiEvent.NavigateBack) | ||
} | ||
|
||
override fun onNavigateToDetail(recipe: Recipe) { | ||
_uiEvent.value = Event(CategoryFeedListUiEvent.NavigateToDetail(recipe)) | ||
} | ||
|
||
companion object { | ||
private const val PAGE_SIZE = 10 | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../java/net/pengcook/android/presentation/category/list/CategoryFeedListViewModelFactory.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,17 @@ | ||
package net.pengcook.android.presentation.category.list | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import net.pengcook.android.data.repository.feed.FeedRepository | ||
|
||
class CategoryFeedListViewModelFactory( | ||
private val feedRepository: FeedRepository, | ||
private val category: String, | ||
) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(CategoryFeedListViewModel::class.java)) { | ||
return CategoryFeedListViewModel(feedRepository, category) as T | ||
} | ||
throw IllegalArgumentException() | ||
} | ||
} |
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
Oops, something went wrong.