Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upper case issue for join group code in particular device #166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 59 additions & 29 deletions app/lib/ui/flow/space/join/join_space_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:style/button/bottom_sticky_overlay.dart';
Expand Down Expand Up @@ -33,17 +32,49 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {

@override
void initState() {
_controllers = List.generate(6, (index) => TextEditingController());
_focusNodes = List.generate(
6,
(index) => FocusNode(onKeyEvent: (node, event) {
if (event.logicalKey == LogicalKeyboardKey.backspace &&
_controllers[index].text.isEmpty) {
if (index > 0) _focusNodes[index - 1].requestFocus();
}
return KeyEventResult.ignored;
}));
super.initState();
_controllers = List.generate(6, (index) => TextEditingController(text: '\u200b'));
_focusNodes = List.generate(6, (index) => FocusNode());

for (int i = 0; i < _controllers.length; i++) {
_controllers[i].addListener(() {
_handleTextChange(i);
});
}
}

void _handleTextChange(int index) {
final text = _controllers[index].text;

if (text.isEmpty) {
_controllers[index].text = '\u200b';
_controllers[index].selection = const TextSelection.collapsed(offset: 1);

if (index > 0) {
_focusNodes[index - 1].requestFocus();
}
} else if (text.length > 1) {
String newText = text.replaceAll('\u200b', '').toUpperCase();
_controllers[index].text = newText;
_controllers[index].selection = TextSelection.collapsed(offset: newText.length);

// Move focus to the next field
if (index < 5) {
_focusNodes[index + 1].requestFocus();
}
_updateJoinSpaceButtonState();
}
}

@override
void dispose() {
for (var controller in _controllers) {
controller.dispose();
}
for (var focusNode in _focusNodes) {
focusNode.dispose();
}
super.dispose();
}

@override
Expand Down Expand Up @@ -130,11 +161,11 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {
}

Widget _buildCodeBox(
BuildContext context,
double width,
int index,
JoinSpaceViewState state,
) {
BuildContext context,
double width,
int index,
JoinSpaceViewState state,
) {
return Container(
width: width,
height: 64,
Expand All @@ -148,7 +179,7 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {
controller: _controllers[index],
focusNode: _focusNodes[index],
textAlign: TextAlign.center,
maxLength: 1,
maxLength: 2,
decoration: const InputDecoration(
border: InputBorder.none,
counterText: '',
Expand All @@ -161,16 +192,13 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {
FocusManager.instance.primaryFocus?.unfocus();
},
onChanged: (text) {
if (text.isEmpty) {
if (index > 0) _focusNodes[index - 1].requestFocus();
} else {
if (index < 5) {
_focusNodes[index + 1].requestFocus();
} else {
FocusManager.instance.primaryFocus?.unfocus();
}
final upperCaseText = text.toUpperCase();
if (_controllers[index].text != upperCaseText) {
_controllers[index].value = TextEditingValue(
text: upperCaseText,
selection: TextSelection.collapsed(offset: upperCaseText.length),
);
}
_updateJoinSpaceButtonState();
},
),
),
Expand Down Expand Up @@ -228,8 +256,10 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {

void _updateJoinSpaceButtonState() {
setState(() {
enabled =
_controllers.every((controller) => controller.text.trim().isNotEmpty);
enabled = _controllers.every((controller) {
final text = controller.text;
return text.length == 1 && text != '\u200b';
});
});
}

Expand Down Expand Up @@ -274,7 +304,7 @@ class _JoinSpaceState extends ConsumerState<JoinSpace> {
final isNetworkOff = await checkInternetConnectivity();
isNetworkOff
? _showSnackBar()
: notifier.joinSpace(inviteCode.toUpperCase());
: notifier.joinSpace();
}

void _showSnackBar() {
Expand Down
42 changes: 20 additions & 22 deletions app/lib/ui/flow/space/join/join_space_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,39 @@ class JoinSpaceViewNotifier extends StateNotifier<JoinSpaceViewState> {
this.authService,
) : super(const JoinSpaceViewState());

Future<void> joinSpace(String code) async {
Future<void> joinSpace() async {
try {
spaceService.joinSpace(state.space?.id ?? '');
state = state.copyWith(verifying: false, spaceJoined: true, error: null);
cp-ishita-g marked this conversation as resolved.
Show resolved Hide resolved
} catch (error, stack) {
state = state.copyWith(error: error, verifying: false);
logger.e(
'JoinSpaceViewNotifier: Error while join space with invitation code',
error: error,
stackTrace: stack,
);
cp-ishita-g marked this conversation as resolved.
Show resolved Hide resolved
}
}

Future<String?> getInvitation(String code) async {
try {
state = state.copyWith(verifying: true, errorInvalidInvitationCode: false, alreadySpaceMember: false);
final invitation = await getInvitation(code);
final invitation = await spaceInvitationService.getInvitation(code);
if (invitation == null) {
state =
state.copyWith(errorInvalidInvitationCode: true, verifying: false);
_resetFlagsAfter30Sec();
return;
return '';
cp-ishita-g marked this conversation as resolved.
Show resolved Hide resolved
}
var spaceId = invitation.space_id;
final userSpaces = authService.currentUser?.space_ids ?? [];

if (userSpaces.contains(spaceId)) {
state = state.copyWith(verifying: false, alreadySpaceMember: true, error: null);
_resetFlagsAfter30Sec();
return;
return '';
}

spaceService.joinSpace(spaceId);
state = state.copyWith(verifying: false, spaceJoined: true, error: null);
} catch (error, stack) {
state = state.copyWith(error: error, verifying: false);
logger.e(
'JoinSpaceViewNotifier: Error while join space with invitation code',
error: error,
stackTrace: stack,
);
}
}

Future<ApiSpaceInvitation?> getInvitation(String code) async {
try {
return await spaceInvitationService.getInvitation(code);
return spaceId;
cp-ishita-g marked this conversation as resolved.
Show resolved Hide resolved
} catch (error, stack) {
logger.e('JoinSpaceViewNotifier: Error while get group invitation',
error: error, stackTrace: stack);
Expand All @@ -71,8 +70,7 @@ class JoinSpaceViewNotifier extends StateNotifier<JoinSpaceViewState> {

void getSpace(String code) async {
try {
final invitation = await getInvitation(code);
var spaceId = invitation?.space_id;
final spaceId = await getInvitation(code);
final space = await spaceService.getSpace(spaceId ?? '');
state = state.copyWith(space: space);
} catch (error, stack) {
Expand Down
30 changes: 15 additions & 15 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.19.0"
color:
dependency: transitive
description:
Expand Down Expand Up @@ -1169,18 +1169,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.7"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.8"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -1577,7 +1577,7 @@ packages:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
version: "0.0.0"
smooth_page_indicator:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1638,10 +1638,10 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.12.0"
state_notifier:
dependency: transitive
description:
Expand Down Expand Up @@ -1670,10 +1670,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.3.0"
style:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1701,10 +1701,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.3"
time:
dependency: transitive
description:
Expand Down Expand Up @@ -1861,10 +1861,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.3.0"
watcher:
dependency: transitive
description:
Expand Down
Loading