Compare commits

...

1 Commits

  1. 32
      lib/api.dart
  2. 1
      lib/main.dart
  3. 27
      lib/models.dart
  4. 23
      lib/models.g.dart
  5. 2
      lib/viewmodels.dart

32
lib/api.dart

@ -5,15 +5,17 @@ import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:path/path.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'models.dart';
class Api {
final http.Client _httpClient;
final FlutterSecureStorage _flutterSecureStorage;
final SharedPreferences _sharedPreferences;
final String tokenKey = 'provile-v2';
Api(this._httpClient, this._flutterSecureStorage);
Api(this._httpClient, this._flutterSecureStorage, this._sharedPreferences);
Future<AppUser?> get user async {
String? json = await _flutterSecureStorage.read(key: tokenKey);
@ -49,6 +51,11 @@ class Api {
final profileResponse =
await getProfile(username, token: authResponse.token, podURI: podURI);
final configResponse = await getConfig(podURI);
this._sharedPreferences.setString("pod_name", configResponse.name);
this._sharedPreferences.setString("pod_url", podURI.toString());
final user = AppUser(
profile: profileResponse.profile,
twter: profileResponse.twter,
@ -224,6 +231,29 @@ class Api {
return jsonDecode(response.body)['Path'];
}
Future<ConfigResponse> getConfig(Uri podURI) async {
http.Response response;
response = await _httpClient.get(
podURI.replace(path: "/api/v1/config"),
headers: {
HttpHeaders.contentTypeHeader: ContentType.json.toString(),
},
);
if (response.statusCode >= 400) {
throw http.ClientException(
'Failed fetch profile. Please try again later',
);
}
return ConfigResponse.fromJson(
jsonDecode(
utf8.decode(response.bodyBytes),
),
);
}
Future<ProfileResponse> getProfile(String? name,
{String? token, Uri? podURI}) async {
http.Response response;

1
lib/main.dart

@ -40,6 +40,7 @@ class MyApp extends StatelessWidget {
final api = Api(
UserAgentClient(http.Client(), this.packageInfo),
FlutterSecureStorage(),
sharedPreferences,
);
return MultiProvider(

27
lib/models.dart

@ -204,6 +204,33 @@ class PostRequest {
Map<String, dynamic> toJson() => _$PostRequestToJson(this);
}
@JsonSerializable()
class ConfigResponse {
final String name;
final String logo;
final String description;
@JsonKey(name: 'max_twt_length')
final int maxTwtLength;
@JsonKey(name: 'open_profiles')
final bool openProfiles;
@JsonKey(name: 'open_registrations')
final bool openRegistrations;
@JsonKey(name: 'whitelisted_domains')
final List<String> whitelistedDomains;
ConfigResponse(this.name, this.logo, this.description, this.maxTwtLength,
this.openProfiles, this.openRegistrations, this.whitelistedDomains);
factory ConfigResponse.fromJson(Map<String, dynamic> json) =>
_$ConfigResponseFromJson(json);
Map<String, dynamic> toJson() => _$ConfigResponseToJson(this);
}
@JsonSerializable()
class ProfileResponse {
final Profile? profile;

23
lib/models.g.dart

@ -140,6 +140,29 @@ Map<String, dynamic> _$PostRequestToJson(PostRequest instance) =>
'text': instance.text,
};
ConfigResponse _$ConfigResponseFromJson(Map<String, dynamic> json) {
return ConfigResponse(
json["name"] as String,
json["logo"] as String,
json["description"] as String,
json["max_twt_length"] as int,
json["open_profiles"] as bool,
json["open_registrations"] as bool,
json["whitelisted_domains"] as List<String>,
);
}
Map<String, dynamic> _$ConfigResponseToJson(ConfigResponse instance) =>
<String, dynamic>{
'name': instance.name,
'logo': instance.logo,
'description': instance.description,
'max_twt_length': instance.maxTwtLength,
'open_profiles': instance.openProfiles,
'open_registrations': instance.openRegistrations,
'whitedlisted_domains': instance.whitelistedDomains,
};
ProfileResponse _$ProfileResponseFromJson(Map<String, dynamic> json) {
return ProfileResponse(
Profile.fromJson(json['profile'] as Map<String, dynamic>),

2
lib/viewmodels.dart

@ -360,8 +360,6 @@ class ThemeViewModel extends ChangeNotifier {
ThemeMode? _themeMode;
ThemeViewModel(this._sharedPreferences) {
assert(this._sharedPreferences != null);
_themeMode = ThemeMode.values[
(_sharedPreferences.getInt(ThemeViewModel.ThemeModeKey) ??
0)]; // Uses ThemeMode.system by default

Loading…
Cancel
Save