2019-12-05 08:26:58 +03:00
# Comet ☄️
2019-12-05 22:44:53 +03:00
[![Build Status ](https://clancey.visualstudio.com/Comet/_apis/build/status/Clancey.Comet?branchName=dev )](https://clancey.visualstudio.com/Comet/_build/latest?definitionId=1& branchName=dev) [![NuGet ](https://img.shields.io/nuget/v/Clancey.Comet.svg )](https://www.nuget.org/packages/Clancey.Comet)
2019-12-05 08:26:58 +03:00
2019-10-22 00:16:51 +03:00
[Chat on Discord ](https://discord.gg/7Ms7ptM )
2019-07-16 10:45:08 +03:00
2019-12-05 08:26:58 +03:00
2019-08-15 06:29:51 +03:00
What is Comet? Comet is a prototype for a new UI Framework/Pattern to write app UI. It follows the Model View Update (MVU) pattern. It magically databinds for you!
2019-07-18 22:12:52 +03:00
2019-07-29 01:54:37 +03:00
Video Preview:
2019-07-29 01:55:12 +03:00
2019-07-29 01:54:37 +03:00
[![Video Demo ](http://img.youtube.com/vi/-Ieg9UadN8s/0.jpg )](http://www.youtube.com/watch?v=-Ieg9UadN8s)
2019-07-16 10:45:08 +03:00
2019-09-04 17:27:13 +03:00
## Getting Started
When you're ready to take a ride on the comet, head over to the wiki and follow the [Getting Started ](https://github.com/Clancey/Comet/wiki/Getting-Started ) guide.
2019-07-22 00:14:45 +03:00
## Key Concepts
2019-07-16 10:45:08 +03:00
2019-08-15 06:29:51 +03:00
Comet is an MVU style pattern.
2019-07-22 00:14:45 +03:00
`View` is a screen. Views have a `Body` method that you can assign either by an attribute `[Body]` :
2019-07-16 10:45:08 +03:00
``` cs
2019-07-24 02:51:42 +03:00
public class MyPage : View {
2019-07-16 10:45:08 +03:00
[Body]
2019-07-23 11:27:38 +03:00
View body () => new Text("Hello World");
2019-07-16 10:45:08 +03:00
}
```
2019-07-22 00:14:45 +03:00
or:
2019-07-16 10:45:08 +03:00
``` cs
2019-07-24 02:51:42 +03:00
public class MyPage : View {
public MyPage() {
2019-07-16 10:45:08 +03:00
Body = body;
}
2019-07-23 11:27:38 +03:00
View body () => new Text("Hello World");
2019-07-16 10:45:08 +03:00
}
```
2019-07-22 00:14:45 +03:00
## Hot Reload
2019-08-15 06:29:51 +03:00
Hot Reload is included by default! The setup is very easy: a Visual Studio extension and a NuGet. Download both from [Releases ](https://github.com/Clancey/Comet/releases ) here on GitHub.
2019-07-16 10:45:08 +03:00
2019-08-15 06:29:51 +03:00
Download and install the VS extension from the [Releases ](https://github.com/Clancey/Comet/releases/ )
2019-07-22 00:14:45 +03:00
Then add to your `AppDelegate.cs` and/or `MainActivity.cs` , or similar. See the sample projects here for examples.
2019-07-16 10:45:08 +03:00
``` cs
#if DEBUG
2019-08-15 06:29:51 +03:00
Comet.Reload.Init();
2019-07-16 10:45:08 +03:00
#endif
```
2019-07-22 00:14:45 +03:00
## State
2019-07-16 10:45:08 +03:00
As of right now there are two supported ways to add state.
2019-07-22 00:14:45 +03:00
### 1. Simple data types like int, bool?
2019-07-16 10:45:08 +03:00
Just add a `State<T>` field to your View
``` cs
2019-07-22 00:14:45 +03:00
class MyPage : View {
2019-07-16 10:45:08 +03:00
readonly State< int > clickCount = 1;
}
```
2019-07-22 00:14:45 +03:00
`View` is state aware. When the state changes, databinding will automatically update, or rebuild the view as needed.
2019-07-16 10:45:08 +03:00
2019-07-22 00:14:45 +03:00
### 2. Do you want to use more complex data types?
2019-07-16 10:45:08 +03:00
2019-08-15 06:29:51 +03:00
You can either implement [INotifyPropertyRead ](https://github.com/Clancey/Comet/blob/master/src/Comet/BindingObject.cs#L13 ) or you can use [BindingObject ](https://github.com/Clancey/Comet/blob/master/src/Comet/BindingObject.cs ) to make it even simpler .
2019-07-16 10:45:08 +03:00
2019-07-22 00:14:45 +03:00
Add it as a Field/Property, and add the `[State]` attribute!
2019-07-16 10:45:08 +03:00
``` cs
public class MainPage : View {
class MyBindingObject : BindingObject {
public bool CanEdit {
get => GetProperty< bool > ();
set => SetProperty (value);
}
public string Text {
get => GetProperty< string > ();
set => SetProperty (value);
}
}
[State]
readonly MyBindingObject state;
}
```
2019-07-22 00:14:45 +03:00
`INotifyPropertyRead` is just like `INotifyPropertyChanged` . Just call `PropertyRead` whenever a property Getter is called. And `PropertyChanged` whenever a property Value changes.
2019-07-16 10:45:08 +03:00
2019-07-22 00:14:45 +03:00
### How do I use the State?
Simply update the stateful value and the framework handles the rest.
2019-07-16 10:45:08 +03:00
``` cs
public class MyPage : View {
2019-07-24 02:51:42 +03:00
readonly State< int > clickCount = 1;
readonly State< string > text = "Hello World";
2019-07-16 10:45:08 +03:00
2019-07-24 02:51:42 +03:00
public MyPage() {
Body = () => new VStack {
new Text (text),
new Button("Update Text", () => state.Text = $"Click Count: {clickCount.Value++}")
};
2019-07-16 10:45:08 +03:00
}
2019-07-24 02:51:42 +03:00
}
2019-07-16 10:45:08 +03:00
```
That is all!, now when the Text Changes everything updates.
2019-07-22 00:14:45 +03:00
### What if I want to format my value without an extra state property?
2019-07-16 10:45:08 +03:00
2019-07-22 00:14:45 +03:00
While `new Button("Update Text", () => state.Text = $"Click Count: {clickCount.Value++}" )` works, it isn't efficient.
2019-07-16 10:45:08 +03:00
2019-07-22 00:14:45 +03:00
Instead, use `new Text(()=> $"Click Count: {clickCount}")` .
2019-07-16 10:45:08 +03:00
``` cs
public class MyPage : View {
2019-07-24 02:51:42 +03:00
readonly State< int > clickCount = new State< int > (1);
2019-07-16 10:45:08 +03:00
2019-07-24 02:51:42 +03:00
public MyPage() {
Body = () => new VStack {
new Text (() => $"Click Count: {clickCount}"),
new Button("Update Text", () => {
clickCount.Value++;
}
};
2019-07-16 10:45:08 +03:00
}
2019-07-24 02:51:42 +03:00
}
2019-07-16 10:45:08 +03:00
```
2019-09-04 17:34:59 +03:00
## What platforms are being targeted?
2019-07-16 10:45:08 +03:00
* iOS
* Android
* UWP
2019-07-24 01:52:10 +03:00
* WPF
2019-07-16 10:45:08 +03:00
* Mac OS
2019-09-04 17:34:59 +03:00
* Xamarin.Forms - all Forms targets: Linux, macOS, Tizen, WPF, and of course Android, iOS, UWP.
2019-07-24 01:52:10 +03:00
* Blazor
2019-07-18 22:30:29 +03:00
# Disclaimer
2019-07-22 00:14:45 +03:00
2019-08-15 06:29:51 +03:00
Comet is a **proof of concept** . There is **no** official support. Use at your own Risk.