Skip to content

Commit

Permalink
feat: update setting page
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Jul 18, 2024
1 parent 135e225 commit 993dfd5
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 328 deletions.
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (s *Server) Serve() error {

setting := api.Group("/setting")
{
setting.POST("/do", HttpHandler(s.SetSetting))
setting.GET("/do", HttpHandler(s.GetSetting))
setting.POST("/general", HttpHandler(s.SetSetting))
setting.GET("/general", HttpHandler(s.GetSetting))
setting.POST("/auth", HttpHandler(s.EnableAuth))
setting.GET("/auth", HttpHandler(s.GetAuthSetting))
}
Expand Down
37 changes: 22 additions & 15 deletions server/setting.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package server

import (
"polaris/log"
"polaris/db"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)


type setSettingIn struct {
Key string `json:"key"`
Value string `json:"value"`
type GeneralSettings struct {
TmdbApiKey string `json:"tmdb_api_key"`
DownloadDir string `json:"download_dir"`
}
func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
var in setSettingIn
var in GeneralSettings
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind json")
}
err := s.db.SetSetting(in.Key, in.Value)
return nil, err
if in.TmdbApiKey != "" {
if err := s.db.SetSetting(db.SettingTmdbApiKey, in.TmdbApiKey); err != nil {
return nil, errors.Wrap(err, "save tmdb api")
}
}
if in.DownloadDir == "" {
if err := s.db.SetSetting(db.SettingDownloadDir, in.DownloadDir); err != nil {
return nil, errors.Wrap(err, "save download dir")
}
}
return nil, nil
}

func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
q := c.Query("key")
log.Infof("query key: %v", q)
if q == "" {
return nil, nil
}
v := s.db.GetSetting(q)
log.Infof("get value for key %v: %v", q, v)
return gin.H{q: v}, nil
tmdb := s.db.GetSetting(db.SettingTmdbApiKey)
downloadDir := s.db.GetSetting(db.SettingDownloadDir)
return &GeneralSettings{
TmdbApiKey: tmdb,
DownloadDir: downloadDir,
}, nil
}
2 changes: 1 addition & 1 deletion ui/lib/movie_watchlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class _MovieDetailsPageState extends ConsumerState<MovieDetailsPage> {
DataCell(Text("${torrent.size?.readableFileSize()}")),
DataCell(Text("${torrent.seeders}")),
DataCell(Text("${torrent.peers}")),
DataCell(IconButton.filledTonal(
DataCell(IconButton(
icon: const Icon(Icons.download),
onPressed: () async {
await ref
Expand Down
1 change: 1 addition & 0 deletions ui/lib/providers/APIs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class APIs {
static final _baseUrl = baseUrl();
static final searchUrl = "$_baseUrl/api/v1/media/search";
static final settingsUrl = "$_baseUrl/api/v1/setting/do";
static final settingsGeneralUrl = "$_baseUrl/api/v1/setting/general";
static final watchlistTvUrl = "$_baseUrl/api/v1/media/tv/watchlist";
static final watchlistMovieUrl = "$_baseUrl/api/v1/media/movie/watchlist";
static final availableMoviesUrl = "$_baseUrl/api/v1/media/movie/resources/";
Expand Down
74 changes: 43 additions & 31 deletions ui/lib/providers/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,64 @@ import 'package:quiver/strings.dart';
import 'package:ui/providers/APIs.dart';
import 'package:ui/providers/server_response.dart';

var settingProvider =
AsyncNotifierProvider.autoDispose.family<EditSettingData, String, String>(
EditSettingData.new);
var settingProvider = AsyncNotifierProvider.autoDispose
<EditSettingData, GeneralSetting>(EditSettingData.new);

var indexersProvider =
AsyncNotifierProvider.autoDispose<IndexerSetting, List<Indexer>>(IndexerSetting.new);
AsyncNotifierProvider.autoDispose<IndexerSetting, List<Indexer>>(
IndexerSetting.new);

var dwonloadClientsProvider =
AsyncNotifierProvider.autoDispose<DownloadClientSetting, List<DownloadClient>>(
DownloadClientSetting.new);
var dwonloadClientsProvider = AsyncNotifierProvider.autoDispose<
DownloadClientSetting, List<DownloadClient>>(DownloadClientSetting.new);

var storageSettingProvider =
AsyncNotifierProvider.autoDispose<StorageSettingData, List<Storage>>(
StorageSettingData.new);

class EditSettingData extends AutoDisposeFamilyAsyncNotifier<String, String> {
String? key;

class EditSettingData extends AutoDisposeAsyncNotifier<GeneralSetting> {
@override
FutureOr<String> build(String arg) async {
FutureOr<GeneralSetting> build() async {
final dio = await APIs.getDio();
key = arg;
var resp = await dio.get(APIs.settingsUrl, queryParameters: {"key": arg});

var resp = await dio.get(APIs.settingsGeneralUrl);
var rrr = ServerResponse.fromJson(resp.data);
if (rrr.code != 0) {
throw rrr.message;
}
var data = rrr.data as Map<String, dynamic>;
var value = data[arg] as String;

return value;
final ss = GeneralSetting.fromJson(rrr.data);
return ss;
}

Future<void> updateSettings(String v) async {
Future<void> updateSettings(GeneralSetting gs) async {
final dio = await APIs.getDio();
var resp = await dio.post(APIs.settingsUrl, data: {
"key": key,
"value": v,
});
var sp = ServerResponse.fromJson(resp.data as Map<String, dynamic>);
var resp = await dio.post(APIs.settingsGeneralUrl, data: gs.toJson());
var sp = ServerResponse.fromJson(resp.data);
if (sp.code != 0) {
throw sp.message;
}
ref.invalidateSelf();
}
}

class GeneralSetting {
String? tmdbApiKey;
String? downloadDIr;

GeneralSetting({this.tmdbApiKey, this.downloadDIr});

factory GeneralSetting.fromJson(Map<String, dynamic> json) {
return GeneralSetting(
tmdbApiKey: json["tmdb_api_key"], downloadDIr: json["download_dir"]);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['tmdb_api_key'] = tmdbApiKey;
data['download_dir'] = downloadDIr;
return data;
}
}

class IndexerSetting extends AutoDisposeAsyncNotifier<List<Indexer>> {
@override
FutureOr<List<Indexer>> build() async {
Expand Down Expand Up @@ -121,7 +132,8 @@ class Indexer {
}
}

class DownloadClientSetting extends AutoDisposeAsyncNotifier<List<DownloadClient>> {
class DownloadClientSetting
extends AutoDisposeAsyncNotifier<List<DownloadClient>> {
@override
FutureOr<List<DownloadClient>> build() async {
final dio = await APIs.getDio();
Expand Down Expand Up @@ -247,7 +259,8 @@ class StorageSettingData extends AutoDisposeAsyncNotifier<List<Storage>> {
}

class Storage {
Storage({this.id, this.name, this.implementation, this.settings, this.isDefault});
Storage(
{this.id, this.name, this.implementation, this.settings, this.isDefault});

final int? id;
final String? name;
Expand All @@ -257,12 +270,11 @@ class Storage {

factory Storage.fromJson(Map<String, dynamic> json1) {
return Storage(
id: json1["id"],
name: json1["name"],
implementation: json1["implementation"],
settings: json.decode(json1["settings"]),
isDefault: json1["default"]
);
id: json1["id"],
name: json1["name"],
implementation: json1["implementation"],
settings: json.decode(json1["settings"]),
isDefault: json1["default"]);
}

Map<String, dynamic> toJson() => {
Expand Down
Loading

0 comments on commit 993dfd5

Please sign in to comment.