This commit is contained in:
Amy Burns 2016-04-12 10:28:50 -04:00
Родитель 31c778ac95
Коммит e3687d5e71
2 изменённых файлов: 26 добавлений и 27 удалений

Просмотреть файл

@ -3,52 +3,51 @@ using System;
namespace Phoneword_iOS
{
public static class PhonewordTranslator
public static class PhoneTranslator
{
public static string ToNumber(string raw)
public static string ToNumber (string raw)
{
if (string.IsNullOrWhiteSpace(raw))
if (string.IsNullOrWhiteSpace (raw))
return "";
else
raw = raw.ToUpperInvariant();
raw = raw.ToUpperInvariant ();
var newNumber = new StringBuilder();
foreach (var c in raw)
{
if (" -0123456789".Contains(c))
newNumber.Append(c);
var newNumber = new StringBuilder ();
foreach (var c in raw) {
if (" -0123456789".Contains (c))
newNumber.Append (c);
else {
var result = TranslateToNumber(c);
var result = TranslateToNumber (c);
if (result != null)
newNumber.Append(result);
}
newNumber.Append (result);
}
// otherwise we've skipped a non-numeric char
}
return newNumber.ToString();
return newNumber.ToString ();
}
static bool Contains (this string keyString, char c)
{
return keyString.IndexOf(c) >= 0;
return keyString.IndexOf (c) >= 0;
}
static int? TranslateToNumber(char c)
static int? TranslateToNumber (char c)
{
if ("ABC".Contains(c))
if ("ABC".Contains (c))
return 2;
else if ("DEF".Contains(c))
else if ("DEF".Contains (c))
return 3;
else if ("GHI".Contains(c))
else if ("GHI".Contains (c))
return 4;
else if ("JKL".Contains(c))
else if ("JKL".Contains (c))
return 5;
else if ("MNO".Contains(c))
else if ("MNO".Contains (c))
return 6;
else if ("PQRS".Contains(c))
else if ("PQRS".Contains (c))
return 7;
else if ("TUV".Contains(c))
else if ("TUV".Contains (c))
return 8;
else if ("WXYZ".Contains(c))
else if ("WXYZ".Contains (c))
return 9;
return null;
}

Просмотреть файл

@ -20,9 +20,9 @@ namespace Phoneword_iOS
TranslateButton.TouchUpInside += (object sender, EventArgs e) => {
// Convert the phone number with text to a number
// using PhoneTranslator.cs
translatedNumber = PhonewordTranslator.ToNumber(
PhoneNumberText.Text);
translatedNumber = PhoneTranslator.ToNumber (
PhoneNumberText.Text);
// Dismiss the keyboard if text field was tapped
PhoneNumberText.ResignFirstResponder ();
@ -30,7 +30,7 @@ namespace Phoneword_iOS
CallButton.SetTitle ("Call ", UIControlState.Normal);
CallButton.Enabled = false;
} else {
CallButton.SetTitle ("Call " + translatedNumber,
CallButton.SetTitle ("Call " + translatedNumber,
UIControlState.Normal);
CallButton.Enabled = true;
}