@@ -38,135 +38,115 @@ private fun String.toLatLngList(): List<LatLng> =
3838 }
3939
4040object PreferenceDataStore {
41+
42+ private lateinit var appContext: Context
43+
44+ fun init (context : Context ) {
45+ appContext = context.applicationContext
46+ }
47+
48+ private val summaryStore
49+ get() = appContext.summaryStore
50+
51+
4152 suspend fun saveWalkSummary (
42- context : Context ,
4353 points : List <LatLng >,
4454 totalDistance : Float ,
4555 totalTime : Long ,
4656 totalSteps : Int ,
4757 ) {
48- context. summaryStore.edit { preferences ->
58+ summaryStore.edit { preferences ->
4959 preferences[POINTS_KEY ] = points.toPreferenceString()
5060 preferences[TOTAL_DISTANCE_KEY ] = totalDistance
5161 preferences[TOTAL_TIME_KEY ] = totalTime
5262 preferences[TOTAL_STEPS_KEY ] = totalSteps
5363 }
5464 }
5565
56- fun getPoints (context : Context ): Flow <List <LatLng >> {
57- return context.summaryStore.data.map { preferences ->
58- preferences[POINTS_KEY ]?.toLatLngList() ? : emptyList()
59- }
66+ fun getPoints (): Flow <List <LatLng >> = summaryStore.data.map {
67+ it[POINTS_KEY ]?.toLatLngList() ? : emptyList()
6068 }
6169
62- fun getTotalDistance (context : Context ): Flow <Float > {
63- return context.summaryStore.data.map { preferences ->
64- preferences[TOTAL_DISTANCE_KEY ] ? : 0f
65- }
70+ fun getTotalDistance (): Flow <Float > = summaryStore.data.map {
71+ it[TOTAL_DISTANCE_KEY ] ? : 0f
6672 }
6773
68- fun getTotalTime (context : Context ): Flow <Long > {
69- return context.summaryStore.data.map { preferences ->
70- preferences[TOTAL_TIME_KEY ] ? : 0L
71- }
74+ fun getTotalTime (): Flow <Long > = summaryStore.data.map {
75+ it[TOTAL_TIME_KEY ] ? : 0L
7276 }
7377
74- fun getTotalSteps (context : Context ): Flow <Int > {
75- return context.summaryStore.data.map { preferences ->
76- preferences[TOTAL_STEPS_KEY ] ? : 0
77- }
78+ fun getTotalSteps (): Flow <Int > = summaryStore.data.map {
79+ it[TOTAL_STEPS_KEY ] ? : 0
7880 }
7981
80- suspend fun clearWalkSummary (context : Context ) {
81- context. summaryStore.edit { preferences ->
82- preferences .remove(POINTS_KEY )
83- preferences .remove(TOTAL_DISTANCE_KEY )
84- preferences .remove(TOTAL_TIME_KEY )
85- preferences .remove(TOTAL_STEPS_KEY )
82+ suspend fun clearWalkSummary () {
83+ summaryStore.edit {
84+ it .remove(POINTS_KEY )
85+ it .remove(TOTAL_DISTANCE_KEY )
86+ it .remove(TOTAL_TIME_KEY )
87+ it .remove(TOTAL_STEPS_KEY )
8688 }
8789 }
8890
89- suspend fun saveLoginInfo (
90- context : Context ,
91- email : String ,
92- password : String
93- ) {
94- context.summaryStore.edit { preferences ->
95- preferences[LOGIN_EMAIL_KEY ] = email
96- preferences[LOGIN_PASSWORD_KEY ] = password
91+ suspend fun saveLoginInfo (email : String , password : String ) {
92+ summaryStore.edit {
93+ it[LOGIN_EMAIL_KEY ] = email
94+ it[LOGIN_PASSWORD_KEY ] = password
9795 }
9896 }
9997
100- fun getLoginEmail (context : Context ): Flow <String > {
101- return context.summaryStore.data.map { preferences ->
102- preferences[LOGIN_EMAIL_KEY ] ? : " "
103- }
98+ fun getLoginEmail (): Flow <String > = summaryStore.data.map {
99+ it[LOGIN_EMAIL_KEY ] ? : " "
104100 }
105101
106- fun getLoginPassword (context : Context ): Flow <String > {
107- return context.summaryStore.data.map { preferences ->
108- preferences[LOGIN_PASSWORD_KEY ] ? : " "
109- }
102+ fun getLoginPassword (): Flow <String > = summaryStore.data.map {
103+ it[LOGIN_PASSWORD_KEY ] ? : " "
110104 }
111105
112- data class LoginInfo (
113- val email : String ,
114- val password : String
115- )
106+ data class LoginInfo (val email : String , val password : String )
116107
117- fun getLoginInfo (context : Context ): Flow <LoginInfo > {
118- return context.summaryStore.data.map { preferences ->
119- LoginInfo (
120- email = preferences[LOGIN_EMAIL_KEY ] ? : " " ,
121- password = preferences[LOGIN_PASSWORD_KEY ] ? : " "
122- )
123- }
108+ fun getLoginInfo (): Flow <LoginInfo > = summaryStore.data.map {
109+ LoginInfo (
110+ email = it[LOGIN_EMAIL_KEY ] ? : " " ,
111+ password = it[LOGIN_PASSWORD_KEY ] ? : " "
112+ )
124113 }
125114
126- suspend fun clearLoginInfo (context : Context ) {
127- context. summaryStore.edit { preferences ->
128- preferences .remove(LOGIN_EMAIL_KEY )
129- preferences .remove(LOGIN_PASSWORD_KEY )
115+ suspend fun clearLoginInfo () {
116+ summaryStore.edit {
117+ it .remove(LOGIN_EMAIL_KEY )
118+ it .remove(LOGIN_PASSWORD_KEY )
130119 }
131120 }
132121
133122 suspend fun saveUserInfo (
134- context : Context ,
135123 userId : Int ,
136124 userName : String ,
137125 petId : Int ,
138126 petName : String
139127 ) {
140- context. summaryStore.edit { preferences ->
141- preferences [USER_ID_KEY ] = userId
142- preferences [USER_NAME_KEY ] = userName
143- preferences [PET_ID_KEY ] = petId
144- preferences [PET_NAME_KEY ] = petName
128+ summaryStore.edit {
129+ it [USER_ID_KEY ] = userId
130+ it [USER_NAME_KEY ] = userName
131+ it [PET_ID_KEY ] = petId
132+ it [PET_NAME_KEY ] = petName
145133 }
146134 }
147135
148- fun getUserId (context : Context ): Flow <Int > {
149- return context.summaryStore.data.map { preferences ->
150- preferences[USER_ID_KEY ] ? : 0
151- }
136+ fun getUserId (): Flow <Int > = summaryStore.data.map {
137+ it[USER_ID_KEY ] ? : 0
152138 }
153139
154- fun getUserName (context : Context ): Flow <String > {
155- return context.summaryStore.data.map { preferences ->
156- preferences[USER_NAME_KEY ] ? : " "
157- }
140+ fun getUserName (): Flow <String > = summaryStore.data.map {
141+ it[USER_NAME_KEY ] ? : " "
158142 }
159143
160- fun getPetId (context : Context ): Flow <Int > {
161- return context.summaryStore.data.map { preferences ->
162- preferences[PET_ID_KEY ] ? : 0
163- }
144+ fun getPetId (): Flow <Int > = summaryStore.data.map {
145+ it[PET_ID_KEY ] ? : 0
164146 }
165147
166- fun getPetName (context : Context ): Flow <String > {
167- return context.summaryStore.data.map { preferences ->
168- preferences[PET_NAME_KEY ] ? : " "
169- }
148+ fun getPetName (): Flow <String > = summaryStore.data.map {
149+ it[PET_NAME_KEY ] ? : " "
170150 }
171151
172152 data class UserInfo (
@@ -176,29 +156,25 @@ object PreferenceDataStore {
176156 val petName : String
177157 )
178158
179- fun getUserInfo (context : Context ): Flow <UserInfo > {
180- return context.summaryStore.data.map { preferences ->
181- UserInfo (
182- userId = preferences[USER_ID_KEY ] ? : 0 ,
183- userName = preferences[USER_NAME_KEY ] ? : " " ,
184- petId = preferences[PET_ID_KEY ] ? : 0 ,
185- petName = preferences[PET_NAME_KEY ] ? : " "
186- )
187- }
159+ fun getUserInfo (): Flow <UserInfo > = summaryStore.data.map {
160+ UserInfo (
161+ userId = it[USER_ID_KEY ] ? : 0 ,
162+ userName = it[USER_NAME_KEY ] ? : " " ,
163+ petId = it[PET_ID_KEY ] ? : 0 ,
164+ petName = it[PET_NAME_KEY ] ? : " "
165+ )
188166 }
189167
190- suspend fun clearUserInfo (context : Context ) {
191- context. summaryStore.edit { preferences ->
192- preferences .remove(USER_ID_KEY )
193- preferences .remove(USER_NAME_KEY )
194- preferences .remove(PET_ID_KEY )
195- preferences .remove(PET_NAME_KEY )
168+ suspend fun clearUserInfo () {
169+ summaryStore.edit {
170+ it .remove(USER_ID_KEY )
171+ it .remove(USER_NAME_KEY )
172+ it .remove(PET_ID_KEY )
173+ it .remove(PET_NAME_KEY )
196174 }
197175 }
198176
199- suspend fun clearAllData (context : Context ) {
200- context.summaryStore.edit { preferences ->
201- preferences.clear()
202- }
177+ suspend fun clearAllData () {
178+ summaryStore.edit { it.clear() }
203179 }
204180}
0 commit comments