Skip to content

Commit

Permalink
update attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Feb 18, 2020
1 parent ca7597c commit 348de0e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
13 changes: 11 additions & 2 deletions src/Constraint.elm
@@ -1,4 +1,4 @@
module Constraint exposing (Model, init, positions)
module Constraint exposing (Model, init, positions, updateAttachments)

import Dict exposing (Dict)

Expand Down Expand Up @@ -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
)
Expand All @@ -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
46 changes: 28 additions & 18 deletions src/Main.elm
Expand Up @@ -88,38 +88,48 @@ 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
| commentPositions =
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
}
}
, Cmd.none
)


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
Expand Down

0 comments on commit 348de0e

Please sign in to comment.