Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add persistence events #33

Closed
wants to merge 1 commit into from
Closed

Add persistence events #33

wants to merge 1 commit into from

Conversation

carson-katri
Copy link
Contributor

This is related to liveview-native/liveview-client-swiftui#1065, and introduces an alternative to native_binding for simplified persistence.

Here's an example of a counter app that persists the count in UserDefaults.

defmodule TestBedWeb.IndexLive do
  use TestBedWeb, :live_view
  use LiveViewNative.LiveView

  @impl true
  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0) |> load_persistent_value(:stored_count, "loaded_count")}
  end

  @impl true
  def render(%{platform_id: :swiftui} = assigns) do
    ~SWIFTUI"""
    <Text><%= @count %></Text>
    <Button phx-click="increment">Increment</Button>
    """
  end

  def handle_event("increment", _params, socket) do
    new_value = socket.assigns.count + 1
    {:noreply, assign(socket, count: new_value) |> push_persistent_value(:stored_count, new_value)}
  end

  def handle_event("loaded_count", count, socket) do
    {:noreply, assign(socket, count: count)}
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants