-
Notifications
You must be signed in to change notification settings - Fork 9
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
59 changed files
with
1,236 additions
and
88 deletions.
There are no files selected for viewing
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
20 changes: 0 additions & 20 deletions
20
app/src/main/java/com/maruchin/domaindrivenandroid/MainActivity.kt
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/maruchin/domaindrivenandroid/core/DataStoreModule.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,24 @@ | ||
package com.maruchin.domaindrivenandroid.core | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
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) | ||
class DataStoreModule { | ||
private val Context.dataStore by preferencesDataStore(name = "preferences") | ||
|
||
@Provides | ||
@Singleton | ||
fun dataStore(@ApplicationContext context: Context): DataStore<Preferences> { | ||
return context.dataStore | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/AccountApi.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,6 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
interface AccountApi { | ||
|
||
suspend fun createAccount(username: String, email: String): AccountJson | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/AccountJson.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,3 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
data class AccountJson(val username: String, val email: String, val collectedPoints: Int) |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/AccountMapper.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,9 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
import com.maruchin.domaindrivenandroid.data.units.Points | ||
|
||
fun AccountJson.toModel() = Account( | ||
username = username, | ||
email = email, | ||
collectedPoints = Points(collectedPoints), | ||
) |
22 changes: 16 additions & 6 deletions
22
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/AccountRepository.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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AccountRepository @Inject constructor() { | ||
private val account = MutableStateFlow<Account?>(sampleAccount) | ||
class AccountRepository @Inject constructor( | ||
private val accountApi: AccountApi, | ||
private val accountStorage: AccountStorage, | ||
) { | ||
|
||
fun getLoggedInAccount(): Flow<Account?> { | ||
return account | ||
return accountStorage.getLoggedInAccount() | ||
} | ||
|
||
suspend fun saveLoggedInAccount(account: Account?) { | ||
this.account.emit(account) | ||
suspend fun saveLoggedInAccount(account: Account) { | ||
accountStorage.saveLoggedInAccount(account) | ||
} | ||
|
||
suspend fun clearLoggedInAccount() { | ||
accountStorage.saveLoggedInAccount(null) | ||
} | ||
|
||
suspend fun createAccount(username: String, email: String) { | ||
val accountFromApi = accountApi.createAccount(username, email) | ||
accountStorage.saveLoggedInAccount(accountFromApi.toModel()) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/AccountStorage.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,10 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AccountStorage { | ||
|
||
fun getLoggedInAccount(): Flow<Account?> | ||
|
||
suspend fun saveLoggedInAccount(account: Account?) | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/DefaultAccountStorage.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,49 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
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 com.maruchin.domaindrivenandroid.data.units.Points | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
val ACCOUNT_USERNAME = stringPreferencesKey("account_username") | ||
val ACCOUNT_EMAIL = stringPreferencesKey("account_email") | ||
val ACCOUNT_COLLECTED_POINTS = intPreferencesKey("account_collected_points") | ||
|
||
class DefaultAccountStorage @Inject constructor( | ||
private val dataStore: DataStore<Preferences> | ||
) : AccountStorage { | ||
|
||
override fun getLoggedInAccount(): Flow<Account?> { | ||
return dataStore.data.map { preferences -> | ||
val username = preferences[ACCOUNT_USERNAME] | ||
val email = preferences[ACCOUNT_EMAIL] | ||
val collectedPoints = preferences[ACCOUNT_COLLECTED_POINTS] | ||
if (username != null && email != null && collectedPoints != null) { | ||
Account( | ||
username = username, | ||
email = email, | ||
collectedPoints = Points(collectedPoints) | ||
) | ||
} else null | ||
} | ||
} | ||
|
||
override suspend fun saveLoggedInAccount(account: Account?) { | ||
dataStore.edit { preferences -> | ||
if (account == null) { | ||
preferences.remove(ACCOUNT_USERNAME) | ||
preferences.remove(ACCOUNT_EMAIL) | ||
preferences.remove(ACCOUNT_COLLECTED_POINTS) | ||
} else { | ||
preferences[ACCOUNT_USERNAME] = account.username | ||
preferences[ACCOUNT_EMAIL] = account.email | ||
preferences[ACCOUNT_COLLECTED_POINTS] = account.collectedPoints.value | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/FakeAccountApi.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,10 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
import javax.inject.Inject | ||
|
||
class FakeAccountApi @Inject constructor() : AccountApi { | ||
|
||
override suspend fun createAccount(username: String, email: String): AccountJson { | ||
return AccountJson(username = username, email = email, collectedPoints = 170) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/maruchin/domaindrivenandroid/data/account/FakeAccountStorage.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,16 @@ | ||
package com.maruchin.domaindrivenandroid.data.account | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
class FakeAccountStorage : AccountStorage { | ||
private val account = MutableStateFlow<Account?>(sampleAccount) | ||
|
||
override fun getLoggedInAccount(): Flow<Account?> { | ||
return account | ||
} | ||
|
||
override suspend fun saveLoggedInAccount(account: Account?) { | ||
this.account.emit(account) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/maruchin/domaindrivenandroid/data/registrationrequest/PersonalData.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,3 @@ | ||
package com.maruchin.domaindrivenandroid.data.registrationrequest | ||
|
||
data class PersonalData(val username: String, val email: String) |
15 changes: 15 additions & 0 deletions
15
...ain/java/com/maruchin/domaindrivenandroid/data/registrationrequest/RegistrationRequest.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,15 @@ | ||
package com.maruchin.domaindrivenandroid.data.registrationrequest | ||
|
||
data class RegistrationRequest( | ||
val personalData: PersonalData? = null, | ||
val termsAndConditionsAccepted: Boolean = false, | ||
) { | ||
|
||
fun setPersonalData(personalData: PersonalData) = copy( | ||
personalData = personalData, | ||
) | ||
|
||
fun acceptTermsAndConditions() = copy( | ||
termsAndConditionsAccepted = true, | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
...om/maruchin/domaindrivenandroid/data/registrationrequest/RegistrationRequestRepository.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 com.maruchin.domaindrivenandroid.data.registrationrequest | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class RegistrationRequestRepository @Inject constructor() { | ||
private val registrationRequest = MutableStateFlow<RegistrationRequest?>(null) | ||
|
||
fun getRegistrationRequest(): Flow<RegistrationRequest?> { | ||
return registrationRequest | ||
} | ||
|
||
suspend fun saveRegistrationRequest(request: RegistrationRequest) { | ||
registrationRequest.emit(request) | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
app/src/main/java/com/maruchin/domaindrivenandroid/domain/DomainModule.kt
This file was deleted.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
...indrivenandroid/domain/registrationrequest/AcceptRegistrationTermsAndConditionsUseCase.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,16 @@ | ||
package com.maruchin.domaindrivenandroid.domain.registrationrequest | ||
|
||
import com.maruchin.domaindrivenandroid.data.registrationrequest.RegistrationRequestRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class AcceptRegistrationTermsAndConditionsUseCase @Inject constructor( | ||
private val registrationRequestRepository: RegistrationRequestRepository, | ||
) { | ||
|
||
suspend operator fun invoke() { | ||
var request = checkNotNull(registrationRequestRepository.getRegistrationRequest().first()) | ||
request = request.acceptTermsAndConditions() | ||
registrationRequestRepository.saveRegistrationRequest(request) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...om/maruchin/domaindrivenandroid/domain/registrationrequest/CompleteRegistrationUseCase.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 com.maruchin.domaindrivenandroid.domain.registrationrequest | ||
|
||
import com.maruchin.domaindrivenandroid.data.account.AccountRepository | ||
import com.maruchin.domaindrivenandroid.data.registrationrequest.RegistrationRequestRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class CompleteRegistrationUseCase @Inject constructor( | ||
private val registrationRequestRepository: RegistrationRequestRepository, | ||
private val accountRepository: AccountRepository, | ||
) { | ||
|
||
suspend operator fun invoke() { | ||
val request = checkNotNull(registrationRequestRepository.getRegistrationRequest().first()) | ||
check(request.termsAndConditionsAccepted) | ||
val personalData = checkNotNull(request.personalData) | ||
accountRepository.createAccount( | ||
username = personalData.username, | ||
email = personalData.email, | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...chin/domaindrivenandroid/domain/registrationrequest/SetRegistrationPersonalDataUseCase.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,17 @@ | ||
package com.maruchin.domaindrivenandroid.domain.registrationrequest | ||
|
||
import com.maruchin.domaindrivenandroid.data.registrationrequest.PersonalData | ||
import com.maruchin.domaindrivenandroid.data.registrationrequest.RegistrationRequestRepository | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class SetRegistrationPersonalDataUseCase @Inject constructor( | ||
private val registrationRequestRepository: RegistrationRequestRepository, | ||
) { | ||
|
||
suspend operator fun invoke(personalData: PersonalData) { | ||
var request = checkNotNull(registrationRequestRepository.getRegistrationRequest().first()) | ||
request = request.setPersonalData(personalData) | ||
registrationRequestRepository.saveRegistrationRequest(request) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...om/maruchin/domaindrivenandroid/domain/registrationrequest/StartNewRegistrationUseCase.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,15 @@ | ||
package com.maruchin.domaindrivenandroid.domain.registrationrequest | ||
|
||
import com.maruchin.domaindrivenandroid.data.registrationrequest.RegistrationRequest | ||
import com.maruchin.domaindrivenandroid.data.registrationrequest.RegistrationRequestRepository | ||
import javax.inject.Inject | ||
|
||
class StartNewRegistrationUseCase @Inject constructor( | ||
private val registrationRequestRepository: RegistrationRequestRepository, | ||
) { | ||
|
||
suspend operator fun invoke() { | ||
val newRequest = RegistrationRequest() | ||
registrationRequestRepository.saveRegistrationRequest(newRequest) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/maruchin/domaindrivenandroid/ui/FieldErrorView.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 com.maruchin.domaindrivenandroid.ui | ||
|
||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
|
||
@Composable | ||
fun FieldErrorView(error: String?) { | ||
if (error != null) { | ||
Text( | ||
text = error, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = MaterialTheme.colorScheme.error, | ||
modifier = Modifier.fillMaxWidth(), | ||
) | ||
} | ||
} |
Oops, something went wrong.