How can I detect the platform at runtime using MvvMCross?
Asked Answered
H

3

5

I want the user to be able to send feedback about my app to an address. Using the email plugin, this is all good, but in the body of the email, I want to pre-populate some information about the app they're running.

Ideally, I'd like the device, the OS, the screen res, the orientation etc, but for now I'd just settle for the OS

Hydrology answered 2/3, 2014 at 0:26 Comment(0)
B
6

It feels quite strange, but I can't remember anyone ever asking for this as a feature and I don't think anyone's made a plugin for it either.

Given your future requirements (screen res, orientation, etc) the easiest way to this is probably to define an interface in your core project:

 public enum OS
 {
    Droid, Touch, WinPhone, WinStore, Mac, Wpf
 }

 public IDetails
 {
     OS OS { get; }
     // whatever else you need
 }

You can then register implementations for this in each UI project - e.g. in Setup for WinPhone, add:

 protected override void InitializeLastChance()
 {
     base. Setup.InitializeLastChance();
     Mvx.RegisterSingleton<IDetails>(new WindowsPhoneDetails());
 }

Where:

 public class WindowsPhoneDetails : IDetails
 {
     public OS OS { get { return OS.WinPhone; } }
 }

For more on this approach, see https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-registering-platform-specific-business-objects-in-setupinitializefirstchance-and-setupinitializelastchance

(Specifically for screen size, also see IDisplayDimensionsService in https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/FractalGen)

Belcher answered 2/3, 2014 at 9:41 Comment(1)
I've done an implementation of this - where's the best place to share it. As a MvvMCross noob, I'm not sure of the etiquette.Hydrology
H
1

Turns out there's already a project up that does this - https://github.com/aritchie/acrmvvmcross Kudos Allan Ritchie!

Unfortunately, there's no Windows Phone implementation et, so I implemented sme of the functionality as follows:

public class WindowsPhoneDeviceInfoService : IDeviceInfoService
{
    public bool IsFrontCameraAvailable
    {
        get { throw new NotImplementedException(); }
    }

    public bool IsRearCameraAvailable
    {
        get { throw new NotImplementedException(); }
    }

    public bool IsSimulator
    {
        get { throw new NotImplementedException(); }
    }

    public string Manufacturer
    {
        get { return DeviceStatus.DeviceManufacturer; }
    }

    public string Model
    {
        get { return DeviceStatus.DeviceName; }
    }

    public string OperatingSystem
    {
        get { return Environment.OSVersion.Version.ToString(); }
    }

    public int ScreenHeight
    {
        get 
        {
            object szo;
            if (DeviceExtendedProperties.TryGetValue("PhysicalScreenResolution", out szo))
            {
                Size sz = (Size)szo;
                if (sz == null)
                    return 0;
                return (int)sz.Height;
            }
            else
            {
                return 0;
            }
        }
    }

    public int ScreenWidth
    {
        get
        {
            object szo;
            if (DeviceExtendedProperties.TryGetValue("PhysicalScreenResolution", out szo))
            {
                Size sz = (Size)szo;
                if (sz == null)
                    return 0;
                return (int)sz.Width;
            }
            else
            {
                return 0;
            }
        }
    }
}

Of course, I also had to register the class in setup.cs:

Mvx.RegisterSingleton<IDeviceInfoService>(new WindowsPhoneDeviceInfoService());
Hydrology answered 14/3, 2014 at 0:42 Comment(0)
L
0

In your ViewModel:

private string _os;
public string Os
{
    get { return _os; }
    set { SetProperty(ref _os, value); }
}

On View create (ViewDidLoad/OnCreate)

ViewModel.Os = "iOS"; or ViewModel.Os = "Android";
Loudermilk answered 8/2, 2018 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.