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 tauri support #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add tauri support #213

wants to merge 1 commit into from

Conversation

qwqtoday
Copy link

@qwqtoday qwqtoday commented Dec 13, 2024

This PR adds building the app using Tauri.

You can build the app using Tauri to Windows, Android or iOS (not yet).

maybe we can deprecate https://github.com/hkbus/mobile-bus-app

Copy link

coderabbitai bot commented Dec 13, 2024

Walkthrough

This pull request introduces several changes to set up a Tauri application named "hk-independent-bus-eta." Key updates include the addition of a new development dependency in package.json, the creation of a Cargo.toml file with specified dependencies, and the introduction of multiple new files such as build.rs, main.rs, and tauri.conf.json to configure the application. Additionally, entries in .gitignore have been updated to exclude specific directories from version control, streamlining the repository.

Changes

File Change Summary
package.json Added development dependency: "@tauri-apps/cli": "^2.1.0" in devDependencies.
src-tauri/.gitignore Added entries to ignore target/ and gen/schemas directories.
src-tauri/Cargo.toml Introduced new file with package metadata, library definitions, and multiple dependencies.
src-tauri/build.rs Added function main() to initiate the Tauri build process.
src-tauri/capabilities/default.json Created new file defining desktop capabilities with an identifier and permissions.
src-tauri/src/lib.rs Added public function run() to set up the Tauri application environment.
src-tauri/src/main.rs Introduced new entry point for the application with function main() calling app_lib::run().
src-tauri/tauri.conf.json New configuration file specifying application metadata, build, app, and bundle settings.
vite.config.ts Modified server configuration based on platform detection, adjusting https and port settings.

Possibly related PRs

Poem

🐰 In the garden of code, we hop and play,
New tools and paths brighten our day.
With Tauri's charm, our app takes flight,
Building dreams, oh what a sight!
Dependencies added, a structure so neat,
Hopping along, our journey's complete! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (1)
src-tauri/src/lib.rs (1)

1-16: Consider adding state management and custom commands

The current implementation lacks common Tauri features that might be needed:

  1. State management for sharing data between frontend and backend
  2. Custom commands for platform-specific functionality

Would you like me to provide an example implementation with state management and custom commands?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5c5f121 and 1dab6dd.

⛔ Files ignored due to path filters (42)
  • src-tauri/Cargo.lock is excluded by !**/*.lock
  • src-tauri/gen/android/.editorconfig is excluded by !**/gen/**
  • src-tauri/gen/android/.gitignore is excluded by !**/gen/**
  • src-tauri/gen/android/app/.gitignore is excluded by !**/gen/**
  • src-tauri/gen/android/app/build.gradle.kts is excluded by !**/gen/**
  • src-tauri/gen/android/app/proguard-rules.pro is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/AndroidManifest.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/java/app/hkbus/MainActivity.kt is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/drawable/ic_launcher_background.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/layout/activity_main.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-hdpi/ic_launcher.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-mdpi/ic_launcher.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png is excluded by !**/*.png, !**/gen/**
  • src-tauri/gen/android/app/src/main/res/values-night/themes.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/values/colors.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/values/strings.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/values/themes.xml is excluded by !**/gen/**
  • src-tauri/gen/android/app/src/main/res/xml/file_paths.xml is excluded by !**/gen/**
  • src-tauri/gen/android/build.gradle.kts is excluded by !**/gen/**
  • src-tauri/gen/android/buildSrc/build.gradle.kts is excluded by !**/gen/**
  • src-tauri/gen/android/buildSrc/src/main/java/app/hkbus/kotlin/BuildTask.kt is excluded by !**/gen/**
  • src-tauri/gen/android/buildSrc/src/main/java/app/hkbus/kotlin/RustPlugin.kt is excluded by !**/gen/**
  • src-tauri/gen/android/gradle.properties is excluded by !**/gen/**
  • src-tauri/gen/android/gradle/wrapper/gradle-wrapper.jar is excluded by !**/*.jar, !**/gen/**
  • src-tauri/gen/android/gradle/wrapper/gradle-wrapper.properties is excluded by !**/gen/**
  • src-tauri/gen/android/gradlew is excluded by !**/gen/**
  • src-tauri/gen/android/gradlew.bat is excluded by !**/gen/**
  • src-tauri/gen/android/settings.gradle is excluded by !**/gen/**
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (9)
  • package.json (1 hunks)
  • src-tauri/.gitignore (1 hunks)
  • src-tauri/Cargo.toml (1 hunks)
  • src-tauri/build.rs (1 hunks)
  • src-tauri/capabilities/default.json (1 hunks)
  • src-tauri/src/lib.rs (1 hunks)
  • src-tauri/src/main.rs (1 hunks)
  • src-tauri/tauri.conf.json (1 hunks)
  • vite.config.ts (2 hunks)
✅ Files skipped from review due to trivial changes (6)
  • src-tauri/src/main.rs
  • src-tauri/.gitignore
  • src-tauri/build.rs
  • src-tauri/capabilities/default.json
  • src-tauri/Cargo.toml
  • src-tauri/tauri.conf.json

@@ -60,6 +60,7 @@
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@tauri-apps/cli": "^2.1.0",
Copy link

@coderabbitai coderabbitai bot Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add missing Tauri dependencies and scripts

While @tauri-apps/cli is added, there are some essential Tauri-related additions missing:

  1. Add @tauri-apps/api dependency for Tauri frontend APIs
  2. Add Tauri-specific scripts for development workflow

Add these to package.json:

 "dependencies": {
+  "@tauri-apps/api": "^2.1.0",
   "@emotion/cache": "^11.11.0",
 },
 "scripts": {
+  "tauri": "tauri",
+  "tauri:dev": "tauri dev",
+  "tauri:build": "tauri build",
   "start": "./scripts/start-all-platforms.cmd",
 }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no thanks

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood.

Comment on lines +23 to +25
https: !mobile,
host: true,
port: 443,
port: mobile ? 80 : 443,
Copy link

@coderabbitai coderabbitai bot Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Review server configuration for security and reliability

Several concerns with the current server configuration:

  1. Disabling HTTPS on mobile platforms reduces security
  2. Hard-coded ports (80/443) require root/admin privileges
  3. No fallback ports if primary ports are unavailable

Consider this safer configuration:

 server: {
-  https: !mobile,
-  host: true,
-  port: mobile ? 80 : 443,
+  https: true,  // Keep HTTPS for all platforms
+  host: true,
+  port: mobile ? 8080 : 8443,  // Use non-privileged ports
+  strictPort: false,  // Allow fallback to next available port
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
https: !mobile,
host: true,
port: 443,
port: mobile ? 80 : 443,
https: true, // Keep HTTPS for all platforms
host: true,
port: mobile ? 8080 : 8443, // Use non-privileged ports
strictPort: false, // Allow fallback to next available port

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well actually originally it was hard coded to 443.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood. If hardcoding port 443 suits your setup, it's acceptable to keep it as is. Thank you for the clarification.


✏️ Learnings added
Learnt from: qwqtoday
PR: hkbus/hk-independent-bus-eta#213
File: vite.config.ts:23-25
Timestamp: 2024-12-14T10:51:32.215Z
Learning: In the `hkbus` application, it's acceptable to hardcode port `443` in `vite.config.ts` for the server configuration.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@@ -5,6 +5,9 @@ import { VitePWA, VitePWAOptions } from "vite-plugin-pwa";
import eslint from "vite-plugin-eslint"

// https://vitejs.dev/config/

const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM);
Copy link

@coderabbitai coderabbitai bot Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve platform detection reliability

The current regex might miss some mobile platforms. Consider using Tauri's built-in platform detection.

-const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM);
+import { Platform } from '@tauri-apps/api/os';
+const mobile = await Platform.isAndroid || await Platform.isIOS;

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no thanks bro

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood. Let me know if I can assist further.

Comment on lines +1 to +16
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Copy link

@coderabbitai coderabbitai bot Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and logging configuration

Several improvements needed for production readiness:

  1. Replace panic with proper error handling
  2. Add configuration for production logging
  3. Add documentation for the public function

Consider this improved implementation:

+//! Tauri application entry point.
+//! This module initializes the application and sets up logging.
+
+use log::LevelFilter;
+use std::error::Error;
+
 #[cfg_attr(mobile, tauri::mobile_entry_point)]
-pub fn run() {
+/// Initializes and runs the Tauri application.
+///
+/// # Errors
+/// Returns an error if the application fails to initialize or run.
+pub fn run() -> Result<(), Box<dyn Error>> {
   tauri::Builder::default()
     .setup(|app| {
-      if cfg!(debug_assertions) {
-        app.handle().plugin(
-          tauri_plugin_log::Builder::default()
-            .level(log::LevelFilter::Info)
-            .build(),
-        )?;
-      }
+      let log_level = if cfg!(debug_assertions) {
+        LevelFilter::Debug
+      } else {
+        LevelFilter::Info
+      };
+      
+      app.handle().plugin(
+        tauri_plugin_log::Builder::default()
+          .level(log_level)
+          .build(),
+      )?;
       Ok(())
     })
     .run(tauri::generate_context!())
-    .expect("error while running tauri application");
+    .map_err(|e| e.into())
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
//! Tauri application entry point.
//! This module initializes the application and sets up logging.
use log::LevelFilter;
use std::error::Error;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
/// Initializes and runs the Tauri application.
///
/// # Errors
/// Returns an error if the application fails to initialize or run.
pub fn run() -> Result<(), Box<dyn Error>> {
tauri::Builder::default()
.setup(|app| {
let log_level = if cfg!(debug_assertions) {
LevelFilter::Debug
} else {
LevelFilter::Info
};
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log_level)
.build(),
)?;
Ok(())
})
.run(tauri::generate_context!())
.map_err(|e| e.into())
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was generated code and I wasn't supposed to modify it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood. If this code is generated and not meant to be modified manually, it's acceptable to leave it as is.


✏️ Learnings added
Learnt from: qwqtoday
PR: hkbus/hk-independent-bus-eta#213
File: src-tauri/src/lib.rs:1-16
Timestamp: 2024-12-14T10:52:10.166Z
Learning: In the hk-independent-bus-eta project, the file `src-tauri/src/lib.rs` contains generated code that should not be manually modified.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood. If this code is generated and not meant to be modified manually, it's acceptable to leave it as is.
✏️ Learnings added

Learnt from: qwqtoday
PR: hkbus/hk-independent-bus-eta#213
File: src-tauri/src/lib.rs:1-16
Timestamp: 2024-12-14T10:52:10.166Z
Learning: In the hk-independent-bus-eta project, the file `src-tauri/src/lib.rs` contains generated code that should not be manually modified.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

please remove that learning, it's not completely true

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qwqtoday, understood. I've removed the learning as per your request.

@qwqtoday
Copy link
Author

I don't have a mac so I can't test the iOS part

@chunlaw
Copy link
Member

chunlaw commented Dec 16, 2024

Your code suggestion is highly appreciated, while I have no experience in Tauri, as well as the committed code under src-tauri, especially, there are lots of png, xml,...

Would you explain more on it? And, how could it be built?

@qwqtoday
Copy link
Author

qwqtoday commented Dec 16, 2024

Your code suggestion is highly appreciated, while I have no experience in Tauri, as well as the committed code under src-tauri, especially, there are lots of png, xml,...

Would you explain more on it? And, how could it be built?

I am unsure what does those png and xml do.

You need to setup a lot of things to build it.

Setup

Rust Environment

To setup the Rust environment, you need to follow the instructions on this page: https://www.rust-lang.org/tools/install

Android SDK & NDK

I assume you have Android Studio installed.

You need to open a project on Android Studio, click File -> Settings -> Languages & Frameworks -> Android SDK.

Android SDK

At that page you have to copy your Android SDK location, and add it to your environment variables as ANDROID_HOME.

Android NDK

At the same page, you need to click SDK Tools. You will probably see the NDK (Side by side) option. You need to enable it, then click Apply.

After you have Android NDK installed, you need to set the NDK location to your environment variables to NDK_HOME. The NDK location should probably be at the NDK folder under the Android SDK location.

Example: C:\Users\user\AppData\Local\Android\Sdk\ndk\28.0.12674087

Building App

yarn tauri android build --apk --target aarch64 --target armv7

@qwqtoday
Copy link
Author

I forgot to mention, you also have to sign the apk after you build it.

@chunlaw
Copy link
Member

chunlaw commented Dec 19, 2024

Have you also checked the developed native app features in https://github.com/hkbus/mobile-bus-app, e.g., 到站提示? Would Tauri support all of them by default?

And, would you state the necessity on building app for Window? I do prefer user to install the App (a.k.a. Add to home screen) via Chrome in Window.

@qwqtoday
Copy link
Author

qwqtoday commented Dec 22, 2024

Have you also checked the developed native app features in https://github.com/hkbus/mobile-bus-app, e.g., 到站提示? Would Tauri support all of them by default?
Probably.

Have you also checked the developed native app features in https://github.com/hkbus/mobile-bus-app, e.g., 到站提示? Would Tauri support all of them by default?

Probably.

And, would you state the necessity on building app for Window? I do prefer user to install the App (a.k.a. Add to home screen) via Chrome in Window.

Building the app on windows, can guarantee users will able to access the app any time, even when https://hkbus.app is down.

This does not mean the app can be used while offline, you still need to able to access https://data.gov.hk/ for the app to function.

@qwqtoday
Copy link
Author

qwqtoday commented Dec 22, 2024

"Building the app on windows, can guarantee users will able to access the app any time, even when https://hkbus.app/ is down."

I have noticed the iOS and Android builds of this app is just showing the app from https://hkbus.app/ . This means if hkbus.app is down, the user wouldn't able use the app anymore.

The Tauri builds includes all the website assets, and can load the app while offline, solving the problem I mentioned above. (This is not the only way to achieve this.)

Users does not need to access https://hkbus.app to use the app with these builds, saving our organization some bandwidth, and potentially gives the user more privacy while using this app (no need to go online).

This approach is also more secure, as if one day the hkbus.app domain has been taken by someone else, they can use it for malicious purposes targeting our app's users.

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