Skip to content

Commit

Permalink
Domain layer refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr7035 committed Jun 11, 2024
1 parent a8d9bd9 commit 4727054
Show file tree
Hide file tree
Showing 117 changed files with 581 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package by.alexandr7035.votekt.data.cache
import androidx.room.Embedded
import androidx.room.Relation
import by.alexandr7035.votekt.domain.core.BlockchainActionStatus
import by.alexandr7035.votekt.domain.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.votings.VoteType
import by.alexandr7035.votekt.domain.model.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.model.proposal.VoteType

data class ProposalWithTransactions(
@Embedded val proposal: ProposalEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import by.alexandr7035.votekt.domain.transactions.TransactionType
import by.alexandr7035.votekt.domain.model.transactions.TransactionType
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -22,7 +22,7 @@ interface TransactionDao {
@Update
suspend fun updateTransaction(transaction: TransactionEntity)

@Query("DELETE FROM transactions")
@Query("DELETE FROM transactions WHERE status != 'PENDING'")
suspend fun clearTransactionHistory()

@Query("SELECT type FROM transactions WHERE hash = :transactionHash")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package by.alexandr7035.votekt.data.cache
import androidx.room.Entity
import androidx.room.PrimaryKey
import by.alexandr7035.ethereum.model.Wei
import by.alexandr7035.votekt.domain.transactions.TransactionDomain
import by.alexandr7035.votekt.domain.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.transactions.TransactionType
import by.alexandr7035.votekt.domain.model.transactions.TransactionDomain
import by.alexandr7035.votekt.domain.model.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.model.transactions.TransactionType
import java.math.BigInteger

@Entity(tableName = "transactions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.util.Log
import by.alexandr7035.crypto.CryptoHelper
import by.alexandr7035.ethereum.core.EthereumClient
import by.alexandr7035.ethereum.model.Wei
import by.alexandr7035.votekt.data.cache.PrefKeys
import by.alexandr7035.votekt.domain.repository.AccountRepository
import by.alexandr7035.votekt.domain.model.account.MnemonicWord
import cash.z.ecc.android.bip39.Mnemonics
import com.cioccarellia.ksprefs.KsPrefs
import by.alexandr7035.votekt.data.cache.PrefKeys
import by.alexandr7035.votekt.domain.account.AccountRepository
import by.alexandr7035.votekt.domain.account.MnemonicWord
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand All @@ -31,7 +31,7 @@ class AccountRepositoryImpl(
return ksPrefs.pull(PrefKeys.ACCOUNT_ADDRESS_KEY, "").isNotBlank()
}

override fun getAccountBalance(): Flow<Wei> = flow {
override fun observeAccountBalance(): Flow<Wei> = flow {
val recentBalance = ksPrefs.pull(PrefKeys.RECENT_BALANCE, "")
if (recentBalance.isNotBlank()) {
emit(Wei(recentBalance))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package by.alexandr7035.votekt.data.repository
import android.content.Context
import android.util.Base64
import androidx.biometric.BiometricManager
import com.cioccarellia.ksprefs.KsPrefs
import by.alexandr7035.votekt.data.cache.PrefKeys
import by.alexandr7035.votekt.data.security.BiometricsManager
import by.alexandr7035.votekt.data.security.CryptoUtils
import by.alexandr7035.votekt.data.security.model.BiometricEncryptedPinWrapper
import by.alexandr7035.votekt.domain.security.AppLockRepository
import by.alexandr7035.votekt.domain.security.model.AuthenticationResult
import by.alexandr7035.votekt.domain.security.model.BiometricsAvailability
import by.alexandr7035.votekt.domain.security.model.EncryptedPinCode
import by.alexandr7035.votekt.domain.security.model.PinCode
import by.alexandr7035.votekt.domain.repository.AppLockRepository
import by.alexandr7035.votekt.domain.model.security.AuthenticationResult
import by.alexandr7035.votekt.domain.model.security.BiometricsAvailability
import by.alexandr7035.votekt.domain.model.security.EncryptedPinCode
import by.alexandr7035.votekt.domain.model.security.PinCode
import com.cioccarellia.ksprefs.KsPrefs
import com.squareup.moshi.Moshi
import javax.crypto.Cipher

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package by.alexandr7035.votekt.domain.repository
package by.alexandr7035.votekt.data.repository

import android.content.Context
import by.alexandr7035.votekt.BuildConfig
import by.alexandr7035.votekt.R
import by.alexandr7035.votekt.domain.model.demo.DemoProposal
import by.alexandr7035.votekt.domain.repository.DemoModeRepository
import kotlin.random.Random

class DemoModeRepositoryImpl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package by.alexandr7035.votekt.data.repository

import android.util.Log
import cash.z.ecc.android.bip39.Mnemonics
import by.alexandr7035.votekt.BuildConfig
import by.alexandr7035.votekt.domain.account.MnemonicRepository
import by.alexandr7035.votekt.domain.account.MnemonicWord
import by.alexandr7035.votekt.domain.account.MnemonicWordConfirm
import by.alexandr7035.votekt.domain.repository.MnemonicRepository
import by.alexandr7035.votekt.domain.model.account.MnemonicWord
import by.alexandr7035.votekt.domain.model.account.MnemonicWordConfirm
import by.alexandr7035.votekt.domain.core.AppError
import by.alexandr7035.votekt.domain.core.ErrorType
import cash.z.ecc.android.bip39.Mnemonics
import java.security.SecureRandom
import java.util.Random

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import by.alexandr7035.ethereum.errors.RequestNotExecutedException
import by.alexandr7035.ethereum.model.EthTransactionEstimation
import by.alexandr7035.ethereum.model.GWEI
import by.alexandr7035.ethereum.model.Wei
import com.cioccarellia.ksprefs.KsPrefs
import by.alexandr7035.votekt.BuildConfig
import by.alexandr7035.votekt.data.cache.PrefKeys
import by.alexandr7035.votekt.data.cache.ProposalsDao
import by.alexandr7035.votekt.domain.account.AccountRepository
import by.alexandr7035.votekt.domain.transactions.PrepareTransactionData
import by.alexandr7035.votekt.domain.transactions.ReviewTransactionData
import by.alexandr7035.votekt.domain.transactions.SendTransactionRepository
import by.alexandr7035.votekt.domain.transactions.TransactionEstimationError
import by.alexandr7035.votekt.domain.transactions.TransactionHash
import by.alexandr7035.votekt.domain.transactions.TransactionRepository
import by.alexandr7035.votekt.domain.transactions.TransactionType
import by.alexandr7035.votekt.domain.repository.AccountRepository
import by.alexandr7035.votekt.domain.model.transactions.PrepareTransactionData
import by.alexandr7035.votekt.domain.model.transactions.ReviewTransactionData
import by.alexandr7035.votekt.domain.repository.SendTransactionRepository
import by.alexandr7035.votekt.domain.model.transactions.TransactionEstimationError
import by.alexandr7035.votekt.domain.model.transactions.TransactionHash
import by.alexandr7035.votekt.domain.repository.TransactionRepository
import by.alexandr7035.votekt.domain.model.transactions.TransactionType
import com.cioccarellia.ksprefs.KsPrefs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -114,7 +114,7 @@ class SendTransactionRepositoryImpl(
}
}

override suspend fun confirmTransaction() = withContext(Dispatchers.IO) {
override suspend fun confirmCurrentTransaction() = withContext(Dispatchers.IO) {
try {
val currentState = _state.value ?: return@withContext

Expand All @@ -130,7 +130,7 @@ class SendTransactionRepositoryImpl(
}
}

override suspend fun cancelTransaction() {
override suspend fun cancelCurrentTransaction() {
transaction = createEmptyTransaction()
state.update { null }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import by.alexandr7035.votekt.data.workers.SyncProposalsWorker
import by.alexandr7035.votekt.domain.core.AppError
import by.alexandr7035.votekt.domain.core.ErrorType
import by.alexandr7035.votekt.domain.core.OperationResult
import by.alexandr7035.votekt.domain.transactions.TransactionDomain
import by.alexandr7035.votekt.domain.transactions.TransactionHash
import by.alexandr7035.votekt.domain.transactions.TransactionRepository
import by.alexandr7035.votekt.domain.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.transactions.TransactionType
import by.alexandr7035.votekt.domain.transactions.isContractInteraction
import by.alexandr7035.votekt.domain.model.transactions.TransactionDomain
import by.alexandr7035.votekt.domain.model.transactions.TransactionHash
import by.alexandr7035.votekt.domain.repository.TransactionRepository
import by.alexandr7035.votekt.domain.model.transactions.TransactionStatus
import by.alexandr7035.votekt.domain.model.transactions.TransactionType
import by.alexandr7035.votekt.domain.model.transactions.isContractInteraction
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
Expand All @@ -33,7 +33,7 @@ class TransactionRepositoryImpl(
private val dispatcher: CoroutineDispatcher,
private val workManager: WorkManager,
) : TransactionRepository {
override fun getTransactions(): Flow<List<TransactionDomain>> {
override fun observeTransactions(): Flow<List<TransactionDomain>> {
return transactionDao.getTransactions().map { list ->
list.map { it.mapToData() }
}.flowOn(dispatcher)
Expand Down Expand Up @@ -130,12 +130,7 @@ class TransactionRepositoryImpl(
return transactionDao.getTransactionType(transactionHash.value)
}

override suspend fun clearTransactions(): OperationResult<Unit> {
return try {
transactionDao.clearTransactionHistory()
OperationResult.Success(Unit)
} catch (e: Exception) {
OperationResult.Failure(AppError(ErrorType.UNKNOWN_ERROR))
}
override suspend fun clearTransactions() {
transactionDao.clearTransactionHistory()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import by.alexandr7035.ethereum.model.EthTransactionInput
import by.alexandr7035.ethereum.model.Wei
import by.alexandr7035.ethereum.model.events.EthereumEvent
import by.alexandr7035.utils.asEthereumAddressString
import com.cioccarellia.ksprefs.KsPrefs
import by.alexandr7035.votekt.data.cache.PrefKeys
import by.alexandr7035.votekt.data.cache.ProposalEntity
import by.alexandr7035.votekt.data.cache.ProposalWithTransactions
import by.alexandr7035.votekt.data.cache.ProposalsDao
import by.alexandr7035.votekt.data.cache.mapDeployStatus
import by.alexandr7035.votekt.data.cache.mapSelfVote
import by.alexandr7035.votekt.data.cache.mapVoteStatus
import by.alexandr7035.votekt.domain.account.AccountRepository
import by.alexandr7035.votekt.domain.repository.AccountRepository
import by.alexandr7035.votekt.domain.core.BlockchainActionStatus
import by.alexandr7035.votekt.domain.core.OperationResult
import by.alexandr7035.votekt.domain.core.Uuid
import by.alexandr7035.votekt.domain.model.contract.ContractConfiguration
import by.alexandr7035.votekt.domain.model.contract.ContractState
import by.alexandr7035.votekt.domain.model.contract.CreateDraftProposal
import by.alexandr7035.votekt.domain.transactions.PrepareTransactionData
import by.alexandr7035.votekt.domain.transactions.SendTransactionRepository
import by.alexandr7035.votekt.domain.votings.Proposal
import by.alexandr7035.votekt.domain.votings.VoteType
import by.alexandr7035.votekt.domain.votings.VotingContractRepository
import by.alexandr7035.votekt.domain.votings.VotingData
import by.alexandr7035.votekt.domain.model.transactions.PrepareTransactionData
import by.alexandr7035.votekt.domain.repository.SendTransactionRepository
import by.alexandr7035.votekt.domain.model.proposal.Proposal
import by.alexandr7035.votekt.domain.model.proposal.VoteType
import by.alexandr7035.votekt.domain.repository.VotingContractRepository
import by.alexandr7035.votekt.domain.model.proposal.VotingData
import com.cioccarellia.ksprefs.KsPrefs
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import by.alexandr7035.ethereum.core.EthereumEventListener
import by.alexandr7035.ethereum.model.events.EthereumEvent
import by.alexandr7035.votekt.domain.core.OperationResult
import by.alexandr7035.votekt.domain.repository.WebsocketManager
import by.alexandr7035.votekt.domain.votings.VotingContractRepository
import by.alexandr7035.votekt.domain.repository.VotingContractRepository
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import by.alexandr7035.ethereum.core.EthereumClient
import by.alexandr7035.ethereum.errors.TransactionReceiptNotFound
import by.alexandr7035.votekt.domain.transactions.TransactionRepository
import by.alexandr7035.votekt.domain.repository.TransactionRepository

class AwaitTransactionWorker(
appContext: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import by.alexandr7035.votekt.domain.votings.VotingContractRepository
import by.alexandr7035.votekt.domain.repository.VotingContractRepository

class SyncProposalsWorker(
appContext: Context,
Expand Down
60 changes: 41 additions & 19 deletions app/src/main/java/by/alexandr7035/votekt/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import by.alexandr7035.crypto.CryptoHelper
import by.alexandr7035.crypto.CryptoHelperImpl
import by.alexandr7035.ethereum.core.EthereumEventListener
import by.alexandr7035.ethereumimpl.impl.EthereumEventListenerImpl
import com.cioccarellia.ksprefs.KsPrefs
import com.cioccarellia.ksprefs.config.EncryptionType
import com.cioccarellia.ksprefs.config.model.AutoSavePolicy
import com.cioccarellia.ksprefs.config.model.CommitStrategy
import by.alexandr7035.votekt.BuildConfig
import by.alexandr7035.votekt.data.cache.TransactionsDatabase
import by.alexandr7035.votekt.data.repository.AccountRepositoryImpl
Expand All @@ -25,16 +21,16 @@ import by.alexandr7035.votekt.data.websockets.WebsocketActivityCallbacks
import by.alexandr7035.votekt.data.websockets.WebsocketManagerImpl
import by.alexandr7035.votekt.data.workers.AwaitTransactionWorker
import by.alexandr7035.votekt.data.workers.SyncProposalsWorker
import by.alexandr7035.votekt.domain.account.AccountRepository
import by.alexandr7035.votekt.domain.account.MnemonicRepository
import by.alexandr7035.votekt.domain.repository.AccountRepository
import by.alexandr7035.votekt.domain.repository.MnemonicRepository
import by.alexandr7035.votekt.domain.repository.BlockchainExplorerRepository
import by.alexandr7035.votekt.domain.repository.DemoModeRepository
import by.alexandr7035.votekt.domain.repository.DemoModeRepositoryImpl
import by.alexandr7035.votekt.data.repository.DemoModeRepositoryImpl
import by.alexandr7035.votekt.domain.repository.WebsocketManager
import by.alexandr7035.votekt.domain.security.AppLockRepository
import by.alexandr7035.votekt.domain.transactions.SendTransactionRepository
import by.alexandr7035.votekt.domain.transactions.TransactionRepository
import by.alexandr7035.votekt.domain.votings.VotingContractRepository
import by.alexandr7035.votekt.domain.repository.AppLockRepository
import by.alexandr7035.votekt.domain.repository.SendTransactionRepository
import by.alexandr7035.votekt.domain.repository.TransactionRepository
import by.alexandr7035.votekt.domain.repository.VotingContractRepository
import by.alexandr7035.votekt.ui.core.AppViewModel
import by.alexandr7035.votekt.ui.feature.account.create.ConfirmPhraseViewModel
import by.alexandr7035.votekt.ui.feature.account.create.GeneratePhraseViewModel
Expand All @@ -48,6 +44,10 @@ import by.alexandr7035.votekt.ui.feature.proposals.details.VotingDetailsViewMode
import by.alexandr7035.votekt.ui.feature.proposals.feed.ProposalsViewModel
import by.alexandr7035.votekt.ui.feature.transactions.history.TransactionsViewModel
import by.alexandr7035.votekt.ui.feature.wallet.WalletViewModel
import com.cioccarellia.ksprefs.KsPrefs
import com.cioccarellia.ksprefs.config.EncryptionType
import com.cioccarellia.ksprefs.config.model.AutoSavePolicy
import com.cioccarellia.ksprefs.config.model.CommitStrategy
import kotlinx.coroutines.Dispatchers
import org.kethereum.model.Address
import org.koin.android.ext.koin.androidApplication
Expand All @@ -73,27 +73,46 @@ val appModule = module {
}
viewModel {
AppViewModel(
accountRepository = get(),
sendTransactionRepository = get(),
web3EventsRepository = get(),
checkAppLockUseCase = get(),
connectToNodeUseCase = get(),
getBlockchainExplorerUrlUseCase = get(),
checkAppLockedWithBiometricsUseCase = get(),
checkAccountCreatedUseCase = get(),
observeOutgoingTransactionUseCase = get(),
confirmOutgoingTransactionUseCase = get(),
cancelCurrentTransactionUseCase = get(),
)
}
viewModel { GeneratePhraseViewModel(get()) }
viewModel {
ConfirmPhraseViewModel(
mnemonicRepository = get(),
accountRepository = get()
addAccountUseCase = get(),
confirmNewAccountUseCase = get(),
getAccountConfirmationData = get(),
)
}
viewModel {
VotingDetailsViewModel(
getContractConfigurationUseCase = get(),
observeProposalByIdUseCase = get(),
voteOnProposalUseCase = get(),
deleteDraftProposalUseCase = get(),
deployDraftProposalUseCase = get(),
)
}
viewModel { VotingDetailsViewModel(get()) }
viewModel { ProposalsViewModel(get(), get()) }
viewModel { TransactionsViewModel(get()) }
viewModel { TransactionsViewModel(
observeTransactionsUseCase = get(),
clearTransactionsUseCase = get(),
) }
viewModel { CreateProposalViewModel(get(), get(), get(), get()) }
viewModel { WalletViewModel(get(), get(), get()) }
viewModel { WalletViewModel(
getSelfAccountUseCase = get(),
observeBalanceUseCase = get(),
contractStateUseCase = get(),
logoutUseCase = get(),
) }

viewModel {
LockScreenViewModel(
Expand All @@ -108,7 +127,10 @@ val appModule = module {
}

viewModel {
CreatePinViewModel(setupAppLockUseCase = get(), checkIfBiometricsAvailableUseCase = get())
CreatePinViewModel(
setupAppLockUseCase = get(),
checkIfBiometricsAvailableUseCase = get()
)
}

viewModel {
Expand Down
Loading

0 comments on commit 4727054

Please sign in to comment.