Skip to content

Commit

Permalink
⬆️ upgrade to flutter_rust_bridge v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gthvmt committed Jan 3, 2024
1 parent 56ef022 commit 060e125
Show file tree
Hide file tree
Showing 88 changed files with 7,496 additions and 1,693 deletions.
14 changes: 9 additions & 5 deletions .devcontainer/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ ENV VERSION=$VERSION \
# Install linux dependency and utils
RUN set -eux; mkdir -p /usr/lib $PUB_CACHE \
&& apt-get update \
&& apt-get install -y openjdk-17-jdk \
&& apt-get install -y locales openjdk-17-jdk \
&& rm -rf /var/lib/apt/lists/* /var/cache/apk/* \
&& mkdir -p ${ANDROID_HOME}/cmdline-tools /root/.android

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

# Install & config Flutter
RUN set -eux; git clone -b ${VERSION} --depth 1 "${FLUTTER_URL}.git" "${FLUTTER_ROOT}" \
&& cd "${FLUTTER_ROOT}" \
Expand Down Expand Up @@ -65,10 +72,7 @@ RUN set -eux; cd "${FLUTTER_HOME}/bin" \
RUN set -eux; apt-get update && apt-get install -y libclang-dev \
&& cargo install just \
cargo-ndk \
flutter_rust_bridge_codegen \
# we dont really need this because we dont use macros in our rust code but for
# now there is no option to disable cargo expand in flutter_rust_bridge_codegen
cargo-expand \
'flutter_rust_bridge_codegen@^2.0.0-dev.3' \
&& rustup component add rustfmt \
&& rustup target add \
aarch64-linux-android \
Expand Down
3 changes: 0 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ clean:
build:
#!{{shebang}}
cd src
flutter pub get
flutter_rust_bridge_codegen --rust-input ./rust/src/api.rs --dart-output ./lib/models/webp_bridge_generated.dart
cd rust; cargo ndk --platform 24 -t armeabi-v7a -t arm64-v8a -o ../android/app/src/main/jniLibs build --release
flutter build apk

rebuild: clean build
45 changes: 4 additions & 41 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
buildscript {
ext.kotlin_version = '1.9.21'
ext.gradle_plugin_version = '7.4.0'
ext.kotlin_version = '1.7.21'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "com.android.tools.build:gradle:$gradle_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -28,42 +29,4 @@ subprojects {

tasks.register("clean", Delete) {
delete rootProject.buildDir
}

//run the following once the rust src changes:
//flutter_rust_bridge_codegen --rust-input ./rust/src/api.rs --dart-output ./lib/models/webp_bridge_generated.dart
//cd ./rust && cargo ndk --platform 24 -t armeabi-v7a -t arm64-v8a -o ../android/app/src/main/jniLibs build --release
//TODO: move this to a makefile

//flutter_rust_bridge docs suggest the bellow. Does not work for me and results in the apk compilation
//looping new gradle tasks and never finishing
// [
// Debug: null,
// Profile: '--release',
// Release: '--release'
// ].each {
// def taskPostfix = it.key
// def profileMode = it.value
// tasks.whenTaskAdded { task ->
// if (task.name == "javaPreCompile$taskPostfix") {
// task.dependsOn "cargoBuild$taskPostfix"
// }
// }
// tasks.register("cargoBuild$taskPostfix", Exec) {
// workingDir "../../rust"
// environment ANDROID_NDK_HOME: "$ANDROID_NDK"
// commandLine 'cargo', 'ndk',
// '--platform', '24',
// // the 2 ABIs below are used by real Android devices
// '-t', 'armeabi-v7a',
// '-t', 'arm64-v8a',
// // the below 2 ABIs are usually used for Android simulators,
// // add or remove these ABIs as needed.
// // '-t', 'x86',
// // '-t', 'x86_64',
// '-o', '../android/app/src/main/jniLibs', 'build'
// if (profileMode != null) {
// args profileMode
// }
// }
// }
}
3 changes: 2 additions & 1 deletion src/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 2 additions & 0 deletions src/flutter_rust_bridge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rust_input: rust/src/api/**/*.rs
dart_output: lib/src/rust
14 changes: 14 additions & 0 deletions src/integration_test/simple_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:seventv_for_whatsapp/main.dart';
import 'package:seventv_for_whatsapp/src/rust/frb_generated.dart';
import 'package:integration_test/integration_test.dart';

void main() {
if (kIsWeb) IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async => await RustLib.init());
testWidgets('Can call rust function', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.textContaining('Result: `Hello, Tom!`'), findsOneWidget);
});
}
2 changes: 1 addition & 1 deletion src/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ class MyApp extends StatelessWidget {
home: const Browser(),
);
}
}
}
15 changes: 0 additions & 15 deletions src/lib/models/ffi.dart

This file was deleted.

13 changes: 7 additions & 6 deletions src/lib/models/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ class Settings {
Settings();

Settings.fromJson(Map<String, dynamic> json)
: defaultPublisher = json['defaultPublisher'];
: defaultPublisher = json['defaultPublisher'];

Map<String, dynamic> toJson() => {
'defaultPublisher': defaultPublisher,
};
'defaultPublisher': defaultPublisher,
};
}

class SettingsManager {
static Future<Settings> load() async {
final sharedPreferences = await SharedPreferences.getInstance();
final jsonString = sharedPreferences.getString(SharedPreferencesKeys.settings);
if(jsonString == null) {
final jsonString =
sharedPreferences.getString(SharedPreferencesKeys.settings);
if (jsonString == null) {
return Settings();
}
return Settings.fromJson(jsonDecode(jsonString));
Expand All @@ -33,4 +34,4 @@ class SettingsManager {
jsonEncode(settings.toJson()),
);
}
}
}
18 changes: 12 additions & 6 deletions src/lib/models/seventv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import 'dart:convert';
import 'dart:async';

class SevenTv {
static const _searchEmotesQuery =
r'''
static const _searchEmotesQuery = r'''
query SearchEmotes($query: String!, $page: Int, $sort: Sort, $limit: Int, $filter: EmoteSearchFilter) {
emotes(query: $query, page: $page, sort: $sort, limit: $limit, filter: $filter) {
count
Expand Down Expand Up @@ -140,7 +139,10 @@ class EmoteTransformer implements StreamTransformer<String, Emote> {
} else if (c == '{' || c == '[') {
_currentDepth++;
if (c == '[') {
if (_buffer.replaceAll(' ', '').toLowerCase().endsWith('"items":[')) {
if (_buffer
.replaceAll(' ', '')
.toLowerCase()
.endsWith('"items":[')) {
_itemsArrayDepth = _currentDepth;
//clear buffer because it now collects emote json
_buffer = '';
Expand All @@ -150,7 +152,8 @@ class EmoteTransformer implements StreamTransformer<String, Emote> {
if (_currentDepth == _itemsArrayDepth + 1 && c == '}') {
//emote object closed
countCollected++;
_controller.add(Emote.fromJson(jsonDecode(_buffer.substring(_buffer.indexOf('{')))));
_controller.add(Emote.fromJson(
jsonDecode(_buffer.substring(_buffer.indexOf('{')))));
_buffer = '';
}
if (_currentDepth == _itemsArrayDepth && _itemsArrayDepth > 0) {
Expand Down Expand Up @@ -205,7 +208,9 @@ class Emote {

Uri? getMaxSizeUrl({Format format = Format.webp}) {
final files = host?.files?.where((f) => f.format == format);
final file = files?.isEmpty ?? true ? null : files?.reduce((a, b) => a.height > b.height ? a : b);
final file = files?.isEmpty ?? true
? null
: files?.reduce((a, b) => a.height > b.height ? a : b);
return file == null ? null : host?.getUrl(file);
}

Expand Down Expand Up @@ -296,7 +301,8 @@ class Host {
throw "url does not lead to a valid webp";
}
// Check if the file is animated by looking for the "ANIM" chunk identifier
final isAnimated = listEquals(bytes.sublist(30, 30 + 4), utf8.encode('ANIM'));
final isAnimated =
listEquals(bytes.sublist(30, 30 + 4), utf8.encode('ANIM'));
return isAnimated;
}
}
Expand Down
Loading

0 comments on commit 060e125

Please sign in to comment.