Skip to content

Commit 29e77ae

Browse files
committed
changed supabase for pocketbase
1 parent 70a13d9 commit 29e77ae

File tree

11 files changed

+133
-185
lines changed

11 files changed

+133
-185
lines changed

lib/UI/toneshare/cloud_login.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import 'dart:io';
2+
13
import 'package:flutter/material.dart';
2-
import 'package:supabase_flutter/supabase_flutter.dart';
4+
import 'package:mighty_plug_manager/modules/cloud/cloudManager.dart';
5+
import 'package:pocketbase/pocketbase.dart';
36

47
import 'toneshare_main.dart';
58

@@ -35,17 +38,17 @@ class _SignInFormState extends State<SignInForm> {
3538

3639
try {
3740
ToneShare.startLoading(context);
38-
var result = await Supabase.instance.client.auth.signInWithPassword(
39-
password: _passwordController.text.trim(),
40-
email: _emailController.text.trim());
41-
} on AuthException catch (e) {
42-
setState(() {
43-
_errorMessage = "Wrong credentials or account not verified";
44-
});
45-
} catch (e) {
46-
setState(() {
41+
await CloudManager.instance.signIn(
42+
email: _emailController.text.trim(),
43+
password: _passwordController.text.trim());
44+
} on ClientException catch (e) {
45+
if (e.isAbort) {
4746
_errorMessage = "No internet connection";
48-
});
47+
} else {
48+
_errorMessage = e.response["message"];
49+
//"Wrong credentials or account not verified";
50+
}
51+
setState(() {});
4952
} finally {
5053
ToneShare.stopLoading(context);
5154
}

lib/UI/toneshare/cloud_signup.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:flutter/material.dart';
2-
import 'package:supabase_flutter/supabase_flutter.dart';
32

43
import 'toneshare_main.dart';
54

@@ -33,7 +32,7 @@ class _SignUpFormState extends State<SignUpForm> {
3332
_errorMessage = "";
3433
});
3534

36-
try {
35+
/*try {
3736
ToneShare.startLoading(context);
3837
var result = await Supabase.instance.client.auth.signUp(
3938
password: _passwordController.text.trim(),
@@ -50,7 +49,7 @@ class _SignUpFormState extends State<SignUpForm> {
5049
});
5150
} finally {
5251
ToneShare.stopLoading(context);
53-
}
52+
}*/
5453
}
5554
}
5655

lib/UI/toneshare/toneshare_home.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:flutter/material.dart';
2-
import 'package:supabase_flutter/supabase_flutter.dart';
2+
import 'package:mighty_plug_manager/modules/cloud/cloudManager.dart';
33

44
import '../widgets/searchTextField.dart';
55

@@ -23,10 +23,11 @@ class _ToneShareHomeState extends State<ToneShareHome> {
2323
void _search(String? query) async {
2424
if (query == null || query.isEmpty) return;
2525

26-
final response = await Supabase.instance.client
26+
final response = null;
27+
/*await Supabase.instance.client
2728
.from("presets")
2829
.select("*")
29-
.textSearch("name", query, type: TextSearchType.websearch);
30+
.textSearch("name", query, type: TextSearchType.websearch);*/
3031

3132
if (response != null) {
3233
// Process results
@@ -44,10 +45,6 @@ class _ToneShareHomeState extends State<ToneShareHome> {
4445
//searchCtrl.removeListener(() {});
4546
}
4647

47-
void _signOut() {
48-
Supabase.instance.client.auth.signOut();
49-
}
50-
5148
@override
5249
Widget build(BuildContext context) {
5350
return Column(
@@ -64,7 +61,9 @@ class _ToneShareHomeState extends State<ToneShareHome> {
6461
);
6562
},
6663
)),
67-
ElevatedButton(child: const Text("Sign Out"), onPressed: _signOut)
64+
ElevatedButton(
65+
child: const Text("Sign Out"),
66+
onPressed: CloudManager.instance.signOut)
6867
],
6968
);
7069
}

lib/UI/toneshare/toneshare_main.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22

3-
import '../../modules/cloud/supabase.dart';
3+
import '../../modules/cloud/cloudManager.dart';
44
import 'cloud_authentication.dart';
55
import 'toneshare_home.dart';
66

@@ -38,13 +38,13 @@ class _ToneShareState extends State<ToneShare> {
3838
@override
3939
void initState() {
4040
super.initState();
41-
SupabaseUtils.instance.addListener(_onAuthChange);
41+
CloudManager.instance.addListener(_onAuthChange);
4242
}
4343

4444
@override
4545
void dispose() {
4646
super.dispose();
47-
SupabaseUtils.instance.removeListener(_onAuthChange);
47+
CloudManager.instance.removeListener(_onAuthChange);
4848
}
4949

5050
void _onAuthChange() {
@@ -54,7 +54,7 @@ class _ToneShareState extends State<ToneShare> {
5454
@override
5555
Widget build(BuildContext context) {
5656
Widget page;
57-
if (SupabaseUtils.instance.signedIn) {
57+
if (CloudManager.instance.signedIn) {
5858
page = ToneShareHome();
5959
} else {
6060
page = const CloudAuthentication();

lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import 'bluetooth/NuxDeviceControl.dart';
1919

2020
//recreate this file with your own api keys
2121
import 'configKeys.dart';
22-
import 'modules/cloud/supabase.dart';
22+
import 'modules/cloud/cloudManager.dart';
2323

2424
//able to create snackbars/messages everywhere
2525
final navigatorKey = GlobalKey<NavigatorState>();
@@ -83,7 +83,7 @@ void main() {
8383
}
8484

8585
mainRunApp() {
86-
if (kDebugMode) SupabaseUtils.instance.initialize();
86+
if (kDebugMode) CloudManager.instance.initialize();
8787
runApp(const App());
8888
}
8989

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:flutter/widgets.dart';
2+
import 'package:pocketbase/pocketbase.dart';
3+
4+
import 'customAuthStore.dart';
5+
6+
class CloudManager extends ChangeNotifier {
7+
final pb = PocketBase('https://mightier-amp.pockethost.io/',
8+
authStore: CustomAuthStore());
9+
10+
/// private constructor
11+
CloudManager._();
12+
13+
/// the one and only instance of this singleton
14+
static final instance = CloudManager._();
15+
16+
bool _signedIn = false;
17+
bool get signedIn => _signedIn;
18+
19+
void initialize() async {
20+
pb.authStore.onChange.listen(_onAuthChange);
21+
22+
_signedIn = pb.authStore.isValid;
23+
}
24+
25+
Future<RecordAuth> signIn({required String email, required String password}) {
26+
return pb.collection('users').authWithPassword(email, password);
27+
}
28+
29+
void signOut() {
30+
pb.authStore.clear();
31+
}
32+
33+
_onAuthChange(AuthStoreEvent event) {
34+
print("Event: ${event.model}");
35+
print("Token: ${event.token}");
36+
37+
_signedIn = event.model != null;
38+
notifyListeners();
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'dart:convert';
2+
3+
import 'package:pocketbase/pocketbase.dart';
4+
5+
import '../../platform/simpleSharedPrefs.dart';
6+
7+
class CustomAuthStore extends AuthStore {
8+
final String key;
9+
10+
CustomAuthStore({this.key = "pb_auth"}) {
11+
final String? raw = SharedPrefs().getValue(key, null);
12+
13+
if (raw != null && raw.isNotEmpty) {
14+
final decoded = jsonDecode(raw);
15+
final token = (decoded as Map<String, dynamic>)["token"] as String? ?? "";
16+
final model =
17+
RecordModel.fromJson(decoded["model"] as Map<String, dynamic>? ?? {});
18+
19+
save(token, model);
20+
}
21+
}
22+
23+
@override
24+
void save(
25+
String newToken,
26+
dynamic /* RecordModel|AdminModel|null */ newModel,
27+
) {
28+
super.save(newToken, newModel);
29+
30+
final encoded =
31+
jsonEncode(<String, dynamic>{"token": newToken, "model": newModel});
32+
33+
SharedPrefs().setValue(key, encoded);
34+
}
35+
36+
@override
37+
void clear() {
38+
super.clear();
39+
40+
SharedPrefs().remove(key);
41+
}
42+
}

lib/modules/cloud/supabase.dart

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/platform/simpleSharedPrefs.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class SharedPrefs {
101101
_savePrefs();
102102
}
103103

104+
void remove(String key) {
105+
_prefsData.remove(key);
106+
_savePrefs();
107+
}
108+
104109
dynamic getValue(String key, dynamic _default) {
105110
if (_prefsData.containsKey(key)) return _prefsData[key];
106111
return _default;

0 commit comments

Comments
 (0)