Skip to content

Commit

Permalink
yeah baby! v2 finally
Browse files Browse the repository at this point in the history
i'm so damn happy
  • Loading branch information
cabiste-dev committed Feb 21, 2022
1 parent e8ab676 commit 690725f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
using TunisiaPrayer.Services;
using Xamarin.Forms;

[assembly: Xamarin.Forms.Dependency(typeof(TunisiaPrayer.Droid.PathService))]
[assembly: Xamarin.Forms.Dependency(typeof(TunisiaPrayer.Droid.DeviceInfoService))]
namespace TunisiaPrayer.Droid
{
public class PathService : IPathService
public class DeviceInfoService : IDeviceInfoService
{
private Context context = Android.App.Application.Context;
public string PublicExternalFolder
Expand All @@ -31,6 +31,15 @@ public string PublicExternalFolder
}
}

public string Architecture
{
get
{
return Android.OS.Build.SupportedAbis[0];

}
}

//public async void install(string filePath)
//{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="PackageInstaller.cs" />
<Compile Include="PathService.cs" />
<Compile Include="DeviceInfoService.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

namespace TunisiaPrayer.Services
{
public interface IPathService
public interface IDeviceInfoService
{
//string InternalFolder { get; }
string PublicExternalFolder { get; }
string Architecture { get; }
//void install(string filePath);
//string PrivateExternalFolder { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="TunisiaPrayer.Views.UpdatesPage">
x:Class="TunisiaPrayer.Views.UpdatesPage"
BackgroundColor="#272c34">

<ContentPage.Content>
<Grid RowDefinitions="*,2*,10*,2*,*" ColumnDefinitions="*,4*,*">
<Button Text="refresh" Clicked="RefreshButton_Clicked" Grid.Row="1" Grid.Column="1"/>
<Label x:Name="rel" Text="" Grid.Row="2" Grid.Column="1" TextColor="Black"/>
<ScrollView Grid.Row="2" Grid.Column="1">

<Label Text="{Binding IsDownloading}" Grid.Row="2" Grid.Column="1" TextColor="Black"/>

<ContentPage.Content>
<Grid RowDefinitions="*,2*,*,10*,2*,*" ColumnDefinitions="*,4*,*">
<!--<Button Text="refresh" Clicked="RefreshButton_Clicked" Grid.Row="1" Grid.Column="1"/>-->
<Label x:Name="rel" Text="" Grid.Row="2" Grid.Column="1" TextColor="White" HorizontalTextAlignment="Center" FontSize="Large"/>
<ScrollView Grid.Row="3" Grid.Column="1">
<Label Text="{Binding LatestReleaseInfo}" TextColor="white"/>
</ScrollView>
<Button Text="Update" IsEnabled="{Binding UpdateAvailable}" Grid.Row="3" Grid.Column="1" Clicked="Button_Clicked"/>


<Button Text="{Binding UpdateButton}" IsEnabled="{Binding UpdateAvailable}" Grid.Row="4" Grid.Column="1" Clicked="UpdateButton_Clicked"/>
<!--<Label x:Name="versionDiff" Grid.Row="3" Grid.Column="1"/>-->
</Grid>
</ContentPage.Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Octokit;
using Xamarin.Forms;
Expand All @@ -17,63 +15,90 @@ namespace TunisiaPrayer.Views
public partial class UpdatesPage : ContentPage
{
public bool UpdateAvailable { get; set; } = false;
public string IsDownloading { get; set; }
IPathService pathService;
public string LatestReleaseInfo { get; set; }
public string UpdateButton { get; set; } = "Update";
IDeviceInfoService deviceService;
IPackageInstaller packageInstaller;
private string fileUri;
private string _fileUri;
private string _newVersionTag;
public UpdatesPage()
{
InitializeComponent();
BindingContext = this;
pathService = DependencyService.Get<IPathService>();
deviceService = DependencyService.Get<IDeviceInfoService>();
packageInstaller = DependencyService.Get<IPackageInstaller>();
}

private async void RefreshButton_Clicked(object sender, EventArgs e)

protected override async void OnAppearing()
{
await CheckForUpdates();
}

private async Task CheckForUpdates()
{
var client = new GitHubClient(new ProductHeaderValue("tunisiaPrayerApp"));
//yes that's the repo's id, it's public
var releases = await client.Repository.Release.GetAll(451233074);

for (int i = 0; i < releases.Count; i++)
if (Connectivity.NetworkAccess != NetworkAccess.Internet)
{
rel.Text = releases[i].Name;
await this.DisplayToastAsync("no internet connection :(", 5000);
return;
}

//releases[0].TagName != VersionTracking.CurrentVersion
if (true)
var client = new GitHubClient(new ProductHeaderValue("tunisiaPrayerApp"));
//yes that's the repo's id, it's public
var releases = await client.Repository.Release.GetAll(451233074);
rel.Text = releases[0].Name;

if (releases[0].TagName != VersionTracking.CurrentVersion)
{
UpdateAvailable = true;
_newVersionTag = releases[0].TagName;
OnPropertyChanged(nameof(UpdateAvailable));
//versionDiff.Text = $"current is {AppInfo.VersionString}, new is {releases[0].TagName}";
ReleaseChanges(releases[0].Body);
}
else
{
rel.Text = "No updates Available";
}
}

private async void Button_Clicked(object sender, EventArgs e)
void ReleaseChanges(string release)
{
if (await StoragePermissionDenied())
LatestReleaseInfo = release;
OnPropertyChanged(nameof(LatestReleaseInfo));
}

private async void UpdateButton_Clicked(object sender, EventArgs e)
{

if (Connectivity.NetworkAccess != NetworkAccess.Internet)
{
await this.DisplayToastAsync("no internet connection :(", 5000);
return;
}

//rel.Text += "\n" + pathService.PublicExternalFolder;
IsDownloading = "downloading";
OnPropertyChanged(nameof(IsDownloading));
Uri url = new Uri($"https://github.com/quran/quran_android/releases/download/v3.1.2/quran-3.1.2.apk");

GetFileUri();
if (await StoragePermissionDenied())
{
return;
}
string appName = $"TunisiaPrayer-{deviceService.Architecture}-v{_newVersionTag}.apk";
Uri url = new Uri($"https://github.com/cabiste69/TunisiaPrayer/releases/download/{_newVersionTag}/{appName}");

GetFileUri(appName);
UpdateButton = "Downloading...";
UpdateAvailable = false;
OnPropertyChanged(nameof(UpdateAvailable));
OnPropertyChanged(nameof(UpdateButton));
WebClient myWebClient = new WebClient();
myWebClient.DownloadFileAsync(url, fileUri);
IsDownloading = "completed downloading";
OnPropertyChanged(nameof(IsDownloading));
myWebClient.DownloadFileAsync(url, _fileUri);
myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(InstallUpdate);
//InstallUpdate(null, null);
}

private void InstallUpdate(object sender, AsyncCompletedEventArgs e)
{
packageInstaller.InstallApk(fileUri);
UpdateButton = "Installing...";
OnPropertyChanged(nameof(UpdateButton));
packageInstaller.InstallApk(_fileUri);
}

//quite confusing but it's correct
Expand All @@ -93,9 +118,9 @@ private async Task<bool> StoragePermissionDenied()
return permissionWrite != PermissionStatus.Granted && permissionRead != PermissionStatus.Granted;

}
private void GetFileUri()
private void GetFileUri(string appName)
{
fileUri = pathService.PublicExternalFolder + "/test.apk";
_fileUri = $"{deviceService.PublicExternalFolder}/{appName}";
}

}
Expand Down

0 comments on commit 690725f

Please sign in to comment.