Skip to content

Commit

Permalink
Get both Odd implementations running
Browse files Browse the repository at this point in the history
  • Loading branch information
Erudition committed Nov 16, 2023
1 parent b787a83 commit fb6cbd3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
78 changes: 62 additions & 16 deletions elm/Components/Odd.elm
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Components.Odd exposing (..)

import Html exposing (th)
import Task
import Webnative exposing (Foundation)
import Webnative.AppInfo
import Webnative.Auth
import Webnative.CID
import Webnative.Configuration
import Webnative.Error exposing (Error(..))
import Webnative.FileSystem exposing (Base(..), FileSystem)
import Webnative.FileSystem exposing (Base(..), FileSystem, exists)
import Webnative.Namespace
import Webnative.Path as Path
import Webnative.Program exposing (Program)
Expand All @@ -19,7 +21,7 @@ import Webnative.Session exposing (Session)

appInfo : Webnative.AppInfo.AppInfo
appInfo =
{ creator = "Webnative", name = "Example" }
{ creator = "Minder", name = "Minder" }


config : Webnative.Configuration.Configuration
Expand Down Expand Up @@ -50,6 +52,10 @@ init =
)


path =
Path.file [ "RON", "profile.ron" ]



-- UPDATE

Expand All @@ -58,6 +64,9 @@ type Msg
= HandleWebnativeError Error
| GotFileContents String
| GotSessionAndFileSystem (Maybe { session : Session, fileSystem : FileSystem })
| ReadFileContents
| WriteFileContents String
| Published Webnative.CID.CID
| Liftoff Foundation
| RegisterUser Program { success : Bool }

Expand Down Expand Up @@ -99,7 +108,22 @@ update msg model =

-- Option (B), link an existing account.
-- See 'Linking' section below.
_ ->
Authenticated program session fileSystem ->
let
createIfExists exists =
if exists then
ReadFileContents

else
WriteFileContents "first"
in
Webnative.FileSystem.exists fileSystem (AppData appInfo) path
|> Webnative.attemptTask
{ ok = createIfExists
, error = HandleWebnativeError
}

Unprepared ->
Cmd.none
)

Expand All @@ -126,30 +150,52 @@ update msg model =
case model of
NotAuthenticated program ->
Authenticated program session fileSystem
|> Debug.log "Authenticated successfully in Odd.elm"

_ ->
model
-- Next action
--------------
, let
path =
Path.file [ "Sub Directory", "hello.txt" ]
in
"👋"
|> Webnative.FileSystem.writeUtf8 fileSystem Private path
|> Task.andThen (\_ -> Webnative.FileSystem.publish fileSystem)
|> Task.andThen (\_ -> Webnative.FileSystem.readUtf8 fileSystem Private path)
|> Webnative.attemptTask
{ ok = GotFileContents
, error = HandleWebnativeError
}
, Task.perform (\_ -> ReadFileContents) (Task.succeed ())
)

ReadFileContents ->
case model of
Authenticated program session fileSystem ->
( model
, Webnative.FileSystem.readUtf8 fileSystem (AppData appInfo) path
|> Webnative.attemptTask
{ ok = GotFileContents
, error = HandleWebnativeError
}
)

_ ->
( model, Cmd.none )

WriteFileContents newContents ->
case model of
Authenticated program session fileSystem ->
( model
, Webnative.FileSystem.writeUtf8 fileSystem (AppData appInfo) path newContents
|> Task.andThen (\_ -> Webnative.FileSystem.publish fileSystem)
|> Webnative.attemptTask
{ ok = Published
, error = HandleWebnativeError
}
)

_ ->
( model, Cmd.none )

Published newCID ->
( Debug.log "Published successfully!" model, Cmd.none )

-----------------------------------------
-- 💾
-----------------------------------------
GotFileContents string ->
Debug.log string ( model, Cmd.none )
Debug.log ("Odd.elm got file contents!" ++ string) ( model, Cmd.none )

-----------------------------------------
-- 🥵
Expand Down
8 changes: 7 additions & 1 deletion elm/Shared.elm
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ init flagsResult route =
}

( replicator, replica ) =
Components.Replicator.init { launchTime = Just flags.launchTime, replicaCodec = Profile.codec, outPort = Effect.setStorage }
Components.Replicator.init
{ launchTime = Just flags.launchTime
, replicaCodec = Profile.codec
, outPort = Effect.setStorage

--, outPort = \stuffToWrite -> Effect.sendMsg (OddUpdate <| Odd.WriteFileContents stuffToWrite)
}

( oddModel, oddInit ) =
Odd.init
Expand Down
20 changes: 11 additions & 9 deletions www/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ window.onerror = function(e){alert(e);}
// START ELM
async function startElmApp() {

installTaskPorts();
await installTaskPorts();

let app = Elm.Main.init({ flags:
{ storedRonMaybe : (null)
Expand All @@ -64,10 +64,10 @@ async function startElmApp() {
}
startElmApp();

function installTaskPorts() {
async function installTaskPorts() {

TaskPort.install({ logCallErrors: true, logInteropErrors: false });

await attachODDElmLibrary();
registerPreferencesTaskPorts();
registerNotificationTaskPorts();
TaskPort.register("changePassphrase", () => getPassphrase(true));
Expand Down Expand Up @@ -138,7 +138,7 @@ function elmStarted(app) {
Toast.show({ text: window.location.href, duration: "short"}).then();
try {
//attachOrbit(app);
attachODD(app);
attachODDManual(app);
} catch (problemWithOrbit)
{
console.error("Failed to attach Orbit to Elm!", problemWithOrbit)
Expand Down Expand Up @@ -210,10 +210,7 @@ async function attachOrbit(elmApp) {
}


async function attachODD(elmApp) {
// Elm library (currently broken)
//const webnativeElm = await import("webnative-elm")
//webnativeElm.init({ TaskPort })
async function attachODDManual(elmApp) {
const oddIntegration = await import('./scripts/odd')
let retrievedRon = await oddIntegration.readData();
if (retrievedRon) {
Expand All @@ -231,9 +228,14 @@ async function attachODD(elmApp) {
console.error("Tried to save empty RON data...")
}
});

}

async function attachODDElmLibrary() {
// Elm library (currently broken)
const webnativeElm = await import("webnative-elm")
await webnativeElm.init({ TaskPort }) // await so it will be ready before elm initializes
console.log("webnative initialized", webnativeElm)
}

// FLIP ANIMATIONS
// Requires patch to ~/.elm/0.19.1/packages/elm/browser/1.0.2/src/Elm/Kernel/Browser.js
Expand Down

0 comments on commit fb6cbd3

Please sign in to comment.