From 822325c0c7c709f9c09f92d684a419b4c5b0f307 Mon Sep 17 00:00:00 2001 From: zoe Date: Sat, 9 Jul 2022 15:24:19 +0200 Subject: [PATCH] make some models --- lib/business_logic/timeline/timeline.dart | 83 +++++++++++++++++++++-- lib/main.dart | 2 +- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/lib/business_logic/timeline/timeline.dart b/lib/business_logic/timeline/timeline.dart index bb9864d..5dd95f4 100644 --- a/lib/business_logic/timeline/timeline.dart +++ b/lib/business_logic/timeline/timeline.dart @@ -1,13 +1,77 @@ import 'dart:convert'; - import 'package:http/http.dart' as http; import '../settings.dart' as settings; import '../../global.dart' as global; -class Timeline {} +class TimelinePartModel { + late List 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 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 getThread() async { + final token = await settings.loadToken(); + final baseUrl = await settings.loadInstanceUrl(); + Map 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 ancestorsJson = json["ancestors"]; + + List posts = [this]; + int i = 0; + while (i < ancestorsJson.length) { + posts.add(PostModel.fromJson(ancestorsJson[i])); + i++; + } + return ThreadModel(posts); + } +} + +class ThreadModel { + late List posts; + ThreadModel(List allPosts) { + posts = allPosts; + } +} Future getTimelineFromServer() async { - const limit = 4; + int limit = 1; final token = await settings.loadToken(); final baseUrl = await settings.loadInstanceUrl(); final url = Uri( @@ -20,12 +84,19 @@ Future getTimelineFromServer() async { headers.addAll(global.defaultHeaders); final response = await http.get(url, headers: headers); - final json = jsonDecode(response.body); + final List json = jsonDecode(response.body); + List ids = []; int i = 0; + if (limit > json.length) { + limit = json.length; + } while (i < limit) { - print(json[i]["id"]); - i ++; + ids.add(json[i]["id"]); + final post = PostModel.fromJson(json[i]); + final thread = await post.getThread(); + print(thread.posts[0].content); + i++; } return response; diff --git a/lib/main.dart b/lib/main.dart index c47bc83..938fef6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,7 +9,7 @@ import 'themes/themes.dart' as themes; import 'global.dart' as global; import 'business_logic/auth/oauth.dart' as oauth; -String _initRoute = "/"; +String _initRoute = "/login"; ThemeData theme = themes.getTheme(themes.available[0]); Locale activeLocale = const Locale("en");