Providing exclusive platform support for the task / intention

I have an application for WP7 and Android, and this application should support for any type of connection (WiFi, NFC, Bluetooth, etc.)

Then I created a layered model with MVVMCross https://github.com/slodge/MvvmCross

I have an interface, for example, Android Bluetooth must implement

interface IConnectionService { List<TargetDevice> FindDevices(); void Connect(TargetDevice targetDevice); void Disconnect(); byte[] Read(); void Write(byte[] command); } 

I want to be able to request a Bluetooth Access from a user, but I do not want to program my user interface specifically for Android Bluetooth, so the presentation model and the presentation do not need to know what intention is used, all this must be handled by a class that implements IConnectionService

The problem is that it should also work on a Windows Phone that does not use intentions, it uses tasks, so how do I create an interface that allows me to make either an Intent request, or a task request, without knowing what type the request is needed?

+2
source share
1 answer

This is similar to how MvvmCross allows users to make phone calls.

When using this template:

ViewModel code uses a platform-independent service through an interface - for example:

 public interface IMvxPhoneCallTask { void MakePhoneCall(string name, string number); } 

consumed

  protected void MakePhoneCall(string name, string number) { var task = this.GetService<IMvxPhoneCallTask>(); task.MakePhoneCall(name, number); } 

at https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/BaseViewModel.cs

The application configuration code implements a platform-specific implementation for the interface - for example:

  RegisterServiceType<IMvxPhoneCallTask, MvxPhoneCallTask>(); 

In WP7 - uses PhoneCallTask - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/WindowsPhone/Platform/Tasks/MvxPhoneCallTask.cs

 public class MvxPhoneCallTask : MvxWindowsPhoneTask, IMvxPhoneCallTask { #region IMvxPhoneCallTask Members public void MakePhoneCall(string name, string number) { var pct = new PhoneCallTask {DisplayName = name, PhoneNumber = number}; DoWithInvalidOperationProtection(pct.Show); } #endregion } 

In Droid - it uses ActionDial Intent - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Platform/Tasks/MvxPhoneCallTask.cs

 public class MvxPhoneCallTask : MvxAndroidTask, IMvxPhoneCallTask { #region IMvxPhoneCallTask Members public void MakePhoneCall(string name, string number) { var phoneNumber = PhoneNumberUtils.FormatNumber(number); var newIntent = new Intent(Intent.ActionDial, Uri.Parse("tel:" + phoneNumber)); StartActivity(newIntent); } #endregion } 

In Touch - it just uses Urls - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Touch/Platform/Tasks/MvxPhoneCallTask.cs

 public class MvxPhoneCallTask : MvxTouchTask, IMvxPhoneCallTask { #region IMvxPhoneCallTask Members public void MakePhoneCall(string name, string number) { var url = new NSUrl("tel:" + number); DoUrlOpen(url); } #endregion } 

In the vnext version of mvvmcross, this approach is further formalized using plugins - see a brief introduction to them in the 'going portable' presentation at http://slodge.blogspot.co.uk/2012/06/mvvm-mvvmcross-monodroid-monotouch- wp7.html

For example, in vNext, the above phone call code is contained in the PhoneCall plugin - https://github.com/slodge/MvvmCross/tree/vnext/Cirrious/Plugins/PhoneCall


One of the problems of your task may be the word "any" - differences in platform implementation can make it difficult to define a cross-platform interface that works on all platforms for any of NFC, Bluetooth, etc., let one of them.

+5
source

All Articles