Skip to content

Commit

Permalink
Migrate to FavoritesRepository2 (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
SIKV authored Jul 10, 2024
1 parent d59671a commit a68e933
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 323 deletions.
14 changes: 0 additions & 14 deletions data/src/main/java/com/github/sikv/photos/data/Extensions.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.sikv.photos.data.repository

import com.github.sikv.photos.data.repository.impl.FavoritesRepository2Impl
import com.github.sikv.photos.data.repository.impl.FavoritesRepositoryImpl
import com.github.sikv.photos.data.repository.impl.PhotosRepositoryImpl
import dagger.Binds
import dagger.Module
Expand All @@ -15,9 +14,6 @@ abstract class RepositoryModule {
@Binds
abstract fun bindPhotosRepository(photosRepository: PhotosRepositoryImpl): PhotosRepository

@Binds
abstract fun bindFavoritesRepository(favoritesRepository: FavoritesRepositoryImpl): FavoritesRepository

@Binds
abstract fun bindFavorites2Repository(favoritesRepository: FavoritesRepository2Impl): FavoritesRepository2
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import com.github.sikv.photos.data.SortBy
import com.github.sikv.photos.data.persistence.FavoritePhotoEntity
import com.github.sikv.photos.data.persistence.FavoritesDao
import com.github.sikv.photos.data.persistence.FavoritesDbQueryBuilder
import com.github.sikv.photos.data.repository.FavoritesRepository
import com.github.sikv.photos.data.repository.FavoritesRepository2
import com.github.sikv.photos.domain.Photo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -44,9 +38,7 @@ class FavoritesRepository2Impl @Inject constructor(

override fun getFavorites(sortBy: SortBy): Flow<List<FavoritePhotoEntity>> {
val query = queryBuilder.buildGetPhotosQuery(sortBy)
return favoritesDao.getPhotos(query).map { photos ->
photos.onEach { it.favorite = true }
}
return favoritesDao.getPhotos(query)
}

override fun getRandom(): FavoritePhotoEntity? = favoritesDao.getRandom()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.github.sikv.photos.data.repository.impl

import com.github.sikv.photos.api.client.ApiClient
import com.github.sikv.photos.data.repository.FavoritesRepository
import com.github.sikv.photos.data.repository.PhotosRepository
import com.github.sikv.photos.domain.Photo
import com.github.sikv.photos.domain.PhotoSource
import javax.inject.Inject

class PhotosRepositoryImpl @Inject constructor(
private val apiClient: ApiClient,
private val favoritesRepository: FavoritesRepository
) : PhotosRepository {

override suspend fun getPhoto(id: String, source: PhotoSource): Photo? {
Expand All @@ -18,18 +16,13 @@ class PhotosRepositoryImpl @Inject constructor(
PhotoSource.UNSPLASH -> apiClient.unsplashClient.getPhoto(id)
PhotoSource.PIXABAY -> apiClient.pixabayClient.getPhoto(id).hits.firstOrNull()
PhotoSource.UNSPECIFIED -> throw NotImplementedError()
}?.apply {
favoritesRepository.populateFavorite(this)
}
}

override suspend fun getCuratedPhotos(page: Int, perPage: Int): List<Photo> {
return apiClient.pexelsClient
.getCuratedPhotos(page + getPageNumberComplement(PhotoSource.PEXELS), perPage)
.photos
.onEach { photo ->
favoritesRepository.populateFavorite(photo)
}
}

override suspend fun searchPhotos(
Expand All @@ -54,8 +47,6 @@ class PhotosRepositoryImpl @Inject constructor(
.hits

PhotoSource.UNSPECIFIED -> throw NotImplementedError()
}.onEach { photo ->
favoritesRepository.populateFavorite(photo)
}
}

Expand Down
5 changes: 0 additions & 5 deletions domain/src/main/java/com/github/sikv/photos/domain/Photo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import android.os.Parcelable

abstract class Photo : Parcelable {

/**
* This SHOULD NOT be used directly. Use FavoritesManager#isFavorite(Photo) instead.
*/
var favorite: Boolean = false

abstract fun getPhotoId(): String

abstract fun getPhotoPreviewUrl(): String
Expand Down
3 changes: 3 additions & 0 deletions feature/photo-details/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ dependencies {
implementation project(":common")
implementation project(":common-ui")
implementation project(':navigation')
implementation project(':photo-usecase')

implementation libs.androidx.fragment
implementation libs.androidx.lifecycle.viewmodel
implementation libs.androidx.lifecycle.runtime.compose
implementation libs.androidx.compose.material3
implementation libs.accompanist.themeadapter.material3

implementation libs.inject
kapt libs.hilt.compiler
implementation libs.hilt.android
implementation libs.androidx.hilt.navigation.compose
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material3.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.github.sikv.photos.common.ui.openUrl
import com.github.sikv.photos.data.createShareIntent
import com.github.sikv.photos.domain.Photo
import com.github.sikv.photos.navigation.args.SetWallpaperFragmentArguments
import com.github.sikv.photos.navigation.route.SetWallpaperRoute
import androidx.navigation.findNavController
import com.github.sikv.photo.usecase.PhotoActionsUseCase
import com.google.accompanist.themeadapter.material3.Mdc3Theme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
Expand All @@ -23,9 +17,7 @@ import javax.inject.Inject
class PhotoDetailsFragment : Fragment() {

@Inject
lateinit var setWallpaperRoute: SetWallpaperRoute

private val viewModel: PhotoDetailsViewModel by viewModels()
lateinit var photoActionsUseCase: PhotoActionsUseCase

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -36,43 +28,26 @@ class PhotoDetailsFragment : Fragment() {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)

setContent {
Mdc3Theme {
Surface {
val uiState = viewModel.uiState.collectAsState()

when (val state = uiState.value) {
PhotoUiState.NoData -> {
// Don't need to handle NoData state.
PhotoDetailsScreen(
onBackClick = {
findNavController().popBackStack()
},
onPhotoAttributionClick = photoActionsUseCase::photoAttributionClick,
onSharePhotoClick = { photo ->
photoActionsUseCase.sharePhoto(requireNotNull(activity), photo)
},
onDownloadPhotoClick = { photo ->
photoActionsUseCase.downloadPhoto(requireNotNull(activity), photo)
},
onSetWallpaperClick = { photo ->
photoActionsUseCase.setWallpaper(requireNotNull(activity), photo)
}
is PhotoUiState.Ready -> {
PhotoDetailsScreen(
photo = state.photo,
onBackPressed = { findNavController().popBackStack() },
isFavorite = state.isFavorite,
onToggleFavorite = { viewModel.toggleFavorite() },
onSharePressed = { sharePhoto(state.photo) },
onDownloadPressed = { viewModel.downloadPhoto() },
onSetWallpaperPressed = { setWallpaper(state.photo) },
onAttributionPressed = { openAttribution(state.photo) }
)
}
}
)
}
}
}
}

private fun sharePhoto(photo: Photo) {
startActivity(photo.createShareIntent())
}

private fun setWallpaper(photo: Photo) {
setWallpaperRoute.present(childFragmentManager, SetWallpaperFragmentArguments(photo))
}

private fun openAttribution(photo: Photo) {
requireContext().openUrl(photo.getPhotoShareUrl())
}
}
Loading

0 comments on commit a68e933

Please sign in to comment.