Skip to content

Commit

Permalink
Android support (#1666)
Browse files Browse the repository at this point in the history
* Added `page.debug` and `page.platform_brightness` props

Close #1649, close #1630

* Store session ID in `window.name`

Fix #1629

* Do not assign random port on Windows with `--ios` flag

Fix #1620

* Update local_connection.py

* Build Android QR
  • Loading branch information
FeodorFitsner authored Aug 2, 2023
1 parent 764b252 commit eb209ed
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 47 deletions.
2 changes: 1 addition & 1 deletion client/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
3 changes: 2 additions & 1 deletion package/lib/src/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class WindowEventAction {

class PageBrightnessChangeAction {
final Brightness brightness;
PageBrightnessChangeAction(this.brightness);
final FletServer server;
PageBrightnessChangeAction(this.brightness, this.server);
}

class RegisterWebClientAction {
Expand Down
8 changes: 8 additions & 0 deletions package/lib/src/flet_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class FletServer {
String _windowLeft = "";
String _isPWA = "";
String _isWeb = "";
String _isDebug = "";
String _platform = "";
String _platformBrightness = "";
int reconnectStarted = 0;
final Map<String, ControlInvokeMethodCallback> controlInvokeMethods;

Expand Down Expand Up @@ -114,7 +116,9 @@ class FletServer {
required String windowLeft,
required String isPWA,
required String isWeb,
required String isDebug,
required String platform,
required String platformBrightness,
}) {
_pageName = pageName;
_pageHash = pageRoute;
Expand All @@ -126,7 +130,9 @@ class FletServer {
_windowLeft = windowLeft;
_isPWA = isPWA;
_isWeb = isWeb;
_isDebug = isDebug;
_platform = platform;
_platformBrightness = platformBrightness;
}

registerWebClientInternal() {
Expand All @@ -145,7 +151,9 @@ class FletServer {
windowHeight: page?.attrString("windowHeight") ?? _windowHeight,
isPWA: _isPWA,
isWeb: _isWeb,
isDebug: _isDebug,
platform: _platform,
platformBrightness: _platformBrightness,
sessionId: _store.state.sessionId)));
_pageHash = "";
}
Expand Down
6 changes: 6 additions & 0 deletions package/lib/src/protocol/register_webclient_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class RegisterWebClientRequest {
final String? windowLeft;
final String? isPWA;
final String? isWeb;
final String? isDebug;
final String? platform;
final String? platformBrightness;
final String? sessionId;

RegisterWebClientRequest(
Expand All @@ -23,7 +25,9 @@ class RegisterWebClientRequest {
this.windowLeft,
this.isPWA,
this.isWeb,
this.isDebug,
this.platform,
this.platformBrightness,
this.sessionId});

Map<String, dynamic> toJson() => <String, dynamic>{
Expand All @@ -37,7 +41,9 @@ class RegisterWebClientRequest {
'windowLeft': windowLeft,
'isPWA': isPWA,
'isWeb': isWeb,
'isDebug': isDebug,
'platform': platform,
'platformBrightness': platformBrightness,
'sessionId': sessionId
};
}
30 changes: 27 additions & 3 deletions package/lib/src/reducers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum Actions { increment, setText, setError }

AppState appReducer(AppState state, dynamic action) {
if (action is PageLoadAction) {
var sessionId = SessionStore.get("sessionId");
var sessionId = SessionStore.sessionId;
return state.copyWith(
pageUri: action.pageUri,
assetsDir: action.assetsDir,
Expand Down Expand Up @@ -95,7 +95,9 @@ AppState appReducer(AppState state, dynamic action) {
windowLeft: wmd.left != null ? wmd.left.toString() : "",
isPWA: isProgressiveWebApp().toString(),
isWeb: kIsWeb.toString(),
platform: defaultTargetPlatform.name.toLowerCase());
isDebug: kDebugMode.toString(),
platform: defaultTargetPlatform.name.toLowerCase(),
platformBrightness: state.displayBrightness.name.toString());

action.server.connect(address: state.pageUri!.toString());
});
Expand Down Expand Up @@ -137,6 +139,28 @@ AppState appReducer(AppState state, dynamic action) {

return state.copyWith(controls: controls);
} else if (action is PageBrightnessChangeAction) {
//
// platform brightness changed
//
debugPrint("New platform brightness: ${action.brightness.name}");

var page = state.controls["page"];
var controls = Map.of(state.controls);
if (page != null && !state.isLoading) {
var pageAttrs = Map.of(page.attrs);
pageAttrs["platformBrightness"] = action.brightness.name.toString();

List<Map<String, String>> props = [
{"i": "page", "platformBrightness": action.brightness.name.toString()},
];

controls[page.id] = page.copyWith(attrs: pageAttrs);
action.server.updateControlProps(props: props);
action.server.sendPageEvent(
eventTarget: "page",
eventName: "platformBrightnessChange",
eventData: action.brightness.name.toString());
}
return state.copyWith(displayBrightness: action.brightness);
} else if (action is RegisterWebClientAction) {
//
Expand All @@ -152,7 +176,7 @@ AppState appReducer(AppState state, dynamic action) {
final sessionId = action.payload.session!.id;

// store sessionId in a cookie
SessionStore.set("sessionId", sessionId);
SessionStore.sessionId = sessionId;

if (state.deepLinkingRoute != "") {
debugPrint(
Expand Down
8 changes: 8 additions & 0 deletions package/lib/src/utils/session_store_non_web.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import 'package:flutter/foundation.dart';

class SessionStore {
static String? get sessionId {
return null;
}

static set sessionId(String? value) {
// nothing to do
}

static String? get(String name) {
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions package/lib/src/utils/session_store_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import 'dart:html' as html;
import 'package:flutter/foundation.dart';

class SessionStore {
static String? get sessionId {
return html.window.name;
}

static set sessionId(String? value) {
html.window.name = value;
}

static String? get(String name) {
debugPrint("Get session storage $name");

Expand Down
3 changes: 2 additions & 1 deletion package/lib/src/widgets/page_media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class _PageMediaState extends State<PageMedia> {

_onScreenBrightnessChanged(Brightness brightness, Function dispatch) {
debugPrint("Send new brightness to reducer: $brightness");
dispatch(PageBrightnessChangeAction(brightness));
dispatch(PageBrightnessChangeAction(
brightness, FletAppServices.of(context).server));
}

@override
Expand Down
2 changes: 1 addition & 1 deletion package/test/protocol/register_webclient_request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ void main() {

final j = json.encode(m);
expect(j,
'{"action":"registerWebClient","payload":{"pageName":"test-page1","pageRoute":null,"pageWidth":null,"pageHeight":null,"windowWidth":null,"windowHeight":null,"windowTop":null,"windowLeft":null,"isPWA":null,"isWeb":null,"platform":null,"sessionId":null}}');
'{"action":"registerWebClient","payload":{"pageName":"test-page1","pageRoute":null,"pageWidth":null,"pageHeight":null,"windowWidth":null,"windowHeight":null,"windowTop":null,"windowLeft":null,"isPWA":null,"isWeb":null,"isDebug":null,"platform":null,"platformBrightness":null,"sessionId":null}}');
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def _create_register_web_client_response(self):
"windowleft": self._client_details.windowLeft,
"pwa": self._client_details.isPWA,
"web": self._client_details.isWeb,
"debug": self._client_details.isDebug,
"platform": self._client_details.platform,
"platformBrightness": self._client_details.platformBrightness,
}
},
),
Expand Down Expand Up @@ -216,8 +218,12 @@ def _process_get_command(self, values: List[str]):
r = self._client_details.isPWA
elif prop_name == "web":
r = self._client_details.isWeb
elif prop_name == "debug":
r = self._client_details.isDebug
elif prop_name == "platform":
r = self._client_details.platform
elif prop_name == "platformBrightness":
r = self._client_details.platformBrightness
elif prop_name == "width":
r = self._client_details.pageWidth
elif prop_name == "height":
Expand Down
54 changes: 40 additions & 14 deletions sdk/python/packages/flet-core/src/flet_core/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def __init__(self, conn: Connection, session_id):
self._add_event_handler("close", self.__on_close.get_handler())
self.__on_resize = EventHandler()
self._add_event_handler("resize", self.__on_resize.get_handler())
self.__on_platform_brightness_change = EventHandler()
self._add_event_handler(
"platformBrightnessChange",
self.__on_platform_brightness_change.get_handler(),
)

self.__last_route = None

Expand Down Expand Up @@ -220,13 +225,11 @@ async def fetch_page_details_async(self):
def __get_page_detail_commands(self):
return [
Command(0, "get", ["page", "route"]),
Command(
0,
"get",
["page", "pwa"],
),
Command(0, "get", ["page", "pwa"]),
Command(0, "get", ["page", "web"]),
Command(0, "get", ["page", "debug"]),
Command(0, "get", ["page", "platform"]),
Command(0, "get", ["page", "platformBrightness"]),
Command(0, "get", ["page", "width"]),
Command(0, "get", ["page", "height"]),
Command(0, "get", ["page", "windowWidth"]),
Expand All @@ -241,15 +244,17 @@ def __set_page_details(self, values):
self._set_attr("route", values[0], False)
self._set_attr("pwa", values[1], False)
self._set_attr("web", values[2], False)
self._set_attr("platform", values[3], False)
self._set_attr("width", values[4], False)
self._set_attr("height", values[5], False)
self._set_attr("windowWidth", values[6], False)
self._set_attr("windowHeight", values[7], False)
self._set_attr("windowTop", values[8], False)
self._set_attr("windowLeft", values[9], False)
self._set_attr("clientIP", values[10], False)
self._set_attr("clientUserAgent", values[11], False)
self._set_attr("debug", values[3], False)
self._set_attr("platform", values[4], False)
self._set_attr("platformBrightness", values[5], False)
self._set_attr("width", values[6], False)
self._set_attr("height", values[7], False)
self._set_attr("windowWidth", values[8], False)
self._set_attr("windowHeight", values[9], False)
self._set_attr("windowTop", values[10], False)
self._set_attr("windowLeft", values[11], False)
self._set_attr("clientIP", values[12], False)
self._set_attr("clientUserAgent", values[13], False)

def update(self, *controls):
with self.__lock:
Expand Down Expand Up @@ -1117,11 +1122,23 @@ def pwa(self):
def web(self) -> bool:
return cast(bool, self._get_attr("web", data_type="bool", def_value=False))

# debug
@property
def debug(self) -> bool:
return cast(bool, self._get_attr("debug", data_type="bool", def_value=False))

# platform
@property
def platform(self):
return self._get_attr("platform")

# platform_brightness
@property
def platform_brightness(self) -> ThemeMode:
brightness = self._get_attr("platformBrightness")
assert brightness is not None
return ThemeMode(brightness)

# client_ip
@property
def client_ip(self):
Expand Down Expand Up @@ -1659,6 +1676,15 @@ def on_resize(self):
def on_resize(self, handler):
self.__on_resize.subscribe(handler)

# on_platform_brightness_change
@property
def on_platform_brightness_change(self):
return self.__on_platform_brightness_change

@on_platform_brightness_change.setter
def on_platform_brightness_change(self, handler):
self.__on_platform_brightness_change.subscribe(handler)

# on_route_change
@property
def on_route_change(self):
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class RegisterWebClientRequestPayload:
windowLeft: str
isPWA: str
isWeb: str
isDebug: str
platform: str
platformBrightness: str
sessionId: str


Expand Down
Loading

0 comments on commit eb209ed

Please sign in to comment.