Skip to content

Instantly share code, notes, and snippets.

View danielkon96's full-sized avatar
🏠
Working from home everyday

Daniel Kondrashevich danielkon96

🏠
Working from home everyday
View GitHub Profile
@danielkon96
danielkon96 / ImageSelectionPage.xaml
Created September 20, 2019 21:50
The frontend UI
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:CV="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
xmlns:FFImage="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
xmlns:FFImageCache="clr-namespace:FFImageLoading.Cache;assembly=FFImageLoading"
mc:Ignorable="d"
x:Class="MultiImagePicker.ImageSelectionPage">
@danielkon96
danielkon96 / RequiredNuGetPackages
Last active September 20, 2019 22:47
The list of required NuGet Packages
All three projects:
- CarouselView.FormsPlguin
- Microsoft.Azure.Storage.Blob
- Xam.Plugin.Media
- Xamarin.FFImageLoading.Forms
MultiImagePicker Project:
- Plugin.Permissions
MultiImagePicker.Android Project:
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Created September 20, 2019 22:56
Select Images method
private async void SelectImagesButton_Clicked(object sender, EventArgs e)
{
//Check users permissions.
var storagePermissions = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
var photoPermissions = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Photos);
if (storagePermissions == PermissionStatus.Granted && photoPermissions == PermissionStatus.Granted)
{
//If we are on iOS, call GMMultiImagePicker.
if (Device.RuntimePlatform == Device.iOS)
{
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Created September 20, 2019 22:59
AskForPermissions Method
/// <summary>
/// Make sure Permissions are given to the users storage.
/// </summary>
/// <returns></returns>
private async Task<bool> AskForPermissions()
{
try
{
await CrossMedia.Current.Initialize();
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Last active September 20, 2019 23:01
Page Constructor
public ImageSelectionPage()
{
InitializeComponent();
//Ask for permissions.
Device.BeginInvokeOnMainThread(async () => await AskForPermissions() );
}
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Created September 20, 2019 23:13
Code example of applying images to a carousel view
//For iOS
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS");
MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS", (s, images) =>
{
//If we have selected images, put them into the carousel view.
if (images.Count > 0)
{
ImgCarouselView.ItemsSource = images;
InfoText.IsVisible = true; //InfoText is optional
}
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Created September 20, 2019 23:16
When we leave the page, unsubscribe.
/// <summary>
/// Unsubsribe from the MessagingCenter on disappearing.
/// </summary>
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid");
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS");
GC.Collect();
}
@danielkon96
danielkon96 / IMediaService.cs
Created September 20, 2019 23:41
Interface for Opening Gallery
/// <summary>
/// Code implementation found in Android Project -> MediaService.cs
/// </summary>
public interface IMediaService
{
void OpenGallery();
void ClearFileDirectory();
}
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Last active September 20, 2019 23:41
Upload Images to Azure Blob Storage
private async void UploadImagesButton_Clicked(object sender, EventArgs e)
{
// Get the list of images we have selected.
List<string> imagePaths = ImgCarouselView.ItemsSource as List<string>;
// If user is using Android, compress the images. (Optional)
if (Device.RuntimePlatform == Device.Android)
{
imagePaths = CompressAllImages(imagePaths);
}
@danielkon96
danielkon96 / ImageSelectionPage.xaml.cs
Last active September 20, 2019 23:42
Compress images for Android.
/// <summary>
/// Compress Android images before uploading them to Azure Blob Storage.
/// </summary>
/// <param name="totalImages"></param>
/// <returns></returns>
private List<string> CompressAllImages(List<string> totalImages)
{
int displayCount = 1;
int totalCount = totalImages.Count;
List<string> compressedImages = new List<string>();