.
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:nextcloud/core.dart';
|
||||
import 'package:nextcloud/nextcloud.dart';
|
||||
import 'package:nextcloud/provisioning_api.dart';
|
||||
import 'package:nextcloud/theming.dart';
|
||||
import 'package:nextcloud/webdav.dart';
|
||||
import 'package:nextcloudapp/app/extension.dart';
|
||||
import 'package:nextcloudapp/models/details.dart';
|
||||
import 'package:nextcloudapp/models/files.dart';
|
||||
|
||||
import 'package:nextcloudapp/service/navigation_service.dart';
|
||||
import 'package:nextcloudapp/service/snackbar_service.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
enum AuthStatus {
|
||||
notAuthenticated,
|
||||
authenticated,
|
||||
authenticating,
|
||||
userNotFound,
|
||||
error,
|
||||
}
|
||||
|
||||
class NextcloudProvider extends ChangeNotifier {
|
||||
//
|
||||
//
|
||||
|
||||
static NextcloudProvider instance = NextcloudProvider();
|
||||
final NavigationService _navigationService = NavigationService.instance;
|
||||
|
||||
late NextcloudClient client;
|
||||
final SharedPreferencesAsync _sharedPreferences = SharedPreferencesAsync();
|
||||
// SharedPreferences? _sharedPreferences;
|
||||
|
||||
AuthStatus? status;
|
||||
UserDetails? userDetails;
|
||||
|
||||
NextcloudProvider() {
|
||||
// _init();
|
||||
_checkLogin();
|
||||
}
|
||||
|
||||
// void _init() async {
|
||||
// _sharedPreferences = SharedPreferencesAsync();
|
||||
// }
|
||||
|
||||
void _checkLogin() async {
|
||||
await Future.delayed(Duration(milliseconds: 300));
|
||||
try {
|
||||
String? _appPassword = await _sharedPreferences.getString('apppassword');
|
||||
print(_appPassword);
|
||||
String? _uri = await _sharedPreferences.getString('uri');
|
||||
if (_appPassword != null && _uri != null) {
|
||||
client = NextcloudClient(
|
||||
Uri.parse(_uri),
|
||||
appPassword: _appPassword,
|
||||
httpClient: NextcloudClientExtension.newHttpClient(),
|
||||
);
|
||||
|
||||
var _user = await client.provisioningApi.users.getCurrentUser();
|
||||
userDetails = _user.body.ocs.data;
|
||||
|
||||
status = AuthStatus.authenticated;
|
||||
_navigationService.navigateToReplacement("home");
|
||||
} else {
|
||||
status = AuthStatus.notAuthenticated;
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
status = AuthStatus.notAuthenticated;
|
||||
SnackBarService.instance.showSnackBarError("$e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<ServerDetails> envData() async {
|
||||
Map _themeValues = {};
|
||||
|
||||
_themeValues['icon'] = await client.theming.icon.getFavicon();
|
||||
_themeValues['systemtheme'] = await client.theming.theming.getManifest();
|
||||
|
||||
return ServerDetails.fromTheme(_themeValues);
|
||||
}
|
||||
|
||||
Future<void> login(String _uri, String _username, String _password,
|
||||
bool _savePassword) async {
|
||||
notifyListeners();
|
||||
status = AuthStatus.error;
|
||||
try {
|
||||
status = AuthStatus.authenticating;
|
||||
client = NextcloudClient(
|
||||
Uri.parse(_uri),
|
||||
loginName: _username,
|
||||
password: _password,
|
||||
httpClient: NextcloudClientExtension.newHttpClient(),
|
||||
);
|
||||
var _user = await client.provisioningApi.users.getCurrentUser();
|
||||
userDetails = _user.body.ocs.data;
|
||||
client.core.appPassword.getAppPassword().then((_response) async {
|
||||
await _sharedPreferences.setString(
|
||||
'apppassword', _response.body.ocs.data.apppassword);
|
||||
await _sharedPreferences.setString('uri', _uri);
|
||||
await _sharedPreferences.setString('username', _username);
|
||||
if (_savePassword) {
|
||||
await _sharedPreferences.setString('password', _password);
|
||||
}
|
||||
});
|
||||
|
||||
status = AuthStatus.authenticated;
|
||||
SnackBarService.instance.showSnackBarSuccess('successfully logged in');
|
||||
_navigationService.navigateToReplacement("home");
|
||||
} on ClientException catch (e) {
|
||||
SnackBarService.instance.showSnackBarError("$e");
|
||||
status = AuthStatus.error;
|
||||
} on Exception catch (e) {
|
||||
status = AuthStatus.error;
|
||||
SnackBarService.instance.showSnackBarError("$e");
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void logout() async {
|
||||
try {
|
||||
var _resp = await client.core.appPassword.deleteAppPassword();
|
||||
if (_resp.statusCode == 200) {
|
||||
_sharedPreferences.remove('apppassword');
|
||||
SnackBarService.instance.showSnackBarSuccess("succssfully loggedout");
|
||||
_navigationService.navigateToReplacement("login");
|
||||
} else {
|
||||
SnackBarService.instance
|
||||
.showSnackBarError(_resp.body.ocs.meta.message!);
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
SnackBarService.instance.showSnackBarError("$e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Files>> listDirectory(String _cd) async {
|
||||
List<Files> _fileList = [];
|
||||
|
||||
try {
|
||||
WebDavClient _client = client.webdav;
|
||||
var _returnVal = await _client.propfind(
|
||||
PathUri.parse(Uri.decodeFull(_cd)),
|
||||
prop: WebDavPropWithoutValues(),
|
||||
depth: WebDavDepth.one,
|
||||
);
|
||||
|
||||
_fileList = _returnVal.responses.map((_value) {
|
||||
return Files.fromWebDavResponse(_value);
|
||||
}).toList();
|
||||
|
||||
_fileList[0].name = "..";
|
||||
_fileList[0].size = 0;
|
||||
_fileList[0].resourceType = 'folder';
|
||||
List<Files> filteredList = List.from(_fileList)..removeAt(0);
|
||||
filteredList.sort((a, b) => b.resourceType!.compareTo(a.resourceType!));
|
||||
filteredList.insert(0, _fileList[0]);
|
||||
return filteredList;
|
||||
} on Exception catch (e) {
|
||||
print(e.toString());
|
||||
return Future.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getIt() async {
|
||||
Directory? storageDirectory = await getApplicationDocumentsDirectory();
|
||||
String _sdPath = storageDirectory.path;
|
||||
var d = Directory(_sdPath);
|
||||
if (!d.existsSync()) {
|
||||
d.createSync(recursive: true);
|
||||
File _fileTemp = File("$_sdPath/.nomedia");
|
||||
_fileTemp.writeAsString('');
|
||||
}
|
||||
|
||||
// File temp = File('$_sdPath/temp.png');
|
||||
|
||||
// await _client.getFile(
|
||||
// PathUri.parse("/files/${userDetails!.id}/Nextcloud.png"), temp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user