diff --git a/src/Main.elm b/src/Main.elm index 627488e..5a71908 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -81,6 +81,7 @@ type Msg | Msg_Autoscroll_Toggle | Msg_DEsmpl_Open Bool | Msg_DEsmpl_EClick Int + | Msg_DEsmpl_LoadWithDelay Example | Msg_DEsmpl_ExampleLoaded (Result Http.Error Model) | Msg_DEsmpl_ListLoaded (Result Http.Error (List Example)) | Msg_DEsmpl_Download @@ -88,6 +89,7 @@ type Msg | Msg_DrgDrp_Hover Bool | Msg_DrgDrp_GotFiles File (List File) | Msg_DrgDrp_DecodedFiles String + | Msg_Nothing update : Msg -> Model -> ( Model, Cmd Msg ) @@ -138,16 +140,23 @@ update msg model = Msg_DEsmpl_EClick i -> -- User chose example. Load it. case valueAt i model.examples of Just example -> - ( model - , Http.get - { url = example.url - , expect = Http.expectJson Msg_DEsmpl_ExampleLoaded modelDecoder - } + ( { model | dragDrop = { old_dragDrop | state = DS_Processing } } + , Task.perform + (\_ -> Msg_DEsmpl_LoadWithDelay example) + <| Process.sleep 500 ) Nothing -> ( model, Cmd.none ) + Msg_DEsmpl_LoadWithDelay example -> + ( model + , Http.get + { url = example.url + , expect = Http.expectJson Msg_DEsmpl_ExampleLoaded modelDecoder + } + ) + Msg_DEsmpl_ExampleLoaded result -> -- When a single example is loaded case result of Ok example_model -> @@ -156,12 +165,17 @@ update msg model = { model | pc_model = example_model.pc_model , exampleLoaderStatus = Success - , exampleChooserOpen = False + , exampleChooserOpen = False + , dragDrop = { old_dragDrop | state = DS_Sucess } } in - ( new_model, cmd_up_lStorage_n_Scroller new_model ) + ( new_model + , cmd_up_lStorage_n_Scroller new_model + ) Err err -> - ( { model | exampleLoaderStatus = Failure <| printHttpError err } + ( { model | exampleLoaderStatus = Failure <| printHttpError err + , dragDrop = { old_dragDrop | state = DS_Sucess } + } , Cmd.none ) @@ -243,6 +257,8 @@ update msg model = , sendProcessing "Done" ) + Msg_Nothing -> (model, Cmd.none) + -- ############################################################################### @@ -453,10 +469,10 @@ viewDragDrop model = , p [] [ case (model.dragDrop.hover, model.dragDrop.state) of - (_,DS_Failure) -> text <| "Oh no, that didn't work. " ++ Debug.toString model.dragDrop - (_,DS_Processing) -> text "Processing. Hold on tight." + (_,DS_Failure) -> text <| "Oh no, that didn't work. " + (_,DS_Processing) -> text "Loading. Hold on tight." (True,_) -> text "Let go to load the file." - (_,DS_Sucess) -> text <| "Success" ++ Debug.toString model.dragDrop + (_,DS_Sucess) -> text <| "Success" (_,DS_Ready) -> text "Let go to load the file." ] ] @@ -513,17 +529,29 @@ subscriptions model = -- TODO: Make it load localStorage when starting -init : () -> ( Model, Cmd Msg ) -init flags = - ({ pc_model = PC.init - , autoscroll = True - , examples = [] - , exampleChooserOpen = False - , examplesListStatus = Waiting - , exampleLoaderStatus = Waiting - , dragDrop = initDragDrop - } - , Cmd.none ) +init : String -> ( Model, Cmd Msg ) +init mayLocalStorage = + case doDecodeModel mayLocalStorage of + Just init_model -> + ({ pc_model = init_model.pc_model + , autoscroll = init_model.autoscroll + , examples = [] + , exampleChooserOpen = False + , examplesListStatus = Waiting + , exampleLoaderStatus = Waiting + , dragDrop = initDragDrop + } + , Cmd.none ) + Nothing -> + ({ pc_model = PC.init + , autoscroll = True + , examples = [] + , exampleChooserOpen = False + , examplesListStatus = Waiting + , exampleLoaderStatus = Waiting + , dragDrop = initDragDrop + } + , Cmd.none ) initDragDrop : DragDrop initDragDrop = @@ -535,7 +563,7 @@ initDragDrop = -main : Program () Model Msg +main : Program String Model Msg main = Browser.element { init = init