From fb6cbd37197f12820245396c6431b8c2f41435e0 Mon Sep 17 00:00:00 2001 From: Erudition Date: Wed, 15 Nov 2023 22:40:17 -0600 Subject: [PATCH] Get both Odd implementations running --- elm/Components/Odd.elm | 78 +++++++++++++++++++++++++++++++++--------- elm/Shared.elm | 8 ++++- www/index.ts | 20 ++++++----- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/elm/Components/Odd.elm b/elm/Components/Odd.elm index b2dd29a4..bd6d862e 100644 --- a/elm/Components/Odd.elm +++ b/elm/Components/Odd.elm @@ -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) @@ -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 @@ -50,6 +52,10 @@ init = ) +path = + Path.file [ "RON", "profile.ron" ] + + -- UPDATE @@ -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 } @@ -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 ) @@ -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 ) ----------------------------------------- -- 🥵 diff --git a/elm/Shared.elm b/elm/Shared.elm index 10a7afda..70c33020 100644 --- a/elm/Shared.elm +++ b/elm/Shared.elm @@ -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 diff --git a/www/index.ts b/www/index.ts index cf4e69e6..f2abe4cd 100644 --- a/www/index.ts +++ b/www/index.ts @@ -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) @@ -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)); @@ -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) @@ -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) { @@ -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