Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/App/Maybe.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module App.Maybe exposing (catMaybes, join, maybeToCommand)
module App.Maybe exposing (catMaybes, isSomething, join, maybeToCommand)

import Message exposing (Msg)

Expand All @@ -22,3 +22,13 @@ maybeToCommand : (a -> Cmd Msg) -> Maybe a -> Cmd Msg
maybeToCommand toCommand m =
Maybe.map toCommand m
|> Maybe.withDefault Cmd.none


isSomething : Maybe a -> Bool
isSomething maybe =
case maybe of
Just _ ->
True

Nothing ->
False
23 changes: 18 additions & 5 deletions app/App/Settings.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module App.Settings
, stop
, dismissedAlertIds
, deviceToken
, promptedForNotifications
, promptedForNotificationsKey
)

import Dict exposing (Dict)
import App.Maybe exposing (join)
import App.Maybe exposing (isSomething, join)
import Types exposing (..)


Expand All @@ -29,6 +31,7 @@ allKeys =
[ dismissedAlertsKey
, stopKey
, deviceTokenKey
, promptedForNotificationsKey
]


Expand All @@ -52,14 +55,14 @@ getValue settings key =
join <| Dict.get key (toDict settings)


stop : Settings -> Maybe String
stop : Settings -> Maybe Stop
stop settings =
getValue settings stopKey
Maybe.map Stop (getValue settings stopKey)


deviceToken : Settings -> Maybe String
deviceToken : Settings -> Maybe DeviceToken
deviceToken settings =
getValue settings deviceTokenKey
Maybe.map DeviceToken (getValue settings deviceTokenKey)


dismissedAlertIds : Settings -> List Int
Expand All @@ -68,3 +71,13 @@ dismissedAlertIds settings =
|> Maybe.withDefault ""
|> String.split ","
|> List.filterMap (Result.toMaybe << String.toInt)


promptedForNotifications : Settings -> Bool
promptedForNotifications settings =
isSomething (getValue settings promptedForNotificationsKey)


promptedForNotificationsKey : String
promptedForNotificationsKey =
"promptedForCancellationsNotifications"
42 changes: 38 additions & 4 deletions app/App/Settings/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module App.Settings.Update exposing (..)
import App.Settings as Settings
import App.Maybe exposing (maybeToCommand)
import FetchAlertsAndSchedules exposing (fetchAlertsAndSchedules)
import NativeUi.Alert exposing (alert)
import NativeUi.AsyncStorage as AsyncStorage
import UpsertInstallation exposing (upsertInstallation)
import Message exposing (..)
import Model exposing (Model)
import Types exposing (..)
import Task


receiveSettings : Model -> SettingsResult -> ( Model, Cmd Msg )
receiveSettings model settingsResult =
receiveSettingsResult : Model -> SettingsResult -> ( Model, Cmd Msg )
receiveSettingsResult model settingsResult =
case settingsResult of
Err _ ->
( model, Cmd.none )
Expand All @@ -35,6 +38,37 @@ onReceiveSettings settings =
Settings.stop settings
in
Cmd.batch
[ maybeStop |> maybeToCommand fetchAlertsAndSchedules
, maybeStop |> maybeToCommand (upsertInstallation maybeDeviceToken)
[ maybePromptForPushNotifications settings
, maybeStop |> maybeToCommand fetchAlertsAndSchedules
, Maybe.map2 upsertInstallation maybeStop maybeDeviceToken
|> Maybe.withDefault Cmd.none
]


maybePromptForPushNotifications : Settings -> Cmd Msg
maybePromptForPushNotifications settings =
if Settings.promptedForNotifications settings then
Cmd.none
else
Cmd.batch
[ setPromptedForNotifications
, prePromptForPushNotifications
]


setPromptedForNotifications : Cmd Msg
setPromptedForNotifications =
Task.attempt
SetItem
(AsyncStorage.setItem Settings.promptedForNotificationsKey "something")


prePromptForPushNotifications : Cmd Msg
prePromptForPushNotifications =
Task.attempt ReceivePushPrePromptResponse <|
alert
"This is what it sounds like when trains cry"
"Purple Train can send you notifications when your trains are cancelled!"
[ { text = "Not Now", value = False }
, { text = "Give Access", value = True }
]
20 changes: 11 additions & 9 deletions app/FetchAlerts.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ fetchAlerts stop =
Http.send ReceiveAlerts <| getAlerts stop


getAlerts : String -> Http.Request Alerts
getAlerts stopId =
Http.get
(baseUrl
++ "/api/v2/stops/"
++ (Http.encodeUri stopId)
++ "/alerts"
)
decodeAlerts
getAlerts : Stop -> Http.Request Alerts
getAlerts stop =
case stop of
Stop stopId ->
Http.get
(baseUrl
++ "/api/v2/stops/"
++ (Http.encodeUri stopId)
++ "/alerts"
)
decodeAlerts


decodeAlerts : Decode.Decoder Alerts
Expand Down
2 changes: 1 addition & 1 deletion app/FetchSchedule.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ getSchedule direction stop =
Http.get
(baseUrl
++ "/api/v2/stops/"
++ (Http.encodeUri stop)
++ (Http.encodeUri <| stopToString stop)
++ "/"
++ toString direction
++ "/trips"
Expand Down
2 changes: 1 addition & 1 deletion app/FetchStops.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ decodeStops =

decodeStop : Decode.Decoder Stop
decodeStop =
Decode.string
Decode.map Stop Decode.string
2 changes: 2 additions & 0 deletions app/Message.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ type Msg
| ToggleAlerts
| ToggleStopPicker
| DeviceTokenChanged String
| ReceivePushPrePromptResponse (Result String Bool)
| ReceivePushToken (Result String String)
28 changes: 28 additions & 0 deletions app/PushNotifications/Update.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module PushNotifications.Update exposing (..)

import NativeUi.Alert as NativeAlert exposing (alert)
import NativeApi.PushNotificationIOS as PushNotificationIOS exposing (register)
import Task
import Message exposing (..)
import UpsertInstallation exposing (upsertInstallation)
import Types exposing (..)


handlePushPrePromptResponse : Result String Bool -> Cmd Msg
handlePushPrePromptResponse result =
case result of
Ok True ->
Task.attempt ReceivePushToken register

_ ->
Cmd.none


receiveDeviceToken : Maybe Stop -> DeviceToken -> Cmd Msg
receiveDeviceToken maybeStop deviceToken =
case maybeStop of
Just stop ->
upsertInstallation stop deviceToken

Nothing ->
Cmd.none
2 changes: 1 addition & 1 deletion app/ReportIssue.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ issueBody direction stop =
Http.jsonBody <|
Encode.object
[ ( "direction", Encode.string <| toString direction )
, ( "stop_id", Encode.string stop )
, ( "stop_id", Encode.string <| stopToString stop )
]


Expand Down
8 changes: 4 additions & 4 deletions app/StopPicker/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ pickerContainer =
]


pickerButton : Msg -> String -> Node Msg
pickerButton message label =
pickerButton : Msg -> Stop -> Node Msg
pickerButton message stop =
Elements.touchableHighlight
[ onPress message
, underlayColor Color.defaultUnderlay
, buttonStyle Color.white
, key label
, key <| stopToString stop
]
[ Elements.view
[]
[ text
[ buttonTextStyle ]
[ Ui.string label ]
[ Ui.string <| stopToString stop ]
]
]

Expand Down
2 changes: 1 addition & 1 deletion app/StopPickerButton/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ stopPickerLabelText { stopPickerOpen, selectedStop } =
"Select your home stop"

( _, Just stop ) ->
stop
stopToString stop


maybeStopPicker : Model -> DataSource Stop -> Maybe (Node Msg)
Expand Down
5 changes: 3 additions & 2 deletions app/Stops/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ pickStop model stop =
, Cmd.batch <|
[ Task.attempt
SetItem
(AsyncStorage.setItem Settings.stopKey stop)
(AsyncStorage.setItem Settings.stopKey <| stopToString stop)
, fetchAlertsAndSchedules stop
, upsertInstallation model.deviceToken stop
, Maybe.map (upsertInstallation stop) model.deviceToken
|> Maybe.withDefault Cmd.none
]
)
22 changes: 18 additions & 4 deletions app/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,26 @@ type alias Stops =
List Stop


type alias Stop =
String
type Stop
= Stop String


type alias DeviceToken =
String
stopToString : Stop -> String
stopToString stop =
case stop of
Stop string ->
string


type DeviceToken
= DeviceToken String


deviceTokenToString : DeviceToken -> String
deviceTokenToString deviceToken =
case deviceToken of
DeviceToken string ->
string


type Loadable a
Expand Down
27 changes: 18 additions & 9 deletions app/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Types exposing (..)
import Model exposing (..)
import Message exposing (..)
import ReportIssue
import App.Settings.Update exposing (receiveSettings)
import App.Settings.Update exposing (receiveSettingsResult)
import Http
import PushNotifications.Update exposing (..)
import Schedule.Alerts.Update exposing (dismissAlert)
import Stops.Update exposing (receiveStops, pickStop)
import Tick.Update exposing (tick)
Expand All @@ -24,13 +25,21 @@ update msg model =
PickStop stop ->
pickStop model stop

SetItem result ->
SetItem _ ->
( model, Cmd.none )

ReceivePushPrePromptResponse accepted ->
( model, handlePushPrePromptResponse accepted )

ReceivePushToken result ->
case result of
Ok _ ->
( model, Cmd.none )
Ok tokenString ->
( { model | deviceToken = Just (DeviceToken tokenString) }
, receiveDeviceToken model.selectedStop (DeviceToken tokenString)
)

Result.Err a ->
( model, Cmd.none )
Err registerError ->
Debug.crash registerError

ReceiveAlerts result ->
( { model | alerts = toLoadable result }, Cmd.none )
Expand Down Expand Up @@ -64,10 +73,10 @@ update msg model =
dismissAlert model alert

ReceiveSettings settingsResult ->
receiveSettings model settingsResult
receiveSettingsResult model settingsResult

DeviceTokenChanged deviceToken ->
( { model | deviceToken = Just deviceToken }
DeviceTokenChanged deviceTokenString ->
( { model | deviceToken = Just <| DeviceToken deviceTokenString }
, Cmd.none
)

Expand Down
19 changes: 7 additions & 12 deletions app/UpsertInstallation.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ import Message exposing (..)
import Types exposing (..)


upsertInstallation : Maybe DeviceToken -> Stop -> Cmd Msg
upsertInstallation maybeDeviceToken stop =
case maybeDeviceToken of
Nothing ->
Cmd.none
upsertInstallation : Stop -> DeviceToken -> Cmd Msg
upsertInstallation stop deviceToken =
Http.send ReceiveInstallationResponse <| putInstallation deviceToken stop

Just deviceToken ->
Http.send ReceiveInstallationResponse <| putInstallation deviceToken stop


upsertInstallationEndpoint : String -> String
upsertInstallationEndpoint : DeviceToken -> String
upsertInstallationEndpoint deviceToken =
baseUrl ++ "/api/v2/installations/" ++ deviceToken
baseUrl ++ "/api/v2/installations/" ++ (deviceTokenToString deviceToken)


putInstallation : String -> Stop -> Http.Request ()
putInstallation : DeviceToken -> Stop -> Http.Request ()
putInstallation deviceToken stop =
put
(upsertInstallationEndpoint deviceToken)
Expand All @@ -34,7 +29,7 @@ installationBody stop =
Http.jsonBody <|
Encode.object
[ ( "operating_system", Encode.string "ios" )
, ( "home_stop_id", Encode.string stop )
, ( "home_stop_id", Encode.string <| stopToString stop )
, ( "push_notifications_enabled", Encode.bool True )
]

Expand Down
9 changes: 9 additions & 0 deletions bin/symlink-elm-native-ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

rm -rf elm-stuff/packages/ohanhi/elm-native-ui
mkdir -p elm-stuff/packages/ohanhi/elm-native-ui

# Assumes you have elm-native-ui cloned one directory above PurpleTrainElm
ln -s `pwd`/../elm-native-ui elm-stuff/packages/ohanhi/elm-native-ui/2.0.0
Loading