app: add code example to data_store, http_request and ui
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 7 Sep 2017 19:11:35 +0000 (15:11 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 8 Sep 2017 21:18:55 +0000 (17:18 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/app/data_store.nit
lib/app/http_request.nit
lib/app/ui.nit

index 9cbe124..cb5ce2a 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Simple data storage services
+# Key/value storage services
 #
-# The implementation varies per platform.
+# The main services is `App::data_store`, a `DataStore` holding any
+# serializable Nit object.
 module data_store
 
 import app_base
@@ -28,11 +29,60 @@ import android::data_store is conditional(android)
 import ios::data_store is conditional(ios)
 
 redef class App
+
        # Services to store and load data
+       #
+       # ~~~
+       # import app::ui
+       # import app::data_store
+       #
+       # class MyWindow
+       #     super Window
+       #
+       #     var state = "Simple string or any serializable class"
+       #
+       #     redef fun on_save_state do app.data_store["state"] = state
+       #
+       #     redef fun on_restore_state
+       #     do
+       #         var state = app.data_store["state"]
+       #         if state isa String then self.state = state
+       #     end
+       # end
+       # ~~~
        var data_store = new DataStore is lazy
 end
 
 # Simple data storage facility
+#
+# Write values with `[]=` and read with `[]`.
+# ~~~
+# import linux::data_store # Needed for testing only
+#
+# class A
+#     serialize
+#
+#     var b = true
+#     var f = 1.234
+# end
+#
+# var data_store = new DataStore
+# data_store["one"] = 1
+# data_store["str"] = "Some string"
+# data_store["a"] = new A
+#
+# assert data_store["one"] == 1
+# assert data_store["str"] == "Some string"
+# assert data_store["a"].as(A).b
+# assert data_store["a"].as(A).f == 1.234
+# assert data_store["other"] == null
+# ~~~
+#
+# Set to `null` to clear a value.
+# ~~~
+# data_store["one"] = null
+# assert data_store["one"] == null
+# ~~~
 class DataStore
 
        # Get the object stored at `key`, or null if nothing is available
index 8ab0f1d..07d4bf3 100644 (file)
 # limitations under the License.
 
 # HTTP request services: `AsyncHttpRequest` and `Text::http_get`
+#
+# ~~~nitish
+# import app::http_request
+#
+# class MyHttpRequest
+#     super AsyncHttpRequest
+#
+#     redef fun uri do return "http://example.com/"
+#
+#     redef fun on_load(data, status) do print "Received: {data or else "null"}"
+#
+#     redef fun on_fail(error) do print "Connection error: {error}"
+# end
+#
+# var req = new MyHttpRequest
+# req.start
+# ~~~
 module http_request
 
 import app_base
@@ -122,9 +139,14 @@ abstract class AsyncHttpRequest
        fun after do end
 end
 
-# Minimal implementation of `AsyncHttpRequest` where `uri` is an attribute
+# Simple `AsyncHttpRequest` where `uri` is an attribute
+#
+# Prints on communication errors and when the remote server returns an
+# HTTP status code not in the 200s.
 #
-# Prints on communication errors and when the server returns an HTTP status code not in the 200s.
+# This class can be instantiated to execute a request where the response is
+# ignored by the application. Alternatively, it can be subclassed to implement
+# `on_load`.
 #
 # ~~~nitish
 # var request = new SimpleAsyncHttpRequest("http://example.com")
index b1f07a5..42e36ce 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Portable UI controls from mobiles apps
+# Portable UI controls for mobiles apps
+#
+# ~~~
+# import app::ui
+#
+# class MyWindow
+#     super Window
+#
+#     var layout = new ListLayout(parent=self)
+#     var lbl = new Label(parent=layout, text="Hello world", align=0.5)
+#     var but = new Button(parent=layout, text="Press here")
+#
+#     redef fun on_event(event) do lbl.text = "Pressed!"
+# end
+#
+# redef fun root_window do return new MyWindow
+# ~~~
 module ui
 
 import app_base