Skip to content

Commit f65b468

Browse files
committed
connect сomplex rules logic
1 parent 96226e8 commit f65b468

File tree

52 files changed

+611
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+611
-169
lines changed

app/src/main/java/com/example/util/simpletimetracker/di/AppModuleBinds.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.example.util.simpletimetracker.di
22

3-
import com.example.util.simpletimetracker.core.interactor.IsSystemInInDarkModeInteractorImpl
3+
import com.example.util.simpletimetracker.core.interactor.GetCurrentDayInteractorImpl
4+
import com.example.util.simpletimetracker.core.interactor.IsSystemInDarkModeInteractorImpl
45
import com.example.util.simpletimetracker.core.interactor.GetUntrackedRecordsInteractorImpl
56
import com.example.util.simpletimetracker.core.mapper.AppColorMapperImpl
67
import com.example.util.simpletimetracker.core.provider.ApplicationDataProvider
8+
import com.example.util.simpletimetracker.domain.interactor.GetCurrentDayInteractor
79
import com.example.util.simpletimetracker.domain.interactor.IsSystemInDarkModeInteractor
810
import com.example.util.simpletimetracker.domain.interactor.GetUntrackedRecordsInteractor
911
import com.example.util.simpletimetracker.domain.mapper.AppColorMapper
@@ -32,5 +34,9 @@ interface AppModuleBinds {
3234

3335
@Binds
3436
@Singleton
35-
fun IsSystemInInDarkModeInteractorImpl.bindIsSystemInDarkModeInteractor(): IsSystemInDarkModeInteractor
37+
fun IsSystemInDarkModeInteractorImpl.bindIsSystemInDarkModeInteractor(): IsSystemInDarkModeInteractor
38+
39+
@Binds
40+
@Singleton
41+
fun GetCurrentDayInteractorImpl.bindGetCurrentDayInteractor(): GetCurrentDayInteractor
3642
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.util.simpletimetracker.core.interactor
2+
3+
import com.example.util.simpletimetracker.core.mapper.TimeMapper
4+
import com.example.util.simpletimetracker.domain.interactor.GetCurrentDayInteractor
5+
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
6+
import com.example.util.simpletimetracker.domain.model.DayOfWeek
7+
import java.util.Calendar
8+
import javax.inject.Inject
9+
10+
class GetCurrentDayInteractorImpl @Inject constructor(
11+
private val timeMapper: TimeMapper,
12+
private val prefsInteractor: PrefsInteractor,
13+
) : GetCurrentDayInteractor {
14+
15+
override suspend fun execute(timestamp: Long): DayOfWeek {
16+
return timeMapper.getDayOfWeek(
17+
timestamp = timestamp,
18+
calendar = Calendar.getInstance(),
19+
startOfDayShift = prefsInteractor.getStartOfDayShift(),
20+
)
21+
}
22+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.example.util.simpletimetracker.domain.interactor.IsSystemInDarkModeIn
66
import dagger.hilt.android.qualifiers.ApplicationContext
77
import javax.inject.Inject
88

9-
class IsSystemInInDarkModeInteractorImpl @Inject constructor(
9+
class IsSystemInDarkModeInteractorImpl @Inject constructor(
1010
@ApplicationContext private val context: Context,
1111
) : IsSystemInDarkModeInteractor {
1212

data_local/schemas/com.example.util.simpletimetracker.data_local.database.AppDatabase/19.json

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 19,
5-
"identityHash": "ad241e6d14d6cdd126148f53774f2570",
5+
"identityHash": "a4d6c87d535bb337cf75d6af25b61350",
66
"entities": [
77
{
88
"tableName": "records",
@@ -543,7 +543,7 @@
543543
},
544544
{
545545
"tableName": "complexRules",
546-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `actionType` INTEGER NOT NULL, `actionSetTags` TEXT NOT NULL, `conditionStartingActivity` TEXT NOT NULL, `conditionCurrentActivity` TEXT NOT NULL, `conditionDaysOfWeek` TEXT NOT NULL)",
546+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `disabled` INTEGER NOT NULL, `actionType` INTEGER NOT NULL, `actionSetTagIds` TEXT NOT NULL, `conditionStartingTypeIds` TEXT NOT NULL, `conditionCurrentTypeIds` TEXT NOT NULL, `conditionDaysOfWeek` TEXT NOT NULL)",
547547
"fields": [
548548
{
549549
"fieldPath": "id",
@@ -552,26 +552,32 @@
552552
"notNull": true
553553
},
554554
{
555-
"fieldPath": "actionType",
555+
"fieldPath": "disabled",
556+
"columnName": "disabled",
557+
"affinity": "INTEGER",
558+
"notNull": true
559+
},
560+
{
561+
"fieldPath": "action",
556562
"columnName": "actionType",
557563
"affinity": "INTEGER",
558564
"notNull": true
559565
},
560566
{
561-
"fieldPath": "actionSetTags",
562-
"columnName": "actionSetTags",
567+
"fieldPath": "actionSetTagIds",
568+
"columnName": "actionSetTagIds",
563569
"affinity": "TEXT",
564570
"notNull": true
565571
},
566572
{
567-
"fieldPath": "conditionStartingActivity",
568-
"columnName": "conditionStartingActivity",
573+
"fieldPath": "conditionStartingTypeIds",
574+
"columnName": "conditionStartingTypeIds",
569575
"affinity": "TEXT",
570576
"notNull": true
571577
},
572578
{
573-
"fieldPath": "conditionCurrentActivity",
574-
"columnName": "conditionCurrentActivity",
579+
"fieldPath": "conditionCurrentTypeIds",
580+
"columnName": "conditionCurrentTypeIds",
575581
"affinity": "TEXT",
576582
"notNull": true
577583
},
@@ -595,7 +601,7 @@
595601
"views": [],
596602
"setupQueries": [
597603
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
598-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ad241e6d14d6cdd126148f53774f2570')"
604+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a4d6c87d535bb337cf75d6af25b61350')"
599605
]
600606
}
601607
}

data_local/src/main/java/com/example/util/simpletimetracker/data_local/database/AppDatabaseMigrations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class AppDatabaseMigrations {
251251
private val migration_18_19 = object : Migration(18, 19) {
252252
override fun migrate(database: SupportSQLiteDatabase) {
253253
database.execSQL(
254-
"CREATE TABLE IF NOT EXISTS `complexRules` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `actionType` INTEGER NOT NULL, `actionSetTags` TEXT NOT NULL, `conditionStartingActivity` TEXT NOT NULL, `conditionCurrentActivity` TEXT NOT NULL, `conditionDaysOfWeek` TEXT NOT NULL)",
254+
"CREATE TABLE IF NOT EXISTS `complexRules` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `disabled` INTEGER NOT NULL, `actionType` INTEGER NOT NULL, `actionSetTagIds` TEXT NOT NULL, `conditionStartingTypeIds` TEXT NOT NULL, `conditionCurrentTypeIds` TEXT NOT NULL, `conditionDaysOfWeek` TEXT NOT NULL)",
255255
)
256256
}
257257
}

data_local/src/main/java/com/example/util/simpletimetracker/data_local/database/ComplexRulesDao.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import androidx.room.Dao
44
import androidx.room.Insert
55
import androidx.room.OnConflictStrategy
66
import androidx.room.Query
7+
import androidx.room.Transaction
78
import com.example.util.simpletimetracker.data_local.model.ComplexRuleDBO
89

910
@Dao
1011
interface ComplexRulesDao {
1112

13+
@Transaction
14+
@Query("select exists(select 1 from complexRules)")
15+
suspend fun isEmpty(): Long
16+
1217
@Query("SELECT * FROM complexRules")
1318
suspend fun getAll(): List<ComplexRuleDBO>
1419

@@ -18,6 +23,12 @@ interface ComplexRulesDao {
1823
@Insert(onConflict = OnConflictStrategy.REPLACE)
1924
suspend fun insert(data: ComplexRuleDBO): Long
2025

26+
@Query("UPDATE complexRules SET disabled = 1 WHERE id = :id")
27+
suspend fun disable(id: Long)
28+
29+
@Query("UPDATE complexRules SET disabled = 0 WHERE id = :id")
30+
suspend fun enable(id: Long)
31+
2132
@Query("DELETE FROM complexRules WHERE id = :id")
2233
suspend fun delete(id: Long)
2334

data_local/src/main/java/com/example/util/simpletimetracker/data_local/mapper/ComplexRuleDataLocalMapper.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ class ComplexRuleDataLocalMapper @Inject constructor() {
1010
fun map(dbo: ComplexRuleDBO): ComplexRule {
1111
return ComplexRule(
1212
id = dbo.id,
13-
action = mapActionType(dbo.actionType),
14-
actionSetTagIds = mapIds(dbo.actionSetTags),
15-
conditionStartingTypeIds = mapIds(dbo.conditionStartingActivity),
16-
conditionCurrentTypeIds = mapIds(dbo.conditionCurrentActivity),
13+
disabled = dbo.disabled,
14+
action = mapActionType(dbo.action),
15+
actionAssignTagIds = mapIds(dbo.actionSetTagIds),
16+
conditionStartingTypeIds = mapIds(dbo.conditionStartingTypeIds),
17+
conditionCurrentTypeIds = mapIds(dbo.conditionCurrentTypeIds),
1718
conditionDaysOfWeek = mapDaysOfWeek(dbo.conditionDaysOfWeek),
1819
)
1920
}
2021

2122
fun map(domain: ComplexRule): ComplexRuleDBO {
2223
return ComplexRuleDBO(
2324
id = domain.id,
24-
actionType = mapActionType(domain.action),
25-
actionSetTags = mapIds(domain.actionSetTagIds),
26-
conditionStartingActivity = mapIds(domain.conditionStartingTypeIds),
27-
conditionCurrentActivity = mapIds(domain.conditionCurrentTypeIds),
25+
disabled = domain.disabled,
26+
action = mapActionType(domain.action),
27+
actionSetTagIds = mapIds(domain.actionAssignTagIds),
28+
conditionStartingTypeIds = mapIds(domain.conditionStartingTypeIds),
29+
conditionCurrentTypeIds = mapIds(domain.conditionCurrentTypeIds),
2830
conditionDaysOfWeek = mapDaysOfWeek(domain.conditionDaysOfWeek),
2931
)
3032
}
@@ -37,20 +39,24 @@ class ComplexRuleDataLocalMapper @Inject constructor() {
3739
return domain.joinToString(separator = ",")
3840
}
3941

40-
private fun mapActionType(dbo: Long): ComplexRule.Action {
42+
private fun mapActionType(
43+
dbo: Long,
44+
): ComplexRule.Action {
4145
return when (dbo) {
4246
0L -> ComplexRule.Action.AllowMultitasking
4347
1L -> ComplexRule.Action.DisallowMultitasking
44-
2L -> ComplexRule.Action.SetTag
48+
2L -> ComplexRule.Action.AssignTag
4549
else -> ComplexRule.Action.AllowMultitasking
4650
}
4751
}
4852

49-
private fun mapActionType(domain: ComplexRule.Action): Long {
53+
private fun mapActionType(
54+
domain: ComplexRule.Action,
55+
): Long {
5056
return when (domain) {
5157
is ComplexRule.Action.AllowMultitasking -> 0L
5258
is ComplexRule.Action.DisallowMultitasking -> 1L
53-
is ComplexRule.Action.SetTag -> 2L
59+
is ComplexRule.Action.AssignTag -> 2L
5460
}
5561
}
5662

data_local/src/main/java/com/example/util/simpletimetracker/data_local/model/ComplexRuleDBO.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ import androidx.room.ColumnInfo
44
import androidx.room.Entity
55
import androidx.room.PrimaryKey
66

7-
// TODO RULES rename actionSetTags to actionSetTagIds etc.
87
@Entity(tableName = "complexRules")
98
data class ComplexRuleDBO(
109
@PrimaryKey(autoGenerate = true)
1110
@ColumnInfo(name = "id")
1211
val id: Long,
1312

13+
@ColumnInfo(name = "disabled")
14+
val disabled: Boolean,
15+
1416
@ColumnInfo(name = "actionType")
15-
val actionType: Long,
17+
val action: Long,
1618

1719
// Longs stored in string comma separated
18-
@ColumnInfo(name = "actionSetTags")
19-
val actionSetTags: String,
20+
@ColumnInfo(name = "actionSetTagIds")
21+
val actionSetTagIds: String,
2022

2123
// Longs stored in string comma separated
22-
@ColumnInfo(name = "conditionStartingActivity")
23-
val conditionStartingActivity: String,
24+
@ColumnInfo(name = "conditionStartingTypeIds")
25+
val conditionStartingTypeIds: String,
2426

2527
// Longs stored in string comma separated
26-
@ColumnInfo(name = "conditionCurrentActivity")
27-
val conditionCurrentActivity: String,
28+
@ColumnInfo(name = "conditionCurrentTypeIds")
29+
val conditionCurrentTypeIds: String,
2830

2931
// Days of week stored in string comma separated
3032
@ColumnInfo(name = "conditionDaysOfWeek")

data_local/src/main/java/com/example/util/simpletimetracker/data_local/repo/ComplexRuleRepoImpl.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class ComplexRuleRepoImpl @Inject constructor(
1919
private var cache: List<ComplexRule>? = null
2020
private val mutex: Mutex = Mutex()
2121

22+
override suspend fun isEmpty(): Boolean = mutex.withLockedCache(
23+
logMessage = "isEmpty",
24+
accessCache = { cache?.isEmpty() },
25+
accessSource = { dao.isEmpty() == 0L },
26+
)
27+
2228
override suspend fun getAll(): List<ComplexRule> = mutex.withLockedCache(
2329
logMessage = "getAll",
2430
accessCache = { cache },
@@ -38,6 +44,18 @@ class ComplexRuleRepoImpl @Inject constructor(
3844
afterSourceAccess = { cache = null },
3945
)
4046

47+
override suspend fun disable(id: Long) = mutex.withLockedCache(
48+
logMessage = "disable",
49+
accessSource = { dao.disable(id) },
50+
afterSourceAccess = { cache = cache?.map { if (it.id == id) it.copy(disabled = true) else it } },
51+
)
52+
53+
override suspend fun enable(id: Long) = mutex.withLockedCache(
54+
logMessage = "enable",
55+
accessSource = { dao.enable(id) },
56+
afterSourceAccess = { cache = cache?.map { if (it.id == id) it.copy(disabled = false) else it } },
57+
)
58+
4159
override suspend fun remove(id: Long) = mutex.withLockedCache(
4260
logMessage = "remove",
4361
accessSource = { dao.delete(id) },
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.util.simpletimetracker.core.extension
1+
package com.example.util.simpletimetracker.domain.extension
22

33
/**
44
* Adds item if it not in the list, otherwise removes it from the list.

0 commit comments

Comments
 (0)