Skip to content

Commit

Permalink
settings-sync: Do not conflict if the user changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl authored and ArturoManzoli committed Aug 14, 2024
1 parent bab7bfd commit 6f22b1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/composables/settingsSyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export function useBlueOsStorage<T>(key: string, defaultValue: MaybeRef<T>): Rem
return vehicleStore.lastConnectedVehicleId
}

const getLastConnectedUser = async (): Promise<string | undefined> => {
const missoinStore = useMissionStore()
return missoinStore.lastConnectedUser
}

const askIfUserWantsToUseBlueOsValue = async (): Promise<boolean> => {
let useBlueOsValue = true

Expand Down Expand Up @@ -146,6 +151,7 @@ export function useBlueOsStorage<T>(key: string, defaultValue: MaybeRef<T>): Rem
const username = await getUsername()
const currentVehicleId = await getCurrentVehicleId()
const lastConnectedVehicleId = await getLastConnectedVehicleId()
const lastConnectedUser = await getLastConnectedUser()

// Clear initial sync routine if there's one left, as we are going to start a new one
clearTimeout(initialSyncTimeout)
Expand All @@ -164,11 +170,12 @@ export function useBlueOsStorage<T>(key: string, defaultValue: MaybeRef<T>): Rem
// By default, if there's a conflict, we use the value from BlueOS.
let useBlueOsValue = true

// If the connected vehicle is the same as the last connected vehicle, and there are conflicts, it means the user
// has made changes while offline, so we ask the user if they want to keep the local value or the one from BlueOS.
// If the connected vehicle is the same as the last connected vehicle, and the user is also the same, and there
// are conflicts, it means the user has made changes while offline, so we ask the user if they want to keep the
// local value or the one from BlueOS.
// If the connected vehicle is different from the last connected vehicle, we just use the value from BlueOS, as we
// don't want to overwrite the value on the new vehicle with the one from the previous vehicle.
if (lastConnectedVehicleId === currentVehicleId) {
if (lastConnectedUser === username && lastConnectedVehicleId === currentVehicleId) {
console.debug(`Conflict with BlueOS for key '${key}'. Asking user what to do.`)
useBlueOsValue = await askIfUserWantsToUseBlueOsValue()
}
Expand Down
5 changes: 5 additions & 0 deletions src/stores/mission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Waypoint, WaypointCoordinates } from '@/types/mission'

export const useMissionStore = defineStore('mission', () => {
const username = useStorage<string>('cockpit-username', '')
const lastConnectedUser = localStorage.getItem('cockpit-last-connected-user') || undefined
const missionName = ref('')
const slideEventsEnabled = useBlueOsStorage('cockpit-slide-events-enabled', true)
const slideEventsCategoriesRequired = useBlueOsStorage(
Expand Down Expand Up @@ -62,6 +63,9 @@ export const useMissionStore = defineStore('mission', () => {
}

onMounted(async () => {
// If theres a username saved, assign it as the last connected user
localStorage.setItem('cockpit-last-connected-user', username.value)
console.log(`Last connected user set to ${username.value}.`)
if (username.value) return

// If no username is set, ask the user to enter one
Expand All @@ -70,6 +74,7 @@ export const useMissionStore = defineStore('mission', () => {

return {
username,
lastConnectedUser,
changeUsername,
missionName,
lastMissionName,
Expand Down

0 comments on commit 6f22b1c

Please sign in to comment.