login with email and save token to datastore
This commit is contained in:
@ -6,11 +6,11 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk 32
|
||||
compileSdk 33
|
||||
|
||||
defaultConfig {
|
||||
applicationId "app.omnivore.omnivore"
|
||||
minSdk 21
|
||||
minSdk 23
|
||||
targetSdk 32
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
@ -111,9 +111,7 @@ dependencies {
|
||||
// implementation "androidx.activity:activity-ktx:1.3.1"
|
||||
|
||||
//Dagger - Hilt
|
||||
implementation 'com.google.dagger:hilt-android:2.43.2'
|
||||
kapt 'com.google.dagger:hilt-compiler:2.43.2'
|
||||
|
||||
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
|
||||
implementation 'com.google.dagger:hilt-android:2.42'
|
||||
kapt 'com.google.dagger:hilt-compiler:2.42'
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
package="app.omnivore.omnivore">
|
||||
|
||||
<application
|
||||
android:name=".OmnivoreApplication"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package app.omnivore.omnivore
|
||||
|
||||
import android.content.Context
|
||||
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 AppModule {
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideDataStoreRepository(
|
||||
@ApplicationContext app: Context
|
||||
): DatastoreRepository = OmnivoreDatastore(app)
|
||||
|
||||
}
|
||||
@ -2,4 +2,10 @@ package app.omnivore.omnivore
|
||||
|
||||
object Constants {
|
||||
const val serverProdURL = "https://api-demo.omnivore.app"
|
||||
const val dataStoreName = "omnivore-datastore"
|
||||
}
|
||||
|
||||
object DatastoreKeys {
|
||||
const val omnivoreAuthToken = "omnivoreAuthToken"
|
||||
const val omnivoreAuthCookieString = "omnivoreAuthCookieString"
|
||||
}
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
package app.omnivore.omnivore
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.first
|
||||
import javax.inject.Inject
|
||||
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
|
||||
name = Constants.dataStoreName
|
||||
)
|
||||
|
||||
interface DatastoreRepository {
|
||||
suspend fun putString(key: String, value: String)
|
||||
suspend fun putInt(key: String, value: Int)
|
||||
suspend fun getString(key: String): String?
|
||||
suspend fun getInt(key: String): Int?
|
||||
}
|
||||
|
||||
class OmnivoreDatastore @Inject constructor(
|
||||
private val context: Context
|
||||
) : DatastoreRepository {
|
||||
override suspend fun putString(key: String, value: String) {
|
||||
val preferencesKey = stringPreferencesKey(key)
|
||||
context.dataStore.edit { preferences ->
|
||||
preferences[preferencesKey] = value
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun putInt(key: String, value: Int) {
|
||||
val preferencesKey = intPreferencesKey(key)
|
||||
context.dataStore.edit { preferences ->
|
||||
preferences[preferencesKey] = value
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getString(key: String): String? {
|
||||
val preferencesKey = stringPreferencesKey(key)
|
||||
val preferences = context.dataStore.data.first()
|
||||
return preferences[preferencesKey]
|
||||
}
|
||||
|
||||
override suspend fun getInt(key: String): Int? {
|
||||
val preferencesKey = intPreferencesKey(key)
|
||||
val preferences = context.dataStore.data.first()
|
||||
return preferences[preferencesKey]
|
||||
}
|
||||
}
|
||||
@ -3,24 +3,40 @@ package app.omnivore.omnivore
|
||||
import android.content.ContentValues
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class LoginViewModel: ViewModel() {
|
||||
@HiltViewModel
|
||||
class LoginViewModel @Inject constructor(
|
||||
private val datastoreRepo: DatastoreRepository
|
||||
): ViewModel() {
|
||||
fun login(email: String, password: String) {
|
||||
val emailLogin = RetrofitHelper.getInstance().create(EmailLoginSubmit::class.java)
|
||||
|
||||
GlobalScope.launch {
|
||||
viewModelScope.launch {
|
||||
val result = emailLogin.submitEmailLogin(
|
||||
EmailLoginCredentials(email = email, password = password)
|
||||
)
|
||||
|
||||
// TODO: parse out result and store auth token
|
||||
// set some variable that compose can observe
|
||||
if (result != null) {
|
||||
Log.d(ContentValues.TAG, result.body().toString())
|
||||
} else {
|
||||
Log.d(ContentValues.TAG, result.body().toString())
|
||||
// TODO: bail early if email is pending
|
||||
// if (result.body()?.pendingEmailVerification == true) {
|
||||
// return void
|
||||
// }
|
||||
|
||||
if (result.body()?.authToken != null) {
|
||||
datastoreRepo.putString(DatastoreKeys.omnivoreAuthToken, result.body()?.authToken!!)
|
||||
}
|
||||
|
||||
if (result.body()?.authCookieString != null) {
|
||||
datastoreRepo.putString(
|
||||
DatastoreKeys.omnivoreAuthCookieString, result.body()?.authCookieString!!
|
||||
)
|
||||
}
|
||||
|
||||
datastoreRepo.getString(DatastoreKeys.omnivoreAuthToken)?.let {
|
||||
Log.d(ContentValues.TAG, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,9 @@ import androidx.compose.ui.unit.dp
|
||||
import app.omnivore.omnivore.ui.theme.OmnivoreTheme
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.setValue
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -43,7 +45,7 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LoginView(viewModel: LoginViewModel = LoginViewModel()) {
|
||||
fun LoginView(viewModel: LoginViewModel) {
|
||||
var email by rememberSaveable { mutableStateOf("") }
|
||||
var password by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
@ -125,10 +127,10 @@ fun LoginFields(
|
||||
// showBackground = true,
|
||||
// name = "Dark Mode"
|
||||
//)
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun DefaultPreview() {
|
||||
OmnivoreTheme {
|
||||
LoginView()
|
||||
}
|
||||
}
|
||||
//@Preview(showBackground = true)
|
||||
//@Composable
|
||||
//fun DefaultPreview() {
|
||||
// OmnivoreTheme {
|
||||
// LoginView()
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
package app.omnivore.omnivore
|
||||
|
||||
import android.app.Application
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
|
||||
@HiltAndroidApp
|
||||
class OmnivoreApplication: Application() {
|
||||
}
|
||||
@ -5,7 +5,8 @@ buildscript {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "com.google.dagger:hilt-android-gradle-plugin:2.43.2"
|
||||
classpath "com.google.dagger:hilt-android-gradle-plugin:2.42"
|
||||
classpath "com.android.tools.build:gradle:7.2.1"
|
||||
}
|
||||
|
||||
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
@ -13,6 +14,9 @@ plugins {
|
||||
id 'com.android.application' version '7.2.1' apply false
|
||||
id 'com.android.library' version '7.2.1' apply false
|
||||
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
|
||||
// id 'com.android.application' version '7.2.2' apply false
|
||||
// id 'com.android.library' version '7.2.2' apply false
|
||||
// id 'org.jetbrains.kotlin.android' version '1.7.20-Beta' apply false
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#Tue Aug 02 13:19:15 PDT 2022
|
||||
#Wed Aug 10 17:39:47 PDT 2022
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
||||
Reference in New Issue
Block a user