Skip to content

Commit

Permalink
feat: Update Ansible Semaphore Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
GSMLG-BOT committed Oct 4, 2023
1 parent 0b018d7 commit 0a4d226
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Run ansible task from your phone.

Download from

- [AppStore]()
- [AppStore](https://apps.apple.com/cn/app/ansible-semaphore-client/id6458789575?l=en-GB)
- [PlayStore](https://play.google.com/store/apps/details?id=org.gsmlg.semaphore)
- [F-Droid]()

Expand Down
6 changes: 3 additions & 3 deletions lib/components/access_key/form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:ansible_semaphore/ansible_semaphore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:material_neumorphic/material_neumorphic.dart';
import 'package:semaphore/adaptive/button.dart';
import 'package:semaphore/adaptive/dropdown.dart';
import 'package:semaphore/adaptive/text_field.dart';
Expand Down Expand Up @@ -197,9 +196,10 @@ class AccessKeyForm extends ConsumerWidget {
.read(accessKeyFormRequestProvider(
accessKey)
.notifier)
.postAccessKey();
if (context.mounted)
.postAccessKey(accessKey.id);
if (context.mounted) {
Navigator.of(context).pop();
}
ref
.read(accessKeyListProvider.notifier)
.loadRows();
Expand Down
11 changes: 5 additions & 6 deletions lib/screens/project/template_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:material_neumorphic/material_neumorphic.dart';
import 'package:pluto_grid/pluto_grid.dart';
import 'package:semaphore/adaptive/floatingAction.dart';
import 'package:semaphore/adaptive/icon.dart';
Expand All @@ -16,18 +15,18 @@ class TemplateScreen extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
// final theme = Theme.of(context);

// final currentProject = ref.watch(currentProjectProvider);
final templateList = ref.watch(templateListProvider);

return AdaptiveScaffold(
drawer: const LocalDrawer(),
appBar: const LocalAppBar(title: 'Task Template'),
floatingAction: AdaptiveFloatingAction(
icon: const AdaptiveIcon(Icons.add),
onPressed: () {},
),
// floatingAction: AdaptiveFloatingAction(
// icon: const AdaptiveIcon(Icons.add),
// onPressed: () {},
// ),
body: SafeArea(
child: PlutoGrid(
mode: PlutoGridMode.readOnly,
Expand Down
31 changes: 22 additions & 9 deletions lib/screens/setting/setting_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:semaphore/adaptive/dialog.dart';
import 'package:semaphore/adaptive/icon.dart';
import 'package:semaphore/adaptive/text.dart';
import 'package:semaphore/components/project/form.dart';
import 'package:semaphore/state/auth.dart';
import 'package:semaphore/state/projects.dart';

class ProjectSetting extends ConsumerWidget {
Expand All @@ -23,6 +24,8 @@ class ProjectSetting extends ConsumerWidget {
final projects = ref.watch(projectsProvider);
final currentProject = ref.watch(currentProjectProvider);

final currentUser = ref.watch(currentUserProvider);

return SizedBox.expand(
child: Padding(
padding: const EdgeInsets.all(24.0),
Expand All @@ -35,15 +38,25 @@ class ProjectSetting extends ConsumerWidget {
runSpacing: 16.0,
children: [
const AdaptiveTextTitle('Switch Projects'),
AdaptiveButton(
color: primaryColor,
child: const Row(mainAxisSize: MainAxisSize.min, children: [
Icon(Icons.add, color: Colors.white),
Text('Add', style: TextStyle(color: Colors.white)),
]),
onPressed: () {
adaptiveDialog(context: context, child: const ProjectForm());
},
currentUser.when(
data: (user) => user?.admin == true
? AdaptiveButton(
color: primaryColor,
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.add, color: Colors.white),
Text('Add',
style: TextStyle(color: Colors.white)),
]),
onPressed: () {
adaptiveDialog(
context: context, child: const ProjectForm());
},
)
: const SizedBox(),
loading: () => const SizedBox(),
error: (error, stack) => const SizedBox(),
),
],
),
Expand Down
20 changes: 15 additions & 5 deletions lib/screens/setting/setting_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:semaphore/adaptive/icon.dart';
Expand All @@ -10,6 +12,7 @@ import 'package:semaphore/screens/home/home_screen.dart';
import 'package:semaphore/screens/setting/setting_project.dart';
import 'package:semaphore/screens/setting/setting_server.dart';
import 'package:semaphore/screens/setting/setting_theme.dart';
import 'package:semaphore/state/projects.dart';
import 'package:semaphore/state/server.dart';

enum SettingsScreenModule { theme, server, project }
Expand Down Expand Up @@ -38,17 +41,24 @@ class SettingsScreen extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final currentServer = ref.watch(serversProvider.notifier).currentServer();
final currentProject = ref.watch(currentProjectProvider);

return AdaptiveScaffold(
appBar: LocalAppBar(
title: 'Settings',
leading: currentServer == null
? const SizedBox()
: AdaptiveIconButton(
icon: const AdaptiveIcon(Icons.arrow_back_ios),
onPressed: () {
ref.read(routerProvider).goNamed(HomeScreen.name);
},
: currentProject.when(
data: (currentProject) => currentProject == null
? const SizedBox()
: AdaptiveIconButton(
icon: const AdaptiveIcon(Icons.arrow_back_ios),
onPressed: () {
ref.read(routerProvider).goNamed(HomeScreen.name);
},
),
loading: () => const SizedBox(),
error: (error, stack) => const SizedBox(),
)),
body: SafeArea(
child: AdaptiveTabView(
Expand Down
7 changes: 6 additions & 1 deletion lib/screens/setting/setting_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:semaphore/adaptive/text.dart';
import 'package:semaphore/adaptive/text_timeago.dart';
import 'package:semaphore/components/server/form.dart';
import 'package:semaphore/state/auth.dart';
import 'package:semaphore/state/projects.dart';
import 'package:semaphore/state/server.dart';

class ServerSetting extends ConsumerWidget {
Expand Down Expand Up @@ -110,12 +111,16 @@ class ServerSetting extends ConsumerWidget {
cells: <DataCell>[
DataCell(
s.isActive == true
? const AdaptiveIcon(Icons.check_box_outlined)
? const AdaptiveIconButton(
icon: AdaptiveIcon(Icons.check_box_outlined))
: AdaptiveIconButton(
onPressed: () {
ref
.read(serversProvider.notifier)
.activeServer(s);
ref
.read(projectsProvider.notifier)
.reloadProjects();
},
icon: const AdaptiveIcon(
Icons.check_box_outline_blank)),
Expand Down
8 changes: 6 additions & 2 deletions lib/state/api_config.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:ansible_semaphore/ansible_semaphore.dart';
import 'package:dio/dio.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:semaphore/database/schema/semaphore_server.dart'
show SemaphoreServer;
import 'package:semaphore/state/server.dart';

part 'api_config.g.dart';
Expand All @@ -9,8 +11,10 @@ part 'api_config.g.dart';
class SemaphoreApi extends _$SemaphoreApi {
@override
AnsibleSemaphore build() {
final currentServer = ref.watch(serversProvider.notifier).currentServer();
if (currentServer == null) {
final servers = ref.watch(serversProvider);
final currentServer = servers.firstWhere((s) => s.isActive == true,
orElse: () => SemaphoreServer());
if (currentServer.isActive == null) {
return AnsibleSemaphore();
}
final apiUrl = currentServer.apiUrl;
Expand Down
24 changes: 18 additions & 6 deletions lib/state/projects.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:ansible_semaphore/ansible_semaphore.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:semaphore/state/api_config.dart';
import 'package:semaphore/state/shared_prefs.dart';

part 'projects.g.dart';

Expand All @@ -11,7 +12,6 @@ class Projects extends _$Projects {
try {
final api = ref.read(semaphoreApiProvider).getProjectsApi();
final resp = await api.projectsGet();
// print('projects resp: $resp');
return resp.data ?? <Project>[];
} catch (e) {
return <Project>[];
Expand All @@ -23,7 +23,6 @@ class Projects extends _$Projects {
state = await AsyncValue.guard(() async {
try {
final resp = await api.projectsGet();
// print('projects resp: $resp');
return resp.data ?? <Project>[];
} catch (e) {
return <Project>[];
Expand Down Expand Up @@ -65,19 +64,32 @@ class Projects extends _$Projects {

@riverpod
class CurrentProject extends _$CurrentProject {
static const saveKey = 'currentProject';

@override
Future<Project?> build() async {
try {
final projects = await ref.read(projectsProvider.future);
print('projects: $projects');
return projects.first;
final projects = ref.watch(projectsProvider);
final pref = await ref.read(sharedPrefsProvider.future);
final projId = pref.getInt(saveKey);
if (projId == null) {
return null;
}
print(('project id', projId));
return projects.asData?.value
.firstWhere((element) => element.id == projId);
} catch (e) {
return null;
}
}

void setCurrent(Project? p) {
Future<void> setCurrent(Project? p) async {
state = AsyncValue.data(p);
if (p != null) {
final pref = await ref.read(sharedPrefsProvider.future);
final ok = await pref.setInt(saveKey, p.id!);
print('set current project: $ok ${p.id}');
}
}
}

Expand Down
38 changes: 25 additions & 13 deletions lib/state/projects/access_key.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:ansible_semaphore/ansible_semaphore.dart';
import 'package:dio/dio.dart' show DioException;
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:pluto_grid/pluto_grid.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:semaphore/adaptive/alert_dialog.dart';
Expand Down Expand Up @@ -55,16 +55,27 @@ class AccessKeyDataTable extends BaseGridData<AccessKey> {
primaryButton: AdaptiveButton(
color: Colors.redAccent,
onPressed: () async {
final api =
ref.read(semaphoreApiProvider).getProjectApi();
final current =
await ref.read(currentProjectProvider.future);
await api.projectProjectIdKeysKeyIdDelete(
projectId: current!.id!, keyId: accessKey.id!);
if (context.mounted) {
Navigator.of(context).pop();
try {
final api = ref
.read(semaphoreApiProvider)
.getProjectApi();
final current =
await ref.read(currentProjectProvider.future);
await api.projectProjectIdKeysKeyIdDelete(
projectId: current!.id!,
keyId: accessKey.id!);
if (context.mounted) {
Navigator.of(context).pop();
}
ref
.read(accessKeyListProvider.notifier)
.loadRows();
} on DioException catch (e) {
print(e.response?.statusCode);
} catch (e, s) {
print(e);
print(s);
}
ref.read(accessKeyListProvider.notifier).loadRows();
},
child: const Text('Delete')),
secondaryButton: AdaptiveButton(
Expand Down Expand Up @@ -223,11 +234,11 @@ class AccessKeyFormRequest extends _$AccessKeyFormRequest {
);
}

Future<AccessKey?> postAccessKey() async {
Future<AccessKey?> postAccessKey(int? keyId) async {
final api = ref.read(semaphoreApiProvider).getProjectApi();
final current = await ref.read(currentProjectProvider.future);
try {
if (item?.id == null) {
if (keyId == null) {
final resp = await api.projectProjectIdKeysPost(
projectId: current?.id ?? 1, accessKey: state);

Expand All @@ -237,13 +248,14 @@ class AccessKeyFormRequest extends _$AccessKeyFormRequest {
return null;
} else {
final resp = await api.projectProjectIdKeysKeyIdPut(
projectId: current?.id ?? 1, keyId: item!.id!, accessKey: state);
projectId: current?.id ?? 1, keyId: keyId, accessKey: state);
if (resp.statusCode == 201) {
return null;
}
return null;
}
} catch (e) {
print(e);
return null;
}
}
Expand Down

0 comments on commit 0a4d226

Please sign in to comment.