-
Notifications
You must be signed in to change notification settings - Fork 1
6주차 미션_담담 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sungs25
wants to merge
8
commits into
main
Choose a base branch
from
damdam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
6주차 미션_담담 #21
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f497c79
first commit
df804f8
feat: myApplication 추가
e9a76be
feat: 레포지토리 & 레포지토리임플 구현
f8bd887
feat: module 구현
8de045e
feat: viewmodel 구현
397b539
feat: MainActivity + Fragment 4개 교체
051693f
refactor: 멘토 피드백 반영 (Adapter 통일, DataStore 클래스화, 패키지 세분화 등)
c2f20eb
Merge branch 'main' into damdam
sungs25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
98 changes: 98 additions & 0 deletions
98
damdam/UMC_Work_6th/app/src/main/java/com/sungs/myapplication/data/local/ProductDataStore.kt
This file contains hidden or 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,98 @@ | ||
| package com.sungs.myapplication.data.local | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.edit | ||
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import androidx.datastore.preferences.preferencesDataStore | ||
| import com.google.gson.Gson | ||
| import com.google.gson.reflect.TypeToken | ||
| import com.sungs.myapplication.data.model.ProductData | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "product_prefs") | ||
|
|
||
| @Singleton | ||
| class ProductDataStore @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| ) { | ||
|
|
||
| private val gson = Gson() | ||
|
|
||
| // 키 정의 | ||
| private companion object { | ||
| val HOME_PRODUCTS_KEY = stringPreferencesKey("home_products") | ||
| val SHOP_PRODUCTS_KEY = stringPreferencesKey("shop_products") | ||
| val FAVORITE_NAMES_KEY = stringPreferencesKey("favorite_names") | ||
| } | ||
|
|
||
| // ── 상품 리스트 저장/로드 ── | ||
|
|
||
| suspend fun saveHomeProducts(products: List<ProductData>) { | ||
| context.dataStore.edit { prefs -> | ||
| prefs[HOME_PRODUCTS_KEY] = gson.toJson(products) | ||
| } | ||
| } | ||
|
|
||
| fun getHomeProducts(): Flow<List<ProductData>> { | ||
| return context.dataStore.data.map { prefs -> | ||
| val json = prefs[HOME_PRODUCTS_KEY] | ||
| if (json != null) { | ||
| gson.fromJson(json, object : TypeToken<List<ProductData>>() {}.type) | ||
| } else emptyList() | ||
| } | ||
| } | ||
|
|
||
| suspend fun saveShopProducts(products: List<ProductData>) { | ||
| context.dataStore.edit { prefs -> | ||
| prefs[SHOP_PRODUCTS_KEY] = gson.toJson(products) | ||
| } | ||
| } | ||
|
|
||
| fun getShopProducts(): Flow<List<ProductData>> { | ||
| return context.dataStore.data.map { prefs -> | ||
| val json = prefs[SHOP_PRODUCTS_KEY] | ||
| if (json != null) { | ||
| gson.fromJson(json, object : TypeToken<List<ProductData>>() {}.type) | ||
| } else emptyList() | ||
| } | ||
| } | ||
|
|
||
| // ── 즐겨찾기(하트) 관리 ── | ||
|
|
||
| suspend fun saveFavorites(favoriteNames: Set<String>) { | ||
| context.dataStore.edit { prefs -> | ||
| prefs[FAVORITE_NAMES_KEY] = gson.toJson(favoriteNames.toList()) | ||
| } | ||
| } | ||
|
|
||
| fun getFavorites(): Flow<Set<String>> { | ||
| return context.dataStore.data.map { prefs -> | ||
| val json = prefs[FAVORITE_NAMES_KEY] | ||
| if (json != null) { | ||
| val list: List<String> = gson.fromJson(json, object : TypeToken<List<String>>() {}.type) | ||
| list.toSet() | ||
| } else emptySet() | ||
| } | ||
| } | ||
|
|
||
| suspend fun toggleFavorite(productName: String) { | ||
| context.dataStore.edit { prefs -> | ||
| val json = prefs[FAVORITE_NAMES_KEY] | ||
| val current: MutableSet<String> = if (json != null) { | ||
| val list: List<String> = gson.fromJson(json, object : TypeToken<List<String>>() {}.type) | ||
| list.toMutableSet() | ||
| } else mutableSetOf() | ||
|
|
||
| if (current.contains(productName)) current.remove(productName) | ||
| else current.add(productName) | ||
|
|
||
| prefs[FAVORITE_NAMES_KEY] = gson.toJson(current.toList()) | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
damdam/UMC_Work_6th/app/src/main/java/com/sungs/myapplication/data/model/ProductData.kt
|
sungs25 marked this conversation as resolved.
|
This file contains hidden or 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 com.sungs.myapplication.data.model | ||
|
|
||
| data class ProductData( | ||
| val name: String, | ||
| val category: String, | ||
| val price: String, | ||
| val imageResId: Int, | ||
| var isFavorite: Boolean = false | ||
| ) |
34 changes: 34 additions & 0 deletions
34
damdam/UMC_Work_6th/app/src/main/java/com/sungs/myapplication/data/remote/dto/UserDto.kt
This file contains hidden or 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,34 @@ | ||
| package com.sungs.myapplication.data.remote.dto | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| // GET /api/users/{id} 응답 | ||
| data class UserResponse( | ||
| val data: UserData, | ||
| val support: SupportData? = null | ||
| ) | ||
|
|
||
| // GET /api/users?page=N 응답 | ||
| data class UserListResponse( | ||
| val page: Int, | ||
| @SerializedName("per_page") val perPage: Int, | ||
| val total: Int, | ||
| @SerializedName("total_pages") val totalPages: Int, | ||
| val data: List<UserData>, | ||
| val support: SupportData? = null | ||
| ) | ||
|
|
||
| // 실제 유저 데이터 | ||
| data class UserData( | ||
| val id: Int, | ||
| val email: String, | ||
| @SerializedName("first_name") val firstName: String, | ||
| @SerializedName("last_name") val lastName: String, | ||
| val avatar: String | ||
| ) | ||
|
|
||
| // ReqRes 응답에 붙어오는 홍보 문구 | ||
| data class SupportData( | ||
| val url: String, | ||
| val text: String | ||
| ) |
20 changes: 9 additions & 11 deletions
20
...h/app/src/main/java/com/sungs/myapplication/data/repository/ProductLocalRepositoryImpl.kt
This file contains hidden or 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,37 +1,35 @@ | ||
| package com.sungs.myapplication.data.repository | ||
|
|
||
| import android.content.Context | ||
| import com.sungs.myapplication.data.ProductData | ||
| import com.sungs.myapplication.data.ProductDataStore | ||
| import com.sungs.myapplication.data.model.ProductData | ||
| import com.sungs.myapplication.data.local.ProductDataStore | ||
| import com.sungs.myapplication.domain.repository.ProductLocalRepository | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.Flow | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class ProductLocalRepositoryImpl @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| private val productDataStore: ProductDataStore | ||
| ) : ProductLocalRepository { | ||
|
|
||
| override fun getHomeProducts(): Flow<List<ProductData>> = | ||
| ProductDataStore.getHomeProducts(context) | ||
| productDataStore.getHomeProducts() | ||
|
|
||
|
sungs25 marked this conversation as resolved.
|
||
| override suspend fun saveHomeProducts(products: List<ProductData>) { | ||
| ProductDataStore.saveHomeProducts(context, products) | ||
| productDataStore.saveHomeProducts(products) | ||
| } | ||
|
|
||
| override fun getShopProducts(): Flow<List<ProductData>> = | ||
| ProductDataStore.getShopProducts(context) | ||
| productDataStore.getShopProducts() | ||
|
|
||
| override suspend fun saveShopProducts(products: List<ProductData>) { | ||
| ProductDataStore.saveShopProducts(context, products) | ||
| productDataStore.saveShopProducts(products) | ||
| } | ||
|
|
||
| override fun getFavorites(): Flow<Set<String>> = | ||
| ProductDataStore.getFavorites(context) | ||
| productDataStore.getFavorites() | ||
|
|
||
| override suspend fun toggleFavorite(productName: String) { | ||
| ProductDataStore.toggleFavorite(context, productName) | ||
| productDataStore.toggleFavorite(productName) | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...6th/app/src/main/java/com/sungs/myapplication/data/repository/UserRemoteRepositoryImpl.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...6th/app/src/main/java/com/sungs/myapplication/domain/repository/ProductLocalRepository.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...k_6th/app/src/main/java/com/sungs/myapplication/domain/repository/UserRemoteRepository.kt
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.