Lottie animation as loading indicator- Xamarin.forms - xamarin.forms

I have a xamarin forms app which contains a pie graph using microcharts. The data for the graph is obtained from Web API. Everything works fine.Every time graph loads data from web API, there is some delay.So the appearance of graph will also takes time. What I am trying to do is use Lottie animation as the loading indicator before the graph appears.The animation shows but it gets flickered and load the graph.
What I have done
My xaml
<Grid>
<forms1:AnimationView
x:Name="AnimationView"
Animation="graphloading.json"
AutoPlay="True"
Margin="10"
BackgroundColor="Transparent"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
<forms:ChartView x:Name="Chart1" isVisible="False"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</Grid>
My Xaml.cs file
protected override async void OnAppearing()
{
await LoadDynamicGraph();
}
private async Task LoadDynamicGraph()
{
await Task.Run(() =>
{
// Graph Loading API call code
if (GraphDataObj[0] != null)
{
Device.BeginInvokeOnMainThread(() =>
{
AnimationView.IsPlaying = false;
AnimationView.IsVisible = false;
Chart1.IsVisible = true;
foreach (var item in GraphDataObj[0].DailyScore)
{
FirstGraphData = new List<ChartEntry>
{
new ChartEntry(float.Parse(item.CompletedItems))
{
Label = "Completed",
// ValueLabel = item.CompletedItems,
Color = SKColor.Parse("#c90484"),
TextColor = SKColor.Parse("#FFFFFF"),
},
new ChartEntry(float.Parse(item.TotalOpenItems))
{
Label = "Total ",
// ValueLabel = item.TotalOpenItems,
Color = SKColor.Parse("#00a8f3"),
TextColor = SKColor.Parse("#FFFFFF"),
}
};
}
Chart1.Chart = new PieChart()
{
Entries = FirstGraphData,
BackgroundColor = SKColor.Parse("#002F4F4F")
};
});
}
});
}
Currently the animation simply just appears and go. I want to make it show some time and then load the graph. Any help is appreciated.

Set this
AnimationView.IsPlaying = false;
AnimationView.IsVisible = false;
Chart1.IsVisible = true;
on the end of your function. It will still be pretty fast. U can try to set timer once.
finish = true;
Device.StartTimer(TimeSpan.FromSeconds(5), () =>
{
If(finish){
finish = false;
return true;}
//there turn off your animation
return false;
});

Related

Xamarin Forms: DisplayAlert background tap firing the cancel event

I am using a DisplayAlert like below in my project.
var answer = await DisplayAlert("Alert", "You are invited to join a group, would you like to accept it or not?", "Accept", "Reject");
if (answer)
{
//accept invitation
}
else
{
//reject invitation
}
Accept and Reject options are working fine. My problem is Reject option is executing when tapping on the background or device back arrow. Is it has a simple solution other than implementing Rg.Plugins.Popup?
I had a similar request once, and I quickly "solved it" with a workaround like this (using DisplayActionSheet):
bool isActionSelected = false;
while(!isActionSelected)
{
string action = await DisplayActionSheet ("You are invited to join a group, would you like to accept it or not?", null, null, "Accept", "Reject");
if (action == "Accept")
{
isActionSelected = true;
//do stuff
}
else if (action == "Reject")
{
isActionSelected = true;
//do stuff
}
else
{
isActionSelected = false;
}
}
This is not suggested, unless you are in a hurry.
So, I would suggest you creating a custom popupView, something like this
<ContentView x:Name="popupView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">
<Label x:Name="myLabel" TextColor="Black" Text="You are invited to join a group, would you like to accept it or not?" />
<Button Text="Accept" TextTransform="None" Clicked="AcceptClicked" />
<Button Text="Reject" TextTransform="None" Clicked="RejectClicked" />
</StackLayout>
</StackLayout>
</ContentView>
then in .cs
popupView.IsVisible = true;
when you want this to appear.
The only solution - Use DependencyService to implement the alert on each platform .
Interface in Forms
public interface IShowAlertService
{
Task<bool> ShowAlert(string title, string message, string ok, string cancel);
}
Android Implementation
[assembly: Dependency(typeof(ShowAlertService))]
namespace XamarinTableView.Droid
{
class ShowAlertService : IShowAlertService
{
TaskCompletionSource<bool> taskCompletionSource;
public Task<bool> ShowAlert(string title, string message, string ok, string cancel)
{
taskCompletionSource = new TaskCompletionSource<bool>();
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.Instance);
AlertDialog alert = dialog.Create();
alert.SetTitle("Title");
alert.SetMessage("Complex Alert");
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
Console.WriteLine("Okay was clicked");
taskCompletionSource.SetResult(true);
});
alert.SetButton2("CANCEL", (c, ev) => {
Console.WriteLine("Cancel was clicked");
taskCompletionSource.SetResult(false);
});
alert.Show();
return taskCompletionSource.Task;
}
}
}
iOS Implementation
[assembly: Dependency(typeof(ShowAlertService))]
namespace XamarinTableView.iOS
{
class ShowAlertService : IShowAlertService
{
TaskCompletionSource<bool> taskCompletionSource;
public Task<bool> ShowAlert(string title, string message, string ok, string cancel)
{
taskCompletionSource = new TaskCompletionSource<bool>();
var okCancelAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
//Add Actions
okCancelAlertController.AddAction(UIAlertAction.Create(ok, UIAlertActionStyle.Default, alert => {
Console.WriteLine("Okay was clicked");
taskCompletionSource.SetResult(true);
}));
okCancelAlertController.AddAction(UIAlertAction.Create(cancel, UIAlertActionStyle.Cancel, alert => {
Console.WriteLine("Cancel was clicked");
taskCompletionSource.SetResult(false);
}));
UIWindow window = UIApplication.SharedApplication.KeyWindow;
var viewController = window.RootViewController;
//Present Alert
viewController.PresentViewController(okCancelAlertController, true, null);
return taskCompletionSource.Task;
}
}
Consume in Forms
bool isOk = await DependencyService.Get<IShowAlertService>().ShowAlert("Alert", "You have been alerted", "OK", "Cancel");
if (isOk)
{
}
else
{
}
In this way clicking outside the alert will not trigger cancel event .
Refer to DisplayAlert Xamarim forms.

Flutter async method slowdown app performance

I'm a newbie in flutter and I can't understand what I might be doing wrong. In my App there is an option for the user to change their avatar's image. He can choose an image from the gallery or take a photo. The new avatar image is saved in the _avatarImage field, and within the setState method the _newImage field is set to true, like this:
Future getNewAvatarImage() async {
Image _image = .... // Take a photo or a image from Gallery
// ...
_avatarImage = _image;
setState(
() => _newImage = true;
);
}
In one part of the code I have the compressAndUpload method which when called compresses an image and sends it to the remote server. This method is asynchronous and is called within the build method whenever the _newImage field is true. Like this
#override
Widget build(BuildContext context) {
if (_newImage) {
_newImage = false;
compressAndUpload(_avatarImage);
}
return Container(
//...
child: _avatarImage,
//
);
The problem is that the new avatar image will take a long time to appear if the compressAndUpload method is called. If this method is commented out the new avatar image comes up quickly.
if (_newImage) {
_newImage = false;
// New image show quickly
// compressAndUpload(_avatarImage);
}
***********
if (_newImage) {
_newImage = false;
// Image takes too long to appear
compressAndUpload(_avatarImage);
}
Where is the problem? The compressAndUpload method is asynchronous and so should not cause delay for the new image to be displayed:
Future<void> compressAndUpload(var image) async {
// Compress image
// upload image
}
UPDATE:
For further clarification I show the complete code of the compressAndUpload method:
Future<void> compressAndUpload(var image) async {
var imageBytes = imagem.readAsBytesSync();
saveImageToPreferences(base64String(imageBytes));
var tempDir = await getTemporaryDirectory();
var path = tempDir.path;
// Reduce size
img.Image image = img.decodeImage(imageBytes);
img.Image smallerImage = img.copyResize(image, width: 1000);
File compressedFileImage =
File('$path\${Explika.getAluno().id}.jpg')
..writeAsBytesSync(img.encodeJpg(smallerImage, quality: 50));
String _urlsegment = Explika.producaoFlag ?
'https://www.remoteserver.pt' : 'http://10.0.2.2';
var stream = http.ByteStream(DelegatingStream.typed(
compressedFileImage.openRead()));
var length = await compressedFileImage.length();
var uri = Uri.parse('$_urlsegment/explika/api/upload');
var request = http.MultipartRequest("POST", uri);
var multipartFile = http.MultipartFile('fotoaluno', stream, length,
filename: '${Explika.getAluno().id}.jpg');
request.files.add(multipartFile);
var response;
try {
response = await request.send();
} catch (e) {
// mostrar falha de rede
//_uploadingImagem = false;
print(e);
return;
}
//Get the response from the server
var responseData = await response.stream.toBytes();
var responseString = String.fromCharCodes(responseData);
print(responseString);
}
Thanks so much for all the comments.
The problem was solved when I found that in the pickImage method it was possible to set the maximum width and the maximum height of the image being selected. Thus, the compression step of the selected image was not required.

How to initialize bar code scanner on App startup

I have a requirement for my xamarin cross platform application that as soon as app start up .QR Scanner set in to read the code. on completing scanning a beep will be ring up.and app again ready for next scanning how can i get this done. what i have done is on button click scanner start, its read code, then i have to press button again to start it again.
public HomePage()
{
Button scanBtn = new Button
{
Text = "Scan Barcode",
HorizontalOptions = LayoutOptions.FillAndExpand,
};
scanBtn.Clicked += async (sender, args) =>
{
var scanResult = await Acr.BarCodes.BarCodes.Instance.Read();
if (!scanResult.Success)
{
await this.DisplayAlert("Alert ! ", "Sorry ! \n Failed to read the Barcode !", "OK");
}
else
{
var endpoint = new EndpointAddress("http://192.168.15.33/SMS/WebServices/SMSService.svc");
var binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
_client = new SMSServiceClient(binding, endpoint);
_client.ValidateStudentAsync("123-admin");
_client.ValidateStudentCompleted += _client_ValidateStudentCompleted; ;
// await this.DisplayAlert("Scan Successful !", String.Format("Barcode Format : {0} \n Barcode Value : {1}", scanResult.Format, scanResult.Code), "OK");
}
};
Content = new StackLayout
{
Children = {
scanBtn
}
};
}
and in app.cs
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new HomePage();
}
protected override void OnStart()
{
MainPage = new HomePage();
}
protected override void OnSleep()
{
MainPage = new HomePage();
}
protected override void OnResume()
{
MainPage = new HomePage();
}
}
You can use ZXing.Net.Mobile for Forms to read QR codes. To initialize this plugin you should call method to init into each project (Android, iOS, UWP) like this:
For Android in MainActivity.cs class call:
ZXing.Net.Mobile.Forms.Droid.Platform.Init();
For iOS in AppDeletage.cs class call
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
And finally to read QR Codes:
private async void Scan() {
var scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) => {
// Stop scanning
scanPage.IsScanning = false;
// Pop the page and show the result
Device.BeginInvokeOnMainThread( async () => {
await Navigation.PopAsync();
await DisplayAlert("Scanned Barcode", result.Text, "OK");
});
};
// Navigate to our scanner page
await Navigation.PushAsync(scanPage);
}

Problems Setting FlipView Items source using Data Virtualization

This is a follow up from my previous question (UWP Problems Setting GridView Items source using x:Bind).
In that case I was using the random access file data source from Mirosoft's Data virtualization sample and the grid was never being populated. The problem there was that I was not raising the property changed event. The grid now works well.
My problem now is that instead of a grid view I am trying to use the data source in a flip view control. The data source does get initialized, the change property notification is raised but nothing is shown in the flip view.
Can a flip view display data from a virtual collection?
Here is my model's code:
public class PhotoFlipViewViewModel : Mvvm.ViewModelBase
{
private FileDataSource _PicturesCollection;
public FileDataSource PicturesCollection
{
get
{
return _PicturesCollection;
}
set
{
if (_PicturesCollection != value)
{
_PicturesCollection = value;
RaisePropertyChanged(() => PicturesCollection);
}
}
}
public PhotoFlipViewViewModel()
{
initdata();
}
async void initdata()
{
StorageLibrary pictures = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
string path = pictures.SaveFolder.Path;
var source = await FileDataSource.GetDataSoure(path);
if (source.Count > 0)
{
PicturesCollection = source;
}
}
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
System.Diagnostics.Debug.WriteLine("PhotoFlipViewViewModel::OnNavigatedToAsync - event fired.");
if ( SessionState.ContainsKey("SelectedPhotoObjectFromGrid"))
{
await Task.CompletedTask;
}
public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
{
if (suspending)
{
}
await Task.CompletedTask;
}
public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = false;
await Task.CompletedTask;
}
}
My FlipView in XAML:
<FlipView x:Name="PhotoOuterFlipView"
BorderBrush="Black"
BorderThickness="1"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
ItemsSource="{x:Bind ViewModel.PicturesCollection, Mode=OneWay}"
ItemTemplate="{StaticResource PictureFlipViewTemplate}">
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
</FlipView>
Thanks, any advice is appreciated.

Windows Metro Style app data binding

I have a requirement to load images from a folder in the project to a stackpanel. Under each image a name should also be shown. The image folder can change at any time and the number of images can also change.(with a maximum of 50 images) I want to know if I can use data binding to handle this. I thought of having image ID's, their sources and the name for each image in an XML so that I can change that XML file whenever the image folder changes, without changing the rest of the code. Is that feasible? If so how? Can someone please guide me? Thank you in advance.
One solution would be to use a Filepicker to let the user select the images inside the folder, and then bind the selected images to an Itemscontrol. That itemscontrol can then be put inside the Stackpanel. Here's a quick sample using that solution.
Here's the codebehind for picking the image files:
private List<EditableImage> availableImagesList = new List<EditableImage>();
private async void FilePicker_Clicked(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
//TODO: add supported image file types
openPicker.FileTypeFilter.Add("jpg,png,gif");
// We prompt the user to pick one or more files
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
if (files.Count > 0)
{
availableImages.DataContext = null;
String fp = ""; // The path of the picked image
int index = availableImagesList.Count;
foreach (StorageFile file in files)
{
// Copying the selected image to local app data folder
//TODO: check if the selected file is actually and image
if (file != null )
{
StorageFile fileCopy = await file.CopyAsync(ApplicationData.Current.LocalFolder, file.DisplayName + file.FileType, NameCollisionOption.ReplaceExisting);
fp = fileCopy.Path;
}
//Creating the image
CustomImage picToAdd = new CustomImage(index+1, file.DisplayName, fp);
//Adding the image as an UI element to the app bar
availableImagesList.Add(picToAdd);
}
availableImages.DataContext = availableImagesList;
}
}
The CustomImage model:
public class CustomImage
{
private static Uri _baseUri = new Uri("ms-appx:///");
private int _id;
public int Id
{
get { return _id; }
set
{
this.SetProperty(ref this._id, value);
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
this.SetProperty(ref this._name, value);
}
}
private string _imgPath;
public string ImgPath
{
get { return _imgPath; }
set
{
this.SetProperty(ref this._imgPath, value);
}
}
private String _imagePath = null;
private ImageSource _image = null;
public ImageSource Image
{
get
{
if (this._image == null && this._imagePath != null)
{
this._image = new BitmapImage(new Uri(CustomImage._baseUri, this._imagePath));
}
return this._image;
}
set
{
this._imagePath = null;
this.SetProperty(ref this._image, value);
}
}
public void SetImage(String path)
{
this._image = null;
this._imagePath = path;
this.OnPropertyChanged("Image");
}
public CustomImage(int id, string name, string imagepath)
{
SetImage(imagepath);
_id = id;
_name = name;
}
}
Here's the XAML for the ItemsControl inside the Stackpanel:
<StackPanel x:Name="loadedImages" HorizontalAlignment="Left" Orientation="Horizontal">
<!--Displaying the selected images in stackpanel-->
<ItemsControl ItemsSource="{Binding}" ItemsPanel="{StaticResource LoadedItemsPanel}">
<ItemsControl.ItemTemplate>
<!--The template for each object that is displayed as an UI element-->
<DataTemplate>
<Grid Height="88" Margin="2,0" Width="88" >
<Image Source="{Binding Image}"/>
<TextBlock Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
In your page resources, you must also define:
<ItemsPanelTemplate x:Key="LoadedItemsPanel">
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>

Resources