-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from ILIYANGERMANOV/loans
WIP Loans + easy improvements
- Loading branch information
Showing
44 changed files
with
2,405 additions
and
114 deletions.
There are no files selected for viewing
757 changes: 757 additions & 0 deletions
757
app/schemas/com.ivy.wallet.persistence.IvyRoomDatabase/119.json
Large diffs are not rendered by default.
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
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,99 @@ | ||
package com.ivy.wallet.logic | ||
|
||
import androidx.compose.ui.graphics.toArgb | ||
import com.ivy.wallet.base.ioThread | ||
import com.ivy.wallet.logic.model.CreateLoanData | ||
import com.ivy.wallet.model.entity.Loan | ||
import com.ivy.wallet.persistence.dao.LoanDao | ||
import com.ivy.wallet.sync.uploader.LoanUploader | ||
|
||
class LoanCreator( | ||
private val paywallLogic: PaywallLogic, | ||
private val dao: LoanDao, | ||
private val uploader: LoanUploader | ||
) { | ||
suspend fun create( | ||
data: CreateLoanData, | ||
onRefreshUI: suspend (Loan) -> Unit | ||
) { | ||
val name = data.name | ||
if (name.isBlank()) return | ||
if (data.amount <= 0) return | ||
|
||
try { | ||
paywallLogic.protectAddWithPaywall( | ||
//TODO: Handle addLoan = true | ||
// addBudget = true, | ||
) { | ||
val newItem = ioThread { | ||
val item = Loan( | ||
name = name.trim(), | ||
amount = data.amount, | ||
type = data.type, | ||
color = data.color.toArgb(), | ||
icon = data.icon, | ||
orderNum = dao.findMaxOrderNum() + 1, | ||
isSynced = false | ||
) | ||
|
||
dao.save(item) | ||
item | ||
} | ||
|
||
onRefreshUI(newItem) | ||
|
||
ioThread { | ||
uploader.sync(newItem) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
|
||
suspend fun edit( | ||
updatedItem: Loan, | ||
onRefreshUI: suspend (Loan) -> Unit | ||
) { | ||
if (updatedItem.name.isBlank()) return | ||
if (updatedItem.amount <= 0.0) return | ||
|
||
try { | ||
ioThread { | ||
dao.save( | ||
updatedItem.copy( | ||
isSynced = false | ||
) | ||
) | ||
} | ||
|
||
onRefreshUI(updatedItem) | ||
|
||
ioThread { | ||
uploader.sync(updatedItem) | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
suspend fun delete( | ||
item: Loan, | ||
onRefreshUI: suspend () -> Unit | ||
) { | ||
try { | ||
ioThread { | ||
dao.flagDeleted(item.id) | ||
} | ||
|
||
onRefreshUI() | ||
|
||
ioThread { | ||
uploader.delete(item.id) | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/ivy/wallet/logic/model/CreateLoanData.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,12 @@ | ||
package com.ivy.wallet.logic.model | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import com.ivy.wallet.model.LoanType | ||
|
||
data class CreateLoanData( | ||
val name: String, | ||
val amount: Double, | ||
val type: LoanType, | ||
val color: Color, | ||
val icon: String? | ||
) |
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,5 @@ | ||
package com.ivy.wallet.model | ||
|
||
enum class LoanType { | ||
BORROW, LEND | ||
} |
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,29 @@ | ||
package com.ivy.wallet.model.entity | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.ivy.wallet.model.LoanType | ||
import com.ivy.wallet.model.Reorderable | ||
import java.util.* | ||
|
||
@Entity(tableName = "loans") | ||
data class Loan( | ||
val name: String, | ||
val amount: Double, | ||
val type: LoanType, | ||
val color: Int = 0, | ||
val icon: String? = null, | ||
val orderNum: Double = 0.0, | ||
|
||
val isSynced: Boolean = false, | ||
val isDeleted: Boolean = false, | ||
|
||
@PrimaryKey | ||
val id: UUID = UUID.randomUUID() | ||
) : Reorderable { | ||
override fun getItemOrderNum() = orderNum | ||
|
||
override fun withNewOrderNum(newOrderNum: Double) = this.copy( | ||
orderNum = newOrderNum | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/ivy/wallet/model/entity/LoanRecord.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,20 @@ | ||
package com.ivy.wallet.model.entity | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import java.time.LocalDateTime | ||
import java.util.* | ||
|
||
@Entity(tableName = "loan_records") | ||
data class LoanRecord( | ||
val loanId: UUID, | ||
val amount: Double, | ||
val note: String?, | ||
val dateTime: LocalDateTime, | ||
|
||
val isSynced: Boolean = false, | ||
val isDeleted: Boolean = false, | ||
|
||
@PrimaryKey | ||
val id: UUID = UUID.randomUUID() | ||
) |
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/network/request/loan/DeleteLoanRecordRequest.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,7 @@ | ||
package com.ivy.wallet.network.request.loan | ||
|
||
import java.util.* | ||
|
||
data class DeleteLoanRecordRequest( | ||
val id: UUID? = null | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/network/request/loan/DeleteLoanRequest.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,7 @@ | ||
package com.ivy.wallet.network.request.loan | ||
|
||
import java.util.* | ||
|
||
data class DeleteLoanRequest( | ||
val id: UUID? = null | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/network/request/loan/LoanRecordsResponse.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,7 @@ | ||
package com.ivy.wallet.network.request.loan | ||
|
||
import com.ivy.wallet.model.entity.LoanRecord | ||
|
||
data class LoanRecordsResponse( | ||
val loanRecords: List<LoanRecord> | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/network/request/loan/LoansResponse.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,7 @@ | ||
package com.ivy.wallet.network.request.loan | ||
|
||
import com.ivy.wallet.model.entity.Loan | ||
|
||
data class LoansResponse( | ||
val loans: List<Loan> | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/network/request/loan/UpdateLoanRecordRequest.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,7 @@ | ||
package com.ivy.wallet.network.request.loan | ||
|
||
import com.ivy.wallet.model.entity.LoanRecord | ||
|
||
data class UpdateLoanRecordRequest( | ||
val loanRecord: LoanRecord? = null | ||
) |
Oops, something went wrong.