From ccb8cbffb85b29191a7283ae093ef416c00b47f1 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Sat, 11 Jul 2020 16:19:53 +0200 Subject: [PATCH] Closes #192 - Add ability to migrate between data-storage methods --- CHANGELOG.md | 1 + src/Applications/Brain.elm | 1 + src/Applications/Brain/Types.elm | 1 + src/Applications/Brain/User/State.elm | 57 +++++++---- src/Applications/UI.elm | 5 + src/Applications/UI/Authentication/State.elm | 94 ++++++++++++------- src/Applications/UI/Settings/ImportExport.elm | 75 ++++++++++----- src/Applications/UI/Types.elm | 2 + src/Applications/UI/User/State.elm | 14 +++ 9 files changed, 172 insertions(+), 78 deletions(-) create mode 100644 src/Applications/UI/User/State.elm diff --git a/CHANGELOG.md b/CHANGELOG.md index 228a2c30..475fc9b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - **Adds album-covers view** (switch to and from list view with icon in nav bar) - Added new background images and replaced two older ones - Adds a keyboard shortcut `L` to quickly select a playlist +- Adds the ability to migrate between data-storage methods - Dark mode for the about page - Fixes issue with shuffle algorithm - Fixes playback issues (eg. clicking same track multiple times) diff --git a/src/Applications/Brain.elm b/src/Applications/Brain.elm index 9849521c..d83f1d73 100644 --- a/src/Applications/Brain.elm +++ b/src/Applications/Brain.elm @@ -74,6 +74,7 @@ init flags = , hypaethralStorage = [] , hypaethralUserData = User.emptyHypaethralData , legacyMode = False + , migratingData = False , origin = "ORIGIN_UNKNOWN" , performingSignIn = False , processingStatus = Processing.NotProcessing diff --git a/src/Applications/Brain/Types.elm b/src/Applications/Brain/Types.elm index b1ff51f1..2b30b3c4 100644 --- a/src/Applications/Brain/Types.elm +++ b/src/Applications/Brain/Types.elm @@ -31,6 +31,7 @@ type alias Model = , hypaethralStorage : List HypaethralBit , hypaethralUserData : User.HypaethralData , legacyMode : Bool + , migratingData : Bool , origin : String , performingSignIn : Bool , processingStatus : Processing.Status diff --git a/src/Applications/Brain/User/State.elm b/src/Applications/Brain/User/State.elm index c6a15dea..6cb25430 100644 --- a/src/Applications/Brain/User/State.elm +++ b/src/Applications/Brain/User/State.elm @@ -163,20 +163,34 @@ signIn json model = -- and retrieve data. let decoder = - Decode.map2 - Tuple.pair + Decode.map3 + (\a b c -> ( a, Maybe.withDefault False b, c )) (Decode.field "method" <| Decode.map methodFromString Decode.string) + (Decode.field "migratingData" <| Decode.maybe Decode.bool) (Decode.field "passphrase" <| Decode.maybe Decode.string) in case Decode.decodeValue decoder json of - Ok ( maybeMethod, Just passphrase ) -> + Ok ( maybeMethod, migratingData, Just passphrase ) -> fabricateSecretKey passphrase - { model | authMethod = maybeMethod, performingSignIn = True } + { model + | authMethod = maybeMethod + , migratingData = migratingData + , performingSignIn = True + } - Ok ( maybeMethod, Nothing ) -> - retrieveAllHypaethralData - { model | authMethod = maybeMethod, performingSignIn = True } + Ok ( maybeMethod, migratingData, Nothing ) -> + (if migratingData then + hypaethralDataRetrieved Json.null + + else + retrieveAllHypaethralData + ) + { model + | authMethod = maybeMethod + , migratingData = migratingData + , performingSignIn = True + } _ -> Return.singleton model @@ -687,7 +701,11 @@ removeEncryptionKey = secretKeyFabricated : Manager secretKeyFabricated model = if model.performingSignIn then - retrieveAllHypaethralData model + if model.migratingData then + hypaethralDataRetrieved Json.null model + + else + retrieveAllHypaethralData model else saveAllHypaethralData model @@ -716,15 +734,20 @@ terminate : Termination -> Manager terminate t model = case t of Authenticated method encodedData -> - let - decodedData = - encodedData - |> User.decodeHypaethralData - |> Result.withDefault model.hypaethralUserData - in - model - |> sendHypaethralDataToUI encodedData decodedData - |> andThen (Common.giveUI Alien.AuthMethod <| encodeMethod method) + { model | migratingData = False } + |> (if model.migratingData then + Return.singleton + + else + encodedData + |> User.decodeHypaethralData + |> Result.withDefault model.hypaethralUserData + |> sendHypaethralDataToUI encodedData + ) + |> (encodeMethod method + |> Common.giveUI Alien.AuthMethod + |> andThen + ) NotAuthenticated -> model diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index b8c075a9..a06244bc 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -54,6 +54,7 @@ import UI.Tracks.ContextMenu as Tracks import UI.Tracks.State as Tracks import UI.Tracks.Types as Tracks import UI.Types exposing (..) +import UI.User.State as User import UI.User.State.Export as User import UI.User.State.Import as User import UI.View exposing (view) @@ -105,6 +106,7 @@ init flags url key = , isTouchDevice = False , isUpgrading = flags.upgrade , lastFm = LastFm.initialModel + , migratingData = False , navKey = key , page = page , pressedKeys = [] @@ -466,6 +468,9 @@ update msg = LoadHypaethralUserData a -> User.loadHypaethralUserData a + MigrateHypaethralUserData -> + User.migrateHypaethralUserData + RequestImport -> User.requestImport diff --git a/src/Applications/UI/Authentication/State.elm b/src/Applications/UI/Authentication/State.elm index e14fe72b..0435c311 100644 --- a/src/Applications/UI/Authentication/State.elm +++ b/src/Applications/UI/Authentication/State.elm @@ -31,6 +31,7 @@ import UI.Common.State as Common exposing (showNotification, showNotificationWit import UI.Ports as Ports import UI.Sources.State as Sources import UI.Types as UI exposing (..) +import UI.User.State.Import as User import Url exposing (Protocol(..), Url) import Url.Ext as Url import User.Layer exposing (..) @@ -336,20 +337,34 @@ showMoreOptions mouseEvent model = signedIn : Json.Value -> Manager -signedIn json = +signedIn json model = -- 🧠 told me which auth method we're using, -- so we can tell the user in the UI. case decodeMethod json of Just method -> - replaceState (Authenticated method) + model + |> replaceState + (Authenticated method) + |> andThen + (\m -> + if m.migratingData then + "Migrated data successfully" + |> Notifications.success + |> showNotificationWithModel { m | migratingData = False } + |> User.saveAllHypaethralData + + else + Return.singleton m + ) Nothing -> - Return.singleton + Return.singleton model signIn : Method -> Manager signIn method model = [ ( "method", encodeMethod method ) + , ( "migratingData", Json.Encode.bool model.migratingData ) , ( "passphrase", Json.Encode.null ) ] |> Json.Encode.object @@ -369,6 +384,7 @@ signInWithPassphrase method passphrase model = else [ ( "method", encodeMethod method ) + , ( "migratingData", Json.Encode.bool model.migratingData ) , ( "passphrase", Json.Encode.string <| hashPassphrase passphrase ) ] |> Json.Encode.object @@ -381,40 +397,46 @@ signInWithPassphrase method passphrase model = signOut : Manager signOut model = - { model - | authentication = Authentication.Unauthenticated - , playlists = [] - , playlistToActivate = Nothing - - -- Queue - -------- - , dontPlay = [] - , nowPlaying = Nothing - , playedPreviously = [] - , playingNext = [] - , selectedQueueItem = Nothing + if model.migratingData then + return + { model | authentication = Authentication.Unauthenticated } + (Ports.toBrain <| Alien.trigger Alien.SignOut) - -- - , repeat = False - , shuffle = False - - -- Sources - ---------- - , processingContext = [] - , sources = [] - - -- Tracks - --------- - , favourites = [] - , hideDuplicates = False - , searchResults = Nothing - , tracks = Tracks.emptyCollection - } - |> Backdrop.setDefault - |> Return.andThen Sources.stopProcessing - |> Return.command (Ports.toBrain <| Alien.trigger Alien.SignOut) - |> Return.command (Ports.activeQueueItemChanged Nothing) - |> Return.command (Nav.pushUrl model.navKey "#/") + else + { model + | authentication = Authentication.Unauthenticated + , playlists = [] + , playlistToActivate = Nothing + + -- Queue + -------- + , dontPlay = [] + , nowPlaying = Nothing + , playedPreviously = [] + , playingNext = [] + , selectedQueueItem = Nothing + + -- + , repeat = False + , shuffle = False + + -- Sources + ---------- + , processingContext = [] + , sources = [] + + -- Tracks + --------- + , favourites = [] + , hideDuplicates = False + , searchResults = Nothing + , tracks = Tracks.emptyCollection + } + |> Backdrop.setDefault + |> Return.andThen Sources.stopProcessing + |> Return.command (Ports.toBrain <| Alien.trigger Alien.SignOut) + |> Return.command (Ports.activeQueueItemChanged Nothing) + |> Return.command (Nav.pushUrl model.navKey "#/") startFlow : Manager diff --git a/src/Applications/UI/Settings/ImportExport.elm b/src/Applications/UI/Settings/ImportExport.elm index c920bae4..fb4fff63 100644 --- a/src/Applications/UI/Settings/ImportExport.elm +++ b/src/Applications/UI/Settings/ImportExport.elm @@ -46,31 +46,56 @@ view userLayerMethod = |> raw |> UI.Kit.intro - -- Import - --------- - , chunk [ C.mb_2, C.mt_8 ] [ UI.Kit.label [] "Import" ] - , UI.Kit.buttonWithColor - UI.Kit.Gray - Normal - RequestImport - (text "Choose file") - , case userLayerMethod of - Just Local -> - otherImportOptions - - Just (RemoteStorage _) -> - otherImportOptions - - _ -> - nothing - - -- Export - --------- - , chunk [ C.mb_2, C.mt_8 ] [ UI.Kit.label [] "Export" ] - , UI.Kit.button - Normal - Export - (text "Export data") + -- + , chunk + [ C.sm__flex, C.sm__minus_mt_6 ] + [ -- Import + --------- + chunk + [ C.flex_auto, C.pr_2 ] + [ chunk [ C.mb_2, C.mt_8 ] [ UI.Kit.label [] "Import" ] + , UI.Kit.buttonWithColor + UI.Kit.Gray + Normal + RequestImport + (text "Choose file") + + -- + , case userLayerMethod of + Just Local -> + otherImportOptions + + Just (RemoteStorage _) -> + otherImportOptions + + _ -> + nothing + ] + + -- Export + --------- + , chunk + [ C.flex_auto, C.pl_2 ] + [ chunk [ C.mb_2, C.mt_8 ] [ UI.Kit.label [] "Export" ] + , UI.Kit.button + Normal + Export + (text "Export data") + + -- + , chunk + [ C.italic, C.leading_normal, C.mt_5, C.text_xs ] + [ text "Other options:" ] + , chunk + [ C.leading_normal, C.mt_2, C.text_sm ] + [ inline [ C.mr_2 ] [ text "•" ] + , UI.Kit.textButton + { label = "Migrate to another storage" + , onClick = MigrateHypaethralUserData + } + ] + ] + ] ] ] diff --git a/src/Applications/UI/Types.elm b/src/Applications/UI/Types.elm index cc676903..9e3f0934 100644 --- a/src/Applications/UI/Types.elm +++ b/src/Applications/UI/Types.elm @@ -70,6 +70,7 @@ type alias Model = , isTouchDevice : Bool , isUpgrading : Bool , lastFm : LastFm.Model + , migratingData : Bool , navKey : Nav.Key , page : Page , pressedKeys : List Keyboard.Key @@ -279,6 +280,7 @@ type Msg | InsertDemo | LoadEnclosedUserData Json.Decode.Value | LoadHypaethralUserData Json.Decode.Value + | MigrateHypaethralUserData | RequestImport | SaveEnclosedUserData ----------------------------------------- diff --git a/src/Applications/UI/User/State.elm b/src/Applications/UI/User/State.elm new file mode 100644 index 00000000..322a2f57 --- /dev/null +++ b/src/Applications/UI/User/State.elm @@ -0,0 +1,14 @@ +module UI.User.State exposing (..) + +import Return exposing (return) +import UI.Authentication.State as Authentication +import UI.Types as UI exposing (..) + + + +-- 🔱 + + +migrateHypaethralUserData : Manager +migrateHypaethralUserData model = + Authentication.signOut { model | migratingData = True } -- knotmirror 0.0.1