Skip to content

Commit c392767

Browse files
(+) Added ContextHelper to Android
(+) Implemented MessageService for Android
1 parent 67c50d1 commit c392767

File tree

11 files changed

+87
-10
lines changed

11 files changed

+87
-10
lines changed

src/Catel.Core/Catel.Core.NET40/Services/LanguageService.android.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Catel.Services
1010
{
1111
using System.Globalization;
12-
using Android.Content.Res;
12+
using global::Android.Content.Res;
1313

1414
public partial class LanguageService
1515
{
@@ -41,7 +41,7 @@ protected override string GetString(ILanguageSource languageSource, string resou
4141
//var element = stringsXmlDocument.XPathSelectElement(xpath);
4242
//return element.Value;
4343

44-
var context = global::Android.App.Application.Context;
44+
var context = Android.ContextHelper.CurrentContext;
4545
var packageName = context.PackageName;
4646

4747
var id = context.Resources.GetIdentifier(resourceName, "string", packageName);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="ContextHelper.cs" company="Catel development team">
3+
// Copyright (c) 2008 - 2014 Catel development team. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------------------------------------------------
6+
7+
8+
namespace Catel.Android
9+
{
10+
using global::Android.Content;
11+
12+
/// <summary>
13+
/// The context helper.
14+
/// </summary>
15+
public static class ContextHelper
16+
{
17+
private static Context _context;
18+
19+
/// <summary>
20+
/// Gets or sets the current context.
21+
/// <para />
22+
/// Note that the setter is made public to allow customization. It will be used primarily by Catel though.
23+
/// </summary>
24+
/// <value>The current context.</value>
25+
public static Context CurrentContext
26+
{
27+
get
28+
{
29+
if (_context == null)
30+
{
31+
_context = global::Android.App.Application.Context;
32+
}
33+
34+
return _context;
35+
}
36+
set { _context = value; }
37+
}
38+
}
39+
}

src/Catel.Core/Catel.Core.Xamarin.Android/Catel.Core.Xamarin.Android.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,15 @@
962962
<Compile Include="..\Catel.Core.NET40\WeakReferences\WeakEventListener.cs">
963963
<Link>WeakReferences\WeakEventListener.cs</Link>
964964
</Compile>
965+
<Compile Include="Android\Helpers\ContextHelper.cs" />
965966
</ItemGroup>
966967
<ItemGroup>
967968
<Content Include="FodyWeavers.xml" />
968969
</ItemGroup>
969970
<ItemGroup>
970971
<None Include="packages.config" />
971972
</ItemGroup>
973+
<ItemGroup />
972974
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
973975
<Import Project="..\..\..\lib\Fody.1.22.1\build\Fody.targets" Condition="Exists('..\..\..\lib\Fody.1.22.1\build\Fody.targets')" />
974976
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

src/Catel.MVVM/Catel.MVVM.NET40/MVVM/Navigation/NavigationAdapter.phone.android.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Catel.MVVM.Navigation
99
{
1010
using System;
11+
using Android;
1112
using Logging;
1213
using global::Android.App;
1314
using global::Android.OS;
@@ -229,6 +230,8 @@ partial void Initialize()
229230
var eventArgs = new NavigatedEventArgs(GetNavigationUri(activity), NavigationMode.New);
230231
RaiseNavigatedTo(eventArgs);
231232
}
233+
234+
ContextHelper.CurrentContext = activity;
232235
}
233236

234237
partial void Uninitialize()

src/Catel.MVVM/Catel.MVVM.NET40/Services/AccelerometerService.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override IAccelerometerValue GetCurrentValue()
9797

9898
partial void Initialize()
9999
{
100-
var context = Application.Context;
100+
var context = Catel.Android.ContextHelper.CurrentContext;
101101
_sensorManager = context.GetSystemService(Context.SensorService) as SensorManager;
102102
_sensor = _sensorManager.GetDefaultSensor(SensorType.Accelerometer);
103103

src/Catel.MVVM/Catel.MVVM.NET40/Services/LocationService.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void OnLocationChanged(object sender, EventArgs e)
118118
/// </summary>
119119
protected override void Initialize()
120120
{
121-
var context = Application.Context;
121+
var context = Catel.Android.ContextHelper.CurrentContext;
122122
_locationManager = context.GetSystemService(Context.LocationService) as LocationManager;
123123

124124
_locationListener = new LocationListener();

src/Catel.MVVM/Catel.MVVM.NET40/Services/MessageService.android.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Catel.Services
1010
{
1111
using System;
12+
using global::Android.App;
1213

1314
public partial class MessageService
1415
{
@@ -23,7 +24,41 @@ public partial class MessageService
2324
/// <exception cref="ArgumentException">The <paramref name="message"/> is <c>null</c> or whitespace.</exception>
2425
protected virtual MessageResult ShowMessageBox(string message, string caption = "", MessageButton button = MessageButton.OK, MessageImage icon = MessageImage.None)
2526
{
26-
throw new MustBeImplementedException();
27+
var messageResult = MessageResult.Cancel;
28+
var context = Catel.Android.ContextHelper.CurrentContext;
29+
var builder = new AlertDialog.Builder(context);
30+
31+
switch (button)
32+
{
33+
case MessageButton.OK:
34+
builder.SetPositiveButton("OK", (sender, e) => { messageResult = MessageResult.OK; });
35+
break;
36+
37+
case MessageButton.OKCancel:
38+
builder.SetPositiveButton("OK", (sender, e) => { messageResult = MessageResult.OK; });
39+
builder.SetCancelable(true);
40+
break;
41+
42+
case MessageButton.YesNo:
43+
builder.SetPositiveButton("Yes", (sender, e) => { messageResult = MessageResult.Yes; });
44+
builder.SetNegativeButton("No", (sender, e) => { messageResult = MessageResult.No; });
45+
break;
46+
47+
case MessageButton.YesNoCancel:
48+
builder.SetPositiveButton("Yes", (sender, e) => { messageResult = MessageResult.Yes; });
49+
builder.SetNegativeButton("No", (sender, e) => { messageResult = MessageResult.No; });
50+
builder.SetCancelable(true);
51+
break;
52+
53+
default:
54+
throw new ArgumentOutOfRangeException("button");
55+
}
56+
57+
builder.SetMessage(message).SetTitle(caption);
58+
59+
builder.Show();
60+
61+
return messageResult;
2762
}
2863
}
2964
}

src/Catel.MVVM/Catel.MVVM.NET40/Services/NavigationService.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ partial void NavigateForward()
105105

106106
partial void NavigateWithParameters(string uri, Dictionary<string, object> parameters)
107107
{
108-
var context = Application.Context;
108+
var context = Catel.Android.ContextHelper.CurrentContext;
109109

110110
var navigationTargetType = TypeCache.GetType(uri);
111111
var intent = new Intent(context, navigationTargetType);

src/Catel.MVVM/Catel.MVVM.NET40/Services/NavigationService.ios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ partial void NavigateForward()
101101
partial void NavigateWithParameters(string uri, Dictionary<string, object> parameters)
102102
{
103103
throw new MustBeImplementedException();
104-
//var context = Application.Context;
104+
//var context = Catel.Android.ContextHelper.CurrentContext;
105105

106106
//var navigationTargetType = TypeCache.GetType(uri);
107107
//var intent = new Intent(context, navigationTargetType);

src/Catel.MVVM/Catel.MVVM.NET40/Services/VibrateService.android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Catel.Services
1010
{
1111
using System;
12-
using global::Android.App;
1312
using global::Android.Content;
1413
using global::Android.OS;
1514

@@ -19,7 +18,7 @@ public partial class VibrateService
1918

2019
partial void Initialize()
2120
{
22-
var context = Application.Context;
21+
var context = Android.ContextHelper.CurrentContext;
2322
_vibrator = context.GetSystemService(Context.VibratorService) as Vibrator;
2423
}
2524

0 commit comments

Comments
 (0)