From 1dfb4ab6f952a2f558b926e62a0eecf1053a1466 Mon Sep 17 00:00:00 2001 From: zoe Date: Sun, 14 Aug 2022 18:44:06 +0200 Subject: [PATCH] switch between accounts!!! wow!!! --- lib/business_logic/timeline/timeline.dart | 25 +++- lib/i18n/en.json | 5 +- lib/main.dart | 1 - lib/pages/login.dart | 1 - lib/pages/timeline/timeline.dart | 145 +++++++++++++++++++--- 5 files changed, 155 insertions(+), 22 deletions(-) diff --git a/lib/business_logic/timeline/timeline.dart b/lib/business_logic/timeline/timeline.dart index 315f23b..34fcda8 100644 --- a/lib/business_logic/timeline/timeline.dart +++ b/lib/business_logic/timeline/timeline.dart @@ -100,8 +100,18 @@ class ThreadModel { } } -Future> getTimelineFromServer(String? index) async { - final activeId = global.settings!.activeIdentity; +enum TimelineType { + public, + local, + home, +} + +Future> getTimelineFromServer( + String? index, + String identity, + TimelineType timelineType, +) async { + final activeId = identity; if (global.settings!.identities[activeId] == null) { return []; } @@ -112,12 +122,21 @@ Future> getTimelineFromServer(String? index) async { if (index != null) { query.addAll({"max_id": index}); } + if (timelineType == TimelineType.local) { + query.addAll({"local": "true"}); + } + + String tlType = "home"; + if (timelineType == TimelineType.public || + timelineType == TimelineType.local) { + tlType = "public"; + } final baseUrl = global.settings!.identities[activeId]!.instanceUrl; final url = Uri( scheme: "https", host: baseUrl, - path: "/api/v1/timelines/home", + path: "/api/v1/timelines/$tlType", queryParameters: query, ); diff --git a/lib/i18n/en.json b/lib/i18n/en.json index c5e958a..d2cd478 100644 --- a/lib/i18n/en.json +++ b/lib/i18n/en.json @@ -22,6 +22,9 @@ "reblogged-by": "reblogged by:", "post-batch-size": "post batch size", "post-batch-size-description": "how many posts to load at a time\nfor slower internet or if you're not sure what to do use a low value (the default in most places is 20)", - "app-settings": "app settings" + "app-settings": "app settings", + "local-timeline": "local", + "home-timeline": "home", + "public-timeline": "federated" } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 21af6f2..d7087e9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,7 +7,6 @@ import 'business_logic/settings.dart' as settings; import 'package:flutter_localizations/flutter_localizations.dart'; import 'themes/themes.dart' as themes; import 'global.dart' as global; -import 'business_logic/auth/oauth.dart' as oauth; ThemeData theme = themes.getTheme(themes.available[0]); Locale activeLocale = const Locale("en"); diff --git a/lib/pages/login.dart b/lib/pages/login.dart index facbcdc..fae7228 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:localization/localization.dart'; import '../business_logic/auth/oauth.dart' as oauth; -import '../global.dart' as global; class Login extends StatefulWidget { const Login({Key? key}) : super(key: key); diff --git a/lib/pages/timeline/timeline.dart b/lib/pages/timeline/timeline.dart index 30dca8e..d278911 100644 --- a/lib/pages/timeline/timeline.dart +++ b/lib/pages/timeline/timeline.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:localization/localization.dart'; import 'package:loris/partials/thread.dart'; import '../../business_logic/timeline/timeline.dart' as tl; +import '../../global.dart' as global; class Timeline extends StatefulWidget { const Timeline({Key? key}) : super(key: key); @@ -16,6 +17,9 @@ class _TimelineState extends State List children = []; bool loading = false; + tl.TimelineType selectedTimelineType = tl.TimelineType.home; + String selectedId = global.settings!.activeIdentity; + @override void initState() { super.initState(); @@ -36,13 +40,15 @@ class _TimelineState extends State } loading = true; - final models = await tl.getTimelineFromServer(oldestId); + final models = await tl.getTimelineFromServer( + oldestId, selectedId, selectedTimelineType); setState(() { children.removeWhere((element) { return element.runtimeType != Thread; }); List threads = []; + for (int i = 0; i < models.length; i++) { threads.add(Thread(model: models[i])); } @@ -60,7 +66,9 @@ class _TimelineState extends State fetchMore(); }, icon: const Icon(Icons.more_horiz), - label: Text("load-more".i18n()), + label: Text( + "load-more".i18n(), + ), ) ], ), @@ -79,23 +87,128 @@ class _TimelineState extends State @override Widget build(BuildContext context) { super.build(context); - return ListView.separated( - physics: const AlwaysScrollableScrollPhysics(), - controller: controller, - itemBuilder: (context, index) { - return children[index]; - }, - separatorBuilder: (context, index) { - return const Divider( - color: Colors.transparent, - ); - }, - itemCount: children.length, - padding: const EdgeInsets.fromLTRB(24, 0, 24, 64), - addAutomaticKeepAlives: false, + List identities = []; + // add identities to dropdown menu + for (int i = 0; i < global.settings!.identities.keys.length; i++) { + identities.add(DropdownMenuItem( + value: global.settings!.identities.keys.toList()[i], + child: Text(global.settings!.identities.keys.toList()[i]), + )); + } + return Column( + children: [ + Container( + color: Theme.of(context).colorScheme.surface, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + IconButton( + onPressed: () { + reload(); + }, + icon: const Icon(Icons.refresh), + ), + DropdownButton( + value: selectedId, + items: identities, + onChanged: (dynamic value) { + setState(() { + selectedId = value ?? global.settings!.activeIdentity; + reload(); + }); + }, + ), + DropdownButton( + value: selectedTimelineType, + items: [ + DropdownMenuItem( + value: tl.TimelineType.home, + child: RichText( + text: TextSpan( + text: "${"home-timeline".i18n()} ", + children: const [ + WidgetSpan( + child: Icon(Icons.home), + ), + ], + ), + ), + ), + DropdownMenuItem( + value: tl.TimelineType.local, + child: RichText( + text: TextSpan( + text: "${"local-timeline".i18n()} ", + children: const [ + WidgetSpan( + child: Icon(Icons.people), + ) + ], + ), + ), + ), + DropdownMenuItem( + value: tl.TimelineType.public, + child: RichText( + text: TextSpan( + text: "${"public-timeline".i18n()} ", + children: const [ + WidgetSpan( + child: Icon(Icons.public), + ), + ], + ), + ), + ), + ], + onChanged: (tl.TimelineType? value) { + setState(() { + selectedTimelineType = value ?? tl.TimelineType.home; + reload(); + }); + }, + ) + ], + ), + ), + Expanded( + child: ListView.separated( + physics: const AlwaysScrollableScrollPhysics(), + controller: controller, + itemBuilder: (context, index) { + return children[index]; + }, + separatorBuilder: (context, index) { + return const Divider( + color: Colors.transparent, + ); + }, + itemCount: children.length, + padding: const EdgeInsets.fromLTRB(24, 0, 24, 64), + addAutomaticKeepAlives: false, + ), + ), + ], ); } + void reload() async { + setState(() { + children = [const LoadingBox()]; + oldestId = null; + }); + await _waitForFetchMore(); + setState(() { + fetchMore(); + }); + } + + Future _waitForFetchMore() async { + while (loading) { + await Future.delayed(const Duration(milliseconds: 500)); + } + } + @override bool get wantKeepAlive => true; }