Skip to content

Commit

Permalink
Add Stacker Saloon
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebueno committed Jun 9, 2024
1 parent 0b4d356 commit b76436e
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 55 deletions.
25 changes: 18 additions & 7 deletions lib/data/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,24 @@ final class Api {
) async {
final response = (responseData['pageProps'] ?? responseData);
final data = response['ssrData'] ?? response['data'];

final itemsMap =
(data['items'] ?? data['topItems'] ?? data['notifications']);
final List items = itemsMap['items'] ?? itemsMap['notifications'];

if (postType == PostType.home) {
// TODO: Temporary solution to get Stacker Saloon on the list
final List? pins = itemsMap['pins'];
if (pins != null && pins.isNotEmpty) {
for (var pin in pins) {
items.insert(pin['position'], pin);
}
}
}

final cursor = itemsMap['cursor'];
if (cursor != null) {
await SharedPrefsManager.create(
await SharedPrefsManager.set(
'${postType.name}-cursor',
cursor,
);
Expand Down Expand Up @@ -169,15 +180,15 @@ final class Api {
}

Future<void> _saveBuildId(String newBuildId) async {
await SharedPrefsManager.create('build-id', newBuildId);
await SharedPrefsManager.set('build-id', newBuildId);
}

Future<String?> _getCurrBuildId() async {
return await SharedPrefsManager.read('build-id');
return await SharedPrefsManager.get('build-id');
}

Future<List<Post>> fetchMorePosts(PostType postType) async {
final cursor = await SharedPrefsManager.read('${postType.name}-cursor');
final cursor = await SharedPrefsManager.get('${postType.name}-cursor');

if (cursor == null) {
throw Exception('Error fetching more');
Expand Down Expand Up @@ -249,7 +260,7 @@ final class Api {
return null;
}

await SharedPrefsManager.create(
await SharedPrefsManager.set(
'me',
jsonEncode(me),
);
Expand Down Expand Up @@ -365,7 +376,7 @@ final class Api {
if (response.statusCode == 403 &&
response.realUri.toString() ==
'$baseUrl/api/auth/error?error=Verification') {
final sessionData = await SharedPrefsManager.read('session');
final sessionData = await SharedPrefsManager.get('session');
if (sessionData == null || sessionData == 'null' || sessionData == '{}') {
_goToLoginFailedPage();

Expand Down Expand Up @@ -401,7 +412,7 @@ final class Api {
return null;
}

await SharedPrefsManager.create(
await SharedPrefsManager.set(
'session',
jsonEncode(sessionResponse.data),
);
Expand Down
6 changes: 3 additions & 3 deletions lib/data/shared_prefs_manager.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:shared_preferences/shared_preferences.dart';

class SharedPrefsManager {
static Future<void> create(String key, dynamic value) async {
static Future<void> set(String key, dynamic value) async {
final prefs = await SharedPreferences.getInstance();

if (value is int) {
Expand All @@ -16,12 +16,12 @@ class SharedPrefsManager {
prefs.setStringList(key, value);
} else {
print(
'SharedPrefsManager.create error: value => $value for key => $key has unexpected type.',
'SharedPrefsManager.set error: value => $value for key => $key has unexpected type.',
);
}
}

static Future<dynamic> read(String key) async {
static Future<dynamic> get(String key) async {
final prefs = await SharedPreferences.getInstance();

return prefs.get(key);
Expand Down
4 changes: 2 additions & 2 deletions lib/data/theme_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThemeNotifier with ChangeNotifier {
}

ThemeNotifier() {
SharedPrefsManager.read('theme-mode').then(
SharedPrefsManager.get('theme-mode').then(
(value) {
setThemeMode(value);

Expand Down Expand Up @@ -81,7 +81,7 @@ class ThemeNotifier with ChangeNotifier {
default:
_themeData = ThemeMode.dark;
}
SharedPrefsManager.create('theme-mode', themeMode);
SharedPrefsManager.set('theme-mode', themeMode);

notifyListeners();
}
Expand Down
7 changes: 3 additions & 4 deletions lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ class Utils {
} else if (await canLaunchUrl(uri)) {
await launchUrl(uri);
} else {
showError(
'Could not launch $url. Make sure you have a web browser installed');
showError('Could not launch $url');

return;
}
Expand All @@ -100,7 +99,7 @@ class Utils {
return;
}

// TODO: We should check if the modal is open before popping
// TODO: We should check if the modal is open before popping it
if (Navigator.of(ctx).canPop()) {
Navigator.of(ctx).pop();
}
Expand Down Expand Up @@ -218,7 +217,7 @@ class Utils {
}

static Future<Session?> getSession() async {
final sessionData = await SharedPrefsManager.read('session');
final sessionData = await SharedPrefsManager.get('session');

if (sessionData == null || sessionData == 'null' || sessionData == '{}') {
return null;
Expand Down
31 changes: 1 addition & 30 deletions lib/views/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import 'package:stacker_news/data/models/session.dart';
import 'package:stacker_news/main.dart';
import 'package:stacker_news/utils.dart';
import 'package:stacker_news/views/pages/auth/sign_in_page.dart';
import 'package:stacker_news/views/pages/new_post/new_post_page.dart';
import 'package:stacker_news/views/pages/notifications/notifications_page.dart';
import 'package:stacker_news/views/pages/profile/profile_page.dart';
import 'package:stacker_news/views/widgets/base_tab.dart';
import 'package:stacker_news/views/widgets/generic_page_scaffold.dart';
import 'package:stacker_news/views/widgets/maybe_new_post_button.dart';
import 'package:stacker_news/views/widgets/sn_logo.dart';

class HomePage extends StatelessWidget {
Expand Down Expand Up @@ -141,32 +141,3 @@ class _MaybeNotificationsButtonState extends State<MaybeNotificationsButton> {
);
}
}

class MaybeNewPostFab extends StatefulWidget {
const MaybeNewPostFab({super.key});

@override
State<MaybeNewPostFab> createState() => _MaybeNewPostFabState();
}

class _MaybeNewPostFabState extends State<MaybeNewPostFab> {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Utils.getSession(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data is Session) {
return FloatingActionButton.extended(
onPressed: () {
Navigator.pushNamed(context, NewPostPage.id);
},
icon: const Icon(Icons.add),
label: const Text('New Post'),
);
}

return Container();
},
);
}
}
33 changes: 33 additions & 0 deletions lib/views/widgets/maybe_new_post_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flutter/material.dart';
import 'package:stacker_news/data/models/session.dart';
import 'package:stacker_news/utils.dart';
import 'package:stacker_news/views/pages/new_post/new_post_page.dart';

class MaybeNewPostFab extends StatefulWidget {
const MaybeNewPostFab({super.key});

@override
State<MaybeNewPostFab> createState() => _MaybeNewPostFabState();
}

class _MaybeNewPostFabState extends State<MaybeNewPostFab> {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Utils.getSession(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data is Session) {
return FloatingActionButton.extended(
onPressed: () {
Navigator.pushNamed(context, NewPostPage.id);
},
icon: const Icon(Icons.add),
label: const Text('New Post'),
);
}

return Container();
},
);
}
}
18 changes: 10 additions & 8 deletions lib/views/widgets/post_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ class _PostItemState extends State<PostItem> {
arguments: _post,
);
},
child: Padding(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
color: item.position == null ? null : SNColors.primary.withOpacity(.27),
child: Row(
children: [
if (widget.isCommentsPage && item.id != null && item.id != '')
Expand Down Expand Up @@ -127,14 +128,15 @@ class _PostItemState extends State<PostItem> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
child: Text(
item.isJob == true
? '${item.company}'
: '${item.sats} sats',
style: label,
if (item.position == null)
Flexible(
child: Text(
item.isJob == true
? '${item.company}'
: '${item.sats} sats',
style: label,
),
),
),
Text(
item.isJob == true
? '${(item.remote == true && item.location == '') ? 'Remote' : item.location}'
Expand Down
2 changes: 1 addition & 1 deletion lib/views/widgets/sn_endpoint_version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SNEndpointVersion extends StatelessWidget {
const SNEndpointVersion({super.key});

Future<String> _getVersion() async =>
await SharedPrefsManager.read('build-id') ?? 'Unknown';
await SharedPrefsManager.get('build-id') ?? 'Unknown';

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit b76436e

Please sign in to comment.