switch between accounts!!! wow!!!

This commit is contained in:
zoe 2022-08-14 18:44:06 +02:00
parent be7293367d
commit 1dfb4ab6f9
5 changed files with 155 additions and 22 deletions

View File

@ -100,8 +100,18 @@ class ThreadModel {
} }
} }
Future<List<ThreadModel>> getTimelineFromServer(String? index) async { enum TimelineType {
final activeId = global.settings!.activeIdentity; public,
local,
home,
}
Future<List<ThreadModel>> getTimelineFromServer(
String? index,
String identity,
TimelineType timelineType,
) async {
final activeId = identity;
if (global.settings!.identities[activeId] == null) { if (global.settings!.identities[activeId] == null) {
return []; return [];
} }
@ -112,12 +122,21 @@ Future<List<ThreadModel>> getTimelineFromServer(String? index) async {
if (index != null) { if (index != null) {
query.addAll({"max_id": index}); 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 baseUrl = global.settings!.identities[activeId]!.instanceUrl;
final url = Uri( final url = Uri(
scheme: "https", scheme: "https",
host: baseUrl, host: baseUrl,
path: "/api/v1/timelines/home", path: "/api/v1/timelines/$tlType",
queryParameters: query, queryParameters: query,
); );

View File

@ -22,6 +22,9 @@
"reblogged-by": "reblogged by:", "reblogged-by": "reblogged by:",
"post-batch-size": "post batch size", "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)", "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"
} }

View File

@ -7,7 +7,6 @@ import 'business_logic/settings.dart' as settings;
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'themes/themes.dart' as themes; import 'themes/themes.dart' as themes;
import 'global.dart' as global; import 'global.dart' as global;
import 'business_logic/auth/oauth.dart' as oauth;
ThemeData theme = themes.getTheme(themes.available[0]); ThemeData theme = themes.getTheme(themes.available[0]);
Locale activeLocale = const Locale("en"); Locale activeLocale = const Locale("en");

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:localization/localization.dart'; import 'package:localization/localization.dart';
import '../business_logic/auth/oauth.dart' as oauth; import '../business_logic/auth/oauth.dart' as oauth;
import '../global.dart' as global;
class Login extends StatefulWidget { class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key); const Login({Key? key}) : super(key: key);

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:localization/localization.dart'; import 'package:localization/localization.dart';
import 'package:loris/partials/thread.dart'; import 'package:loris/partials/thread.dart';
import '../../business_logic/timeline/timeline.dart' as tl; import '../../business_logic/timeline/timeline.dart' as tl;
import '../../global.dart' as global;
class Timeline extends StatefulWidget { class Timeline extends StatefulWidget {
const Timeline({Key? key}) : super(key: key); const Timeline({Key? key}) : super(key: key);
@ -16,6 +17,9 @@ class _TimelineState extends State<Timeline>
List<Widget> children = []; List<Widget> children = [];
bool loading = false; bool loading = false;
tl.TimelineType selectedTimelineType = tl.TimelineType.home;
String selectedId = global.settings!.activeIdentity;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -36,13 +40,15 @@ class _TimelineState extends State<Timeline>
} }
loading = true; loading = true;
final models = await tl.getTimelineFromServer(oldestId); final models = await tl.getTimelineFromServer(
oldestId, selectedId, selectedTimelineType);
setState(() { setState(() {
children.removeWhere((element) { children.removeWhere((element) {
return element.runtimeType != Thread; return element.runtimeType != Thread;
}); });
List<Thread> threads = []; List<Thread> threads = [];
for (int i = 0; i < models.length; i++) { for (int i = 0; i < models.length; i++) {
threads.add(Thread(model: models[i])); threads.add(Thread(model: models[i]));
} }
@ -60,7 +66,9 @@ class _TimelineState extends State<Timeline>
fetchMore(); fetchMore();
}, },
icon: const Icon(Icons.more_horiz), icon: const Icon(Icons.more_horiz),
label: Text("load-more".i18n()), label: Text(
"load-more".i18n(),
),
) )
], ],
), ),
@ -79,23 +87,128 @@ class _TimelineState extends State<Timeline>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context); super.build(context);
return ListView.separated( List<DropdownMenuItem> identities = [];
physics: const AlwaysScrollableScrollPhysics(), // add identities to dropdown menu
controller: controller, for (int i = 0; i < global.settings!.identities.keys.length; i++) {
itemBuilder: (context, index) { identities.add(DropdownMenuItem(
return children[index]; value: global.settings!.identities.keys.toList()[i],
}, child: Text(global.settings!.identities.keys.toList()[i]),
separatorBuilder: (context, index) { ));
return const Divider( }
color: Colors.transparent, return Column(
); children: [
}, Container(
itemCount: children.length, color: Theme.of(context).colorScheme.surface,
padding: const EdgeInsets.fromLTRB(24, 0, 24, 64), child: Row(
addAutomaticKeepAlives: false, 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<void> _waitForFetchMore() async {
while (loading) {
await Future.delayed(const Duration(milliseconds: 500));
}
}
@override @override
bool get wantKeepAlive => true; bool get wantKeepAlive => true;
} }