Skip to content

Instantly share code, notes, and snippets.

@MobileRez
Last active January 20, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MobileRez/91b93c3fee922708cd0abdf6ed2fb049 to your computer and use it in GitHub Desktop.
Save MobileRez/91b93c3fee922708cd0abdf6ed2fb049 to your computer and use it in GitHub Desktop.
ChromeTabTestCode
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace LvlUpHealth.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
SimpleAuth.NativeSafariAuthenticator.Activate();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
if (SimpleAuth.Native.OpenUrl(app, url, options))
return true;
return base.OpenUrl(app, url, options);
}
}
}
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using LvlUpHealth.Helpers;
using LvlUpHealth.Modules.Fitbit;
using SimpleAuth;
using Xamarin.Forms;
namespace LvlUpHealth.ViewModel
{
public class AppsDevicesViewModel
{
private ICommand showFitbitLoginCommand;
public AppsDevicesViewModel()
{
}
#region Tasks
async Task ExecuteShowFitbitLoginCommandAsync()
{
var fbl = new FitbitLoginAuthentication();
await fbl.AuthorizeFitbitLogin();
}
#endregion
#region FitbitAuthentication
#endregion
public ICommand ShowFitbitLoginCommand =>
showFitbitLoginCommand ?? (showFitbitLoginCommand = new Command(async () => await ExecuteShowFitbitLoginCommandAsync()));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LvlUpHealth.Helpers
{
public static class ApiKeys
{
public const string AppCenterAndroid = "MobileCenterAndroid";
public const string AppCenteriOS = "MobileCenteriOS";
public const string AppCenterUWP = "MobileCenterUWP";
public static string FitbitAuthorizationEndpoint = "https://www.fitbit.com/oauth2/authorize";
public static string FitBitTokenEndpoint = "https://api.fitbit.com/oauth2/token";
public static string FitbitApiEndpoint = "https://api.fitbit.com";
public static string FitbitRedirectionEndpoint = "com.lvluphealth://fitbitcallback";
public static string FitbitClientId = "FitbitClientId";
public static string FitbitClientSecret = "FitbitClientSecret";
public const string FitBitServiceId = "FitBit";
public static readonly string[] FitbitScope =
{
"activity",
"nutrition",
"location",
"heartrate",
"profile",
"settings",
"sleep",
"social",
"weight"
};
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LvlUpHealth.Helpers;
using SimpleAuth;
namespace LvlUpHealth.Modules.Fitbit
{
public class FitbitLoginAuthentication
{
public FitbitLoginAuthentication()
{
}
public async Task AuthorizeFitbitLogin()
{
try
{
//AuthenticatedApi api;
var oAuthAuthenticatior = new OAuthAuthenticator(ApiKeys.FitbitAuthorizationEndpoint,
ApiKeys.FitBitTokenEndpoint,
ApiKeys.FitbitRedirectionEndpoint, ApiKeys.FitbitClientId, ApiKeys.FitbitClientSecret);
OAuthApi oAuth = new OAuthApi(ApiKeys.FitBitServiceId, oAuthAuthenticatior)
{
Scopes = ApiKeys.FitbitScope
};
var account = await oAuth.Authenticate();
Settings.Current.FitBitXamAuthAccount = account;
Settings.Current.IsFitbit = true;
Console.WriteLine(account.Identifier);
App.Current.MainPage.DisplayAlert("Success!", "User is logged in", "Ok");
}
catch (TaskCanceledException)
{
Console.WriteLine("Canceled");
App.Current.MainPage.DisplayAlert("Error", "User Canceled", "Ok");
}
catch (Exception ex)
{
Console.WriteLine(ex);
App.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>11.2</string>
<key>CFBundleDisplayName</key>
<string>LvL Up Health</string>
<key>CFBundleIdentifier</key>
<string>com.lvluphealth</string>
<key>CFBundleVersion</key>
<string>0.1.3</string>
<key>CFBundleIconFiles</key>
<array>
<string>Icon-60@2x</string>
<string>Icon-60@3x</string>
<string>Icon-76</string>
<string>Icon-76@2x</string>
<string>Default</string>
<string>Default@2x</string>
<string>Default-568h@2x</string>
<string>Default-Portrait</string>
<string>Default-Portrait@2x</string>
<string>Icon-Small-40</string>
<string>Icon-Small-40@2x</string>
<string>Icon-Small-40@3x</string>
<string>Icon-Small</string>
<string>Icon-Small@2x</string>
<string>Icon-Small@3x</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>CFBundleName</key>
<string>LvlUpHealth</string>
<key>CFBundleShortVersionString</key>
<string>0.3</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>OAuthCallback</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.lvluphealth</string>
</array>
<key>CFBundleURLTypes</key>
<string>None</string>
</dict>
</array>
</dict>
</plist>
using System.Reflection;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using SimpleAuth;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
namespace LvlUpHealth.Droid
{
[Activity(Label = "Lvl Up Health",
Icon = "@drawable/icon",
LaunchMode = LaunchMode.SingleTask,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(savedInstanceState);
SimpleAuth.NativeCustomTabsAuthenticator.Activate(this.Application);
Forms.Init(this, savedInstanceState);
typeof(Color).GetProperty("Accent", BindingFlags.Public | BindingFlags.Static).SetValue(null, Color.FromHex("#757575"));
//Native.OnActivityResult()
LoadApplication(new App());
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
SimpleAuth.Native.OnActivityResult(requestCode, resultCode, data);
}
private void UnregisterManagers()
{
//UpdateManager.Unregister();
}
protected override void OnPause()
{
base.OnPause();
UnregisterManagers();
}
protected override void OnDestroy()
{
base.OnDestroy();
UnregisterManagers();
}
}
}
// Helpers/Settings.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Plugin.Settings;
using Plugin.Settings.Abstractions;
using SimpleAuth;
namespace LvlUpHealth.Helpers
{
/// <summary>
/// This is the Settings static class that can be used in your Core solution or in any
/// of your client applications. All settings are laid out the same exact way with getters
/// and setters.
/// </summary>
public class Settings : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private static Settings settings;
public static ISettings AppSettings
{
get { return CrossSettings.Current; }
}
public static Settings Current
{
get { return settings ?? (settings = new Settings()); }
}
private const string AppThemeKey = "current_theme";
private const string UwpPlatformKey = "UWP_Platform";
private const string FitbitAccountKey = "modules_fitbitaccount";
private const string IsFitbitKey = "modules_isfitbit";
private static int appThemeDefault;
private static int uwpPlatformDefault;
private static Account fitBitXamAuthAccountDefault;
private static bool isFitbitDefualt = false;
public int AppTheme
{
get { return AppSettings.GetValueOrDefault(AppThemeKey, appThemeDefault); }
set
{
if (AppSettings.AddOrUpdateValue(AppThemeKey, value))
{
this.OnPropertyChanged(nameof(appThemeDefault));
}
}
}
public int UwpPlatform
{
get { return AppSettings.GetValueOrDefault(UwpPlatformKey, uwpPlatformDefault); }
set
{
if (AppSettings.AddOrUpdateValue(UwpPlatformKey, value))
{
this.OnPropertyChanged();
}
}
}
public Account FitBitXamAuthAccount
{
//get { return AppSettings.GetValueOrDefault(FitbitAccountKey, fitBitXamAuthAccountDefault); }
get;
set;
}
//public AccountStore XamAuthStore
//{
// get;
// set;
//}
public bool IsFitbit
{
get { return AppSettings.GetValueOrDefault(IsFitbitKey, isFitbitDefualt); }
set
{
if (AppSettings.AddOrUpdateValue(IsFitbitKey, value))
{
this.OnPropertyChanged();
}
}
}
private void OnPropertyChanged([CallerMemberName]string name = "") => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment