Skip to content

Commit

Permalink
details 개선사항 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseoRyu committed Jan 23, 2025
1 parent e108e13 commit dab15f6
Show file tree
Hide file tree
Showing 28 changed files with 207 additions and 210 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:name=".ui.main.App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -16,7 +16,7 @@
android:theme="@style/Theme.Deepsleep"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".ui.main.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.flab.deepsleep.data.api

import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.unplash.SinglePhoto

import com.flab.deepsleep.data.entity.photos.SearchPhotos
import com.flab.deepsleep.data.entity.unplash.SearchPhotos
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash

import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
data class Exif(
@SerializedName("aperture")
val aperture: String?,
Expand All @@ -21,4 +17,4 @@ data class Exif(
val model: String?,
@SerializedName("name")
val name: String?
) : Parcelable
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash
import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash
import com.google.gson.annotations.SerializedName

data class SearchPhotos(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash

import android.os.Parcelable
import com.flab.deepsleep.data.entity.room.Photo
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
data class SinglePhoto(
@SerializedName("blur_hash")
val blurHash: String?,
Expand Down Expand Up @@ -36,17 +32,5 @@ data class SinglePhoto(
@SerializedName("urls")
val urls: Urls?,
@SerializedName("user")
val user: User?,
var isLike: Boolean = false
) : Parcelable

fun SinglePhoto.toPhoto(): Photo {
return Photo(
id = this.id,
likes = this.likes,
urls = this.urls?.raw,
createdAt = this.createdAt,
username = this.user?.username,
isLike = this.isLike
)
}
val user: User?
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash

import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
data class Urls(
@SerializedName("full")
val full: String?,
Expand All @@ -16,4 +13,4 @@ data class Urls(
val small: String?,
@SerializedName("thumb")
val thumb: String?
) : Parcelable
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.flab.deepsleep.data.entity.photos
package com.flab.deepsleep.data.entity.unplash

import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
data class User(
@SerializedName("bio")
val bio: String?,
Expand All @@ -26,4 +23,4 @@ data class User(
val updatedAt: String?,
@SerializedName("username")
val username: String?
) : Parcelable
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package com.flab.deepsleep.data.repository.db

import com.flab.deepsleep.data.entity.room.Photo
import com.flab.deepsleep.data.entity.room.PhotoDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext

class OffLinePhotoRepository(private val photoDao: PhotoDao) : PhotoRepository {
override fun getAllPhotos(): Flow<List<Photo>> = photoDao.getAll()
override fun getSinglePhoto(id: String): Flow<Photo?> = photoDao.getPhoto(id)
override suspend fun insertPhoto(photo: Photo) = photoDao.insert(photo)
override suspend fun deletePhoto(photo: Photo) = photoDao.delete(photo)
override suspend fun insertPhoto(photo: Photo) =
withContext(Dispatchers.IO) { photoDao.insert(photo) }

override suspend fun deletePhoto(photo: Photo) =
withContext(Dispatchers.IO) { photoDao.delete(photo) }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.flab.deepsleep.data.repository.photo

import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.photos.SearchPhotos
import com.flab.deepsleep.data.entity.unplash.SinglePhoto
import com.flab.deepsleep.data.entity.unplash.SearchPhotos

interface UnplashRepository {
suspend fun getRandomPhotos(count: Int): List<SinglePhoto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.flab.deepsleep.data.repository.photo

import com.flab.deepsleep.BuildConfig
import com.flab.deepsleep.data.api.UnplashService
import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.photos.SearchPhotos
import com.flab.deepsleep.data.entity.unplash.SinglePhoto
import com.flab.deepsleep.data.entity.unplash.SearchPhotos
import javax.inject.Inject

class UnplashRepositoryImpl @Inject constructor(private val unplashService: UnplashService) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.flab.deepsleep.data.source

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.unplash.SinglePhoto

class PhotoPagingSource(
private val photos: List<SinglePhoto>
Expand Down
33 changes: 16 additions & 17 deletions app/src/main/java/com/flab/deepsleep/ui/details/DetailsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.flab.deepsleep.R
import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.databinding.ActivityDetailsBinding
import com.flab.deepsleep.ui.photo.PhotoViewModel
import com.flab.deepsleep.ui.photo.UiItem
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
import java.text.NumberFormat
Expand All @@ -20,26 +19,26 @@ class DetailsActivity : AppCompatActivity() {
private val detailsBinding: ActivityDetailsBinding by lazy {
ActivityDetailsBinding.inflate(layoutInflater)
}
private val photoViewModel: PhotoViewModel by viewModels()
private val singlePhoto: SinglePhoto? by lazy {
@Suppress("DEPRECATION") intent.getParcelableExtra<SinglePhoto>("singlePhoto")
private val detailsViewModel: DetailsViewModel by viewModels()
private val uiItem: UiItem? by lazy {
@Suppress("DEPRECATION") intent.getParcelableExtra<UiItem>("uiItem")
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(detailsBinding.root)

singlePhoto?.let {
loadImage(it.urls?.raw)
uiItem?.let {
loadImage(it.urls)
bindPhotoDetails(it)
/* 좋아요 표시 */
singlePhoto?.id?.let { photoViewModel.loadPhotoLikeStatus(it) }
uiItem?.id?.let { detailsViewModel.loadPhotoLikeStatus(it) }
} ?: run {
loadImage(null)
Timber.d("singlePhoto is null")
}

photoViewModel.isLiked.observe(this) { isLiked ->
detailsViewModel.isLiked.observe(this) { isLiked ->
detailsBinding.detailBtHeart.isSelected = isLiked
}
}
Expand All @@ -52,25 +51,25 @@ class DetailsActivity : AppCompatActivity() {
.into(detailsBinding.detailsImageView)
}

private fun bindPhotoDetails(singlePhoto: SinglePhoto) {
private fun bindPhotoDetails(uiItem: UiItem) {
val numberFormat = NumberFormat.getNumberInstance(Locale.KOREA)
val result = numberFormat.format(singlePhoto.likes)
val result = numberFormat.format(uiItem.likes)
detailsBinding.apply {
detailDescription.text = singlePhoto.description ?: "No description available"
detailCreateAt.text = singlePhoto.createdAt?.take(10) ?: "Unknown date"
detailDescription.text = uiItem.description ?: "No description available"
detailCreateAt.text = uiItem.createdAt?.take(10) ?: "Unknown date"
detailLikes.text = result
detailUsername.text = singlePhoto.user?.username
detailUsername.text = uiItem.username
}

detailsBinding.detailBtHeart.setOnClickListener {
photoViewModel.insertPhoto(singlePhoto)
detailsViewModel.insertPhoto(uiItem)
}
}

companion object {
fun startActivity(context: Context, singlePhoto: SinglePhoto) {
fun startActivity(context: Context, uiItem: UiItem) {
val intent = Intent(context, DetailsActivity::class.java).apply {
putExtra("singlePhoto", singlePhoto)
putExtra("uiItem", uiItem)
}
context.startActivity(intent)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.flab.deepsleep.ui.details

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.flab.deepsleep.data.repository.db.PhotoRepository
import com.flab.deepsleep.ui.photo.UiItem
import com.flab.deepsleep.ui.photo.toPhoto
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DetailsViewModel @Inject constructor(
private val photoRepository: PhotoRepository
) : ViewModel() {

/* Bookmark */
private val _isLiked = MutableLiveData<Boolean>()
val isLiked: LiveData<Boolean> get() = _isLiked

/* 즐겨찾기 추가 */
fun insertPhoto(uiItem: UiItem) {
viewModelScope.launch {
photoRepository.insertPhoto(uiItem.toPhoto())
}
}

fun loadPhotoLikeStatus(photoId: String) {
viewModelScope.launch {
photoRepository.getSinglePhoto(photoId).collectLatest { photo ->
photo?.let {
_isLiked.value = photo.isLike
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package com.flab.deepsleep
package com.flab.deepsleep.ui.main

import android.app.Application
import com.flab.deepsleep.utils.TimberDebugTree
import com.flab.deepsleep.BuildConfig
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber


@HiltAndroidApp
class MainApplication: Application(){

class App : Application() {
override fun onCreate() {
super.onCreate()

if (BuildConfig.DEBUG) {
// Timber Initialize
Timber.uprootAll()
Timber.plant(object : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
val threadName = Thread.currentThread().name
return "<$threadName> (${element.fileName}:${element.lineNumber})#${element.methodName} "
}
})

}
}
}

}
Loading

0 comments on commit dab15f6

Please sign in to comment.