/// <summary>
/// Fetch from the <see cref = "Remote" />.
/// </summary>
/// <param name="remote">The remote to fetch</param>
/// <param name="tagFetchMode">Optional parameter indicating what tags to download.</param>
/// <param name="onProgress">Progress callback. Corresponds to libgit2 progress callback.</param>
/// <param name="onCompletion">Completion callback. Corresponds to libgit2 completion callback.</param>
/// <param name="onUpdateTips">UpdateTips callback. Corresponds to libgit2 update_tips callback.</param>
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
/// <param name="credentials">Credentials to use for username/password authentication.</param>
public virtual void Fetch(
Remote remote,
TagFetchMode tagFetchMode = TagFetchMode.Auto,
ProgressHandler onProgress = null,
CompletionHandler onCompletion = null,
UpdateTipsHandler onUpdateTips = null,
TransferProgressHandler onTransferProgress = null,
Credentials credentials = null)
{
Ensure.ArgumentNotNull(remote, "remote");
// We need to keep a reference to the git_cred_acquire_cb callback around
// so it will not be garbage collected before we are done with it.
// Note that we also have a GC.KeepAlive call at the end of the method.
NativeMethods.git_cred_acquire_cb credentialCallback = null;
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
{
var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);
if (credentials != null)
{
credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
Proxy.git_remote_set_cred_acquire_cb(
remoteHandle,
credentialCallback,
IntPtr.Zero);
}
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
// where the managed layer could move the git_remote_callbacks to a different location in memory,
// but libgit2 would still reference the old address.
//
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
try
{
Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
Proxy.git_remote_download(remoteHandle, onTransferProgress);
Proxy.git_remote_update_tips(remoteHandle);
}
finally
{
Proxy.git_remote_disconnect(remoteHandle);
}
}
// To be safe, make sure the credential callback is kept until
// alive until at least this point.
GC.KeepAlive(credentialCallback);
}
private ProgressBar progressBar; // Progress spinner to use for table operations
// Called when the activity initially gets created
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Activity_To_Do);
// Initialize the progress bar
progressBar = FindViewById<ProgressBar>(Resource.Id.loadingProgressBar);
progressBar.Visibility = ViewStates.Gone;
// Create ProgressFilter to handle busy state
var progressHandler = new ProgressHandler ();
progressHandler.BusyStateChange += (busy) => {
if (progressBar != null)
progressBar.Visibility = busy ? ViewStates.Visible : ViewStates.Gone;
};
try
{
// PUSH NOTIFICATIONS: Register for push notifications
System.Diagnostics.Debug.WriteLine("Registering...");
// Initialize our Gcm Service Hub
GcmService.Initialize(this);
GcmService.Register(this);
// MOBILE SERVICES: Setup azure mobile services - this is separate from push notifications
CurrentPlatform.Init ();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL and key
client = new MobileServiceClient(
Constants.ApplicationURL,
Constants.ApplicationKey, progressHandler);
// Get the Mobile Service Table instance to use
todoTable = client.GetTable<TodoItem>();
// USER INTERFACE: setup the Android UI
textNewTodo = FindViewById<EditText>(Resource.Id.textNewTodo);
// Create an adapter to bind the items with the view
adapter = new TodoItemAdapter(this, Resource.Layout.Row_List_To_Do);
var listViewTodo = FindViewById<ListView>(Resource.Id.listViewTodo);
listViewTodo.Adapter = adapter;
// Load the items from the Mobile Service
await RefreshItemsFromTableAsync();
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog(new Exception ("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
private ProgressBar progressBar; // Progress spinner to use for table operations
// Called when the activity initially gets created
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Activity_To_Do);
// Initialize the progress bar
progressBar = FindViewById<ProgressBar>(Resource.Id.loadingProgressBar);
progressBar.Visibility = ViewStates.Gone;
// Create ProgressFilter to handle busy state
// Create ProgressFilter to handle busy state
var progressHandler = new ProgressHandler ();
progressHandler.BusyStateChange += (busy) => {
if (progressBar != null)
progressBar.Visibility = busy ? ViewStates.Visible : ViewStates.Gone;
};
try
{
// Check to ensure everything's setup right
PushClient.CheckDevice(this);
PushClient.CheckManifest(this);
// Register for push notifications
System.Diagnostics.Debug.WriteLine("Registering...");
PushClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
CurrentPlatform.Init ();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL and key
client = new MobileServiceClient(
Constants.ApplicationURL,
Constants.ApplicationKey, progressHandler);
// Get the Mobile Service Table instance to use
todoTable = client.GetTable<TodoItem>();
textNewTodo = FindViewById<EditText>(Resource.Id.textNewTodo);
// Create an adapter to bind the items with the view
adapter = new TodoItemAdapter(this, Resource.Layout.Row_List_To_Do);
var listViewTodo = FindViewById<ListView>(Resource.Id.listViewTodo);
listViewTodo.Adapter = adapter;
// Load the items from the Mobile Service
await RefreshItemsFromTableAsync();
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog(new Exception ("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
protected override async void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Activity_To_Do);
progressBar = FindViewById<ProgressBar> (Resource.Id.loadingProgressBar);
// Initialize the progress bar
progressBar.Visibility = ViewStates.Gone;
// Create ProgressFilter to handle busy state
var progressHandler = new ProgressHandler ();
progressHandler.BusyStateChange += (busy) => {
if (progressBar != null)
progressBar.Visibility = busy ? ViewStates.Visible : ViewStates.Gone;
};
try {
CurrentPlatform.Init ();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL and key
client = new MobileServiceClient (
applicationURL,
applicationKey, progressHandler);
string path =
Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "localstore.db");
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
var store = new MobileServiceSQLiteStore(path);
store.DefineTable<ToDoItem>();
await client.SyncContext.InitializeAsync(store, new SyncHandler(this));
// Get the Mobile Service Table instance to use
toDoTable = client.GetSyncTable <ToDoItem> ();
textNewToDo = FindViewById<EditText> (Resource.Id.textNewToDo);
// Create an adapter to bind the items with the view
adapter = new ToDoItemAdapter (this, Resource.Layout.Row_List_To_Do);
var listViewToDo = FindViewById<ListView> (Resource.Id.listViewToDo);
listViewToDo.Adapter = adapter;
// Load the items from the Mobile Service
await RefreshItemsFromTableAsync ();
} catch (Java.Net.MalformedURLException) {
CreateAndShowDialog (new Exception ("There was an error creating the Mobile Service. Verify the URL"), "Error");
} catch (Exception e) {
CreateAndShowDialog (e, "Error");
}
}
private ProgressBar progressBar; // Progress spinner to use for table operations
// Called when the activity initially gets created
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Activity_To_Do);
// Initialize the progress bar
progressBar = FindViewById<ProgressBar>(Resource.Id.loadingProgressBar);
progressBar.Visibility = ViewStates.Gone;
// TODO:: Uncomment the following code when using a mobile service
// Create ProgressFilter to handle busy state
var progressHandler = new ProgressHandler ();
progressHandler.BusyStateChange += (busy) => {
if (progressBar != null)
progressBar.Visibility = busy ? ViewStates.Visible : ViewStates.Gone;
};
try
{
// TODO:: Uncomment the following code to create the mobile services client
CurrentPlatform.Init ();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL and key
client = new MobileServiceClient(
Constants.ApplicationURL,
Constants.ApplicationKey, progressHandler);
// Get the Mobile Service Table instance to use
todoTable = client.GetTable<TodoItem>();
textNewTodo = FindViewById<EditText>(Resource.Id.textNewTodo);
// Create an adapter to bind the items with the view
adapter = new TodoItemAdapter(this, Resource.Layout.Row_List_To_Do);
var listViewTodo = FindViewById<ListView>(Resource.Id.listViewTodo);
listViewTodo.Adapter = adapter;
// Load the items from the Mobile Service
await RefreshItemsFromTableAsync();
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog(new Exception ("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
/// <summary>
/// Fetch from the <see cref="Remote"/>.
/// </summary>
/// <param name="remote">The remote to fetch</param>
/// <param name="tagFetchMode">Optional parameter indicating what tags to download.</param>
/// <param name="onProgress">Progress callback. Corresponds to libgit2 progress callback.</param>
/// <param name="onUpdateTips">UpdateTips callback. Corresponds to libgit2 update_tips callback.</param>
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
/// <param name="credentials">Credentials to use for username/password authentication.</param>
public virtual void Fetch(
Remote remote,
TagFetchMode? tagFetchMode = null,
ProgressHandler onProgress = null,
UpdateTipsHandler onUpdateTips = null,
TransferProgressHandler onTransferProgress = null,
Credentials credentials = null)
{
Ensure.ArgumentNotNull(remote, "remote");
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
{
var callbacks = new RemoteCallbacks(onProgress, onTransferProgress, onUpdateTips, credentials);
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
if (tagFetchMode.HasValue)
{
Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode.Value);
}
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
// where the managed layer could move the git_remote_callbacks to a different location in memory,
// but libgit2 would still reference the old address.
//
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
try
{
Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
Proxy.git_remote_download(remoteHandle);
Proxy.git_remote_update_tips(remoteHandle);
}
finally
{
Proxy.git_remote_disconnect(remoteHandle);
}
}
}
private void handleProgress(int i)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.InvokeRequired)
{
ProgressHandler call = new ProgressHandler(handleProgress);
this.BeginInvoke(call, new object[] { i });
}
else
{
if (0 <= i && i <= 100)
{
progressBar1.Value = i;
progressBar1.Invalidate();
}
else this.Close();
}
}
private ProgressBar progressBar; // Progress spinner to use for table operations
// Called when the activity initially gets created
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Activity_To_Do);
// Initialize the progress bar
progressBar = FindViewById<ProgressBar>(Resource.Id.loadingProgressBar);
progressBar.Visibility = ViewStates.Gone;
// Create ProgressFilter to handle busy state
var progressHandler = new ProgressHandler ();
progressHandler.BusyStateChange += (busy) => {
if (progressBar != null)
progressBar.Visibility = busy ? ViewStates.Visible : ViewStates.Gone;
};
try
{
CurrentPlatform.Init ();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL and key
client = new MobileServiceClient(
Constants.ApplicationURL,
Constants.ApplicationKey, progressHandler);
await Authenticate();
await CreateTable();
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog(new Exception ("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
请发表评论