Skip to content

Commit

Permalink
Merge pull request #5 from MayconCardoso/feature/split-share
Browse files Browse the repository at this point in the history
[Feature] Split Share
  • Loading branch information
MayconCardoso authored Oct 15, 2020
2 parents 92db8be + 9fb64d1 commit 8abad92
Show file tree
Hide file tree
Showing 44 changed files with 835 additions and 165 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ apply plugin: 'com.adarshr.test-logger'
android {
defaultConfig {
applicationId "com.mctech.stocktradetracking"
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
multiDexEnabled true
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mctech.stocktradetracking.di.data

import androidx.room.Room
import com.mctech.stocktradetracking.data.database.AppDatabase
import com.mctech.stocktradetracking.data.database.migration.bindMigrations
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module

Expand All @@ -11,7 +12,7 @@ val databaseModule = module {
androidContext(),
AppDatabase::class.java,
"stock-trade-tracking-database"
).build()
).bindMigrations().build()
}

single {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package com.mctech.stocktradetracking.di

import com.mctech.stocktradetracking.domain.stock_share.interaction.*
import com.mctech.stocktradetracking.domain.stock_share.interaction.CloseStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.DeleteStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.EditStockSharePriceCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.FilterStockDailyListCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.FilterStockShareListCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.GetFinalBalanceCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.GetFinalDailyBalanceCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.GetMarketStatusCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.GroupStockShareListCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.ObserveStockClosedListCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.ObserveStockShareListCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SaveStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SelectBestDailyStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SelectBestStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SelectWorstDailyStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SelectWorstStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SellStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SplitStockShareCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.SyncStockSharePriceCase
import com.mctech.stocktradetracking.domain.stock_share.interaction.strategies.ComputeBalanceStrategy
import com.mctech.stocktradetracking.domain.stock_share.interaction.strategies.FilterStockListStrategy
import com.mctech.stocktradetracking.domain.stock_share.interaction.strategies.ObserveStockListStrategy
Expand All @@ -15,38 +33,39 @@ import org.koin.dsl.module


val stockShareUseCasesModule = module {
single { SaveStockShareCase(service = get(), logger = get()) }
single { DeleteStockShareCase(service = get(), logger = get()) }
single { CloseStockShareCase(service = get(), logger = get()) }
single { EditStockSharePriceCase(service = get(), logger = get()) }
single { SellStockShareCase(service = get(), logger = get()) }
single { SyncStockSharePriceCase(service = get(), logger = get()) }
single { GetMarketStatusCase(service = get(), logger = get()) }
single { GetFinalBalanceCase() }
single { GroupStockShareListCase() }
single { ObserveCurrentFilterCase(service = get()) }

single(named("closedListObserver")) { ObserveStockClosedListCase(service = get()) as ObserveStockListStrategy }

single(named("stockFilter")) { FilterStockShareListCase(groupStockShareListCase = get()) as FilterStockListStrategy }
single(named("dailyStockFilter")) { FilterStockDailyListCase(groupStockShareListCase = get()) as FilterStockListStrategy }

single(named("dailyWorstSelector")) { SelectWorstDailyStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("dailyBestSelector")) { SelectBestDailyStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("stockWorstSelector")) { SelectWorstStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("stockBestSelector")) { SelectBestStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }

single(named("stockBalance")) { GetFinalBalanceCase() as ComputeBalanceStrategy }
single(named("dailyBalance")) { GetFinalDailyBalanceCase() as ComputeBalanceStrategy }
single { SaveStockShareCase(service = get(), logger = get()) }
single { DeleteStockShareCase(service = get(), logger = get()) }
single { CloseStockShareCase(service = get(), logger = get()) }
single { EditStockSharePriceCase(service = get(), logger = get()) }
single { SellStockShareCase(service = get(), logger = get()) }
single { SyncStockSharePriceCase(service = get(), logger = get()) }
single { GetMarketStatusCase(service = get(), logger = get()) }
single { GetFinalBalanceCase() }
single { GroupStockShareListCase() }
single { ObserveCurrentFilterCase(service = get()) }
single { SplitStockShareCase(service = get(), logger = get()) }

single(named("closedListObserver")) { ObserveStockClosedListCase(service = get()) as ObserveStockListStrategy }

single(named("stockFilter")) { FilterStockShareListCase(groupStockShareListCase = get()) as FilterStockListStrategy }
single(named("dailyStockFilter")) { FilterStockDailyListCase(groupStockShareListCase = get()) as FilterStockListStrategy }

single(named("dailyWorstSelector")) { SelectWorstDailyStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("dailyBestSelector")) { SelectBestDailyStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("stockWorstSelector")) { SelectWorstStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }
single(named("stockBestSelector")) { SelectBestStockShareCase(groupStockShareListCase = get()) as SelectStockStrategy }

single(named("stockBalance")) { GetFinalBalanceCase() as ComputeBalanceStrategy }
single(named("dailyBalance")) { GetFinalDailyBalanceCase() as ComputeBalanceStrategy }
}

val stockShareFilterUseCasesModule = module {
single { ObserveStockShareListCase(service = get()) as ObserveStockListStrategy }
single { SaveStockShareFilterCase(service = get(), logger = get()) }
single { ObserveStockShareListCase(service = get()) as ObserveStockListStrategy }
single { SaveStockShareFilterCase(service = get(), logger = get()) }
}

val timelineUseCasesModule = module {
single { CreatePeriodCase(service = get(), logger = get()) }
single { EditPeriodCase(service = get(), logger = get()) }
single { GetCurrentPeriodBalanceCase(service = get(), logger = get()) }
single { CreatePeriodCase(service = get(), logger = get()) }
single { EditPeriodCase(service = get(), logger = get()) }
single { GetCurrentPeriodBalanceCase(service = get(), logger = get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,61 @@ import com.mctech.stocktradetracking.R
import com.mctech.stocktradetracking.domain.stock_share.entity.StockShare
import com.mctech.stocktradetracking.domain.timeline_balance.entity.TimelineBalance
import com.mctech.stocktradetracking.feature.stock_share.StockShareNavigator
import com.mctech.stocktradetracking.feature.stock_share.edit_position.StockShareEditPositionFragmentDirections
import com.mctech.stocktradetracking.feature.timeline_balance.TimelineBalanceNavigator
import com.mctech.stocktradetracking.feature.timeline_balance.list_period.TimelineBalanceListFragmentDirections

object AppNavigatorHandler :
StockShareNavigator,
TimelineBalanceNavigator,
StockShareFilterNavigator
{
private var navController: NavController? = null

fun bind(navController: NavController) {
this.navController = navController
}
StockShareNavigator,
TimelineBalanceNavigator,
StockShareFilterNavigator {
private var navController: NavController? = null

fun unbind() {
navController = null
}
fun bind(navController: NavController) {
this.navController = navController
}

override fun navigateBack() {
navController?.navigateUp()
}
fun unbind() {
navController = null
}

override fun fromStockListToEditPosition(stockShare: StockShare) {
val bundle = Bundle().apply {
putSerializable("stockShare", stockShare)
}
navController?.navigate(
R.id.action_global_stockShareEditPriceFragment,
bundle
)
}
override fun navigateBack() {
navController?.navigateUp()
}

override fun fromStockListToBuyPosition() {
navController?.navigate(R.id.action_global_stockShareBuyFragment)
override fun fromStockListToEditPosition(stockShare: StockShare) {
val bundle = Bundle().apply {
putSerializable("stockShare", stockShare)
}
navController?.navigate(
R.id.action_global_stockShareEditPriceFragment,
bundle
)
}

override fun fromStockListToFilter() {
navController?.navigate(R.id.action_global_stockShareFilterFragment)
}
override fun fromEditToSplitPosition(stockShare: StockShare) {
val destination = StockShareEditPositionFragmentDirections
.actionStockShareEditPriceFragmentToStockSplitPositionFragment(stockShare)
navController?.navigate(destination)
}

override fun fromTimelineToEditPeriod(currentPeriod: TimelineBalance) {
val destination = TimelineBalanceListFragmentDirections.actionTimelineBalanceFragmentToTimelineBalanceEditPeriodFragment(
currentPeriod
)
navController?.navigate(destination)
}
override fun fromStockListToBuyPosition() {
navController?.navigate(R.id.action_global_stockShareBuyFragment)
}

override fun fromTimelineToOpenPeriod() {
val destination = TimelineBalanceListFragmentDirections.actionTimelineBalanceFragmentToTimelineBalanceAddPeriodFragment()
navController?.navigate(destination)
}
override fun fromStockListToFilter() {
navController?.navigate(R.id.action_global_stockShareFilterFragment)
}

override fun fromTimelineToEditPeriod(currentPeriod: TimelineBalance) {
val destination = TimelineBalanceListFragmentDirections
.actionTimelineBalanceFragmentToTimelineBalanceEditPeriodFragment(currentPeriod)
navController?.navigate(destination)
}

override fun fromTimelineToOpenPeriod() {
val destination = TimelineBalanceListFragmentDirections
.actionTimelineBalanceFragmentToTimelineBalanceAddPeriodFragment()
navController?.navigate(destination)
}
}
21 changes: 21 additions & 0 deletions app/src/main/res/navigation/stock_trade_navigation_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
android:name="stockShare"
app:argType="com.mctech.stocktradetracking.domain.stock_share.entity.StockShare" />

<action
android:id="@+id/action_stockShareEditPriceFragment_to_stockSplitPositionFragment"
app:destination="@id/stockSplitPositionFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpToInclusive="true" />

</fragment>

<fragment
Expand Down Expand Up @@ -109,5 +118,17 @@
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />

<fragment
android:id="@+id/stockSplitPositionFragment"
android:name="com.mctech.stocktradetracking.feature.stock_share.split_position.StockSplitPositionFragment"
android:label="Split Stock"
tools:layout="@layout/fragment_stock_share_split">

<argument
android:name="stockShare"
app:argType="com.mctech.stocktradetracking.domain.stock_share.entity.StockShare" />

</fragment>


</navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.mctech.stocktradetracking.data.timeline_balance.database.TimelineBala
import com.mctech.stocktradetracking.data.timeline_balance.database.TimelineBalanceDatabaseEntity

@Database(
version = 1,
version = 2,
entities = [
StockShareDatabaseEntity::class,
TimelineBalanceDatabaseEntity::class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mctech.stocktradetracking.data.database.migration

import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

object Migrations {
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE stock_share ADD COLUMN initialShareAmount INTEGER NOT NULL default 0")
database.execSQL("ALTER TABLE stock_share ADD COLUMN initialPurchasePrice REAL NOT NULL default 0.0")
database.execSQL("UPDATE stock_share set initialShareAmount = shareAmount")
database.execSQL("UPDATE stock_share set initialPurchasePrice = purchasePrice")
}
}
}

fun <T : RoomDatabase> RoomDatabase.Builder<T>.bindMigrations(): RoomDatabase.Builder<T> {
addMigrations(
Migrations.MIGRATION_1_2
)
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface StockShareDao {
@Query("SELECT * FROM stock_share WHERE isPositionOpened = 0 ORDER BY code")
fun observeStockClosedList(): Flow<List<StockShare>>


// ============================================================================
// Single shot
// ============================================================================
Expand All @@ -34,6 +33,10 @@ interface StockShareDao {
@Query("SELECT * FROM stock_share WHERE isPositionOpened = 1 AND code = :code LIMIT 1")
suspend fun loadStockSharePosition(code: String): StockShare?

@Transaction
@Query("SELECT * FROM stock_share WHERE isPositionOpened = 1 AND code = :code")
suspend fun loadAllStockSharesByCode(code: String): List<StockShare>

@Transaction
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun save(stockShare: StockShareDatabaseEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ data class StockShareDatabaseEntity(
@PrimaryKey
val id: Long? = null,
val code: String,
val initialShareAmount: Long,
val shareAmount: Long,
val initialPurchasePrice: Double,
val purchasePrice: Double,
var salePrice: Double = purchasePrice,
val purchaseDate: Date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import com.mctech.stocktradetracking.domain.stock_share.entity.StockShare
import kotlinx.coroutines.flow.Flow

interface LocalStockShareDataSource {
suspend fun observeStockShareList(): Flow<List<StockShare>>
suspend fun observeStockClosedList(): Flow<List<StockShare>>
fun observeStockShareList(): Flow<List<StockShare>>
fun observeStockClosedList(): Flow<List<StockShare>>

suspend fun getMarketStatus(): MarketStatus
suspend fun getAllByCode(code: String): List<StockShare>

suspend fun getDistinctStockCode(): List<String>

suspend fun saveStockShare(share: StockShare)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class LocalStockShareDataSourceImpl(
private val stockShareDao: StockShareDao
) : LocalStockShareDataSource {

override suspend fun observeStockShareList(): Flow<List<StockShare>> {
override fun observeStockShareList(): Flow<List<StockShare>> {
return stockShareDao.observeAllOpenedPosition()
}

override suspend fun observeStockClosedList(): Flow<List<StockShare>> {
override fun observeStockClosedList(): Flow<List<StockShare>> {
return stockShareDao.observeStockClosedList()
}

Expand Down Expand Up @@ -46,6 +46,10 @@ class LocalStockShareDataSourceImpl(
}
}

override suspend fun getAllByCode(code: String): List<StockShare> {
return stockShareDao.loadAllStockSharesByCode(code)
}

override suspend fun getDistinctStockCode(): List<String> {
return stockShareDao.loadDistinctStockCodes()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.mctech.stocktradetracking.domain.stock_share.entity.StockShare
fun StockShare.toDatabaseEntity() = StockShareDatabaseEntity(
id = id,
code = code,
initialShareAmount = initialShareAmount,
shareAmount = shareAmount,
initialPurchasePrice = initialPurchasePrice,
purchasePrice = purchasePrice,
salePrice = salePrice,
purchaseDate = purchaseDate,
Expand All @@ -19,7 +21,9 @@ fun StockShare.toDatabaseEntity() = StockShareDatabaseEntity(
fun StockShareDatabaseEntity.toBusinessEntity() = StockShare(
id = id,
code = code,
initialShareAmount = initialShareAmount,
shareAmount = shareAmount,
initialPurchasePrice = initialPurchasePrice,
purchasePrice = purchasePrice,
salePrice = salePrice,
purchaseDate = purchaseDate,
Expand Down
Loading

0 comments on commit 8abad92

Please sign in to comment.