This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathComposer.cs
More file actions
executable file
·191 lines (158 loc) · 7.53 KB
/
Composer.cs
File metadata and controls
executable file
·191 lines (158 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using CoreGraphics;
using CodeHub.iOS.Views;
using Foundation;
using UIKit;
using System.Collections.Generic;
namespace CodeHub.iOS.ViewControllers
{
public class Composer : BaseViewController
{
public UIBarButtonItem SendItem;
public Action<string> ReturnAction;
protected readonly UITextView TextView;
protected UIView ScrollingToolbarView;
private UIImage _normalButtonImage;
private UIImage _pressedButtonImage;
public bool EnableSendButton
{
get { return SendItem.Enabled; }
set { SendItem.Enabled = value; }
}
public Composer () : base (null, null)
{
Title = "New Comment";
EdgesForExtendedLayout = UIRectEdge.None;
SendItem = new UIBarButtonItem(UIBarButtonSystemItem.Save);
NavigationItem.RightBarButtonItem = SendItem;
TextView = new UITextView(new CGRect(CGPoint.Empty, View.Bounds.Size));
TextView.Font = UIFont.PreferredBody;
TextView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
// Work around an Apple bug in the UITextView that crashes
if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR)
TextView.AutocorrectionType = UITextAutocorrectionType.No;
View.AddSubview (TextView);
_normalButtonImage = ImageFromColor(UIColor.White);
_pressedButtonImage = ImageFromColor(UIColor.FromWhiteAlpha(0.0f, 0.4f));
}
private UIImage ImageFromColor(UIColor color)
{
UIGraphics.BeginImageContext(new CGSize(1, 1));
var context = UIGraphics.GetCurrentContext();
context.SetFillColor(color.CGColor);
context.FillRect(new CGRect(0, 0, 1, 1));
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
public static UIButton CreateAccessoryButton(UIImage image, Action action)
{
var btn = CreateAccessoryButton(string.Empty, action);
// btn.AutosizesSubviews = true;
btn.SetImage(image, UIControlState.Normal);
btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
btn.ImageEdgeInsets = new UIEdgeInsets(6, 6, 6, 6);
// var imageView = new UIImageView(image);
// imageView.Frame = new RectangleF(4, 4, 24, 24);
// imageView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
// btn.Add(imageView);
return btn;
}
public static UIButton CreateAccessoryButton(string title, Action action)
{
var fontSize = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone ? 22 : 28f;
var btn = new UIButton(UIButtonType.System);
btn.Frame = new CGRect(0, 0, 32, 32);
btn.SetTitle(title, UIControlState.Normal);
btn.BackgroundColor = UIColor.White;
btn.Font = UIFont.SystemFontOfSize(fontSize);
btn.Layer.CornerRadius = 7f;
btn.Layer.MasksToBounds = true;
btn.AdjustsImageWhenHighlighted = false;
btn.TouchUpInside += (sender, e) => action();
return btn;
}
private float CalculateHeight(UIInterfaceOrientation orientation)
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
return 44;
// If pad
if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
return 64;
return 88f;
}
public override void WillRotate(UIInterfaceOrientation toInterfaceOrientation, double duration)
{
base.WillRotate(toInterfaceOrientation, duration);
if (TextView.InputAccessoryView != null)
{
UIView.Animate(duration, 0, UIViewAnimationOptions.BeginFromCurrentState, () =>
{
var frame = TextView.InputAccessoryView.Frame;
frame.Height = CalculateHeight(toInterfaceOrientation);
TextView.InputAccessoryView.Frame = frame;
}, null);
}
}
public void SetAccesoryButtons(IEnumerable<UIButton> buttons)
{
foreach (var button in buttons)
{
button.SetBackgroundImage(_normalButtonImage, UIControlState.Normal);
button.SetBackgroundImage(_pressedButtonImage, UIControlState.Highlighted);
}
var height = CalculateHeight(UIApplication.SharedApplication.StatusBarOrientation);
ScrollingToolbarView = new ScrollingToolbarView(new CGRect(0, 0, View.Bounds.Width, height), buttons);
ScrollingToolbarView.BackgroundColor = UIColor.FromWhiteAlpha(0.7f, 1.0f);
TextView.InputAccessoryView = ScrollingToolbarView;
}
public string Text
{
get { return TextView.Text; }
set { TextView.Text = value; }
}
void KeyboardChange(NSNotification notification)
{
var nsValue = notification.UserInfo.ObjectForKey (UIKeyboard.FrameEndUserInfoKey) as NSValue;
if (nsValue == null) return;
var kbdBounds = nsValue.RectangleFValue;
var keyboard = View.Window.ConvertRectToView(kbdBounds, View);
UIView.Animate(
1.0f, 0, UIViewAnimationOptions.CurveEaseIn,
() => TextView.Frame = new CGRect(0, 0, View.Bounds.Width, keyboard.Top), null);
}
NSObject _hideNotification, _showNotification;
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
_showNotification = NSNotificationCenter.DefaultCenter.AddObserver(
new NSString("UIKeyboardWillShowNotification"), KeyboardChange);
_hideNotification = NSNotificationCenter.DefaultCenter.AddObserver(
new NSString("UIKeyboardWillHideNotification"), KeyboardChange);
TextView.BecomeFirstResponder ();
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
if (_hideNotification != null)
NSNotificationCenter.DefaultCenter.RemoveObserver(_hideNotification);
if (_showNotification != null)
NSNotificationCenter.DefaultCenter.RemoveObserver(_showNotification);
}
public void PresentAsModal(UIViewController parent, Action<string> onSave, Action onClose = null)
{
onClose = onClose ?? new Action(() => parent.DismissViewController(true, null));
var closeButton = new UIBarButtonItem(UIBarButtonSystemItem.Cancel);
NavigationItem.LeftBarButtonItem = closeButton;
OnActivation(d =>
{
d(closeButton.GetClickedObservable()
.Subscribe(_ => onClose?.Invoke()));
d(SendItem.GetClickedObservable()
.Subscribe(_ => onSave?.Invoke(this.Text)));
});
var navigationController = new UINavigationController(this);
parent.PresentViewController(navigationController, true, null);
}
}
}