@snap/valdi 1.0.7 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/BUILD.bazel.template +16 -1
- package/.bootstrap/modules/polyglot_bridge_module/web/Calculator.ts +11 -0
- package/.bootstrap/modules/polyglot_bridge_module/web/tsconfig.json +16 -0
- package/.bootstrap/modules/polyglot_view_module/BUILD.bazel.template +75 -0
- package/.bootstrap/modules/polyglot_view_module/android/PolyglotNativeView.kt.template +41 -0
- package/.bootstrap/modules/polyglot_view_module/android/PolyglotNativeViewAttributesBinder.kt.template +17 -0
- package/.bootstrap/modules/polyglot_view_module/ios/SCPolyglotView.h +8 -0
- package/.bootstrap/modules/polyglot_view_module/ios/SCPolyglotView.m +23 -0
- package/.bootstrap/modules/polyglot_view_module/macos/SCPolyglotView.h +8 -0
- package/.bootstrap/modules/polyglot_view_module/macos/SCPolyglotView.m +23 -0
- package/.bootstrap/modules/polyglot_view_module/src/PolyglotCustomView.tsx.template +23 -0
- package/.bootstrap/modules/polyglot_view_module/web/PolyglotWeb.ts.template +30 -0
- package/.bootstrap/modules/polyglot_view_module/web/tsconfig.json +16 -0
- package/.metadata/AGENTS.md.template +8 -274
- package/.metadata/skill-reference.tsx.template +8 -0
- package/.metadata/skill-tests-BUILD.bazel.template +21 -0
- package/.metadata/skill-tests-README.md.template +17 -0
- package/.metadata/skill.md.template +27 -0
- package/README.md +12 -3
- package/bundled-skills/CONTRIBUTING_SKILLS.md +74 -0
- package/bundled-skills/registry.json +21 -0
- package/bundled-skills/skills/valdi-android/skill.md +89 -0
- package/bundled-skills/skills/valdi-async/skill.md +246 -0
- package/bundled-skills/skills/valdi-async/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-async/tests/README.md +25 -0
- package/bundled-skills/skills/valdi-async/tests/src/reference.tsx +210 -0
- package/bundled-skills/skills/valdi-async/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-bazel/skill.md +141 -0
- package/bundled-skills/skills/valdi-compiler/skill.md +108 -0
- package/bundled-skills/skills/valdi-component-tests/skill.md +430 -0
- package/bundled-skills/skills/valdi-component-tests/tests/BUILD.bazel +24 -0
- package/bundled-skills/skills/valdi-component-tests/tests/README.md +35 -0
- package/bundled-skills/skills/valdi-component-tests/tests/src/ProfileCard.tsx +38 -0
- package/bundled-skills/skills/valdi-component-tests/tests/test/ProfileCardTest.spec.tsx +107 -0
- package/bundled-skills/skills/valdi-component-tests/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-cpp-runtime/skill.md +103 -0
- package/bundled-skills/skills/valdi-custom-view/skill.md +64 -0
- package/bundled-skills/skills/valdi-inspect/skill.md +199 -0
- package/bundled-skills/skills/valdi-ios/skill.md +93 -0
- package/bundled-skills/skills/valdi-migrate/skill.md +175 -0
- package/bundled-skills/skills/valdi-migrate/tests/BUILD.bazel +23 -0
- package/bundled-skills/skills/valdi-migrate/tests/README.md +70 -0
- package/bundled-skills/skills/valdi-migrate/tests/check_antipatterns.py +89 -0
- package/bundled-skills/skills/valdi-migrate/tests/compose_example.kt +94 -0
- package/bundled-skills/skills/valdi-migrate/tests/flutter_example.dart +129 -0
- package/bundled-skills/skills/valdi-migrate/tests/react_example.tsx +98 -0
- package/bundled-skills/skills/valdi-migrate/tests/src/expected_valdi.tsx +267 -0
- package/bundled-skills/skills/valdi-migrate/tests/tsconfig.json +3 -0
- package/bundled-skills/skills/valdi-overview/skill.md +63 -0
- package/bundled-skills/skills/valdi-perf/skill.md +197 -0
- package/bundled-skills/skills/valdi-perf/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-perf/tests/README.md +26 -0
- package/bundled-skills/skills/valdi-perf/tests/src/reference.tsx +202 -0
- package/bundled-skills/skills/valdi-perf/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-polyglot-module/skill.md +124 -0
- package/bundled-skills/skills/valdi-profile/skill.md +126 -0
- package/bundled-skills/skills/valdi-setup/skill.md +129 -0
- package/bundled-skills/skills/valdi-testing/skill.md +65 -0
- package/bundled-skills/skills/valdi-tsx/skill.md +385 -0
- package/bundled-skills/skills/valdi-tsx/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-tsx/tests/README.md +29 -0
- package/bundled-skills/skills/valdi-tsx/tests/src/reference.tsx +173 -0
- package/bundled-skills/skills/valdi-tsx/tests/tsconfig.json +1 -0
- package/dist/commands/bootstrap.js +0 -7
- package/dist/commands/inspect.js +16 -0
- package/dist/commands/inspect_commands/contexts.js +55 -0
- package/dist/commands/inspect_commands/devices.js +53 -0
- package/dist/commands/inspect_commands/heap.js +59 -0
- package/dist/commands/inspect_commands/select.js +43 -0
- package/dist/commands/inspect_commands/snapshot.js +64 -0
- package/dist/commands/inspect_commands/status.js +68 -0
- package/dist/commands/inspect_commands/tree.js +86 -0
- package/dist/commands/newModule.js +10 -14
- package/dist/commands/profile.js +16 -0
- package/dist/commands/profile_commands/capture.js +106 -0
- package/dist/commands/skills.js +16 -0
- package/dist/commands/skills_commands/create.js +125 -0
- package/dist/commands/skills_commands/install.js +96 -0
- package/dist/commands/skills_commands/list.js +83 -0
- package/dist/commands/skills_commands/remove.js +41 -0
- package/dist/commands/skills_commands/update.js +59 -0
- package/dist/core/constants.js +0 -1
- package/dist/core/packageFiles.spec.js +29 -0
- package/dist/setup/versions.js +1 -1
- package/dist/utils/daemonClient.js +342 -0
- package/dist/utils/hermesClient.js +252 -0
- package/dist/utils/skillsAdapters.js +140 -0
- package/dist/utils/skillsRegistry.js +40 -0
- package/dist/utils/stringUtils.spec.js +1 -1
- package/package.json +20 -5
- package/.bazelignore +0 -2
- package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +0 -21
- package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +0 -28
- package/.bootstrap/modules/ios_android_bridge_module/src/Calculator.d.ts +0 -4
- package/.bootstrap/modules/ios_android_view_module/BUILD.bazel.template +0 -41
- package/.bootstrap/modules/ios_android_view_module/android/CircleView.kt.template +0 -42
- package/.bootstrap/modules/ios_android_view_module/android/CircleViewAttributesBinder.kt.template +0 -60
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.h +0 -6
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.m +0 -82
- package/.bootstrap/modules/ios_android_view_module/src/Circle.tsx.template +0 -15
- package/.config/copyconfig.json +0 -10
- package/.editorconfig +0 -21
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -72
- package/.github/README.md +0 -164
- package/.metadata/.cursorrules.template +0 -60
- package/.prettierignore +0 -2
- package/.prettierrc.json +0 -7
- package/LINUX_COMPATIBILITY.md +0 -248
- package/prettier.config.js +0 -4
- package/scripts/sync-agents-md.sh +0 -44
- package/scripts/update-agents-templates.ts +0 -170
- package/spec/support/jasmine.json +0 -6
- package/src/commands/bootstrap.ts +0 -410
- package/src/commands/build.ts +0 -89
- package/src/commands/devSetup.ts +0 -17
- package/src/commands/doctor.ts +0 -1832
- package/src/commands/export.ts +0 -131
- package/src/commands/hotreload.ts +0 -108
- package/src/commands/install.ts +0 -93
- package/src/commands/lint.ts +0 -15
- package/src/commands/lint_commands/lintCheck.ts +0 -53
- package/src/commands/lint_commands/lintFormat.ts +0 -49
- package/src/commands/log.ts +0 -108
- package/src/commands/newModule.ts +0 -286
- package/src/commands/package.ts +0 -102
- package/src/commands/projectsync.ts +0 -488
- package/src/commands/test.ts +0 -68
- package/src/core/constants.ts +0 -98
- package/src/core/errors.ts +0 -10
- package/src/index.ts +0 -27
- package/src/setup/DevSetupHelper.ts +0 -290
- package/src/setup/linuxSetup.ts +0 -261
- package/src/setup/macOSSetup.ts +0 -156
- package/src/setup/setupEntryPoint.ts +0 -64
- package/src/setup/versions.ts +0 -8
- package/src/utils/ArgumentsResolver.ts +0 -23
- package/src/utils/BazelClient.ts +0 -285
- package/src/utils/Digraph.ts +0 -81
- package/src/utils/LoadingIndicator.ts +0 -43
- package/src/utils/applicationUtils.ts +0 -276
- package/src/utils/buildInfo.ts +0 -99
- package/src/utils/byString.ts +0 -3
- package/src/utils/cliUtils.ts +0 -112
- package/src/utils/copyBootstrapFiles.ts +0 -30
- package/src/utils/deviceUtils.ts +0 -76
- package/src/utils/errorUtils.ts +0 -63
- package/src/utils/fileUtils.ts +0 -257
- package/src/utils/githubUtils.ts +0 -102
- package/src/utils/jsonUtils.ts +0 -30
- package/src/utils/lintUtils.ts +0 -190
- package/src/utils/linuxDistro.spec.ts +0 -219
- package/src/utils/linuxDistro.ts +0 -326
- package/src/utils/logUtils.ts +0 -9
- package/src/utils/pathUtils.ts +0 -12
- package/src/utils/stringUtils.spec.ts +0 -95
- package/src/utils/stringUtils.ts +0 -103
- package/src/utils/tempDir.ts +0 -24
- package/src/utils/zipUtils.ts +0 -14
- package/test/ValdiSmokeTest.ts +0 -279
- package/test/helpers/AsyncHelpers.ts +0 -113
- package/test/helpers/CommandHelpers.ts +0 -37
- package/test/helpers/ProjectHelpers.ts +0 -153
- package/test/helpers/StreamHelpers.ts +0 -37
- package/test/helpers/TypeScriptClient.ts +0 -217
- package/tsconfig.dist.json +0 -8
- package/tsconfig.json +0 -42
- /package/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/android/CalculatorModuleFactory.kt.template +0 -0
- /package/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/ios/SCCCalculatorModuleFactory.m.template +0 -0
- /package/.bootstrap/modules/{cpp_bridge_module → polyglot_bridge_module}/src/Calculator.d.ts +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# valdi-migrate skill tests
|
|
2
|
+
|
|
3
|
+
Validates that code migrated to Valdi using the `valdi-migrate` skill is free of
|
|
4
|
+
anti-patterns (React hooks, Compose APIs, inline lambdas, `map()` in render loops, etc.)
|
|
5
|
+
and that the output actually compiles.
|
|
6
|
+
|
|
7
|
+
## Files
|
|
8
|
+
|
|
9
|
+
| File | Description |
|
|
10
|
+
|------|-------------|
|
|
11
|
+
| `BUILD.bazel` | Bazel build target — compiles `src/` and `output/` together |
|
|
12
|
+
| `tsconfig.json` | TypeScript config |
|
|
13
|
+
| `check_antipatterns.py` | Linter — exits 0 if clean, 1 if violations found |
|
|
14
|
+
| `flutter_example.dart` | Source Flutter code to migrate |
|
|
15
|
+
| `compose_example.kt` | Source Jetpack Compose code to migrate |
|
|
16
|
+
| `react_example.tsx` | Source React code to migrate |
|
|
17
|
+
| `src/expected_valdi.tsx` | Reference correct Valdi output (canonical ground truth) |
|
|
18
|
+
| `output/` | Skill-generated migrations — checked in after running integration tests |
|
|
19
|
+
|
|
20
|
+
## Integration tests
|
|
21
|
+
|
|
22
|
+
These tests verify the skill produces correct, compilable Valdi from each source framework.
|
|
23
|
+
|
|
24
|
+
### 1. Generate migrations
|
|
25
|
+
|
|
26
|
+
Ask Claude (with the `valdi-migrate` skill active) to migrate each source file:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
Migrate flutter_example.dart to Valdi
|
|
30
|
+
Migrate compose_example.kt to Valdi
|
|
31
|
+
Migrate react_example.tsx to Valdi
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Save each output to the `output/` directory:
|
|
35
|
+
- `output/flutter_migrated.tsx`
|
|
36
|
+
- `output/compose_migrated.tsx`
|
|
37
|
+
- `output/react_migrated.tsx`
|
|
38
|
+
|
|
39
|
+
### 2. Anti-pattern check
|
|
40
|
+
|
|
41
|
+
Verify none of the outputs contain framework-specific patterns:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
cd client/src/open_source/ai-skills/skills/valdi-migrate/tests
|
|
45
|
+
|
|
46
|
+
python3 check_antipatterns.py output/flutter_migrated.tsx # must pass
|
|
47
|
+
python3 check_antipatterns.py output/compose_migrated.tsx # must pass
|
|
48
|
+
python3 check_antipatterns.py output/react_migrated.tsx # must pass
|
|
49
|
+
|
|
50
|
+
# Sanity check — source files should still fail
|
|
51
|
+
python3 check_antipatterns.py react_example.tsx # expected: fail
|
|
52
|
+
python3 check_antipatterns.py compose_example.kt # expected: fail
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 3. Compile check
|
|
56
|
+
|
|
57
|
+
Verify all outputs (reference + migrations) build with the Valdi compiler:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cd client/src/open_source
|
|
61
|
+
bazel build //ai-skills/skills/valdi-migrate/tests:tests
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Expected: `Build completed successfully`
|
|
65
|
+
|
|
66
|
+
## Updating the tests
|
|
67
|
+
|
|
68
|
+
- **Source examples** (`flutter_example.dart`, `compose_example.kt`, `react_example.tsx`): add new patterns you want the skill to handle correctly.
|
|
69
|
+
- **Reference output** (`src/expected_valdi.tsx`): update when Valdi APIs change (import paths, type signatures, etc.). Must always compile and pass the linter.
|
|
70
|
+
- **Skill** (`../skill.md`): update when adding new source frameworks or fixing incorrect migration guidance.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Validates a Valdi TSX file for known anti-patterns.
|
|
4
|
+
Usage: python3 check_antipatterns.py <file.tsx>
|
|
5
|
+
Returns exit code 0 if clean, 1 if violations found.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
import re
|
|
10
|
+
|
|
11
|
+
ANTI_PATTERNS = [
|
|
12
|
+
# React hooks
|
|
13
|
+
(r'\buseState\b', 'React hook: useState'),
|
|
14
|
+
(r'\buseEffect\b', 'React hook: useEffect'),
|
|
15
|
+
(r'\buseContext\b', 'React hook: useContext'),
|
|
16
|
+
(r'\buseMemo\b', 'React hook: useMemo'),
|
|
17
|
+
(r'\buseCallback\b', 'React hook: useCallback'),
|
|
18
|
+
(r'\buseRef\b', 'React hook: useRef'),
|
|
19
|
+
|
|
20
|
+
# Compose APIs
|
|
21
|
+
(r'@Composable', 'Compose: @Composable annotation'),
|
|
22
|
+
(r'\bremember\s*\{', 'Compose: remember { }'),
|
|
23
|
+
(r'\bmutableStateOf\b', 'Compose: mutableStateOf'),
|
|
24
|
+
(r'\bLaunchedEffect\b', 'Compose: LaunchedEffect'),
|
|
25
|
+
(r'\bDisposableEffect\b', 'Compose: DisposableEffect'),
|
|
26
|
+
|
|
27
|
+
# Functional components
|
|
28
|
+
(r'const\s+\w+\s*=\s*\([^)]*\)\s*=>\s*[<(]', 'Functional component arrow fn'),
|
|
29
|
+
(r'function\s+\w+\s*\([^)]*\)\s*\{[^}]*return\s*<', 'Functional component with return JSX'),
|
|
30
|
+
|
|
31
|
+
# Wrong naming
|
|
32
|
+
(r'\bthis\.props\b', 'Wrong: this.props (should be this.viewModel)'),
|
|
33
|
+
(r'\bonMount\b', 'Wrong: onMount (should be onCreate)'),
|
|
34
|
+
(r'\bonUnmount\b', 'Wrong: onUnmount (should be onDestroy)'),
|
|
35
|
+
(r'\bmarkNeedsRender\b', 'Wrong: markNeedsRender (use this.setState)'),
|
|
36
|
+
(r'\bscheduleRender\b', 'Wrong: scheduleRender (deprecated)'),
|
|
37
|
+
|
|
38
|
+
# return JSX in onRender
|
|
39
|
+
(r'onRender\s*\(\s*\)\s*\{[^}]*return\s*<', 'onRender() should not return JSX'),
|
|
40
|
+
|
|
41
|
+
# Inline lambdas in JSX props (e.g. onTap={() => ...})
|
|
42
|
+
(r'on\w+\s*=\s*\{\s*\(\s*\)\s*=>', 'Inline lambda in JSX prop (use class arrow fn)'),
|
|
43
|
+
|
|
44
|
+
# map() in JSX context
|
|
45
|
+
(r'\.map\s*\([^)]*=>\s*[^)]*<\w', 'map() returns array (use forEach in onRender)'),
|
|
46
|
+
|
|
47
|
+
# new Style inside onRender
|
|
48
|
+
(r'onRender\s*\(\s*\)\s*\{(?:[^}]|\{[^}]*\})*new\s+Style\s*<', 'new Style() inside onRender()'),
|
|
49
|
+
|
|
50
|
+
# Wrong import paths (discovered by build failures)
|
|
51
|
+
# Provider is two separate files, not a directory import
|
|
52
|
+
(r"from 'valdi_core/src/provider'",
|
|
53
|
+
"Wrong import: use 'valdi_core/src/provider/createProvider' and/or 'valdi_core/src/provider/withProviders'"),
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
def check_file(path: str) -> list[tuple[int, str, str]]:
|
|
57
|
+
violations = []
|
|
58
|
+
with open(path) as f:
|
|
59
|
+
lines = f.readlines()
|
|
60
|
+
for lineno, line in enumerate(lines, 1):
|
|
61
|
+
stripped = line.lstrip()
|
|
62
|
+
# Skip comment lines
|
|
63
|
+
if stripped.startswith('//') or stripped.startswith('*') or stripped.startswith('#'):
|
|
64
|
+
continue
|
|
65
|
+
for pattern, description in ANTI_PATTERNS:
|
|
66
|
+
if re.search(pattern, line):
|
|
67
|
+
violations.append((lineno, description, line.rstrip()))
|
|
68
|
+
return violations
|
|
69
|
+
|
|
70
|
+
def main():
|
|
71
|
+
if len(sys.argv) < 2:
|
|
72
|
+
print(f'Usage: {sys.argv[0]} <file.tsx>', file=sys.stderr)
|
|
73
|
+
sys.exit(2)
|
|
74
|
+
|
|
75
|
+
path = sys.argv[1]
|
|
76
|
+
violations = check_file(path)
|
|
77
|
+
|
|
78
|
+
if not violations:
|
|
79
|
+
print(f'✅ {path}: no anti-patterns found')
|
|
80
|
+
sys.exit(0)
|
|
81
|
+
else:
|
|
82
|
+
print(f'❌ {path}: {len(violations)} violation(s) found')
|
|
83
|
+
for lineno, desc, text in violations:
|
|
84
|
+
print(f' Line {lineno}: [{desc}]')
|
|
85
|
+
print(f' {text}')
|
|
86
|
+
sys.exit(1)
|
|
87
|
+
|
|
88
|
+
if __name__ == '__main__':
|
|
89
|
+
main()
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Jetpack Compose example — @Composable functions, remember, LaunchedEffect,
|
|
2
|
+
// LazyColumn, navigation, CompositionLocal.
|
|
3
|
+
// Goal: migrate this to Valdi.
|
|
4
|
+
|
|
5
|
+
import androidx.compose.foundation.layout.*
|
|
6
|
+
import androidx.compose.foundation.lazy.LazyColumn
|
|
7
|
+
import androidx.compose.foundation.lazy.items
|
|
8
|
+
import androidx.compose.material3.*
|
|
9
|
+
import androidx.compose.runtime.*
|
|
10
|
+
import androidx.compose.ui.Modifier
|
|
11
|
+
import androidx.compose.ui.unit.dp
|
|
12
|
+
import androidx.navigation.NavController
|
|
13
|
+
|
|
14
|
+
// --- CompositionLocal theme ---
|
|
15
|
+
val LocalAppTheme = compositionLocalOf { AppTheme() }
|
|
16
|
+
data class AppTheme(val primary: String = "#FFFC00")
|
|
17
|
+
|
|
18
|
+
// --- Stateless greeting ---
|
|
19
|
+
@Composable
|
|
20
|
+
fun Greeting(name: String) {
|
|
21
|
+
Text("Hello, $name")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// --- Stateful counter ---
|
|
25
|
+
@Composable
|
|
26
|
+
fun Counter(label: String) {
|
|
27
|
+
var count by remember { mutableStateOf(0) }
|
|
28
|
+
Button(onClick = { count++ }) {
|
|
29
|
+
Text("$label: $count")
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// --- Data loading with LaunchedEffect ---
|
|
34
|
+
@Composable
|
|
35
|
+
fun UserProfile(userId: String) {
|
|
36
|
+
var profile by remember { mutableStateOf<Profile?>(null) }
|
|
37
|
+
|
|
38
|
+
LaunchedEffect(userId) {
|
|
39
|
+
profile = fetchProfile(userId) // suspend fun
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (profile == null) {
|
|
43
|
+
CircularProgressIndicator()
|
|
44
|
+
} else {
|
|
45
|
+
Text(profile!!.name)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// --- LazyColumn list ---
|
|
50
|
+
@Composable
|
|
51
|
+
fun UserList(users: List<User>) {
|
|
52
|
+
LazyColumn {
|
|
53
|
+
items(users, key = { it.id }) { user ->
|
|
54
|
+
UserRow(user = user)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- Layout with Column / Row / Modifier ---
|
|
60
|
+
@Composable
|
|
61
|
+
fun ProfileCard(user: User) {
|
|
62
|
+
Column(
|
|
63
|
+
modifier = Modifier
|
|
64
|
+
.fillMaxWidth()
|
|
65
|
+
.padding(16.dp)
|
|
66
|
+
) {
|
|
67
|
+
Row(modifier = Modifier.fillMaxWidth()) {
|
|
68
|
+
Text(user.name, style = MaterialTheme.typography.headlineSmall)
|
|
69
|
+
Spacer(Modifier.weight(1f))
|
|
70
|
+
Text(user.handle)
|
|
71
|
+
}
|
|
72
|
+
Spacer(modifier = Modifier.height(8.dp))
|
|
73
|
+
Text(user.bio)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// --- Navigation ---
|
|
78
|
+
@Composable
|
|
79
|
+
fun HomeScreen(navController: NavController) {
|
|
80
|
+
Button(onClick = { navController.navigate("detail/42") }) {
|
|
81
|
+
Text("Go to Detail")
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- CompositionLocal consumer ---
|
|
86
|
+
@Composable
|
|
87
|
+
fun ThemedBadge(text: String) {
|
|
88
|
+
val theme = LocalAppTheme.current
|
|
89
|
+
Box(
|
|
90
|
+
modifier = Modifier.background(Color(android.graphics.Color.parseColor(theme.primary)))
|
|
91
|
+
) {
|
|
92
|
+
Text(text)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Flutter example — mix of StatelessWidget, StatefulWidget, ListView.builder,
|
|
2
|
+
// navigation, SharedPreferences, and a Provider consumer.
|
|
3
|
+
// Goal: migrate this to Valdi.
|
|
4
|
+
|
|
5
|
+
import 'package:flutter/material.dart';
|
|
6
|
+
import 'package:provider/provider.dart';
|
|
7
|
+
import 'package:shared_preferences/shared_preferences.dart';
|
|
8
|
+
|
|
9
|
+
// --- Theme service (ChangeNotifier) ---
|
|
10
|
+
class ThemeService extends ChangeNotifier {
|
|
11
|
+
Color primary = Colors.yellow;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// --- Stateless greeting ---
|
|
15
|
+
class Greeting extends StatelessWidget {
|
|
16
|
+
final String name;
|
|
17
|
+
const Greeting({required this.name});
|
|
18
|
+
|
|
19
|
+
@override
|
|
20
|
+
Widget build(BuildContext context) {
|
|
21
|
+
return Text('Hello, $name');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// --- Stateful counter ---
|
|
26
|
+
class Counter extends StatefulWidget {
|
|
27
|
+
final String label;
|
|
28
|
+
const Counter({required this.label});
|
|
29
|
+
@override
|
|
30
|
+
State<Counter> createState() => _CounterState();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class _CounterState extends State<Counter> {
|
|
34
|
+
int _count = 0;
|
|
35
|
+
|
|
36
|
+
void _increment() {
|
|
37
|
+
setState(() { _count++; });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@override
|
|
41
|
+
Widget build(BuildContext context) {
|
|
42
|
+
return GestureDetector(
|
|
43
|
+
onTap: _increment,
|
|
44
|
+
child: Text('${widget.label}: $_count'),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// --- User list with async fetch and cancellation ---
|
|
50
|
+
class UserList extends StatefulWidget {
|
|
51
|
+
@override
|
|
52
|
+
State<UserList> createState() => _UserListState();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class _UserListState extends State<UserList> {
|
|
56
|
+
List<String> _users = [];
|
|
57
|
+
|
|
58
|
+
@override
|
|
59
|
+
void initState() {
|
|
60
|
+
super.initState();
|
|
61
|
+
_loadUsers();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
Future<void> _loadUsers() async {
|
|
65
|
+
// simulated network fetch
|
|
66
|
+
await Future.delayed(Duration(milliseconds: 500));
|
|
67
|
+
setState(() { _users = ['Alice', 'Bob', 'Carol']; });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@override
|
|
71
|
+
Widget build(BuildContext context) {
|
|
72
|
+
return ListView.builder(
|
|
73
|
+
itemCount: _users.length,
|
|
74
|
+
itemBuilder: (context, index) {
|
|
75
|
+
return ListTile(title: Text(_users[index]));
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// --- Settings screen using SharedPreferences ---
|
|
82
|
+
class SettingsScreen extends StatefulWidget {
|
|
83
|
+
@override
|
|
84
|
+
State<SettingsScreen> createState() => _SettingsScreenState();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class _SettingsScreenState extends State<SettingsScreen> {
|
|
88
|
+
bool _notifications = false;
|
|
89
|
+
|
|
90
|
+
@override
|
|
91
|
+
void initState() {
|
|
92
|
+
super.initState();
|
|
93
|
+
_load();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
Future<void> _load() async {
|
|
97
|
+
final prefs = await SharedPreferences.getInstance();
|
|
98
|
+
setState(() { _notifications = prefs.getBool('notifications') ?? false; });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
Future<void> _toggle() async {
|
|
102
|
+
final prefs = await SharedPreferences.getInstance();
|
|
103
|
+
final next = !_notifications;
|
|
104
|
+
await prefs.setBool('notifications', next);
|
|
105
|
+
setState(() { _notifications = next; });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@override
|
|
109
|
+
Widget build(BuildContext context) {
|
|
110
|
+
return Column(
|
|
111
|
+
children: [
|
|
112
|
+
Text('Notifications: $_notifications'),
|
|
113
|
+
ElevatedButton(onPressed: _toggle, child: Text('Toggle')),
|
|
114
|
+
],
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// --- Provider consumer ---
|
|
120
|
+
class ThemedButton extends StatelessWidget {
|
|
121
|
+
@override
|
|
122
|
+
Widget build(BuildContext context) {
|
|
123
|
+
final theme = context.watch<ThemeService>();
|
|
124
|
+
return Container(
|
|
125
|
+
color: theme.primary,
|
|
126
|
+
child: Text('Themed'),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// React example — hooks, Context, FlatList, fetch, styled-components.
|
|
2
|
+
// Goal: migrate this to Valdi.
|
|
3
|
+
|
|
4
|
+
import React, { useState, useEffect, useContext, createContext } from 'react';
|
|
5
|
+
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
|
|
6
|
+
import styled from 'styled-components/native';
|
|
7
|
+
|
|
8
|
+
// --- Context / theme ---
|
|
9
|
+
const ThemeContext = createContext({ primary: '#FFFC00' });
|
|
10
|
+
|
|
11
|
+
// --- Stateless greeting ---
|
|
12
|
+
const Greeting: React.FC<{ name: string }> = ({ name }) => {
|
|
13
|
+
return <Text>Hello, {name}</Text>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// --- Stateful counter ---
|
|
17
|
+
const Counter: React.FC<{ label: string }> = ({ label }) => {
|
|
18
|
+
const [count, setCount] = useState(0);
|
|
19
|
+
return (
|
|
20
|
+
<TouchableOpacity onPress={() => setCount(c => c + 1)}>
|
|
21
|
+
<Text>{label}: {count}</Text>
|
|
22
|
+
</TouchableOpacity>
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// --- Data fetching with useEffect ---
|
|
27
|
+
interface User { id: string; name: string; }
|
|
28
|
+
|
|
29
|
+
const UserList: React.FC = () => {
|
|
30
|
+
const [users, setUsers] = useState<User[]>([]);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
let cancelled = false;
|
|
35
|
+
fetch('https://api.example.com/users')
|
|
36
|
+
.then(r => r.json())
|
|
37
|
+
.then(data => {
|
|
38
|
+
if (!cancelled) {
|
|
39
|
+
setUsers(data);
|
|
40
|
+
setLoading(false);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return () => { cancelled = true; };
|
|
44
|
+
}, []);
|
|
45
|
+
|
|
46
|
+
if (loading) return <Text>Loading...</Text>;
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<FlatList
|
|
50
|
+
data={users}
|
|
51
|
+
keyExtractor={u => u.id}
|
|
52
|
+
renderItem={({ item }) => <Text>{item.name}</Text>}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// --- styled-components ---
|
|
58
|
+
const Card = styled.View`
|
|
59
|
+
background-color: #ffffff;
|
|
60
|
+
border-radius: 12px;
|
|
61
|
+
padding: 16px;
|
|
62
|
+
shadow-color: #000;
|
|
63
|
+
shadow-opacity: 0.15;
|
|
64
|
+
shadow-radius: 8px;
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
const CardTitle = styled.Text`
|
|
68
|
+
font-size: 18px;
|
|
69
|
+
font-weight: bold;
|
|
70
|
+
`;
|
|
71
|
+
|
|
72
|
+
const ProfileCard: React.FC<{ name: string; bio: string }> = ({ name, bio }) => (
|
|
73
|
+
<Card>
|
|
74
|
+
<CardTitle>{name}</CardTitle>
|
|
75
|
+
<Text>{bio}</Text>
|
|
76
|
+
</Card>
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// --- Context consumer ---
|
|
80
|
+
const ThemedBadge: React.FC<{ text: string }> = ({ text }) => {
|
|
81
|
+
const theme = useContext(ThemeContext);
|
|
82
|
+
return (
|
|
83
|
+
<View style={{ backgroundColor: theme.primary }}>
|
|
84
|
+
<Text>{text}</Text>
|
|
85
|
+
</View>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// --- Re-render bug: inline lambda + map() ---
|
|
90
|
+
const BadList: React.FC<{ items: { id: string; label: string }[] }> = ({ items }) => (
|
|
91
|
+
<View>
|
|
92
|
+
{items.map(item => (
|
|
93
|
+
<TouchableOpacity key={item.id} onPress={() => console.log(item.id)}>
|
|
94
|
+
<Text>{item.label}</Text>
|
|
95
|
+
</TouchableOpacity>
|
|
96
|
+
))}
|
|
97
|
+
</View>
|
|
98
|
+
);
|