Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Revert "Refactor observer lifecycle (#14828)"
Browse files Browse the repository at this point in the history
This reverts commit f95f671.
  • Loading branch information
jfversluis committed Nov 9, 2021
1 parent 7f870c3 commit ee98ac9
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public abstract class EntryRendererBase<TControl> : ViewRenderer<Entry, TControl

bool _disposed;
IDisposable _selectedTextRangeObserver;
IDisposable _clearButtonSublayerObserver;
bool _nativeSelectionIsUpdating;

bool _cursorPositionChangePending;
Expand Down Expand Up @@ -93,7 +92,8 @@ protected override void Dispose(bool disposing)
Control.EditingDidEnd -= OnEditingEnded;
Control.ShouldChangeCharacters -= ShouldChangeCharacters;
_selectedTextRangeObserver?.Dispose();
_clearButtonSublayerObserver?.Dispose();

ClearButton?.Layer?.RemoveObserver(this, new NSString("sublayers"));
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
textField.ShouldChangeCharacters += ShouldChangeCharacters;
_selectedTextRangeObserver = textField.AddObserver("selectedTextRange", NSKeyValueObservingOptions.New, UpdateCursorFromControl);

_clearButtonSublayerObserver = ClearButton?.Layer.AddObserver(new NSString("sublayers"), NSKeyValueObservingOptions.New, UpdateClearButtonSublayer);
ClearButton?.Layer.AddObserver(this, new NSString("sublayers"), NSKeyValueObservingOptions.New, IntPtr.Zero);
}

// When we set the control text, it triggers the UpdateCursorFromControl event, which updates CursorPosition and SelectionLength;
Expand Down Expand Up @@ -160,6 +160,12 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
UpdateClearButtonVisibility();
}

public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
{
if (keyPath == new NSString("sublayers") && _defaultClearImage == null)
UpdateClearButtonVisibility();
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Entry.PlaceholderProperty.PropertyName || e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
Expand Down Expand Up @@ -434,15 +440,6 @@ void UpdateCursorFromControl(NSObservedChange obj)
}
}

void UpdateClearButtonSublayer(NSObservedChange obj)
{
if (Control == null || Element == null)
return;

if (_defaultClearImage == null)
UpdateClearButtonVisibility();
}

void UpdateCursorSelection()
{
if (_nativeSelectionIsUpdating || Control == null || Element == null)
Expand Down

0 comments on commit ee98ac9

Please sign in to comment.