Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge: iOS: implement data_store and missing life cycle callbacks
Implement the main missing features of _app.nit_ on iOS: `data_store` and life-cycle hooks (like `on_save_state`). These changes can be seen on the calculator app, as it preserves its context using the `data_store`. `data_store` is implemented with `NSUserDefaults` to store objects sertialized to Json. It is very similar to Android's implementation using shared preferences. This may be a bit limited as it is not meant to hold large strings, and some data objects (like game saves) should instead be saved to a file. This PR also implements all life-cycle callbacks in iOS, until now only `on_create` was implemented. We may have to update _app.nit_ life-cycle to fit better with the life-cycle of iOS, the states between a running app and a fully stopped app are different between iOS and Android. I'm thinking of removing the two callbacks on_start/on_stop and keep only the more general callbacks on_create/on_destroy and on_resume/on_pause, and the services on_restore_state/on_save_state. Pull-Request: #1947 Reviewed-by: Jean Privat <jean@pryen.org>
- Loading branch information
Showing
8 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This file is part of NIT ( http://www.nitlanguage.org ). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Implements `app::data_store` using `NSUserDefaults` | ||
module data_store | ||
|
||
import app::data_store | ||
import cocoa::foundation | ||
private import json::serialization | ||
|
||
redef class App | ||
redef var data_store = new UserDefaultView | ||
end | ||
|
||
private class UserDefaultView | ||
super DataStore | ||
|
||
# The `NSUserDefaults` used to implement `DataStore` | ||
var user_defaults = new NSUserDefaults.standard_user_defaults is lazy | ||
|
||
redef fun [](key) | ||
do | ||
var nsstr = user_defaults.string_for_key(key.to_nsstring) | ||
|
||
if nsstr.address_is_null then return null | ||
|
||
# TODO report errors | ||
var deserializer = new JsonDeserializer(nsstr.to_s) | ||
return deserializer.deserialize | ||
end | ||
|
||
redef fun []=(key, value) | ||
do | ||
var nsobject: NSString | ||
|
||
if value == null then | ||
nsobject = new NSString.nil | ||
else | ||
var serialized_string = new StringWriter | ||
var serializer = new JsonSerializer(serialized_string) | ||
serializer.serialize(value) | ||
|
||
# TODO report errors | ||
nsobject = serialized_string.to_s.to_nsstring | ||
end | ||
|
||
user_defaults.set_object(nsobject, key.to_nsstring) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters