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 {
final activeId = global.settings!.activeIdentity;
enum TimelineType {
public,
local,
home,
}
Future<List<ThreadModel>> getTimelineFromServer(
String? index,
String identity,
TimelineType timelineType,
) async {
final activeId = identity;
if (global.settings!.identities[activeId] == null) {
return [];
}
@ -112,12 +122,21 @@ Future<List<ThreadModel>> 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,
);

View File

@ -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"
}

View File

@ -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");

View File

@ -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);

View File

@ -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<Timeline>
List<Widget> 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<Timeline>
}
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<Thread> threads = [];
for (int i = 0; i < models.length; i++) {
threads.add(Thread(model: models[i]));
}
@ -60,7 +66,9 @@ class _TimelineState extends State<Timeline>
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<Timeline>
@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<DropdownMenuItem> 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<void> _waitForFetchMore() async {
while (loading) {
await Future.delayed(const Duration(milliseconds: 500));
}
}
@override
bool get wantKeepAlive => true;
}