generated from GO-SOPT-ANDROID/android-template
-
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.
- Loading branch information
Showing
10 changed files
with
162 additions
and
22 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
app/src/main/java/org/android/go/sopt/data/repository/AuthRepositoryImpl.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,22 @@ | ||
package org.android.go.sopt.data.repository | ||
|
||
import org.android.go.sopt.data.entity.User | ||
import org.android.go.sopt.data.source.LocalPrefDataSource | ||
import org.android.go.sopt.domain.AuthRepository | ||
import javax.inject.Inject | ||
|
||
class AuthRepositoryImpl @Inject constructor( | ||
private val localPrefDataSource: LocalPrefDataSource, | ||
) : AuthRepository { | ||
override fun setAutoLogin(isAutoLogin: Boolean) { | ||
localPrefDataSource.isAutoLogin = isAutoLogin | ||
} | ||
|
||
override fun getAutoLogin(): Boolean = localPrefDataSource.isAutoLogin | ||
|
||
override fun setSignedUpUser(user: User) { | ||
localPrefDataSource.signedUpUser = user | ||
} | ||
|
||
override fun getSignedUpUser(): User? = localPrefDataSource.signedUpUser | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/org/android/go/sopt/data/source/LocalPrefDataSource.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,37 @@ | ||
package org.android.go.sopt.data.source | ||
|
||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import org.android.go.sopt.data.entity.User | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class LocalPrefDataSource @Inject constructor( | ||
private val prefs: SharedPreferences, | ||
) { | ||
var isAutoLogin: Boolean | ||
set(value) = prefs.edit { putBoolean(PREF_IS_AUTO_LOGIN, value) } | ||
get() = prefs.getBoolean(PREF_IS_AUTO_LOGIN, false) | ||
|
||
var signedUpUser: User? | ||
set(value) = prefs.edit { | ||
val user = GsonBuilder().create().toJson(value) | ||
putString(PREF_SIGNED_UP_USER, user) | ||
} | ||
get() { | ||
val user = prefs.getString(PREF_SIGNED_UP_USER, "") | ||
return try { | ||
Gson().fromJson(user, User::class.java) | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
companion object { | ||
const val PREF_IS_AUTO_LOGIN = "IS_AUTO_LOGIN" | ||
const val PREF_SIGNED_UP_USER = "SIGNED_UP_USER" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/org/android/go/sopt/di/RepositoryModule.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 org.android.go.sopt.di | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.android.go.sopt.data.repository.AuthRepositoryImpl | ||
import org.android.go.sopt.domain.AuthRepository | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class RepositoryModule { | ||
@Binds | ||
@Singleton | ||
abstract fun providesAuthRepository( | ||
authRepositoryImpl: AuthRepositoryImpl, | ||
): AuthRepository | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/org/android/go/sopt/di/SharedPrefModule.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 org.android.go.sopt.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKey | ||
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 SharedPrefModule { | ||
@Provides | ||
@Singleton | ||
fun providesLocalPreferences(@ApplicationContext context: Context): SharedPreferences = | ||
EncryptedSharedPreferences.create( | ||
context, | ||
context.packageName, | ||
MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(), | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/org/android/go/sopt/domain/AuthRepository.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 org.android.go.sopt.domain | ||
|
||
import org.android.go.sopt.data.entity.User | ||
|
||
interface AuthRepository { | ||
fun setAutoLogin(isAutoLogin: Boolean) | ||
|
||
fun getAutoLogin(): Boolean | ||
|
||
fun setSignedUpUser(user: User) | ||
|
||
fun getSignedUpUser(): User? | ||
} |
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
18 changes: 8 additions & 10 deletions
18
app/src/main/java/org/android/go/sopt/presentation/main/MainActivity.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
14 changes: 14 additions & 0 deletions
14
app/src/main/java/org/android/go/sopt/presentation/main/MainViewModel.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,14 @@ | ||
package org.android.go.sopt.presentation.main | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import org.android.go.sopt.data.entity.User | ||
import org.android.go.sopt.domain.AuthRepository | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class MainViewModel @Inject constructor( | ||
private val authRepository: AuthRepository, | ||
) : ViewModel() { | ||
fun getSignedUpUser(): User = authRepository.getSignedUpUser() ?: User() | ||
} |
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