This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
262 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<external-path name="external_download" path="Download"/> | ||
</paths> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:coronavirusstatus/helpers/permissions.dart'; | ||
import 'package:coronavirusstatus/helpers/updater.dart'; | ||
import 'package:coronavirusstatus/providers/settings_data.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class SettingsList extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Consumer<SettingsData>( | ||
builder: (context, model, _) => ListView( | ||
children: <Widget>[ | ||
UpdateItem( | ||
version: model.version, | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
class UpdateItem extends StatelessWidget { | ||
final String version; | ||
|
||
const UpdateItem({ | ||
Key key, | ||
this.version, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ListTile( | ||
title: Text('Check For Updates'), | ||
subtitle: Text(this.version), | ||
onTap: () { | ||
Permissions.requestPermission(); | ||
Updater().start(context); | ||
}, | ||
trailing: Icon(Icons.refresh), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:permission_handler/permission_handler.dart'; | ||
|
||
class Permissions { | ||
static void requestPermission() async { | ||
if (!await Permission.storage.isPermanentlyDenied) { | ||
await Permission.storage.request(); | ||
} | ||
} | ||
|
||
static Future<bool> storagePermission() async { | ||
return Permission.storage.isGranted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:coronavirusstatus/helpers/permissions.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:ota_update/ota_update.dart'; | ||
import 'package:package_info/package_info.dart'; | ||
import 'package:device_info/device_info.dart'; | ||
import 'dart:io' show Platform; | ||
import 'package:flutter/material.dart'; | ||
|
||
class Updater { | ||
void start(BuildContext context) async { | ||
if (await Permissions.storagePermission()) { | ||
if (Platform.isAndroid) { | ||
var dat = await _fetchData(); | ||
if (await _check(dat.version)) { | ||
var platform = await _platform(); | ||
if (platform != "") { | ||
Scaffold.of(context).showSnackBar(SnackBar( | ||
content: Text('New Version Available'), | ||
action: SnackBarAction( | ||
label: 'Update', | ||
onPressed: () { | ||
_update(dat.apks[platform]); | ||
}, | ||
), | ||
)); | ||
} | ||
} else { | ||
Scaffold.of(context).showSnackBar(SnackBar( | ||
content: Text('Alread On Newset Version'), | ||
)); | ||
} | ||
} | ||
} else { | ||
Scaffold.of(context).showSnackBar(SnackBar( | ||
content: Text('Could not get storage permissions'), | ||
)); | ||
} | ||
} | ||
|
||
Future<bool> _check(String version) async { | ||
PackageInfo packageInfo = await PackageInfo.fromPlatform(); | ||
return (packageInfo.version.compareTo(version) == -1); | ||
} | ||
|
||
Future<Data> _fetchData() async { | ||
Map<String, String> ans = Map(); | ||
var res = await http.get( | ||
"https://api.github.com/repos/Ayush1325/Coronavirus-Status/releases/latest"); | ||
Map<String, dynamic> body = jsonDecode(res.body); | ||
|
||
String version = body["tag_name"]; | ||
version = version.substring(1); | ||
List<dynamic> assets = body['assets']; | ||
Map<String, String> apks = Map.fromEntries(assets.map((e) { | ||
Map<String, dynamic> item = e; | ||
return MapEntry( | ||
item['name'].toString(), item['browser_download_url'].toString()); | ||
})); | ||
return Data(version, apks); | ||
} | ||
|
||
Future<String> _platform() async { | ||
Map<String, String> abis = { | ||
'x86': 'app-x86_64-release.apk', | ||
'arm64-v8a': 'app-arm64-v8a-release.apk', | ||
'armeabi-v7a': 'app-armeabi-v7a-release.apk', | ||
}; | ||
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); | ||
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; | ||
var temp = androidInfo.supportedAbis; | ||
for (MapEntry e in abis.entries) { | ||
if (temp.contains(e.key)) { | ||
return e.value; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
void _update(String url) { | ||
try { | ||
OtaUpdate().execute(url, destinationFilename: 'covid19.apk').listen( | ||
(OtaEvent event) { | ||
print('EVENT: ${event.status} : ${event.value}'); | ||
}, | ||
); | ||
} catch (e) { | ||
print('Failed to make OTA update. Details: $e'); | ||
} | ||
} | ||
} | ||
|
||
class Data { | ||
final String version; | ||
final Map<String, String> apks; | ||
|
||
Data(this.version, this.apks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:coronavirusstatus/components/nav_drawer.dart'; | ||
import 'package:coronavirusstatus/components/settings_list.dart'; | ||
import 'package:coronavirusstatus/providers/settings_data.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class Settings extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('Settings'), | ||
), | ||
drawer: NavDrawer(), | ||
body: ChangeNotifierProvider( | ||
create: (_) => SettingsData(), | ||
child: SettingsList(), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:package_info/package_info.dart'; | ||
|
||
class SettingsData extends ChangeNotifier { | ||
String version; | ||
|
||
SettingsData() { | ||
_dummyData(); | ||
refresh(); | ||
} | ||
|
||
void _dummyData() { | ||
this.version = ""; | ||
} | ||
|
||
void refresh() async { | ||
this.version = await _appVersion(); | ||
notifyListeners(); | ||
} | ||
|
||
Future<String> _appVersion() async { | ||
PackageInfo packageInfo = await PackageInfo.fromPlatform(); | ||
return packageInfo.version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters