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

playing with templates #142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"crux_platform",
"crux_time"
]
resolver = "1"

[workspace.package]
authors = ["Red Badger Consulting Limited"]
Expand Down
1 change: 1 addition & 0 deletions crux_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/main.rs"
anyhow.workspace = true
clap = { version = "4.3.21", features = ["derive"] }
console = "0.15.7"
heck = "0.4.1"
ignore = "0.4.20"
ramhorns = "0.14.0"
serde = { workspace = true, features = ["derive"] }
Expand Down
26 changes: 16 additions & 10 deletions crux_cli/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use heck::{ToKebabCase, ToPascalCase, ToSnakeCase};
use ramhorns::Content;

use crate::config::{Core, Shell, Workspace};
Expand Down Expand Up @@ -27,29 +28,34 @@ impl CoreContext {
#[derive(Content)]
pub struct ShellContext {
pub workspace: String,
pub workspace_pascal: String,
pub core_dir: String,
pub core_name: String,
pub type_gen: String,
pub type_gen_pascal: String,
pub shell_dir: String,
pub shell_name: String,
pub shell_name_dashes: String,
}

impl ShellContext {
pub fn new(workspace: &Workspace, core: &Core, shell: &Shell) -> Context {
let type_gen = core
.type_gen
.as_ref()
.map(|x| x.to_string_lossy().to_string())
.or(Some("".into()))
.unwrap();
Context::Shell(Self {
workspace: workspace.name.to_ascii_lowercase().replace(" ", "_"),
workspace: workspace.name.to_snake_case(),
workspace_pascal: workspace.name.to_pascal_case(),
core_dir: core.source.to_string_lossy().to_string(),
core_name: core.name.replace("-", "_"),
type_gen: core
.type_gen
.as_ref()
.map(|x| x.to_string_lossy().to_string())
.or(Some("".into()))
.unwrap(),
core_name: core.name.to_snake_case(),
type_gen: type_gen.clone(),
type_gen_pascal: type_gen.to_pascal_case(),
shell_dir: shell.source.to_string_lossy().to_string(),
shell_name: shell.name.replace("-", "_"),
shell_name_dashes: shell.name.replace("_", "-"),
shell_name: shell.name.to_snake_case(),
shell_name_dashes: shell.name.to_kebab_case(),
})
}
}
10 changes: 5 additions & 5 deletions examples/simple_counter/iOS/SimpleCounter/core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import SharedTypes
@MainActor
class Core: ObservableObject {
@Published var view: ViewModel

init() {
self.view = try! .bincodeDeserialize(input: [UInt8](SimpleCounter.view()))
view = try! .bincodeDeserialize(input: [UInt8](SimpleCounter.view()))
}

func update(_ event: Event) {
let effects = [UInt8](processEvent(Data(try! event.bincodeSerialize())))

let requests: [Request] = try! .bincodeDeserialize(input: effects)
for request in requests {
processEffect(request)
}
}

func processEffect(_ request: Request) {
switch request.effect {
case .render:
Expand Down
12 changes: 6 additions & 6 deletions templates/simple_counter/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ plugins {
}

android {
namespace 'com.example.counter'
namespace 'com.example.{{workspace}}'
compileSdk 34

defaultConfig {
applicationId "com.example.counter"
applicationId "com.example.{{workspace}}"
minSdk 33
targetSdk 34
versionCode 1
Expand Down Expand Up @@ -40,7 +40,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.7'
kotlinCompilerExtensionVersion '1.5.3'
}
packagingOptions {
resources {
Expand All @@ -57,7 +57,7 @@ dependencies {
implementation composeBom
androidTestImplementation composeBom

implementation 'androidx.compose.material3:material3:1.2.0-alpha05'
implementation 'androidx.compose.material3:material3:1.2.0-alpha06'

// Optional - Integration with activities
implementation("androidx.activity:activity-compose:1.7.2")
Expand All @@ -66,8 +66,8 @@ dependencies {
// Optional - Integration with LiveData
implementation("androidx.compose.runtime:runtime-livedata")

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'

implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.{{workspace}}

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import com.example.{{workspace}}.{{core_name}}.processEvent
import com.example.{{workspace}}.{{core_name}}.view
import com.example.{{workspace}}.{{type_gen}}.Effect
import com.example.{{workspace}}.{{type_gen}}.Event
import com.example.{{workspace}}.{{type_gen}}.Request
import com.example.{{workspace}}.{{type_gen}}.Requests
import com.example.{{workspace}}.{{type_gen}}.ViewModel

class Core : androidx.lifecycle.ViewModel() {
var view: ViewModel by mutableStateOf(ViewModel.bincodeDeserialize(view()))
private set

fun update(event: Event) {
val effects = processEvent(event.bincodeSerialize())

val requests = Requests.bincodeDeserialize(effects)
for (request in requests) {
processEffect(request)
}
}

private fun processEffect(request: Request) {
when (request.effect) {
is Effect.Render -> {
this.view = ViewModel.bincodeDeserialize(view())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@file:OptIn(ExperimentalUnsignedTypes::class)

package com.example.{{workspace}}

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.{{workspace}}.{{type_gen}}.Event
import com.example.{{workspace}}.ui.theme.CounterTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CounterTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
) {
View()
}
}
}
}
}

@Composable
fun View(core: Core = viewModel()) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize()
.padding(10.dp),
) {
Text(text = core.view.count.toString(), modifier = Modifier.padding(10.dp))
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
Button(
onClick = { core.update(Event.Reset()) }, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.error
)
) { Text(text = "Reset", color = Color.White) }
Button(
onClick = { core.update(Event.Increment()) }, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary
)
) { Text(text = "Increment", color = Color.White) }
Button(
onClick = { core.update(Event.Decrement()) }, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondary
)
) { Text(text = "Decrement", color = Color.White) }
}
}
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
CounterTheme {
View()
}
}
Loading