-
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.
Merge pull request #87 from everymeals/feature/univ_save
[feature/univ_save] 대학 정보 저장 및 불러오기 구현
- Loading branch information
Showing
27 changed files
with
298 additions
and
23 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/everymeal/everymeal_android/di/DataBaseModule.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,27 @@ | ||
package com.everymeal.everymeal_android.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStoreFile | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataBaseModule { | ||
|
||
private const val DATASTORE_NAME = "everymeal_datastore" | ||
|
||
@Singleton | ||
@Provides | ||
fun providePreferencesDataStore(@ApplicationContext context: Context): DataStore<Preferences> = | ||
PreferenceDataStoreFactory.create( | ||
produceFile = { context.preferencesDataStoreFile(DATASTORE_NAME) } | ||
) | ||
} |
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
9 changes: 9 additions & 0 deletions
9
data/src/main/java/com/everymeal/data/datasource/local/LocalDataSource.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 com.everymeal.data.datasource.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface LocalDataSource { | ||
suspend fun saveUniversity(index : Int, univName : String) | ||
|
||
suspend fun getUniversityIndex() : Flow<String> | ||
} |
43 changes: 43 additions & 0 deletions
43
data/src/main/java/com/everymeal/data/datasource/local/LocalDataSourceImpl.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,43 @@ | ||
package com.everymeal.data.datasource.local | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
object DataStoreKey { | ||
val UNIVERSITY_INDEX = stringPreferencesKey("univ_index") | ||
val UNIVERSITY_NAME = stringPreferencesKey("univ_name") | ||
} | ||
|
||
class LocalDataSourceImpl @Inject constructor( | ||
private val dataStore: DataStore<Preferences> | ||
) : LocalDataSource { | ||
override suspend fun saveUniversity(index: Int, univName: String) { | ||
dataStore.edit { | ||
it[DataStoreKey.UNIVERSITY_INDEX] = index.toString() | ||
it[DataStoreKey.UNIVERSITY_NAME] = univName | ||
} | ||
} | ||
|
||
override suspend fun getUniversityIndex(): Flow<String> { | ||
return dataStore.data | ||
.catch { exception -> | ||
if (exception is IOException) { | ||
exception.printStackTrace() | ||
emit(emptyPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
.map { prefs -> | ||
prefs[DataStoreKey.UNIVERSITY_INDEX].orEmpty() | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSource.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,7 +1,7 @@ | ||
package com.everymeal.data.datasource.onboarding | ||
|
||
import com.everymeal.data.model.onboarding.UniversityData | ||
import com.everymeal.data.model.onboarding.UniversityResponse | ||
|
||
interface OnboardingDataSource { | ||
suspend fun getUniversity(): Result<List<UniversityData>> | ||
suspend fun getUniversity(): Result<List<UniversityResponse>> | ||
} |
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
19 changes: 19 additions & 0 deletions
19
data/src/main/java/com/everymeal/data/repository/local/LocalRepositoryImpl.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,19 @@ | ||
package com.everymeal.data.repository.local | ||
|
||
import com.everymeal.data.datasource.local.LocalDataSource | ||
import com.everymeal.domain.repository.local.LocalRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
class LocalRepositoryImpl @Inject constructor( | ||
private val localDataSource: LocalDataSource | ||
) : LocalRepository { | ||
|
||
override suspend fun saveUniversity(index: Int, univName: String) { | ||
localDataSource.saveUniversity(index, univName) | ||
} | ||
|
||
override suspend fun getUniversityIndex(): Flow<String> { | ||
return localDataSource.getUniversityIndex() | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.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,11 +1,11 @@ | ||
package com.everymeal.data.service.onboarding | ||
|
||
import com.everymeal.data.model.BaseResponse | ||
import com.everymeal.data.model.onboarding.UniversityData | ||
import com.everymeal.data.model.onboarding.UniversityResponse | ||
import retrofit2.http.GET | ||
|
||
interface OnboardingApi { | ||
|
||
@GET("/api/v1/universities") | ||
suspend fun getUniversity(): BaseResponse<List<UniversityData>> | ||
suspend fun getUniversity(): BaseResponse<List<UniversityResponse>> | ||
} |
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
domain/src/main/java/com/everymeal/domain/repository/local/LocalRepository.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 com.everymeal.domain.repository.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface LocalRepository { | ||
suspend fun saveUniversity(index : Int, univName : String) | ||
|
||
suspend fun getUniversityIndex() : Flow<String> | ||
} |
13 changes: 13 additions & 0 deletions
13
domain/src/main/java/com/everymeal/domain/usecase/local/GetUniversityIndexUseCase.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,13 @@ | ||
package com.everymeal.domain.usecase.local | ||
|
||
import com.everymeal.domain.repository.local.LocalRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
class GetUniversityIndexUseCase @Inject constructor( | ||
private val localRepository: LocalRepository | ||
) { | ||
suspend operator fun invoke() : Flow<String> { | ||
return localRepository.getUniversityIndex() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
domain/src/main/java/com/everymeal/domain/usecase/local/SaveUniversityUseCase.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,16 @@ | ||
package com.everymeal.domain.usecase.local | ||
|
||
import com.everymeal.domain.repository.local.LocalRepository | ||
import com.everymeal.domain.repository.onboarding.OnboardingRepository | ||
import javax.inject.Inject | ||
|
||
class SaveUniversityUseCase @Inject constructor( | ||
private val localRepository: LocalRepository | ||
) { | ||
suspend operator fun invoke( | ||
index : Int, | ||
univName : String | ||
) { | ||
localRepository.saveUniversity(index, univName) | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
domain/src/main/java/com/everymeal/domain/usecase/onboarding/GetUniversityUseCase.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,10 +1,13 @@ | ||
package com.everymeal.domain.usecase.onboarding | ||
|
||
import com.everymeal.domain.model.onboarding.GetUniversityEntity | ||
import com.everymeal.domain.repository.onboarding.OnboardingRepository | ||
import javax.inject.Inject | ||
|
||
class GetUniversityUseCase @Inject constructor( | ||
private val onboardingRepository: OnboardingRepository | ||
) { | ||
suspend operator fun invoke() = onboardingRepository.getUniversity() | ||
suspend operator fun invoke() :Result<GetUniversityEntity> { | ||
return onboardingRepository.getUniversity() | ||
} | ||
} |
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
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.