mirror of
https://github.com/go-vikunja/app
synced 2024-06-01 02:06:51 +00:00
changed how client is implemented to avoid confusion with client being null
This commit is contained in:
parent
bfe599f573
commit
fcbdb694b7
|
@ -4,24 +4,29 @@ import 'dart:core';
|
|||
import 'package:http/http.dart' as http;
|
||||
import 'package:vikunja_app/api/response.dart';
|
||||
import 'package:vikunja_app/components/string_extension.dart';
|
||||
import 'package:vikunja_app/global.dart';
|
||||
|
||||
class Client {
|
||||
VikunjaGlobalState global;
|
||||
final JsonDecoder _decoder = new JsonDecoder();
|
||||
final JsonEncoder _encoder = new JsonEncoder();
|
||||
final String _token;
|
||||
final String _base;
|
||||
String _token;
|
||||
String _base;
|
||||
bool authenticated;
|
||||
|
||||
String get base => _base;
|
||||
String get token => _token;
|
||||
|
||||
Client(this._token, String base, {this.authenticated = true})
|
||||
: _base = base.endsWith('/api/v1') ? base : '$base/api/v1';
|
||||
//Client(this._token, String base, {this.authenticated = true})
|
||||
// : _base = base.endsWith('/api/v1') ? base : '$base/api/v1';
|
||||
|
||||
bool operator ==(dynamic otherClient) {
|
||||
return otherClient._token == _token;
|
||||
}
|
||||
|
||||
Client(this.global, {String token, String base, bool authenticated = false})
|
||||
{ configure(token: token, base: base, authenticated: authenticated);}
|
||||
|
||||
@override
|
||||
int get hashCode => _token.hashCode;
|
||||
|
||||
|
@ -30,6 +35,20 @@ class Client {
|
|||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
void configure({String token, String base, bool authenticated}) {
|
||||
if(token != null)
|
||||
_token = token;
|
||||
if(base != null)
|
||||
_base = base.endsWith('/api/v1') ? base : '$base/api/v1';
|
||||
if(authenticated != null)
|
||||
this.authenticated = authenticated;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_token = _base = null;
|
||||
authenticated = false;
|
||||
}
|
||||
|
||||
Future<Response> get(String url,
|
||||
[Map<String, List<String>> queryParameters]) {
|
||||
// TODO: This could be moved to a seperate function
|
||||
|
|
|
@ -10,6 +10,7 @@ import 'package:vikunja_app/api/label_task_bulk.dart';
|
|||
import 'package:vikunja_app/api/labels.dart';
|
||||
import 'package:vikunja_app/api/list_implementation.dart';
|
||||
import 'package:vikunja_app/api/namespace_implementation.dart';
|
||||
import 'package:vikunja_app/api/server_implementation.dart';
|
||||
import 'package:vikunja_app/api/task_implementation.dart';
|
||||
import 'package:vikunja_app/api/user_implementation.dart';
|
||||
import 'package:vikunja_app/managers/notifications.dart';
|
||||
|
@ -41,9 +42,10 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
final FlutterSecureStorage _storage = new FlutterSecureStorage();
|
||||
|
||||
User _currentUser;
|
||||
Client _client;
|
||||
bool _loading = true;
|
||||
bool expired = false;
|
||||
Client _client;
|
||||
UserService _newUserService;
|
||||
|
||||
|
||||
User get currentUser => _currentUser;
|
||||
|
@ -52,7 +54,9 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
|
||||
UserManager get userManager => new UserManager(_storage);
|
||||
|
||||
UserService newUserService(base) => new UserAPIService(Client(null, base));
|
||||
UserService get newUserService => _newUserService;
|
||||
|
||||
ServerService get serverService => new ServerAPIService(client);
|
||||
|
||||
NamespaceService get namespaceService => new NamespaceAPIService(client);
|
||||
|
||||
|
@ -100,6 +104,8 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_client = Client(this);
|
||||
_newUserService = UserAPIService(client);
|
||||
_loadCurrentUser();
|
||||
tz.initializeTimeZones();
|
||||
iOSSpecifics = notifs.IOSNotificationDetails();
|
||||
|
@ -128,9 +134,9 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
}
|
||||
// Set current user in storage
|
||||
await _storage.write(key: 'currentUser', value: newUser.id.toString());
|
||||
client.configure(token: token, base: base, authenticated: true);
|
||||
setState(() {
|
||||
_currentUser = newUser;
|
||||
_client = Client(token, base);
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
|
@ -172,7 +178,7 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
_storage.deleteAll().then((_) {
|
||||
Navigator.pop(context);
|
||||
setState(() {
|
||||
_client = null;
|
||||
client.reset();
|
||||
_currentUser = null;
|
||||
});
|
||||
}).catchError((err) {
|
||||
|
@ -198,20 +204,20 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
});
|
||||
return;
|
||||
}
|
||||
_client = Client(token, base);
|
||||
client.configure(token: token, base: base);
|
||||
var loadedCurrentUser;
|
||||
try {
|
||||
loadedCurrentUser = await UserAPIService(client).getCurrentUser();
|
||||
} on ApiException catch (e) {
|
||||
dev.log("Error code: " + e.errorCode.toString(),level: 1000);
|
||||
if (e.errorCode ~/ 100 == 4) {
|
||||
_client.authenticated = false;
|
||||
client.authenticated = false;
|
||||
if (e.errorCode == 401) {
|
||||
// token has expired, but we can reuse username and base. user just has to enter password again
|
||||
expired = true;
|
||||
}
|
||||
setState(() {
|
||||
_client.authenticated = false;
|
||||
client.authenticated = false;
|
||||
_currentUser = null;
|
||||
_loading = false;
|
||||
});
|
||||
|
@ -222,7 +228,6 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
|
|||
loadedCurrentUser = User(int.tryParse(currentUser), "", "");
|
||||
}
|
||||
setState(() {
|
||||
_client = client;
|
||||
_currentUser = loadedCurrentUser;
|
||||
_loading = false;
|
||||
});
|
||||
|
|
|
@ -9,6 +9,8 @@ import 'package:vikunja_app/models/namespace.dart';
|
|||
import 'package:vikunja_app/models/task.dart';
|
||||
import 'package:vikunja_app/models/user.dart';
|
||||
|
||||
import '../models/server.dart';
|
||||
|
||||
enum TaskServiceOptionSortBy {id, title, description, done, done_at, due_date, created_by_id, list_id, repeat_after, priority, start_date, end_date, hex_color, percent_done, uid, created, updated}
|
||||
enum TaskServiceOptionOrderBy {asc,desc}
|
||||
enum TaskServiceOptionFilterBy {done, due_date, reminder_dates}
|
||||
|
@ -111,7 +113,7 @@ abstract class TaskService {
|
|||
}
|
||||
|
||||
abstract class UserService {
|
||||
Future<UserTokenPair> login(String username, password, {bool rememberMe = false});
|
||||
Future<UserTokenPair> login(String username, password, {bool rememberMe = false, String totp});
|
||||
Future<UserTokenPair> register(String username, email, password);
|
||||
Future<User> getCurrentUser();
|
||||
}
|
||||
|
@ -133,3 +135,7 @@ abstract class LabelTaskService {
|
|||
abstract class LabelTaskBulkService {
|
||||
Future<List<Label>> update(Task task, List<Label> labels);
|
||||
}
|
||||
|
||||
abstract class ServerService {
|
||||
Future<Server> getInfo();
|
||||
}
|
Loading…
Reference in New Issue
Block a user