Skip to content

Commit

Permalink
add support for Clock Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
m0nac0 committed Dec 30, 2021
1 parent e660818 commit 45e9a6a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ building it in an admin shell!
- VideoPlayer only supported on Android/iOS/web and only supports network files (http:// or https:// URLs)
- Drawing/Animation: none
- Maps: none
- Sensors: only Clock (no Timer)
- Sensors: only Clock (Timer never fires while app is in the background)
- Social: Sharing, Texting and PhoneCall (but no direct messages/calls; no intercepting of messages/calls)
- Storage: complete, except CloudDB
- File does not have functionality for Delete, Append, LegacyMode
Expand Down
9 changes: 9 additions & 0 deletions lib/compiler/aia_compiler_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ Map<String, Map<String, Expression>> properties = {
...visibleProperties,
...sizeProperties,
},
"Clock": {
"TimerEnabled": ltrue,
"TimerInterval": literalNum(1000),
//TODO implement TimerAlwaysFires
"TimerAlwaysFires": ltrue,
},
"PhoneCall": {"PhoneNumber": literalString("")},
"Texting": {
"PhoneNumber": literalString(""),
Expand Down Expand Up @@ -199,6 +205,9 @@ Map<String, List<Event>> events = {
"VideoPlayer": [
Event("Completed"),
],
"Clock": [
Event("Timer"),
],
"File": [
Event("AfterFileSaved", {"fileName": refer("String")}),
Event("GotText", {"text": refer("String")}),
Expand Down
13 changes: 7 additions & 6 deletions lib/compiler/aia_to_dart_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,7 @@ class AIAToDartCompiler {
..name = "sharedPrefs"
..type = r("SharedPreferences")
..modifier));
b.methods.add(Method((m) => m
..name = "initState"
..body = Block.of([
r("initSharedPrefs")([]).statement,
r("super.initState").statement
])));
state.addInitStateStatement(r("initSharedPrefs")([]).statement);
b.methods.add(Method((m) => m
..name = "initSharedPrefs"
..modifier = MethodModifier.async
Expand All @@ -209,6 +204,12 @@ class AIAToDartCompiler {
.statement,
])));
}
if (state.initStateStatements.isNotEmpty) {
b.methods.add(Method((m) => m
..name = "initState"
..body = Block.of(
[...state.initStateStatements, r("super.initState").statement])));
}
if (state.usesEnsureNum) {
b.methods.add(Method((m) => m
..name = "ensureNum"
Expand Down
44 changes: 44 additions & 0 deletions lib/compiler/parse_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,50 @@ class ComponentParser {
}
case "Clock":
{
var timer = getPropertyDartName(componentName, "Timer");
var timerEnabled = getPropertyDartName(componentName, "TimerEnabled");
state.ensureFieldExists(timer);

state.addInitStateStatement(wrapWithSetState(r(timerEnabled)
.assign(getComponentBoolProperty(component, "TimerEnabled") ??
defaultValues[
getPropertyDartName(componentName, "TimerEnabled")]!)
.statement));

// get TimerEnabled => Timer != null;
state.gettersSetters.add(Method((m) => m
..type = MethodType.getter
..name = timerEnabled
..returns = r("bool")
..body = r(timer).notEqualTo(literalNull).code
..lambda = true));

state.gettersSetters.add(Method((m) => m
..type = MethodType.setter
..name = timerEnabled
..requiredParameters.add(Parameter((p) => p..name = "value"))
..body = Block.of([
r(timer).nullSafeProperty("cancel")([]).statement,
r(timer)
.assign(r("value").conditional(
r("Timer.periodic", asyncPackage)([
r("Duration")([], {
"milliseconds": getDartExpressionForProperty(
propsDartNames,
"TimerInterval",
getComponentNumProperty(
component, "TimerInterval"),
defaultValues)
}),
Method((m) => m
..requiredParameters
.add(Parameter((p) => p..name = "_"))
..body = safeCallEventHandler(getDartEventHandler(
state, componentName, "Timer"))).closure
]),
literalNull))
.statement
])));
return null;
}
case "Player":
Expand Down
6 changes: 6 additions & 0 deletions lib/compiler/parsing_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ParsingState {
bool usesSafeStringAddition = false;
bool usesEnsureNum = false;

List<Code> initStateStatements = [];

// First all blocks are processed. We put an entry with value null for
// all component-attribute-variables they need. E.g. if the user has a
// PhoneCall1.MakePhoneCall block, that puts a <"PhoneCall1_PhoneNumber",null>
Expand All @@ -51,4 +53,8 @@ class ParsingState {
void ensureFieldExists(String name) {
fields.putIfAbsent(name, () => null);
}

void addInitStateStatement(Code statement) {
initStateStatements.add(statement);
}
}
1 change: 1 addition & 0 deletions lib/compiler/util_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const lfalse = literalFalse;
const materialPackage = "package:flutter/material.dart";
const mathPackage = "dart:math";
const convertPackage = "dart:convert";
const asyncPackage = "dart:async";

// packages supported by Dart/Flutter team
const httpPackage = "package:http/http.dart";
Expand Down

0 comments on commit 45e9a6a

Please sign in to comment.