Base url

This commit is contained in:
LittleSheep 2024-11-10 22:14:27 +08:00
parent ac70624c4e
commit 49cabd1f39
5 changed files with 135 additions and 12 deletions

View File

@ -98,5 +98,8 @@
"settingsNetworkServer": "HyperNet Server", "settingsNetworkServer": "HyperNet Server",
"settingsNetworkServerDescription": "Set the HyperNet server address, choose ours or build your own.", "settingsNetworkServerDescription": "Set the HyperNet server address, choose ours or build your own.",
"settingsNetworkServerReset": "Reset to Official Server", "settingsNetworkServerReset": "Reset to Official Server",
"settingsNetworkServerResetDescription": "Reset to the official server address of Solar Network." "settingsNetworkServerResetDescription": "Reset to the official server address of Solar Network.",
"settingsNetworkServerPreset": "Present HyperNet Server",
"settingsNetworkServerPresetDescription": "You can choose one of our preset HyperNet server addresses from the list on the right.",
"settingsNetworkServerSaved": "Server address saved."
} }

View File

@ -98,5 +98,8 @@
"settingsNetworkServer": "HyperNet 服务器", "settingsNetworkServer": "HyperNet 服务器",
"settingsNetworkServerDescription": "设置 HyperNet 服务器地址,选择我们提供的,或者自己搭建。", "settingsNetworkServerDescription": "设置 HyperNet 服务器地址,选择我们提供的,或者自己搭建。",
"settingsNetworkServerReset": "重设为官方服务器", "settingsNetworkServerReset": "重设为官方服务器",
"settingsNetworkServerResetDescription": "重设为 Solar Network 的服务器地址。" "settingsNetworkServerResetDescription": "重设为 Solar Network 的服务器地址。",
"settingsNetworkServerPreset": "预设的 HyperNet 服务器",
"settingsNetworkServerPresetDescription": "你可以在旁边的列表中选择我们提供的预设 HyperNet 服务器地址。",
"settingsNetworkServerSaved": "服务器地址已保存。"
} }

View File

@ -4,25 +4,30 @@ import 'dart:developer';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:dio_smart_retry/dio_smart_retry.dart'; import 'package:dio_smart_retry/dio_smart_retry.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/adapters/sn_network_universal.dart'; import 'package:surface/providers/adapters/sn_network_universal.dart';
const kUseLocalNetwork = true;
const kAtkStoreKey = 'nex_user_atk'; const kAtkStoreKey = 'nex_user_atk';
const kRtkStoreKey = 'nex_user_rtk'; const kRtkStoreKey = 'nex_user_rtk';
const kNetworkServerDefault = 'https://api.sn-next.solsynth.dev';
const kNetworkServerStoreKey = 'app_server_url';
const kNetworkServerDirectory = [
('SN Preview', 'https://api.sn-next.solsynth.dev'),
('SN Stable', 'https://api.sn.solsynth.dev'),
('Local', 'http://localhost:8001'),
];
class SnNetworkProvider { class SnNetworkProvider {
late Dio client; late Dio client;
late final SharedPreferences _prefs;
late final FlutterSecureStorage _storage = FlutterSecureStorage(); late final FlutterSecureStorage _storage = FlutterSecureStorage();
SnNetworkProvider() { SnNetworkProvider() {
client = Dio(); client = Dio();
client.options.baseUrl = kUseLocalNetwork
? 'http://localhost:8001'
: 'https://api.sn.solsynth.dev';
client.interceptors.add(RetryInterceptor( client.interceptors.add(RetryInterceptor(
dio: client, dio: client,
retries: 3, retries: 3,
@ -86,6 +91,12 @@ class SnNetworkProvider {
); );
client = addClientAdapter(client); client = addClientAdapter(client);
SharedPreferences.getInstance().then((prefs) {
_prefs = prefs;
client.options.baseUrl =
_prefs.getString(kNetworkServerStoreKey) ?? kNetworkServerDefault;
});
} }
String getAttachmentUrl(String ky) { String getAttachmentUrl(String ky) {
@ -112,9 +123,7 @@ class SnNetworkProvider {
if (rtk == null) return null; if (rtk == null) return null;
final dio = Dio(); final dio = Dio();
dio.options.baseUrl = kUseLocalNetwork dio.options.baseUrl = client.options.baseUrl;
? 'http://localhost:8001'
: 'https://api.sn.solsynth.dev';
final resp = await dio.post('/cgi/id/auth/token', data: { final resp = await dio.post('/cgi/id/auth/token', data: {
'grant_type': 'refresh_token', 'grant_type': 'refresh_token',
@ -127,4 +136,8 @@ class SnNetworkProvider {
return atk; return atk;
} }
void setBaseUrl(String url) {
client.options.baseUrl = url;
}
} }

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -10,8 +11,10 @@ import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:styled_widget/styled_widget.dart'; import 'package:styled_widget/styled_widget.dart';
import 'package:surface/providers/sn_network.dart';
import 'package:surface/providers/theme.dart'; import 'package:surface/providers/theme.dart';
import 'package:surface/theme.dart'; import 'package:surface/theme.dart';
import 'package:surface/widgets/dialog.dart';
import 'package:surface/widgets/navigation/app_scaffold.dart'; import 'package:surface/widgets/navigation/app_scaffold.dart';
class SettingsScreen extends StatefulWidget { class SettingsScreen extends StatefulWidget {
@ -25,6 +28,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
SharedPreferences? _prefs; SharedPreferences? _prefs;
String _docBasepath = '/'; String _docBasepath = '/';
final TextEditingController _serverUrlController = TextEditingController();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -37,12 +42,22 @@ class _SettingsScreenState extends State<SettingsScreen> {
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
setState(() { setState(() {
_prefs = prefs; _prefs = prefs;
_serverUrlController.text =
prefs.getString(kNetworkServerStoreKey) ?? kNetworkServerDefault;
}); });
}); });
} }
@override
void dispose() {
_serverUrlController.dispose();
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final sn = context.read<SnNetworkProvider>();
return AppScaffold( return AppScaffold(
body: SingleChildScrollView( body: SingleChildScrollView(
child: Column( child: Column(
@ -127,6 +142,95 @@ class _SettingsScreenState extends State<SettingsScreen> {
.fontSize(17) .fontSize(17)
.tr() .tr()
.padding(horizontal: 20), .padding(horizontal: 20),
TextField(
controller: _serverUrlController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'settingsNetworkServer'.tr(),
helperText: 'settingsNetworkServerDescription'.tr(),
prefixIcon: const Icon(Symbols.dns),
suffixIcon: IconButton(
icon: const Icon(Symbols.save),
onPressed: () {
sn.setBaseUrl(_serverUrlController.text);
_prefs?.setString(
kNetworkServerStoreKey,
_serverUrlController.text,
);
context.showSnackbar('settingsNetworkServerSaved'.tr());
setState(() {});
},
),
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
).padding(horizontal: 16, top: 8, bottom: 4),
ListTile(
title: Text('settingsNetworkServerPreset').tr(),
subtitle: Text('settingsNetworkServerPresetDescription').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.lists),
trailing: DropdownButtonHideUnderline(
child: DropdownButton2<String>(
isExpanded: true,
items: [
...kNetworkServerDirectory,
if (!kNetworkServerDirectory
.map((ele) => ele.$2)
.contains(_serverUrlController.text))
('Custom', _serverUrlController.text),
]
.map(
(item) => DropdownMenuItem<String>(
value: item.$2,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(item.$1).fontSize(14),
Text(item.$2, overflow: TextOverflow.ellipsis)
.fontSize(11)
],
),
),
)
.toList(),
value: _serverUrlController.text,
onChanged: (String? value) {
if (value == null) return;
_serverUrlController.text = value;
_prefs?.setString(kNetworkServerStoreKey, value);
context.showSnackbar('settingsNetworkServerSaved'.tr());
setState(() {});
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.symmetric(
horizontal: 16,
vertical: 5,
),
height: 40,
width: 140,
),
menuItemStyleData: const MenuItemStyleData(
height: 60,
),
),
),
),
ListTile(
title: Text('settingsNetworkServerReset').tr(),
subtitle: Text('settingsNetworkServerResetDescription').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.reset_wrench),
trailing: const Icon(Symbols.chevron_right),
onTap: () {
_serverUrlController.text = kNetworkServerDefault;
_prefs?.remove(kNetworkServerStoreKey);
context.showSnackbar('settingsNetworkServerSaved'.tr());
setState(() {});
},
),
], ],
), ),
].expand((ele) => [ele, const Gap(16)]).toList(), ].expand((ele) => [ele, const Gap(16)]).toList(),

View File

@ -34,7 +34,7 @@ class PostItem extends StatelessWidget {
class _PostBottomAction extends StatelessWidget { class _PostBottomAction extends StatelessWidget {
final SnPost data; final SnPost data;
const _PostBottomAction({super.key, required this.data}); const _PostBottomAction({required this.data});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {