Advertisement
Guest User

Hello, World!

a guest
Jan 25th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.17 KB | None | 0 0
  1. // Translator.fs
  2.  
  3. namespace Phoneword_FSharp
  4.  
  5. open System
  6. open System.Text
  7.  
  8. module Translator =
  9.  
  10.     let translate (s:string) =
  11.         let isNumType c = Seq.exists ((=) c) " -0123456789" in
  12.             seq { for c in s do
  13.                     match c with
  14.                     | _ when isNumType c    -> yield c
  15.                     | 'A' | 'B' | 'C'       -> yield '2'
  16.                     | 'D' | 'E' | 'F'       -> yield '3'
  17.                     | 'G' | 'H' | 'I'       -> yield '4'
  18.                     | 'J' | 'K' | 'L'       -> yield '5'
  19.                     | 'M' | 'N' | 'O'       -> yield '6'
  20.                     | 'P' | 'Q' | 'R' | 'S' -> yield '7'
  21.                     | 'T' | 'U' | 'V'       -> yield '8'
  22.                     | 'W' | 'X' | 'Y' | 'Z' -> yield '9'
  23.                     | _                     -> ()
  24.                 }
  25.             |> String.Concat
  26.    
  27.     let toNumber raw =
  28.         if String.IsNullOrWhiteSpace raw
  29.             then "" else translate (raw.ToUpperInvariant())
  30.    
  31. // MainActivity.fs
  32.  
  33. namespace Phoneword_FSharp
  34.  
  35. open System
  36.  
  37. open Android.App
  38. open Android.Content
  39. open Android.OS
  40. open Android.Runtime
  41. open Android.Views
  42. open Android.Widget
  43.  
  44. open System.Resources
  45.  
  46. [<Activity (Label = "Phoneword", MainLauncher = true)>]
  47. type MainActivity () =
  48.     inherit Activity ()
  49.  
  50.     let mutable translatedNumber = String.Empty
  51.  
  52.     override this.OnCreate (bundle) =
  53.  
  54.         base.OnCreate (bundle)
  55.  
  56.         // Set our view from the "main" layout resource
  57.         this.SetContentView (Resource_Layout.Main)
  58.  
  59.         // Get our UI controls from the loaded layout
  60.         let view = this.LayoutInflater.Inflate (Resource_Layout.Main, null)
  61.         let phoneNumberText = this.FindViewById<EditText> (Phoneword_FSharp.Resource_Id.PhoneNumberText)
  62.         let translateButton = this.FindViewById<Button> (Phoneword_FSharp.Resource_Id.TranslateButton)
  63.         let callButton = this.FindViewById<Button> (Phoneword_FSharp.Resource_Id.CallButton)
  64.  
  65.         // Disable the "Call" button
  66.         callButton.Enabled <- false
  67.  
  68.         translateButton.Click.Add(fun _ ->
  69.             // Translate user's alphanumeric phone number to numeric
  70.             translatedNumber <- Translator.toNumber (phoneNumberText.Text)
  71.  
  72.             if String.IsNullOrWhiteSpace translatedNumber
  73.                 then
  74.                     callButton.Text <- "Call"
  75.                     callButton.Enabled <- false
  76.                 else
  77.                     callButton.Text <- "Call " + translatedNumber
  78.                     callButton.Enabled <- true
  79.         )
  80.  
  81.         callButton.Click.Add(fun _ ->
  82.             let callDialog = new AlertDialog.Builder (this)
  83.  
  84.             let startCall _ _ =
  85.                 let callIntent = new Intent (Intent.ActionCall)
  86.                 callIntent.SetData (Android.Net.Uri.Parse <| "tel:" + translatedNumber)
  87.                 |> this.StartActivity
  88.  
  89.             ignore <| callDialog.SetMessage ("Call " + translatedNumber + "?")
  90.             ignore <| callDialog.SetNeutralButton ("Call", startCall)
  91.             ignore <| callDialog.SetNegativeButton ("Cancel", fun _ _ -> ())
  92.             ignore <| callDialog.Show ()
  93.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement