Created
September 23, 2016 03:21
-
-
Save GuyCarver/8680b1b5010c5d6a3b45baffd01853ae to your computer and use it in GitHub Desktop.
F# version of QuickText.cs example for Continuous app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Quick Text Example | |
// | |
// Enables you to text quickly by defining standard messages. | |
// | |
// 1. Add the ability to delete messages | |
// 2. Add the most recent message at the top | |
// | |
open System | |
open System.Collections.ObjectModel | |
open System.Linq | |
open Foundation | |
open UIKit | |
open Xamarin.Forms | |
// | |
// Setup a "database" of quick messages stored in a text file. | |
// | |
let messages = ObservableCollection<string> () | |
let ReadMessages () = | |
try | |
let lines = System.IO.File.ReadLines ("QuickTextMessages.txt") | |
messages.Clear() | |
for line in lines do | |
//printfn "%s" line | |
messages.Add(line) | |
with | |
| ex -> messages.Add("helli") | |
//printfn "%s" ex.ToString() | |
let SaveMessages () = | |
try | |
System.IO.File.WriteAllLines ("QuickTextMessages.txt", messages) | |
with | |
| ex -> printfn "%s" ex.ToString() | |
ReadMessages () | |
// | |
// Construct the UI | |
// | |
let lst = ListView() | |
lst.ItemsSource <- messages | |
let listPage = ContentPage ( | |
Content = lst, | |
Title = "Quick Text" | |
) | |
let addItem = ToolbarItem ( | |
Text = "Add" | |
) | |
listPage.ToolbarItems.Add addItem | |
//put in separate function cuz the interpreter was not correctly reporting compile errors as a lambda in the function below (also a lambda). | |
let handleadd (itm : UIAlertView) = | |
itm.Clicked.Add (fun (a : UIButtonEventArgs) -> | |
if (a.ButtonIndex <> itm.CancelButtonIndex) then | |
messages.Add itm.GetTextField(nint(0)).Text | |
SaveMessages () | |
) | |
// | |
// Add interaction | |
// | |
addItem.Clicked.Add (fun (a : EventArgs) -> | |
// Create an add message UI | |
let alert = new UIAlertView ("Add Message", "", null, "Cancel", "Add") | |
alert.AlertViewStyle <- UIAlertViewStyle.PlainTextInput | |
// Handle the user input | |
handleadd alert | |
alert.Show() | |
) | |
lst.ItemSelected.Add (fun (e : SelectedItemChangedEventArgs) -> | |
match e.SelectedItem with | |
| null -> () | |
| _ -> | |
// Get the selected message | |
let message = (string)e.SelectedItem; | |
// Deselect the list cell | |
lst.SelectedItem <- null | |
// Create the activity UI | |
let actItems = [| | |
new NSString(message) :> NSObject | |
|] | |
let act = new UIActivityViewController (actItems, Array.empty<UIActivity>) | |
// Display the activity UI | |
let window = UIApplication.SharedApplication.KeyWindow | |
act.PopoverPresentationController.SourceView <- window | |
window.RootViewController.PresentViewController(act, true, null) | |
) | |
// | |
// Display | |
// | |
let Main = NavigationPage(listPage) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment