Some time ago I blogged about my first experiments and „playing around“ with Xamarin.
In my opinion the best way to learn a new technology is by actually using it, so I decided to port my app „Daily Activities“ to Android by using Xamarin. Ok, beside from simple „using the technology“ some basic knowledge of the tech you are using might be a good thing too but…let’s assume we have this, if not there is google, and stack overflow. 😉
My app „Daily Activities“ currently runs on windows phone 8.1 and also on windows 10 and windows phone 10 developer preview, since it’s a universal windows platform app that runs on all windows 10 devices.
The app uses the phone’s motion sensors and SensorCore SDK to collect steps data (walking/running), activities through a day and your visited locations to show them on a map.
In theory it should run on HoloLens…hey MS, can I get one of these? 😉
Porting the app from WP 8.1 to a universal windows platform app was not that hard, since the WP 8.1 version already is based on WinRT and porting an existing WinRT app is much simpler then porting a SL 8.1 app since the two platforms (WinRT and UWP) are more similar. This is also the moment when you really learn to value some MVVM architecture.
So let’s see what the current app does, which might be interesting for porting
- “Business Logic” for calculating steps to distance (based on user settings) and calories
This should be very simple to port, in fact nothing should be to do here.
- It uses the SensorCore SDK to access motion sensors on Lumia devices, which is different for WP and UWP
Yes, there is a new API for UWP that provides access to sensors. See https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.devices.sensors.motion(v=vs.105).aspx
and the samples from MS (https://msdn.microsoft.com/en-us/library/dn932095.aspx), which use a factory to create an „Activity Sensor implementation“ based on SensorCoreSDK or an implementation based on „Microsoft.Devices.Sensors“ and abstract the actual implementation away by implementing an interface.
Wow, MS uses some pattern in their own samples, quite unusual 😉
Unfortunately the implementation based on „Microsoft.Devices.Sensors“ is not working on my Lumia Test phone but the SensorCore based implementation does, so currently only this one is uses in Daily Activities.
Anyway this will be interesting, since accessing motion sensors on android devices might be a bit different I guess. 😉
Good thing is that the current implementation already uses IOC and the SensorCore implementation is injected – so in theory the „android implementation“ needs to be in injected and the rest of the app should not need to know anything about where the actual sensor data came from….we will see if this works.
Of course I also need to figure out how to create this actual android implementation.
- Save and load data from OneDrive
This is used to save/restore collected sensor data and also for data synchronization.
The windows phone app can upload collected steps data in a ‚maintenance task‘ (when the phone is connected to AC and has an internet connection).
The UWP app running on the PC (which usually has no motion sensor and is not carried around when walking/running) can download this „collected data“ from OneDrive, which is done in a maintenance task too. So you can see the data you „collected“ on your phone in the UWP app on your PC.
Yes, this already works. 🙂
Since the OneDrive API is based on http requests, this should be simple to port (in theory). But is using OneDrive as a data storage a good idea? Maybe it’s better to use Google Drive for android phones?
On the other hand, using OneDrive definitely is a good idea for the UWP app on the PC but if the phone stores collected data on Google Drive, this data needs to be move to OneDrive in some way…or the UWP app running on the PC needs to access this data directly in google drive.
I will try the simplest approach here: Force Android users to use OneDrive, if they want to store and sync their steps and activity data to their PC. 🙂
- The app stores local Settings
What’s the problem here?
There is not „Isolated Storage“ on Xamarin/Android.
Good thing is, I already found an implementation that should solve this problem by creating an abstraction for Isolated Storage: https://github.com/jamesmundy/Xamarin.Android.IsolatedStorageSettings/tree/master/IsolatedStorageSettings/IsolatedStorageSettings
- The app stores roaming settings that are „roamed“ on all windows devices automatically
Nice feature of UWP…but only on UWP as far as I know. Storing these settings in OneDrive too should be a working solution. When changed on any device (windows phone, android phone, PC) it is stored to and loaded from OneDrive.
- Uses MVVMLight portable library to implement MVVMLight
Should not be a problem since MVVMLight supports Xamarin.
- Background Tasks
As said: The current implementation use 2 different background tasks.
- A ‚maintenance task‘ which is allowed to run for a longer time, use more resources but only runs when the device is connected to AC and has an internet connection.
This task is used to store sensor data locally on the phone and additionally upload this data to OneDrive, if the user selected this option.
- A „normal“ background task that updates the live tile and shows steps, burned calories, distance…and more on the live tile.
Btw. I am using a ‚XamlRenderingBackgroundTask‘ in the second background task implementation for WP and UWP since adaptive live tiles are some kind of crap.
They can show text in different size on the live tile (wow) but drawing a simple progress bar (to visualize 1234/10000 steps) is not possible…it would have been sooooo much nicer if MS had used REAL XAML for defining live tile content instead the xml “adaptive live tile language thing“.
I don’t know how to port this at the Moment. I need to find out how to do a „maintenance task“ in android. The live tile most likely should be replaced by an android widget…I will have to find out how to do this in Xamarin for Android too.
- Local Notifications
“Take 500 more steps to reach your goal!”
Currently not used in WP and UWP but I really want to add this in a future update.
Don’t know how to do this in Xamarin for Android and no, I don’t want to use Azure for local (!) notifications.
- Localization
Currently the app uses German and English localization in a „.resw“-file.
Maybe I can use the multilingual app toolkit (which is a good idea anyway) and create XLIFF files: https://dev.windows.com/en-us/develop/multilingual-app-toolkit
Does Android work with these, I don’t know but should not be hard to find out.
- Controls to visualize data
The WP and UWP implementation use a flipview control to show data per day and per week and navigate to next/previous days and weeks. Additionally, the app uses a chart control from Syncfusion which *tataaaa* is available for Xamarin.Forms 🙂
Don’t know how to port the flipview at the moment, maybe I can replace it with a control from Syncfusion, which is available for Xamarin.Forms too?
All other UI elements like text boxes, labels, combo boxes are available for Xamarin.Forms, which I really want to use as much as possible since this should make a future implementation for Xamarin.iOS much simpler…at some time in the future maybe.
- Show visited locations on a map
Hopefully there is some maps control available in Syncfusion or as a Xamarin component that can be used for this task. I did not check this but I highly expect it. 🙂
- App Navigation, Dialog Services, …
Is provided by MVVMLight in the current Apps as services that are registered in a ViewModelLocator. These services are also implemented for Xamarin.Forms so this tas should not be a problem (in theory)
Most likely there are things I forgot…but it has to start somewhere. 🙂
Will be interesting and maybe some developers may find this useful.
The app already contains some PCL projects, which are currently configured for WP 8.1 WinRT and UWP. Simply adding Xamarin.Android to the PCL project did not work since they also contain some common used implementation that is based on Isolate Storage, which is no problem as long as you only support WP and UWP.
Will move this to a new PCL and have to migrate it to the Isolated Storage abstraction from: https://github.com/jamesmundy/Xamarin.Android.IsolatedStorageSettings/tree/master/IsolatedStorageSettings/IsolatedStorageSettings
Next thing to do is create a PCL for all REALLY platform independent code 😉
I will blog about my future progress.