diff --git a/src/Constraint.elm b/src/Constraint.elm index d7cf12e..32bb1ba 100644 --- a/src/Constraint.elm +++ b/src/Constraint.elm @@ -1,4 +1,4 @@ -module Constraint exposing (Model, init, positions) +module Constraint exposing (Model, init, positions, updateAttachments) import Dict exposing (Dict) @@ -50,7 +50,7 @@ solveWithoutFocus (Model guts) = Dict.get id guts.heights |> Maybe.withDefault 0 in - if Debug.log "ideal position" idealPosition >= Debug.log "progress line" progressLine then + if idealPosition >= progressLine then ( Dict.insert id idealPosition finalPositions , idealPosition + height + guts.margin ) @@ -65,6 +65,15 @@ solveWithoutFocus (Model guts) = } +updateAttachments : Dict Int Float -> Model -> Model +updateAttachments attachments (Model guts) = + if attachments == guts.attachments then + Model guts + + else + Model { guts | attachments = attachments } |> solveWithoutFocus + + positions : Model -> Dict Int Float positions (Model guts) = guts.positions diff --git a/src/Main.elm b/src/Main.elm index 2bd7eb1..4698292 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -88,8 +88,15 @@ update msg model = Nothing -> ( model, Cmd.none ) - AttachmentsMoved tops -> - ( model, Cmd.none ) + AttachmentsMoved attachments -> + ( { model + | commentPositions = + Maybe.map + (Constraint.updateAttachments (finalAttachments attachments)) + model.commentPositions + } + , Cmd.none + ) SetUpCommentConstraints ( attachments, commentHeights ) -> ( { model @@ -97,22 +104,7 @@ update msg model = Just <| Constraint.init { heights = Dict.fromList commentHeights - , attachments = - List.foldl - (\( commentId, top ) soFar -> - Dict.update commentId - (\current -> - case current of - Nothing -> - Just top - - Just otherTop -> - Just (min top otherTop) - ) - soFar - ) - Dict.empty - attachments + , attachments = finalAttachments attachments , margin = 10 } } @@ -120,6 +112,24 @@ update msg model = ) +finalAttachments : List ( Int, Float ) -> Dict Int Float +finalAttachments = + List.foldl + (\( commentId, top ) soFar -> + Dict.update commentId + (\current -> + case current of + Nothing -> + Just top + + Just otherTop -> + Just (min top otherTop) + ) + soFar + ) + Dict.empty + + findNewAttachmentTopsTask : List Attachment -> Task Never (List ( Int, Float )) findNewAttachmentTopsTask attachments = -- TODO: this may need Process.sleep 0 to be accurate in all cases