|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Xamarin.Forms.CustomAttributes; |
| 5 | +using Xamarin.Forms.Internals; |
| 6 | +using Xamarin.Forms.Shapes; |
| 7 | + |
| 8 | +#if UITEST |
| 9 | +using Xamarin.Forms.Core.UITests; |
| 10 | +using Xamarin.UITest; |
| 11 | +using NUnit.Framework; |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace Xamarin.Forms.Controls.Issues |
| 15 | +{ |
| 16 | +#if UITEST |
| 17 | + [Category(UITestCategories.Shape)] |
| 18 | +#endif |
| 19 | + [Preserve(AllMembers = true)] |
| 20 | + [Issue(IssueTracker.Github, 10623, "Tap Gesture not working on iOS [Bug]", PlatformAffected.iOS)] |
| 21 | + public class Issue10623 : TestContentPage |
| 22 | + { |
| 23 | + public Issue10623() |
| 24 | + { |
| 25 | + Device.SetFlags(new List<string>(Device.Flags ?? new List<string>()) { "Shapes_Experimental" }); |
| 26 | + |
| 27 | + var layout = new StackLayout(); |
| 28 | + |
| 29 | + var instructions = new Label |
| 30 | + { |
| 31 | + Padding = 12, |
| 32 | + BackgroundColor = Color.Black, |
| 33 | + TextColor = Color.White, |
| 34 | + Text = "Tap the rating control. If you can modify the number of selected stars, the test has passed." |
| 35 | + }; |
| 36 | + |
| 37 | + var rating = new RatingControl |
| 38 | + { |
| 39 | + Size = 30, |
| 40 | + Max = 5, |
| 41 | + FillColor = Brush.Gold, |
| 42 | + StrokeColor = Brush.Silver, |
| 43 | + StrokeThickness = 2, |
| 44 | + HorizontalOptions = LayoutOptions.Center |
| 45 | + }; |
| 46 | + |
| 47 | + layout.Children.Add(instructions); |
| 48 | + layout.Children.Add(rating); |
| 49 | + |
| 50 | + Content = layout; |
| 51 | + } |
| 52 | + |
| 53 | + protected override void Init() |
| 54 | + { |
| 55 | + Title = "Issue 10623"; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public class RatingControl : StackLayout |
| 60 | + { |
| 61 | + public RatingControl() |
| 62 | + { |
| 63 | + Orientation = StackOrientation.Horizontal; |
| 64 | + } |
| 65 | + |
| 66 | + public static BindableProperty RatingProperty = BindableProperty.Create(nameof(Rating), typeof(double), typeof(RatingControl), 0.0, BindingMode.OneWay, propertyChanged: (bindable, newValue, oldValue) => |
| 67 | + { |
| 68 | + var control = (RatingControl)bindable; |
| 69 | + |
| 70 | + if (newValue != oldValue) |
| 71 | + { |
| 72 | + control.Draw(); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + readonly List<Point> _originalFullStarPoints = new List<Point>() |
| 77 | + { |
| 78 | + new Point(96,1.12977573), |
| 79 | + new Point(66.9427701,60.0061542), |
| 80 | + new Point(1.96882894,69.4474205), |
| 81 | + new Point(48.9844145,115.27629), |
| 82 | + new Point(37.8855403,179.987692), |
| 83 | + new Point(96,149.435112), |
| 84 | + new Point(154.11446,179.987692), |
| 85 | + new Point(143.015586,115.27629), |
| 86 | + new Point(190.031171,69.4474205), |
| 87 | + new Point(125.05723,60.0061542), |
| 88 | + new Point(96,1.12977573), |
| 89 | + }; |
| 90 | + |
| 91 | + readonly List<Point> _originalHalfStarPoints = new List<Point>() |
| 92 | + { |
| 93 | + new Point(96,1.12977573), |
| 94 | + new Point(66.9427701,60.0061542), |
| 95 | + new Point(1.96882894,69.4474205), |
| 96 | + new Point(48.9844145,115.27629), |
| 97 | + new Point(37.8855403,179.987692), |
| 98 | + new Point(96,149.435112), |
| 99 | + new Point(96,1.12977573) |
| 100 | + }; |
| 101 | + |
| 102 | + readonly PointCollection _fullStarPoints = new PointCollection(); |
| 103 | + readonly PointCollection _halfStarPoints = new PointCollection(); |
| 104 | + |
| 105 | + double _ratio; |
| 106 | + |
| 107 | + private void Draw() |
| 108 | + { |
| 109 | + Children.Clear(); |
| 110 | + |
| 111 | + var newRatio = Size / 200; |
| 112 | + |
| 113 | + if (newRatio != _ratio) |
| 114 | + { |
| 115 | + _ratio = newRatio; |
| 116 | + |
| 117 | + CalculatePoints(_fullStarPoints, _originalFullStarPoints); |
| 118 | + CalculatePoints(_halfStarPoints, _originalHalfStarPoints); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + for (var i = 1; i <= Max; i++) |
| 123 | + { |
| 124 | + if (Rating >= i) |
| 125 | + { |
| 126 | + Children.Add(GetFullStar()); |
| 127 | + } |
| 128 | + else if (Rating > i - 1) |
| 129 | + { |
| 130 | + Children.Add(GetHalfStar()); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + Children.Add(GetEmptyStar()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + UpdateTapGestureRecognizers(); |
| 139 | + } |
| 140 | + |
| 141 | + private void CalculatePoints(PointCollection calculated, List<Point> original) |
| 142 | + { |
| 143 | + calculated.Clear(); |
| 144 | + |
| 145 | + foreach (var point in original) |
| 146 | + { |
| 147 | + var x = point.X * _ratio; |
| 148 | + var y = point.Y * _ratio; |
| 149 | + |
| 150 | + var p = new Point(x, y); |
| 151 | + |
| 152 | + calculated.Add(p); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private Polygon GetFullStar() |
| 157 | + { |
| 158 | + var fullStar = new Polygon() |
| 159 | + { |
| 160 | + Points = _fullStarPoints, |
| 161 | + Fill = FillColor, |
| 162 | + StrokeThickness = StrokeThickness, |
| 163 | + Stroke = StrokeColor |
| 164 | + }; |
| 165 | + |
| 166 | + return fullStar; |
| 167 | + } |
| 168 | + |
| 169 | + private Grid GetHalfStar() |
| 170 | + { |
| 171 | + var grid = new Grid(); |
| 172 | + |
| 173 | + var halfStar = new Polygon() |
| 174 | + { |
| 175 | + Points = _halfStarPoints, |
| 176 | + Fill = _fillColor, |
| 177 | + Stroke = Brush.Transparent, |
| 178 | + StrokeThickness = 0, |
| 179 | + }; |
| 180 | + |
| 181 | + var emptyStar = new Polygon() |
| 182 | + { |
| 183 | + Points = _fullStarPoints, |
| 184 | + StrokeThickness = StrokeThickness, |
| 185 | + Stroke = StrokeColor |
| 186 | + }; |
| 187 | + |
| 188 | + grid.Children.Add(halfStar); |
| 189 | + grid.Children.Add(emptyStar); |
| 190 | + |
| 191 | + return grid; |
| 192 | + } |
| 193 | + |
| 194 | + private Polygon GetEmptyStar() |
| 195 | + { |
| 196 | + var emptyStar = new Polygon() |
| 197 | + { |
| 198 | + Points = _fullStarPoints, |
| 199 | + StrokeThickness = StrokeThickness, |
| 200 | + Stroke = StrokeColor |
| 201 | + }; |
| 202 | + |
| 203 | + return emptyStar; |
| 204 | + } |
| 205 | + |
| 206 | + private void Set<T>(ref T field, T newValue) |
| 207 | + { |
| 208 | + if (!EqualityComparer<T>.Default.Equals(field, newValue)) |
| 209 | + { |
| 210 | + field = newValue; |
| 211 | + Draw(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public double Rating |
| 216 | + { |
| 217 | + get => (double)GetValue(RatingProperty); |
| 218 | + set => SetValue(RatingProperty, value); |
| 219 | + } |
| 220 | + |
| 221 | + int _max = 5; |
| 222 | + public int Max |
| 223 | + { |
| 224 | + get => _max; |
| 225 | + set => Set(ref _max, value); |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + Brush _fillColor = Brush.Yellow; |
| 230 | + public Brush FillColor |
| 231 | + { |
| 232 | + get => _fillColor; |
| 233 | + set => Set(ref _fillColor, value); |
| 234 | + } |
| 235 | + |
| 236 | + Brush _strokeColor = Brush.Black; |
| 237 | + public Brush StrokeColor |
| 238 | + { |
| 239 | + get => _strokeColor; |
| 240 | + set => Set(ref _strokeColor, value); |
| 241 | + } |
| 242 | + |
| 243 | + double _strokeThickness = 0; |
| 244 | + public double StrokeThickness |
| 245 | + { |
| 246 | + get => _strokeThickness; |
| 247 | + set => Set(ref _strokeThickness, value); |
| 248 | + } |
| 249 | + |
| 250 | + double _size = 50; |
| 251 | + public double Size |
| 252 | + { |
| 253 | + get => _size; |
| 254 | + set => Set(ref _size, value); |
| 255 | + } |
| 256 | + |
| 257 | + private void UpdateTapGestureRecognizers() |
| 258 | + { |
| 259 | + foreach (var star in Children) |
| 260 | + { |
| 261 | + if (!star.GestureRecognizers.Any()) |
| 262 | + { |
| 263 | + var recognizer = new TapGestureRecognizer(); |
| 264 | + recognizer.Tapped += TapGestureRecognizer_Tapped; |
| 265 | + star.GestureRecognizers.Add(recognizer); |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + private void TapGestureRecognizer_Tapped(object sender, EventArgs e) |
| 271 | + { |
| 272 | + var star = (View)sender; |
| 273 | + |
| 274 | + var index = Children.IndexOf(star); |
| 275 | + |
| 276 | + Rating = index + 1; |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments