Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue #42 #59

Merged
merged 2 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Droid/RefreshSample.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<AndroidResgenClass>Resource</AndroidResgenClass>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidApplication>True</AndroidApplication>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<AssemblyName>RefreshSample.Droid</AssemblyName>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
Expand All @@ -39,9 +38,6 @@
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<ConsolePause>false</ConsolePause>
<AndroidSupportedAbis>armeabi-v7a;x86</AndroidSupportedAbis>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<AndroidResgenClass>Resource</AndroidResgenClass>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<AssemblyName>Refractored.XamForms.PullToRefresh.Droid</AssemblyName>
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
<NuGetPackageImportStamp>
Expand Down
64 changes: 34 additions & 30 deletions PullToRefresh/PullToRefresh.iOS/PullToRefreshLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
using Xamarin.Forms.Platform.iOS;


[assembly:ExportRenderer(typeof(PullToRefreshLayout), typeof(PullToRefreshLayoutRenderer))]
[assembly: ExportRenderer(typeof(PullToRefreshLayout), typeof(PullToRefreshLayoutRenderer))]
namespace Refractored.XamForms.PullToRefresh.iOS
{

/// <summary>
/// Pull to refresh layout renderer.
/// </summary>
[Preserve(AllMembers=true)]
[Preserve(AllMembers = true)]
public class PullToRefreshLayoutRenderer : ViewRenderer<PullToRefreshLayout, UIView>
{

/// <summary>
/// Used for registration with dependency service
/// </summary>
Expand All @@ -44,34 +44,25 @@ public async static void Init()
}

UIRefreshControl refreshControl;
UIView refreshControlParent;


/// <summary>
/// Raises the element changed event.
/// </summary>
/// <param name="e">E.</param>
protected override void OnElementChanged (ElementChangedEventArgs<Refractored.XamForms.PullToRefresh.PullToRefreshLayout> e)
protected override void OnElementChanged(ElementChangedEventArgs<Refractored.XamForms.PullToRefresh.PullToRefreshLayout> e)
{
base.OnElementChanged (e);
base.OnElementChanged(e);

if (e.OldElement != null || Element == null)
return;



refreshControl = new UIRefreshControl ();
refreshControl = new UIRefreshControl();

refreshControl.ValueChanged += OnRefresh;

try
{
TryInsertRefresh(this);
}
catch(Exception ex)
{
Debug.WriteLine("View is not supported in PullToRefreshLayout: " + ex);
}

this.refreshControlParent = this;

UpdateColors();
UpdateIsRefreshing();
Expand Down Expand Up @@ -104,7 +95,7 @@ bool TryOffsetRefresh(UIView view, bool refreshing, int index = 0)

if (view is UICollectionView)
{

var uiCollectionView = view as UICollectionView;
if (!set)
{
Expand All @@ -114,22 +105,22 @@ bool TryOffsetRefresh(UIView view, bool refreshing, int index = 0)

if (origininalY < 0)
return true;

if (refreshing)
uiCollectionView.SetContentOffset(new CoreGraphics.CGPoint(0, origininalY - refreshControl.Frame.Size.Height), true);
else
uiCollectionView.SetContentOffset(new CoreGraphics.CGPoint(0, origininalY), true);
return true;
}


if (view is UIWebView)
{
//can't do anything
return true;
}


if (view is UIScrollView)
{
var uiScrollView = view as UIScrollView;
Expand Down Expand Up @@ -162,17 +153,18 @@ bool TryOffsetRefresh(UIView view, bool refreshing, int index = 0)

return false;
}


bool TryInsertRefresh(UIView view, int index = 0)
{

this.refreshControlParent = view;

if (view is UITableView)
{
var uiTableView = view as UITableView;
uiTableView = view as UITableView;
view.InsertSubview(refreshControl, index);

return true;
}

Expand All @@ -185,7 +177,7 @@ bool TryInsertRefresh(UIView view, int index = 0)
return true;
}


if (view is UIWebView)
{
var uiWebView = view as UIWebView;
Expand Down Expand Up @@ -256,10 +248,19 @@ void UpdateIsRefreshing()

void UpdateIsSwipeToRefreshEnabled()
{
refreshControl.Enabled = RefreshView.IsPullToRefreshEnabled;
if (RefreshView.IsPullToRefreshEnabled)
{
this.TryInsertRefresh(this.refreshControlParent);
}
else
{
if (this.refreshControl.Superview != null)
{
this.refreshControl.RemoveFromSuperview();
}
}
}


/// <summary>
/// Helpers to cast our element easily
/// Will throw an exception if the Element is not correct
Expand All @@ -273,13 +274,14 @@ public Refractored.XamForms.PullToRefresh.PullToRefreshLayout RefreshView


bool isRefreshing;

/// <summary>
/// Gets or sets a value indicating whether this instance is refreshing.
/// </summary>
/// <value><c>true</c> if this instance is refreshing; otherwise, <c>false</c>.</value>
public bool IsRefreshing
{
get { return isRefreshing;}
get { return isRefreshing; }
set
{
bool changed = IsRefreshing != value;
Expand All @@ -290,7 +292,7 @@ public bool IsRefreshing
else
refreshControl.EndRefreshing();

if(changed)
if (changed)
TryOffsetRefresh(this, IsRefreshing);
}
}
Expand Down Expand Up @@ -335,10 +337,12 @@ protected override void Dispose(bool disposing)
{
refreshControl.ValueChanged -= OnRefresh;
}

this.refreshControlParent = null;
}

}


}

21 changes: 19 additions & 2 deletions RefreshSample/ViewModels/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public TestViewModel(Page page)
Items = new ObservableCollection<string>();
}

bool canRefresh = true;

public bool CanRefresh
{
get { return canRefresh; }
set
{
if (canRefresh == value)
return;

canRefresh = value;
OnPropertyChanged("CanRefresh");
}
}


bool isBusy;

public bool IsBusy
Expand Down Expand Up @@ -70,7 +86,9 @@ async Task ExecuteRefreshCommand()

IsBusy = false;

page.DisplayAlert("Refreshed", "You just refreshed the page! Nice job!", "OK");
page.DisplayAlert("Refreshed", "You just refreshed the page! Nice job! Pull to refresh is now disabled", "OK");
this.CanRefresh = false;

return false;
});
}
Expand All @@ -88,7 +106,6 @@ public void OnPropertyChanged(string propertyName)

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

}
}

26 changes: 14 additions & 12 deletions RefreshSample/Views/ListViewPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Xamarin.Forms;
using RefreshSample.ViewModels;
using Refractored.XamForms.PullToRefresh;
using System.Threading.Tasks;

namespace RefreshSample.Views
{
Expand All @@ -25,30 +26,31 @@ public ListViewPage(bool insideLayout)
{
Title = "ListView (Pull to Refresh)";

BindingContext = new TestViewModel (this);
BindingContext = new TestViewModel(this);

var listView = new ListView ();
var listView = new ListView();
//ListView now has a built in pull to refresh!
//You most likely could enable the listview pull to refresh and use it instead of the refresh view
//listView.IsPullToRefreshEnabled = true;
//
//listView.SetBinding<TestViewModel>(ListView.IsRefreshingProperty, vm => vm.IsBusy, BindingMode.OneWay);
//listView.SetBinding<TestViewModel>(ListView.RefreshCommandProperty, vm => vm.RefreshCommand);



listView.SetBinding(ItemsView<Cell>.ItemsSourceProperty, new Binding("Items"));

//ListView now has a built in pull to refresh!
//You most likely could disable the listview pull to refresh and re-enable this
//However, I wouldn't recommend that.
var refreshView = new PullToRefreshLayout {
var refreshView = new PullToRefreshLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Content = new StackLayout
{
Spacing = 0,
Children =
{
Spacing = 0,
Children =
{
new Label
{
Expand All @@ -61,20 +63,20 @@ public ListViewPage(bool insideLayout)
},
listView
}
},
},
RefreshColor = Color.FromHex("#3498db")
};

refreshView.SetBinding(PullToRefreshLayout.IsRefreshingProperty, , new Binding("IsBusy", BindingMode.OneWay));
refreshView.SetBinding(PullToRefreshLayout.IsRefreshingProperty, new Binding("IsBusy", BindingMode.OneWay));
refreshView.SetBinding(PullToRefreshLayout.RefreshCommandProperty, new Binding("RefreshCommand"));

refreshView.SetBinding(PullToRefreshLayout.IsPullToRefreshEnabledProperty, new Binding("CanRefresh"));

if (insideLayout)
{
Content = new StackLayout
{
Spacing = 0,
Children =
Spacing = 0,
Children =
{
new Label
{
Expand Down