Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
seamuslowry committed Sep 9, 2024
1 parent 92707e4 commit 3502730
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ val MIGRATION_7_8: Migration = object : Migration(7, 8) {
db.execSQL("ALTER TABLE Item_Configuration ADD COLUMN orderOverride INTEGER;")
db.execSQL("ALTER TABLE Item_Configuration ADD COLUMN lastModifiedDate INTEGER NOT NULL DEFAULT(unixepoch('subsec') * 1000);")
// trigger to update last modified date on updates
db.execSQL("""
db.execSQL(
"""
CREATE TRIGGER update_lastModifiedDate_trigger
AFTER UPDATE ON Item_Configuration
FOR EACH ROW
Expand All @@ -24,6 +25,7 @@ val MIGRATION_7_8: Migration = object : Migration(7, 8) {
SET lastModifiedDate = unixepoch('subsec') * 1000
WHERE id = OLD.id;
END;
""")
""",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class ItemConfiguration(
val trackingType: TrackingType = LimitedOptionTrackingType.ONE_TO_TEN,
val active: Boolean = true,
val orderOverride: Long? = null,
val lastModifiedDate: Instant = Instant.now()
val lastModifiedDate: Instant = Instant.now(),
) : Comparable<ItemConfiguration> {
override fun compareTo(other: ItemConfiguration): Int = order.compareTo(other.order)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -57,7 +58,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
import io.github.fornewid.placeholder.foundation.PlaceholderHighlight
import io.github.fornewid.placeholder.material3.fade
import io.github.fornewid.placeholder.material3.placeholder
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import seamuslowry.daytracker.R
import seamuslowry.daytracker.models.Item
Expand All @@ -70,6 +70,7 @@ import seamuslowry.daytracker.ui.shared.ArrowPicker
import seamuslowry.daytracker.ui.shared.TrackerEntry
import sh.calvin.reorderable.ReorderableItem
import sh.calvin.reorderable.rememberReorderableLazyListState
import java.time.Instant
import java.time.LocalDate

val SUPPORTED_TRACKING_TYPES = listOf(
Expand All @@ -83,33 +84,49 @@ val SUPPORTED_TRACKING_TYPES = listOf(
fun EntryScreen(
viewModel: EntryViewModel = hiltViewModel(),
) {
val items by viewModel.items.collectAsState()
val savedItems by viewModel.items.collectAsState()
val itemsLoading by viewModel.itemsLoading.collectAsState()
val state = viewModel.state
val date by viewModel.date.collectAsState()
val scope = rememberCoroutineScope()

val awaitingSaveItems = remember { mutableStateListOf<ItemWithConfiguration>() }

var items by remember {
mutableStateOf(savedItems)
}

LaunchedEffect(savedItems) {
items = mergeItemWithConfigurations(savedItems, awaitingSaveItems)
}

Log.d(TAG, "START - ${savedItems == items}")

Log.d(TAG, savedItems.toString())
Log.d(TAG, awaitingSaveItems.toString())
Log.d(TAG, items.toString())

val itemsUpdatedChannel = remember { Channel<Unit>() }
Log.d(TAG, "END - ${savedItems == items}")

val lazyColumnState = rememberLazyListState()
val reorderableLazyColumnState = rememberReorderableLazyListState(
lazyListState = lazyColumnState,
scrollThresholdPadding = WindowInsets.systemBars.asPaddingValues(),
) { from, to ->
val fromConfiguration = items.find { it.item.id == from.key }?.configuration ?: return@rememberReorderableLazyListState
val toConfiguration = items.find { it.item.id == to.key }?.configuration ?: return@rememberReorderableLazyListState

viewModel.swap(fromConfiguration, toConfiguration)
val fromElement = items.find { it.item.id == from.key } ?: return@rememberReorderableLazyListState
val toElement = items.find { it.item.id == to.key } ?: return@rememberReorderableLazyListState

// wait for the items to update
itemsUpdatedChannel.receive()
}
awaitingSaveItems.addAll(
listOf(
fromElement.copy(configuration = fromElement.configuration.copy(orderOverride = toElement.configuration.order, lastModifiedDate = Instant.now())),
toElement.copy(configuration = toElement.configuration.copy(orderOverride = fromElement.configuration.order, lastModifiedDate = Instant.now())),
),
)
items = mergeItemWithConfigurations(savedItems, awaitingSaveItems)
Log.d(TAG, "items immediate $items")

LaunchedEffect(items) {
// notify the list is updated
itemsUpdatedChannel.trySend(Unit)
viewModel.swap(fromElement.configuration, toElement.configuration)
Log.d(TAG, "ENDING swap")
}

LazyColumn(
Expand Down Expand Up @@ -162,6 +179,15 @@ fun EntryScreen(
}
}

private fun mergeItemWithConfigurations(
savedItems: List<ItemWithConfiguration>,
awaitingSaveItems: List<ItemWithConfiguration>,
) = (savedItems + awaitingSaveItems)
.groupBy { it.configuration.id }
.mapNotNull { (_, values) ->
values.maxByOrNull { it.configuration.lastModifiedDate }
}.sorted()

@Composable
fun ItemEntry(
modifier: Modifier = Modifier,
Expand Down

0 comments on commit 3502730

Please sign in to comment.