Skip to content

Commit a2f2c65

Browse files
committed
fix a bunch of stuff
1 parent f22114f commit a2f2c65

File tree

25 files changed

+152
-90
lines changed

25 files changed

+152
-90
lines changed

app/src/main/java/com/codingwithmitch/openapi/di/auth/AuthModule.kt

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.codingwithmitch.openapi.di.auth
22

3-
import android.content.SharedPreferences
43
import com.codingwithmitch.openapi.api.auth.OpenApiAuthService
4+
import com.codingwithmitch.openapi.interactors.auth.Login
5+
import com.codingwithmitch.openapi.interactors.auth.Register
6+
import com.codingwithmitch.openapi.interactors.session.CheckPreviousAuthUser
7+
import com.codingwithmitch.openapi.interactors.session.Logout
58
import com.codingwithmitch.openapi.persistence.account.AccountDao
69
import com.codingwithmitch.openapi.persistence.auth.AuthTokenDao
7-
import com.codingwithmitch.openapi.repository.auth.AuthRepository
8-
import com.codingwithmitch.openapi.repository.auth.AuthRepositoryImpl
9-
import com.codingwithmitch.openapi.session.SessionManager
1010
import dagger.Module
1111
import dagger.Provides
1212
import dagger.hilt.InstallIn
@@ -30,25 +30,53 @@ object AuthModule{
3030

3131
@Singleton
3232
@Provides
33-
fun provideAuthRepository(
34-
sessionManager: SessionManager,
33+
fun provideCheckPrevAuthUser(
34+
accountDao: AccountDao,
3535
authTokenDao: AuthTokenDao,
36+
): CheckPreviousAuthUser {
37+
return CheckPreviousAuthUser(
38+
accountDao,
39+
authTokenDao
40+
)
41+
}
42+
43+
@Singleton
44+
@Provides
45+
fun provideLogin(
46+
service: OpenApiAuthService,
3647
accountDao: AccountDao,
37-
openApiAuthService: OpenApiAuthService,
38-
preferences: SharedPreferences,
39-
editor: SharedPreferences.Editor
40-
): AuthRepository {
41-
return AuthRepositoryImpl(
42-
authTokenDao,
48+
authTokenDao: AuthTokenDao,
49+
): Login {
50+
return Login(
51+
service,
4352
accountDao,
44-
openApiAuthService,
45-
sessionManager,
46-
preferences,
47-
editor
53+
authTokenDao
4854
)
4955
}
5056

57+
@Singleton
58+
@Provides
59+
fun provideLogout(
60+
service: OpenApiAuthService,
61+
accountDao: AccountDao,
62+
authTokenDao: AuthTokenDao,
63+
): Logout {
64+
return Logout(authTokenDao)
65+
}
5166

67+
@Singleton
68+
@Provides
69+
fun provideRegister(
70+
service: OpenApiAuthService,
71+
accountDao: AccountDao,
72+
authTokenDao: AuthTokenDao,
73+
): Register {
74+
return Register(
75+
service,
76+
accountDao,
77+
authTokenDao
78+
)
79+
}
5280
}
5381

5482

app/src/main/java/com/codingwithmitch/openapi/interactors/auth/Login.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.codingwithmitch.openapi.models.AuthToken
66
import com.codingwithmitch.openapi.persistence.account.AccountDao
77
import com.codingwithmitch.openapi.persistence.account.toEntity
88
import com.codingwithmitch.openapi.persistence.auth.AuthTokenDao
9+
import com.codingwithmitch.openapi.persistence.auth.toEntity
910
import com.codingwithmitch.openapi.util.*
1011
import com.codingwithmitch.openapi.util.ErrorHandling.Companion.ERROR_SAVE_AUTH_TOKEN
1112
import kotlinx.coroutines.flow.Flow
@@ -22,6 +23,7 @@ class Login(
2223
password: String,
2324
): Flow<DataState<AuthToken>> = flow {
2425
try {
26+
emit(DataState.loading<AuthToken>())
2527
val loginResponse = service.login(email, password)
2628
// Incorrect login credentials counts as a 200 response from server, so need to handle that
2729
if(loginResponse.response.equals(ErrorHandling.GENERIC_AUTH_ERROR)){
@@ -42,7 +44,7 @@ class Login(
4244
loginResponse.pk,
4345
loginResponse.token
4446
)
45-
val result = authTokenDao.insert(authToken)
47+
val result = authTokenDao.insert(authToken.toEntity())
4648
// can't proceed unless token can be cached
4749
if(result < 0){
4850
throw Exception(ERROR_SAVE_AUTH_TOKEN)

app/src/main/java/com/codingwithmitch/openapi/interactors/auth/Register.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.codingwithmitch.openapi.models.AuthToken
66
import com.codingwithmitch.openapi.persistence.account.AccountDao
77
import com.codingwithmitch.openapi.persistence.account.toEntity
88
import com.codingwithmitch.openapi.persistence.auth.AuthTokenDao
9+
import com.codingwithmitch.openapi.persistence.auth.toEntity
910
import com.codingwithmitch.openapi.util.*
1011
import com.codingwithmitch.openapi.util.ErrorHandling.Companion.ERROR_SAVE_AUTH_TOKEN
1112
import kotlinx.coroutines.flow.Flow
@@ -24,6 +25,7 @@ class Register(
2425
confirmPassword: String,
2526
): Flow<DataState<AuthToken>> = flow {
2627
try {
28+
emit(DataState.loading<AuthToken>())
2729
if(password != confirmPassword){
2830
throw Exception("Passwords must match")
2931
}
@@ -52,7 +54,7 @@ class Register(
5254
registerResponse.pk,
5355
registerResponse.token
5456
)
55-
val result = authTokenDao.insert(authToken)
57+
val result = authTokenDao.insert(authToken.toEntity())
5658
// can't proceed unless token can be cached
5759
if(result < 0){
5860
throw Exception(ERROR_SAVE_AUTH_TOKEN)

app/src/main/java/com/codingwithmitch/openapi/interactors/session/CheckPreviousAuthUser.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CheckPreviousAuthUser(
1919
fun execute(
2020
email: String,
2121
): Flow<DataState<AuthToken>> = flow {
22+
emit(DataState.loading<AuthToken>())
2223
var authToken: AuthToken? = null
2324
try{
2425
val entity = accountDao.searchByEmail(email)
@@ -33,21 +34,21 @@ class CheckPreviousAuthUser(
3334
}
3435
}catch (e: Exception){
3536
e.printStackTrace()
36-
emitNoPreviousAuthUser()
37+
emit(returnNoPreviousAuthUser())
3738
}
3839
}
3940

4041
/**
4142
* If no user was previously authenticated then emit this error. The UI is waiting for it.
4243
*/
43-
private fun emitNoPreviousAuthUser(): Flow<DataState<AuthToken>> = flow{
44-
emit(DataState.error<AuthToken>(
44+
private fun returnNoPreviousAuthUser(): DataState<AuthToken> {
45+
return DataState.error<AuthToken>(
4546
response = Response(
4647
SuccessHandling.RESPONSE_CHECK_PREVIOUS_AUTH_USER_DONE,
4748
UIComponentType.None(),
4849
MessageType.Error()
4950
)
50-
))
51+
)
5152
}
5253
}
5354

app/src/main/java/com/codingwithmitch/openapi/persistence/AppDatabase.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package com.codingwithmitch.openapi.persistence
22

33
import androidx.room.Database
44
import androidx.room.RoomDatabase
5-
import com.codingwithmitch.openapi.models.Account
6-
import com.codingwithmitch.openapi.models.AuthToken
7-
import com.codingwithmitch.openapi.models.BlogPost
85
import com.codingwithmitch.openapi.persistence.account.AccountDao
6+
import com.codingwithmitch.openapi.persistence.account.AccountEntity
97
import com.codingwithmitch.openapi.persistence.auth.AuthTokenDao
8+
import com.codingwithmitch.openapi.persistence.auth.AuthTokenEntity
109
import com.codingwithmitch.openapi.persistence.blog.BlogPostDao
10+
import com.codingwithmitch.openapi.persistence.blog.BlogPostEntity
1111

12-
@Database(entities = [AuthToken::class, Account::class, BlogPost::class], version = 1)
12+
@Database(entities = [AuthTokenEntity::class, AccountEntity::class, BlogPostEntity::class], version = 1)
1313
abstract class AppDatabase: RoomDatabase() {
1414

1515
abstract fun getAuthTokenDao(): AuthTokenDao

app/src/main/java/com/codingwithmitch/openapi/persistence/auth/AuthTokenDao.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import androidx.room.Dao
44
import androidx.room.Insert
55
import androidx.room.OnConflictStrategy
66
import androidx.room.Query
7-
import com.codingwithmitch.openapi.models.AuthToken
87

98
@Dao
109
interface AuthTokenDao {
1110

1211
@Insert(onConflict = OnConflictStrategy.REPLACE)
13-
suspend fun insert(authToken: AuthToken): Long
12+
suspend fun insert(authToken: AuthTokenEntity): Long
1413

1514
@Query("DELETE FROM auth_token")
1615
suspend fun clearTokens()

app/src/main/java/com/codingwithmitch/openapi/persistence/auth/AuthTokenEntity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import androidx.room.ColumnInfo
44
import androidx.room.Entity
55
import androidx.room.ForeignKey
66
import androidx.room.PrimaryKey
7-
import com.codingwithmitch.openapi.models.Account
87
import com.codingwithmitch.openapi.models.AuthToken
8+
import com.codingwithmitch.openapi.persistence.account.AccountEntity
99
import com.google.gson.annotations.Expose
1010

1111
@Entity(
1212
tableName = "auth_token",
1313
foreignKeys = [
1414
ForeignKey(
15-
entity = Account::class,
15+
entity = AccountEntity::class,
1616
parentColumns = ["pk"],
1717
childColumns = ["account_pk"],
1818
onDelete = ForeignKey.CASCADE

app/src/main/java/com/codingwithmitch/openapi/persistence/blog/BlogPostEntity.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import androidx.room.ColumnInfo
44
import androidx.room.Entity
55
import androidx.room.PrimaryKey
66
import com.codingwithmitch.openapi.models.BlogPost
7-
import com.codingwithmitch.openapi.util.DateUtils
8-
import kotlinx.android.parcel.Parcelize
97

10-
@Parcelize
118
@Entity(tableName = "blog_post")
129
data class BlogPostEntity(
1310

app/src/main/java/com/codingwithmitch/openapi/session/SessionManager.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ constructor(
3939
// Check if a user was authenticated in a previous session
4040
preferences.getString(PreferenceKeys.PREVIOUS_AUTH_USER, null)?.let { email ->
4141
onTriggerEvent(SessionEvents.CheckPreviousAuthUser(email))
42-
}
42+
}?: onFinishCheckingPrevAuthUser()
4343
}
4444

4545
fun onTriggerEvent(event: SessionEvents){
@@ -59,14 +59,15 @@ constructor(
5959
private fun checkPreviousAuthUser(email: String){
6060
state.value?.let { state ->
6161
checkPreviousAuthUser.execute(email).onEach { dataState ->
62+
this.state.value = state.copy(isLoading = dataState.isLoading)
6263
dataState.data?.let { authToken ->
6364
this.state.value = state.copy(authToken = authToken)
6465
onTriggerEvent(SessionEvents.Login(authToken))
6566
}
6667

6768
dataState.stateMessage?.let { stateMessage ->
68-
if(stateMessage.response.equals(RESPONSE_CHECK_PREVIOUS_AUTH_USER_DONE)){
69-
this.state.value = state.copy(didCheckForPreviousAuthUser = true)
69+
if(stateMessage.response.message.equals(RESPONSE_CHECK_PREVIOUS_AUTH_USER_DONE)){
70+
onFinishCheckingPrevAuthUser()
7071
}
7172
else{
7273
appendToMessageQueue(stateMessage)
@@ -87,6 +88,7 @@ constructor(
8788
private fun logout(){
8889
state.value?.let { state ->
8990
logout.execute().onEach { dataState ->
91+
this.state.value = state.copy(isLoading = dataState.isLoading)
9092
dataState.data?.let { response ->
9193
if(response.message.equals(SUCCESS_LOGOUT)){
9294
this.state.value = state.copy(authToken = null)
@@ -100,6 +102,12 @@ constructor(
100102
}
101103
}
102104

105+
private fun onFinishCheckingPrevAuthUser(){
106+
state.value?.let { state ->
107+
this.state.value = state.copy(didCheckForPreviousAuthUser = true)
108+
}
109+
}
110+
103111
private fun appendToMessageQueue(stateMessage: StateMessage){
104112
// TODO
105113
}

app/src/main/java/com/codingwithmitch/openapi/session/SessionState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.codingwithmitch.openapi.session
33
import com.codingwithmitch.openapi.models.AuthToken
44

55
data class SessionState(
6+
val isLoading: Boolean = false,
67
val authToken: AuthToken? = null,
78
val didCheckForPreviousAuthUser: Boolean = false,
89
)

0 commit comments

Comments
 (0)