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

Update Quickstart Apps with New Init flow #46

Merged
merged 33 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3e93f87
Update swift quickstart initialization with new syntax
skylerjokiel Mar 3, 2025
2e913fb
fix env.sample
skylerjokiel Mar 3, 2025
41f2802
fix: updated to include Android Kotlin fixes
biozal Mar 3, 2025
ebba924
fix: updated Android implementation for custom URL and websocket Url …
biozal Mar 3, 2025
b3c6533
doc: updated kotlin code with links to docs
biozal Mar 3, 2025
9c39cee
updated to include Rust support
biozal Mar 3, 2025
03e8e4c
fix: updated javascript-web with comments and using customURL
biozal Mar 3, 2025
2b721fa
fix: fixed Swift to use DITTO for prefix because without that prefix …
biozal Mar 3, 2025
72b5602
update: standardized readme files and names of environment variables
biozal Mar 3, 2025
5240feb
updated React Native readme with notes about possible issues with ter…
biozal Mar 3, 2025
c4ddf94
updated to fix kotlin having wrong auth env name
biozal Mar 3, 2025
717a253
Update android-kotlin/QuickStartTasks/app/src/main/java/live/ditto/qu…
biozal Mar 3, 2025
9432b19
Update javascript-web/src/App.tsx
biozal Mar 3, 2025
56d481c
Update rust-tui/src/bin/main.rs
biozal Mar 3, 2025
f89c714
Update rust-tui/src/bin/main.rs
biozal Mar 3, 2025
4554b26
removed extra logging statement added by accident
biozal Mar 3, 2025
3b075d6
updated for Android to remove logging
biozal Mar 18, 2025
3b745c4
updated .NET projects to latest 4.10 release and customUrl
biozal Mar 18, 2025
04fb09f
fixed: updated Flutter to 4.10 and customUrl/websocket
biozal Mar 18, 2025
e1cd594
fixed .NET to include websocket URL
biozal Mar 18, 2025
9e7cbc2
updated Android, flutter description and updated Swift to 4.10.0
biozal Mar 18, 2025
d3d3ce7
fixed updated Android kotlin and java to 4.10.0
biozal Mar 18, 2025
feaf11d
updated rust to version 4.10
biozal Mar 18, 2025
cf03570
updated js projects to 4.10.0
biozal Mar 18, 2025
e0baf04
updateds to Android C++ for 4.10 and Custom Auth URL
biozal Mar 18, 2025
a08c0a1
fixed: Updated React Native to 4.10.0 and updated initialization code
biozal Mar 18, 2025
7bb6349
fix: updated cpp-tui
biozal Mar 19, 2025
85b7a9c
Apply suggestions from code review
biozal Mar 20, 2025
105b409
updates based on feedback from PR
biozal Mar 20, 2025
152e767
updates based on feedback from PR
biozal Mar 20, 2025
4d9ec9e
added comments about having to disable cloud sync
biozal Mar 20, 2025
81d2f61
updated comment in kotlin
biozal Mar 20, 2025
53c6f7c
fix issue with missing websocket and auth url for parse with sync
biozal Mar 20, 2025
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
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# A Ditto AppID and Playground token can be obtained from https://portal.ditto.live
DITTO_APP_ID=""
DITTO_PLAYGROUND_TOKEN=""

DITTO_AUTH_URL=""
DITTO_WEBSOCKET_URL=""
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To obtain your Ditto identity and configure the quickstart apps with it, follow
1. Copy the `.env.sample` file to `.env`.
- in a terminal: `cp .env.sample .env`.
- in a macOS Finder window, press `⇧⌘.` (SHIFT+CMD+period) to show hidden files.
1. Save your app ID and online playground token in the `.env` file.
1. Save your App ID, Online Playground Token, Auth URL, and WebSocket URL in the `.env` file.

Please see the app-specific README files for details on the tools necessary to
build and run them.
Expand Down
25 changes: 23 additions & 2 deletions android-cpp/QuickStartTasksCPP/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ fun loadEnvProperties(): Properties {
return properties
}

// Define BuildConfig.DITTO_APP_ID and BuildConfig.DITTO_PLAYGROUND_TOKEN
// Define BuildConfig.DITTO_APP_ID, BuildConfig.DITTO_PLAYGROUND_TOKEN,
// BuildConfig.DITTO_CUSTOM_AUTH_URL, BuildConfig.DITTO_WEBSOCKET_URL
// based on values in the .env file
//
// More information can be found here:
// https://docs.ditto.live/sdk/latest/install-guides/cpp#importing-and-initializing-ditto
androidComponents {
onVariants {
val prop = loadEnvProperties()
Expand All @@ -41,6 +45,23 @@ androidComponents {
"Ditto online playground authentication token"
)
)
it.buildConfigFields.put(
"DITTO_AUTH_URL",
BuildConfigField(
"String",
"${prop["DITTO_AUTH_URL"]}",
"Ditto Auth URL"
)
)

it.buildConfigFields.put(
"DITTO_WEBSOCKET_URL",
BuildConfigField(
"String",
"${prop["DITTO_WEBSOCKET_URL"]}",
"Ditto Websocket URL"
)
)
}
}

Expand Down Expand Up @@ -137,5 +158,5 @@ dependencies {
debugImplementation(libs.androidx.ui.test.manifest)

// Ditto C++ SDK
implementation("live.ditto:ditto-cpp:4.9.2")
implementation("live.ditto:ditto-cpp:4.10.0")
}
52 changes: 39 additions & 13 deletions android-cpp/QuickStartTasksCPP/app/src/main/cpp/tasks_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,35 @@ vector<string> tasks_json_from(const ditto::QueryResult &result) {
}

/// Initialize a Ditto instance.
unique_ptr<ditto::Ditto> init_ditto(JNIEnv *env, jobject context, string app_id,
unique_ptr<ditto::Ditto> init_ditto(JNIEnv *env,
jobject context,
string app_id,
string online_playground_token,
bool enable_cloud_sync,
string persistence_dir,
bool is_running_on_emulator) {
bool is_running_on_emulator,
string custom_url,
const string& websocket_url) {
try {

// TODO UPDATE TO USE CUSTOM URL AND WEBSOCKET
// Docs: https://docs.ditto.live/sdk/latest/install-guides/cpp#importing-and-initializing-ditto

const auto identity = ditto::Identity::OnlinePlayground(
std::move(app_id), std::move(online_playground_token),
enable_cloud_sync);
std::move(app_id),
std::move(online_playground_token),
enable_cloud_sync,
std::move(custom_url)
);

auto ditto =
make_unique<ditto::Ditto>(identity, std::move(persistence_dir));

if (is_running_on_emulator) {
// Some transports don't work correctly on emulator, so disable them.
ditto->update_transport_config([](ditto::TransportConfig &config) {
config.peer_to_peer.bluetooth_le.enabled = false;
config.peer_to_peer.wifi_aware.enabled = false;
ditto->update_transport_config([websocket_url](ditto::TransportConfig &config) {
config.enable_all_peer_to_peer();
config.connect.websocket_urls.insert(websocket_url);
});
}

Expand All @@ -75,13 +86,27 @@ class TasksPeer::Impl { // NOLINT(cppcoreguidelines-special-member-functions)
shared_ptr<ditto::SyncSubscription> tasks_subscription;

public:
Impl(JNIEnv *env, jobject context, string app_id, string online_playground_token,
Impl(JNIEnv *env,
jobject context,
string app_id,
string online_playground_token,
bool enable_cloud_sync,
string persistence_dir,
bool is_running_on_emulator)
bool is_running_on_emulator,
string custom_auth_url,
const string& websocket_url)
: mtx(new mutex()),
ditto(init_ditto(env, context, std::move(app_id), std::move(online_playground_token),
enable_cloud_sync, std::move(persistence_dir), is_running_on_emulator)) {}
ditto(init_ditto(
env,
context,
std::move(app_id),
std::move(online_playground_token),
enable_cloud_sync,
std::move(persistence_dir),
is_running_on_emulator,
std::move(custom_auth_url),
websocket_url
)) {}

~Impl() noexcept {
try {
Expand Down Expand Up @@ -291,10 +316,11 @@ class TasksPeer::Impl { // NOLINT(cppcoreguidelines-special-member-functions)
}; // class TasksPeer::Impl

TasksPeer::TasksPeer(JNIEnv *env, jobject context, string app_id, string online_playground_token,
bool enable_cloud_sync, string persistence_dir, bool is_running_on_emulator)
bool enable_cloud_sync, string persistence_dir, bool is_running_on_emulator,
string custom_auth_url, const string& websocket_url)
: impl(make_unique<Impl>(env, context, std::move(app_id), std::move(online_playground_token),
enable_cloud_sync, std::move(persistence_dir),
is_running_on_emulator)) {}
is_running_on_emulator, std::move(custom_auth_url), std::move(websocket_url))) {}

TasksPeer::~TasksPeer() noexcept {
try {
Expand Down
10 changes: 8 additions & 2 deletions android-cpp/QuickStartTasksCPP/app/src/main/cpp/tasks_peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ class TasksPeer {
static std::string get_ditto_sdk_version();

/// Construct a new TasksPeer object.
TasksPeer(JNIEnv *env, jobject context, std::string ditto_app_id,
TasksPeer(JNIEnv *env,
jobject context,
std::string ditto_app_id,
std::string ditto_online_playground_token,
bool enable_cloud_sync, std::string ditto_persistence_dir, bool is_running_on_emulator);
bool enable_cloud_sync,
std::string ditto_persistence_dir,
bool is_running_on_emulator,
std::string ditto_custom_auth_url,
const std::string& ditto_websocket_url);

virtual ~TasksPeer() noexcept;

Expand Down
27 changes: 21 additions & 6 deletions android-cpp/QuickStartTasksCPP/app/src/main/cpp/taskslib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ jobject native_task_to_java_task(JNIEnv *const env, const Task &native_task) {

extern "C"
JNIEXPORT void JNICALL
Java_live_ditto_quickstart_tasks_TasksLib_initDitto(JNIEnv *env, jobject thiz, jobject context,
Java_live_ditto_quickstart_tasks_TasksLib_initDitto(JNIEnv *env,
jobject thiz,
jobject context,
jstring app_id,
jstring token,
jstring persistence_dir,
jboolean is_running_on_emulator) {
jboolean is_running_on_emulator,
jstring custom_auth_url,
jstring websocket_url) {
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Java_live_ditto_quickstart_tasks_TasksLib_initDitto; SDK version: %s; emulator: %s",
TasksPeer::get_ditto_sdk_version().c_str(),
Expand All @@ -81,10 +85,21 @@ Java_live_ditto_quickstart_tasks_TasksLib_initDitto(JNIEnv *env, jobject thiz, j
auto app_id_str = jstring_to_string(env, app_id);
auto token_str = jstring_to_string(env, token);
auto persistence_dir_str = jstring_to_string(env, persistence_dir);
peer = std::make_shared<TasksPeer>(env, context, std::move(app_id_str),
std::move(token_str),
true, std::move(persistence_dir_str),
is_running_on_emulator);
auto custom_auth_url_str = jstring_to_string(env, custom_auth_url);
auto websocket_url_str = jstring_to_string(env, websocket_url);

peer = std::make_shared<TasksPeer>(
env,
context,
std::move(app_id_str),
std::move(token_str),
true,
std::move(persistence_dir_str),
is_running_on_emulator,
std::move(custom_auth_url_str),
std::move(websocket_url_str)
);

} catch (const std::exception &err) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "initDitto failed: %s", err.what());
throw_java_exception(env, err.what());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class TasksApplication : Application() {
private fun setupDitto() {
val appId = BuildConfig.DITTO_APP_ID
val token = BuildConfig.DITTO_PLAYGROUND_TOKEN
val authUrl = BuildConfig.DITTO_AUTH_URL
val webSocketURL = BuildConfig.DITTO_WEBSOCKET_URL

try {
val appContext = applicationContext()
Expand All @@ -87,7 +89,9 @@ class TasksApplication : Application() {
appId,
token,
persistenceDir.path,
isProbablyRunningOnEmulator
isProbablyRunningOnEmulator,
authUrl,
webSocketURL
)
TasksLib.startSync()
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ object TasksLib {
appId: String,
token: String,
persistenceDir: String,
isRunningOnEmulator: Boolean
isRunningOnEmulator: Boolean,
customAuthUrl: String,
websocketUrl: String
)

// Terminate the Ditto client.
Expand Down
8 changes: 4 additions & 4 deletions android-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
## Getting Started

To get started, you'll first need to create an app in the [Ditto Portal] with the
"Online Playground" authentication type. You'll need to find your AppId and Playground Token
(at the top of the page) in order to use this quickstart.
"Online Playground" authentication type. You'll need to find your AppId, Online Playground Token, Auth URL, and Websocket URL (at the top of the page) in order to use this quickstart.

In `app/src/main/java/com/example/dittotasks/MainActivity.java`, find the following variables:

```java
private String DITTO_APP_ID = "";
private String DITTO_PLAYGROUND_TOKEN = "";
private String DITTO_AUTH_URL = "";
private String DITTO_WEBSOCKET_URL = "";
```

Paste your own AppID and Playground Token into these variables, then click the Run button in
Android Studio to launch the app in an emulator.
Paste your own AppID, Playground Token, Auth URL, and Websocket URL into these variables, then click the Run button in Android Studio to launch the app in an emulator.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import live.ditto.DittoSyncSubscription;
import live.ditto.android.DefaultAndroidDittoDependencies;
import live.ditto.transports.DittoSyncPermissions;
import live.ditto.transports.DittoTransportConfig;

public class MainActivity extends ComponentActivity {
private TaskAdapter taskAdapter;
Expand All @@ -40,8 +41,13 @@ public class MainActivity extends ComponentActivity {
DittoSyncSubscription taskSubscription;
DittoStoreObserver taskObserver;

private String DITTO_APP_ID = "";
private String DITTO_PLAYGROUND_TOKEN = "";
private String DITTO_APP_ID = "<put your Ditto Portal App ID here>";
private String DITTO_PLAYGROUND_TOKEN = "<put your Ditto Portal Playground Token here>";
private String DITTO_AUTH_URL = "<put your Ditto Portal Auth URL here>";
private String DITTO_WEBSOCKET_URL = "<put your Ditto Portal WebSocket URL here>";

// This is required to be set to false to use the correct URLs
private Boolean DITTO_ENABLE_CLOUD_SYNC = false;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -86,11 +92,36 @@ void initDitto() {

try {
DittoDependencies androidDependencies = new DefaultAndroidDittoDependencies(getApplicationContext());
var identity = new DittoIdentity.OnlinePlayground(androidDependencies, DITTO_APP_ID, DITTO_PLAYGROUND_TOKEN);
/*
* Setup Ditto Identity
* https://docs.ditto.live/sdk/latest/install-guides/java#integrating-and-initializing
*/
var identity = new DittoIdentity
.OnlinePlayground(
androidDependencies,
DITTO_APP_ID,
DITTO_PLAYGROUND_TOKEN,
DITTO_ENABLE_CLOUD_SYNC, // This is required to be set to false to use the correct URLs
DITTO_AUTH_URL);
ditto = new Ditto(androidDependencies, identity);

// Set the Ditto Websocket URL
DittoTransportConfig config = new DittoTransportConfig();
config.getConnect().getWebsocketUrls().add(DITTO_WEBSOCKET_URL);

// Enable all P2P transports
config.enableAllPeerToPeer();
ditto.setTransportConfig(config);

// disable sync with v3 peers, required for DQL
ditto.disableSyncWithV3();

// register subscription
// https://docs.ditto.live/sdk/latest/sync/syncing-data#creating-subscriptions
taskSubscription = ditto.sync.registerSubscription("SELECT * FROM tasks");

// register observer for live query
// https://docs.ditto.live/sdk/latest/crud/observing-data-changes#setting-up-store-observers
taskObserver = ditto.store.registerObserver("SELECT * FROM tasks WHERE deleted=false ORDER BY _id", null, result -> {
var tasks = result.getItems().stream().map(Task::fromQueryItem).collect(Collectors.toCollection(ArrayList::new));
runOnUiThread(() -> {
Expand All @@ -106,6 +137,7 @@ void initDitto() {
}

// Request permissions for Ditto
// https://docs.ditto.live/sdk/latest/install-guides/java#requesting-permissions-at-runtime
void requestPermissions() {
DittoSyncPermissions permissions = new DittoSyncPermissions(this);
String[] missing = permissions.missingPermissions(permissions.requiredPermissions());
Expand All @@ -123,6 +155,9 @@ private void createTask(String title) {
HashMap<String, Object> args = new HashMap<>();
args.put("task", task);
try {

// Add tasks into the ditto collection using DQL INSERT statement
// https://docs.ditto.live/sdk/latest/crud/write#inserting-documents
ditto.store.execute("INSERT INTO tasks DOCUMENTS (:task)", args);
} catch (DittoError e) {
e.printStackTrace();
Expand All @@ -135,6 +170,8 @@ private void editTaskTitle(Task task, String newTitle) {
args.put("title", newTitle);

try {
// Update tasks into the ditto collection using DQL UPDATE statement
// https://docs.ditto.live/sdk/latest/crud/update#updating
ditto.store.execute("UPDATE tasks SET title=:title WHERE _id=:id", args);
} catch (DittoError e) {
e.printStackTrace();
Expand All @@ -147,6 +184,8 @@ private void toggleTask(Task task) {
args.put("done", !task.isDone());

try {
// Update tasks into the ditto collection using DQL UPDATE statement
// https://docs.ditto.live/sdk/latest/crud/update#updating
ditto.store.execute("UPDATE tasks SET done=:done WHERE _id=:id", args);
} catch (DittoError e) {
e.printStackTrace();
Expand All @@ -157,6 +196,8 @@ private void deleteTask(Task task) {
HashMap<String, Object> args = new HashMap<>();
args.put("id", task.getId());
try {
// UPDATE DQL Statement using Soft-Delete pattern
// https://docs.ditto.live/sdk/latest/crud/delete#soft-delete-pattern
ditto.store.execute("UPDATE tasks SET deleted=true WHERE _id=:id", args);
} catch (DittoError e) {
e.printStackTrace();
Expand All @@ -171,6 +212,9 @@ private void toggleSync() {
boolean isSyncActive = ditto.isSyncActive();
var nextColor = isSyncActive ? null : ColorStateList.valueOf(0xFFBB86FC);
var nextText = isSyncActive ? "Sync Inactive" : "Sync Active";

// implement Ditto Sync
// https://docs.ditto.live/sdk/latest/sync/start-and-stop-sync
try {
if (isSyncActive) {
ditto.stopSync();
Expand Down
2 changes: 1 addition & 1 deletion android-java/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
agp = "8.7.3"
constraintlayout = "2.2.0"
ditto = "4.9.0"
ditto = "4.10.0"
kotlin = "2.0.0"
coreKtx = "1.10.1"
junit = "4.13.2"
Expand Down
Loading
Loading