Skip to content

Commit

Permalink
Merge pull request #69 from TiagoDvl/feature/63_-_Edit_Quick_Expense
Browse files Browse the repository at this point in the history
[63] Stable Implementation for Expenses
  • Loading branch information
TiagoDvl authored Sep 17, 2023
2 parents ca0ca00 + 1f60663 commit 891e50a
Show file tree
Hide file tree
Showing 35 changed files with 1,354 additions and 142 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build_artifact_and_release_firebase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ jobs:
java-version: '17'
cache: 'gradle'

- name: Update Secrets
env:
MAPS_API_KEY: ${{ secrets.MAPS_API_KEY }}
run: echo 'MAPS_API_KEY=${{ secrets.MAPS_API_KEY }}' > ./local.properties

- name: Build the Release AAB
run: ./gradlew bundleRelease
env:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pull_request_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ jobs:
java-version: '17'
cache: 'gradle'

- name: Update Secrets
env:
MAPS_API_KEY: ${{ secrets.MAPS_API_KEY }}
run: echo 'MAPS_API_KEY=${{ secrets.MAPS_API_KEY }}' > ./local.properties
- name: Unit tests
run: ./gradlew testQa
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ plugins {
id 'kotlin-kapt'
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}

android {
compileSdk 33
compileSdk 34
namespace 'br.com.tick.teira'

defaultConfig {
Expand Down Expand Up @@ -101,6 +102,7 @@ dependencies {
implementation "androidx.hilt:hilt-work:$hiltWorkVersion"
implementation "androidx.work:work-runtime-ktx:$workManagerVersion"


kapt "androidx.hilt:hilt-compiler:$hiltCompilerVersion"
kapt "com.google.dagger:hilt-compiler:$hiltVersion"

Expand Down
20 changes: 18 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Teira"
tools:targetApi="31">

<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -31,6 +32,21 @@
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
</application>

</manifest>
<!-- Holds information to google maps api -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />

<!-- Setup for holding reference and creating files for photos -->
<provider
android:name="br.com.tick.utils.ComposeFileProvider"
android:authorities="br.com.tick.teira.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ buildscript {
composeMaterialVersion = '1.2.0-beta03'
composeFoundation = '1.4.0-alpha03'
composeUi = '1.4.0-alpha03'
composeUiUtil = '1.5.0'
composeMaterial3Version = '1.1.0-rc01'
coroutinesVersion = '1.6.4'
coreKtxVersion = '1.9.0'
Expand All @@ -22,7 +23,7 @@ buildscript {
hiltNavigationComposeVersion = '1.0.0'
roomVersion = '2.4.3'
dataStoreVersion = '1.0.0'
navigationVersion = '2.5.3'
navigationVersion = '2.7.0'
vicoVersion = '1.6.4'
composeColorPicker = '0.7.0'
firebaseCrashlytics = '18.3.5'
Expand All @@ -31,6 +32,10 @@ buildscript {
hiltWorkVersion = '1.0.0'
hiltCompilerVersion = '1.0.0'
acompanistPermissionVersion = '0.30.0'
googleMaps = '2.14.0'
playServicesMap = '18.1.0'
playServicesLocation = '21.0.1'
coilVersion = '2.4.0'

dependencies {
classpath("com.google.dagger:hilt-android-gradle-plugin:$hiltVersion")
Expand All @@ -47,6 +52,7 @@ plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
}

task clean(type: Delete) {
Expand Down
3 changes: 2 additions & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

android {
compileSdk 33
compileSdk 34
namespace 'br.com.tick.sdk'

defaultConfig {
Expand Down Expand Up @@ -48,6 +48,7 @@ dependencies {
kapt "com.google.dagger:hilt-compiler:$hiltVersion"

implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"
implementation "com.google.android.gms:play-services-maps:$playServicesMap"

// Unit Tests dependencies
testImplementation "junit:junit:$jUnitVersion"
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/main/java/br/com/tick/sdk/database/ExpenseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package br.com.tick.sdk.database
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import br.com.tick.sdk.database.entities.Expense
import kotlinx.coroutines.flow.Flow

Expand All @@ -12,6 +13,9 @@ interface ExpenseDao {
@Insert
suspend fun addExpense(expense: Expense)

@Update
suspend fun updateExpense(expense: Expense)

@Query("DELETE FROM expense WHERE expense_id = :expenseId")
suspend fun removeExpenseById(expenseId: Int)

Expand All @@ -20,4 +24,7 @@ interface ExpenseDao {

@Query("SELECT * FROM expense")
fun getAllExpenses(): Flow<List<Expense>>

@Query("SELECT * FROM expense WHERE expense_id = :expenseId")
fun getExpense(expenseId: Int): Flow<Expense>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.com.tick.sdk.database.converters

import androidx.room.TypeConverter
import com.google.android.gms.maps.model.LatLng

class LatLngConverter {

companion object {
const val LAT_LNG_SPLITTER = "|"
}

@TypeConverter
fun toString(location: LatLng?): String? {
if (location == null) return null

return "${location.latitude}$LAT_LNG_SPLITTER${location.longitude}"
}

@TypeConverter
fun fromString(location: String?): LatLng? {
if (location == null) return null

val locationFromString = location.split(LAT_LNG_SPLITTER)
return LatLng(locationFromString[0].toDouble(), locationFromString[1].toDouble())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class LocalDateConverter {
fun fromLong(value: Long): LocalDate {
return LocalDate.ofEpochDay(value)
}
}
}
13 changes: 11 additions & 2 deletions sdk/src/main/java/br/com/tick/sdk/database/entities/Expense.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import br.com.tick.sdk.database.converters.LatLngConverter
import br.com.tick.sdk.database.converters.LocalDateConverter
import com.google.android.gms.maps.model.LatLng
import java.time.LocalDate

@Entity
@TypeConverters(LocalDateConverter::class)
@TypeConverters(LocalDateConverter::class, LatLngConverter::class)
data class Expense(

@PrimaryKey(autoGenerate = true)
Expand All @@ -25,5 +27,12 @@ data class Expense(
val value: Double,

@ColumnInfo(name = "date")
val date: LocalDate
val date: LocalDate,

@ColumnInfo(name = "location")
val location: LatLng?,

@ColumnInfo(name = "photoUri")
val photoUri: String?

)
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package br.com.tick.sdk.domain

import android.net.Uri
import com.google.android.gms.maps.model.LatLng
import java.time.LocalDate

data class CategorizedExpense(
val expenseId: Int,
val name: String,
val expenseValue: Double,
val date: LocalDate,
val category: ExpenseCategory
val category: ExpenseCategory,
val location: LatLng?,
val picture: Uri?
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
package br.com.tick.sdk.repositories.categorizedexpense

import android.net.Uri
import br.com.tick.sdk.domain.CategorizedExpense
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.flow.Flow
import java.time.LocalDate

interface CategorizedExpenseRepository {

suspend fun addExpense(categoryId: Int, name: String, value: Double, expenseDate: LocalDate)
suspend fun addExpense(
categoryId: Int,
name: String,
value: Double,
expenseDate: LocalDate,
location: LatLng? = null,
photoUri: Uri? = null
)

suspend fun updateExpense(
expenseId: Int,
categoryId: Int,
name: String,
value: Double,
expenseDate: LocalDate,
location: LatLng? = null,
photoUri: Uri? = null
)

suspend fun removeExpense(expenseId: Int)

fun getCategorizedExpenses(): Flow<List<CategorizedExpense>>

suspend fun getAccountingCycleExpenses(): Flow<List<CategorizedExpense>>

fun getCategorizedExpense(expenseId: Int): Flow<CategorizedExpense>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.com.tick.sdk.repositories.categorizedexpense

import android.net.Uri
import br.com.tick.sdk.database.CategoryColorDao
import br.com.tick.sdk.database.CategoryDao
import br.com.tick.sdk.database.ExpenseDao
Expand All @@ -8,6 +9,7 @@ import br.com.tick.sdk.database.entities.Expense
import br.com.tick.sdk.domain.CategorizedExpense
import br.com.tick.sdk.domain.ExpenseCategory
import br.com.tick.sdk.domain.getAccountingDateDayOfMonth
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
Expand All @@ -26,14 +28,40 @@ class CategorizedExpensesRepositoryImpl @Inject constructor(
categoryId: Int,
name: String,
value: Double,
expenseDate: LocalDate
expenseDate: LocalDate,
location: LatLng?,
photoUri: Uri?
) {
expenseDao.addExpense(
Expense(
categoryId = categoryId,
name = name,
value = value,
date = expenseDate
date = expenseDate,
location = location,
photoUri = photoUri?.toString()
)
)
}

override suspend fun updateExpense(
expenseId: Int,
categoryId: Int,
name: String,
value: Double,
expenseDate: LocalDate,
location: LatLng?,
photoUri: Uri?
) {
expenseDao.updateExpense(
Expense(
expenseId = expenseId,
categoryId = categoryId,
name = name,
value = value,
date = expenseDate,
location = location,
photoUri = photoUri?.toString()
)
)
}
Expand All @@ -43,7 +71,7 @@ class CategorizedExpensesRepositoryImpl @Inject constructor(
}

override fun getCategorizedExpenses(): Flow<List<CategorizedExpense>> {
return expenseDao.getExpenses().map { expenses ->
return expenseDao.getExpenses().filterNotNull().map { expenses ->
expenses.map { categorize(it) }
}
}
Expand All @@ -63,7 +91,7 @@ class CategorizedExpensesRepositoryImpl @Inject constructor(
pivot.withDayOfMonth(userAccountingDayOfMonth)
}

return expenseDao.getAllExpenses().map { expenses ->
return expenseDao.getAllExpenses().filterNotNull().map { expenses ->
expenses.filter { expense ->
val yearDiff = nextAccountingDate.year - expense.date.year
val monthDiff = nextAccountingDate.month.value - expense.date.month.value
Expand All @@ -83,6 +111,10 @@ class CategorizedExpensesRepositoryImpl @Inject constructor(
}
}

override fun getCategorizedExpense(expenseId: Int): Flow<CategorizedExpense> {
return expenseDao.getExpense(expenseId).filterNotNull().map { categorize(it) }
}

private suspend fun categorize(expense: Expense): CategorizedExpense {
val category = categoryDao.getCategoryById(expense.categoryId)
val categoryColorId = category.categoryColorId
Expand All @@ -102,7 +134,9 @@ class CategorizedExpensesRepositoryImpl @Inject constructor(
name,
value,
date,
expenseCategory
expenseCategory,
location,
photoUri?.let { Uri.parse(it) }
)
}
}
Expand Down
Loading

0 comments on commit 891e50a

Please sign in to comment.