-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-140: Setup and create
PermissionHandlerService
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 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,52 @@ | ||
import 'dart:io'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
|
||
class PermissionHandlerService { | ||
static final PermissionHandlerService _instance = PermissionHandlerService._internal(); | ||
|
||
factory PermissionHandlerService() { | ||
return _instance; | ||
} | ||
|
||
PermissionHandlerService._internal(); | ||
|
||
|
||
Future<PermissionStatus> requestPermissionForPhotoActions() { | ||
if (Platform.isIOS) { | ||
return _handlePhotosPermissionIOSAction(); | ||
} else { | ||
return _handlePhotosPermissionAndroidAction(); | ||
} | ||
} | ||
|
||
Future<PermissionStatus> _handlePhotosPermissionIOSAction() async { | ||
final currentStatus = await Permission.photos.status; | ||
return _handlePermission(currentStatus); | ||
} | ||
|
||
Future<PermissionStatus> _handlePhotosPermissionAndroidAction() async { | ||
final currentStatus = await Permission.storage.status; | ||
return _handlePermission(currentStatus); | ||
} | ||
|
||
|
||
Future<PermissionStatus> _handlePermission(PermissionStatus currentStatus) async { | ||
switch (currentStatus) { | ||
case PermissionStatus.denied: | ||
case PermissionStatus.limited: | ||
final newStatus = Platform.isIOS | ||
? await Permission.mediaLibrary.request() | ||
: await Permission.storage.request(); | ||
return newStatus.isGranted ? PermissionStatus.granted : newStatus; | ||
|
||
case PermissionStatus.granted: | ||
case PermissionStatus.restricted: | ||
case PermissionStatus.permanentlyDenied: | ||
return currentStatus; | ||
} | ||
} | ||
|
||
void goToSettingsForPermissionActions() { | ||
openAppSettings(); | ||
} | ||
} |
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