make some models
This commit is contained in:
parent
3f068f246e
commit
822325c0c7
|
@ -1,13 +1,77 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import '../settings.dart' as settings;
|
import '../settings.dart' as settings;
|
||||||
import '../../global.dart' as global;
|
import '../../global.dart' as global;
|
||||||
|
|
||||||
class Timeline {}
|
class TimelinePartModel {
|
||||||
|
late List<ThreadModel> threads;
|
||||||
|
late int minId;
|
||||||
|
late int maxId;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Visibility { public, unlisted, private, direct }
|
||||||
|
|
||||||
|
class PostModel {
|
||||||
|
late String id;
|
||||||
|
late String uri;
|
||||||
|
late String content;
|
||||||
|
late Visibility visibility;
|
||||||
|
late bool sensitive;
|
||||||
|
late String spoilerText;
|
||||||
|
late bool favourited;
|
||||||
|
late bool reblogged;
|
||||||
|
|
||||||
|
PostModel.fromJson(Map<String, dynamic> json) {
|
||||||
|
id = json["id"] as String;
|
||||||
|
uri = json["uri"] as String;
|
||||||
|
content = json["content"] as String;
|
||||||
|
visibility = Visibility.values.firstWhere(
|
||||||
|
// ignore: prefer_interpolation_to_compose_strings
|
||||||
|
(element) => element.toString() == "Visibility." + json["visibility"]);
|
||||||
|
sensitive = json["sensitive"] as bool;
|
||||||
|
spoilerText = json["spoiler_text"] as String;
|
||||||
|
favourited = json["favourited"] as bool;
|
||||||
|
reblogged = json["reblogged"] as bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ThreadModel> getThread() async {
|
||||||
|
final token = await settings.loadToken();
|
||||||
|
final baseUrl = await settings.loadInstanceUrl();
|
||||||
|
Map<String, String> headers = {"Authorization": "Bearer $token"};
|
||||||
|
headers.addAll(global.defaultHeaders);
|
||||||
|
|
||||||
|
final url = Uri(
|
||||||
|
scheme: "https",
|
||||||
|
host: baseUrl,
|
||||||
|
path: "/api/v1/statuses/$id/context",
|
||||||
|
);
|
||||||
|
final response = await http.get(url, headers: headers);
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
return ThreadModel([this]);
|
||||||
|
}
|
||||||
|
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
final List<dynamic> ancestorsJson = json["ancestors"];
|
||||||
|
|
||||||
|
List<PostModel> posts = [this];
|
||||||
|
int i = 0;
|
||||||
|
while (i < ancestorsJson.length) {
|
||||||
|
posts.add(PostModel.fromJson(ancestorsJson[i]));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return ThreadModel(posts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThreadModel {
|
||||||
|
late List<PostModel> posts;
|
||||||
|
ThreadModel(List<PostModel> allPosts) {
|
||||||
|
posts = allPosts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<http.Response> getTimelineFromServer() async {
|
Future<http.Response> getTimelineFromServer() async {
|
||||||
const limit = 4;
|
int limit = 1;
|
||||||
final token = await settings.loadToken();
|
final token = await settings.loadToken();
|
||||||
final baseUrl = await settings.loadInstanceUrl();
|
final baseUrl = await settings.loadInstanceUrl();
|
||||||
final url = Uri(
|
final url = Uri(
|
||||||
|
@ -20,12 +84,19 @@ Future<http.Response> getTimelineFromServer() async {
|
||||||
headers.addAll(global.defaultHeaders);
|
headers.addAll(global.defaultHeaders);
|
||||||
|
|
||||||
final response = await http.get(url, headers: headers);
|
final response = await http.get(url, headers: headers);
|
||||||
final json = jsonDecode(response.body);
|
final List<dynamic> json = jsonDecode(response.body);
|
||||||
|
|
||||||
|
List<String> ids = [];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
if (limit > json.length) {
|
||||||
|
limit = json.length;
|
||||||
|
}
|
||||||
while (i < limit) {
|
while (i < limit) {
|
||||||
print(json[i]["id"]);
|
ids.add(json[i]["id"]);
|
||||||
i ++;
|
final post = PostModel.fromJson(json[i]);
|
||||||
|
final thread = await post.getThread();
|
||||||
|
print(thread.posts[0].content);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
|
|
@ -9,7 +9,7 @@ import 'themes/themes.dart' as themes;
|
||||||
import 'global.dart' as global;
|
import 'global.dart' as global;
|
||||||
import 'business_logic/auth/oauth.dart' as oauth;
|
import 'business_logic/auth/oauth.dart' as oauth;
|
||||||
|
|
||||||
String _initRoute = "/";
|
String _initRoute = "/login";
|
||||||
ThemeData theme = themes.getTheme(themes.available[0]);
|
ThemeData theme = themes.getTheme(themes.available[0]);
|
||||||
Locale activeLocale = const Locale("en");
|
Locale activeLocale = const Locale("en");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue