Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ dependencies {
//data store (with flow)
implementation(libs.androidx.datastore.preferences)

// EncryptedSharedPreferences
implementation(libs.androidx.security.crypto)

// naver maps
implementation (libs.map.sdk)

Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/eatssu/android/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.eatssu.android

import android.app.Application
import android.content.Context
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import com.eatssu.android.domain.model.TokenState
Expand All @@ -23,9 +22,6 @@ import javax.inject.Inject
/** App: 앱이 살아있는 동안 공통 리소스 관리를 위한 클래스 */
@HiltAndroidApp
class App : Application(), Configuration.Provider {
companion object{
lateinit var appContext: Context //todo 이거 빼기
}

/** 앱 전체에서 사용할 수 있는 CoroutineScope(독립적인 공간을 만들어 안정성 높임)
* 자식 CoroutineScope가 취소되더라도 부모 CoroutineScope는 취소되지 않음
Expand All @@ -39,7 +35,6 @@ class App : Application(), Configuration.Provider {
super.onCreate()
FirebaseApp.initializeApp(this)

appContext = this
KakaoSdk.init(this,BuildConfig.KAKAO_NATIVE_APP_KEY)

if (BuildConfig.DEBUG) {
Expand Down
149 changes: 0 additions & 149 deletions app/src/main/java/com/eatssu/android/data/MySharedPreferences.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.eatssu.android.data.local

import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.eatssu.android.domain.model.College
import com.eatssu.android.domain.model.Department
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

private val Context.accountDataStore by preferencesDataStore(name = "account")

@Singleton
class AccountDataStore @Inject constructor(
@ApplicationContext private val context: Context
) {
companion object Keys {
val EMAIL = stringPreferencesKey("MY_EMAIL")
val NAME = stringPreferencesKey("MY_NAME")
val COLLEGE_ID = intPreferencesKey("MY_COLLEGE_ID")
val COLLEGE_NAME = stringPreferencesKey("MY_COLLEGE")
val DEPT_ID = intPreferencesKey("MY_DEPARTMENT_ID")
val DEPT_NAME = stringPreferencesKey("MY_DEPARTMENT")
}

val email: Flow<String> = context.accountDataStore.data.map { it[EMAIL].orEmpty() }
suspend fun setEmail(v: String) = context.accountDataStore.edit { it[EMAIL] = v }

val name: Flow<String> = context.accountDataStore.data.map { it[NAME].orEmpty() }
suspend fun setName(v: String) = context.accountDataStore.edit { it[NAME] = v }

val college: Flow<College?> = context.accountDataStore.data.map {
val id = it[COLLEGE_ID] ?: -1
val name = it[COLLEGE_NAME]
if (id >= 0 && !name.isNullOrBlank()) College(id, name) else null
}

suspend fun setCollege(v: College) = context.accountDataStore.edit {
it[COLLEGE_ID] = v.collegeId
it[COLLEGE_NAME] = v.collegeName
}

val department: Flow<Department?> = context.accountDataStore.data.map {
val id = it[DEPT_ID] ?: -1
val name = it[DEPT_NAME]
if (id >= 0 && !name.isNullOrBlank()) Department(id, name) else null
}

suspend fun setDepartment(v: Department) = context.accountDataStore.edit {
it[DEPT_ID] = v.departmentId
it[DEPT_NAME] = v.departmentName
}

suspend fun clear() = context.accountDataStore.edit { it.clear() }
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
// PreferencesRepository.kt
package com.eatssu.android.data.repository
package com.eatssu.android.data.local

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class PreferencesRepository(private val context: Context) {
private val Context.settingDataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
class SettingDataStore @Inject constructor(
@ApplicationContext private val context: Context
) {

companion object {
private val DAILY_NOTIFICATION_KEY = booleanPreferencesKey("daily_notification")
}

val dailyNotificationStatus: Flow<Boolean> = context.dataStore.data
val dailyNotificationStatus: Flow<Boolean> = context.settingDataStore.data
.map { preferences ->
preferences[DAILY_NOTIFICATION_KEY] ?: false // Default value is false
}

suspend fun setDailyNotificationStatus(status: Boolean) {
context.dataStore.edit { preferences ->
context.settingDataStore.edit { preferences ->
preferences[DAILY_NOTIFICATION_KEY] = status
}
}

suspend fun clear() = context.settingDataStore.edit { it.clear() }
}
26 changes: 26 additions & 0 deletions app/src/main/java/com/eatssu/android/data/local/TokenStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.eatssu.android.data.local

import android.content.SharedPreferences
import androidx.core.content.edit
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TokenStore @Inject constructor(
private val securePrefs: SharedPreferences
) {
private companion object {
const val KEY_ACCESS = "ACCESS_TOKEN"
const val KEY_REFRESH = "REFRESH_TOKEN"
}

var accessToken: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로그인, 세션 관련 처리할 때 token이 null인지가 아니라 empty인지를 확인하는 부분 볼 때마다 ""인지 null인지 사람이 개발하다 헷갈려서 미래에 문제가 생길 수도 있겠다 생각했는데, 이번 기회에 바꾸면 좋을 것 같아요. 어떻게 생각하시나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullorempty를 검사하면 되지 않을까요??

제훈님 말씀은 ""을 저장하지 않게 하자 그리고 null 아니면 토큰만 반환하도록 하자인가용?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullOrEmpty를 검사하면 되긴 하지만 ""라는 빈 공백 값이 가능한 상황은 최대한 줄이면 좋다고 생각했어요! 말씀해주신대로 Token 아니면 null을 반환해 ""를 쓰지 말자가 맞습니다!

get() = securePrefs.getString(KEY_ACCESS, "").orEmpty()
set(v) = securePrefs.edit { putString(KEY_ACCESS, v) }

var refreshToken: String
get() = securePrefs.getString(KEY_REFRESH, "").orEmpty()
set(v) = securePrefs.edit { putString(KEY_REFRESH, v) }

fun clear() = securePrefs.edit { remove(KEY_ACCESS); remove(KEY_REFRESH) }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.repository
package com.eatssu.android.data.local

import android.content.Context
import androidx.datastore.core.DataStore
Expand All @@ -7,16 +7,18 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.eatssu.common.enums.Restaurant
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.first
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

private val Context.widgetPrefsDataStore: DataStore<Preferences> by preferencesDataStore(name = "widget_prefs")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위젯용 data store을 건들이지는 않아서 기존 바탕화면에 적재한 위젯에 영향은 없을듯합니다!


@Singleton
class WidgetPreferencesRepository @Inject constructor(
private val context: Context,
class WidgetDataStore @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val Context.widgetPrefsDataStore: DataStore<Preferences> by preferencesDataStore(name = "widget_prefs")
private fun fileKeyRestaurantKey(fileKey: String) =
stringPreferencesKey("widget_restaurant_by_fileKey_$fileKey")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

data class ChangeNicknameRequest(
val nickname: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

data class CheckValidTokenRequest(
val token: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

data class UserDepartmentRequest(
val departmentId: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eatssu.android.data.dto.request
package com.eatssu.android.data.remote.dto.request

import com.google.gson.annotations.SerializedName

Expand Down
Loading