Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! fixup! fixup! TW-1193: Store/get/d…
Browse files Browse the repository at this point in the history
…elete active account in persistent storage
  • Loading branch information
nqhhdev committed Jan 3, 2024
1 parent ae5fd63 commit b451aa9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
18 changes: 10 additions & 8 deletions lib/pages/chat_list/chat_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,14 @@ class ChatListController extends State<ChatList>

void setActiveClient(Client client) {
context.go('/rooms');
activeFilter = AppConfig.separateChatTypes
? ActiveFilter.messages
: ActiveFilter.allChats;
activeSpaceId = null;
conversationSelectionNotifier.value.clear();
Matrix.of(context).setActiveClient(client);
setState(() {
activeFilter = AppConfig.separateChatTypes
? ActiveFilter.messages
: ActiveFilter.allChats;
activeSpaceId = null;
conversationSelectionNotifier.value.clear();
Matrix.of(context).setActiveClient(client);
});
_clientStream.add(client);
}

Expand Down Expand Up @@ -779,14 +781,14 @@ class ChatListController extends State<ChatList>
scrollController.addListener(_onScroll);
_waitForFirstSync();
_hackyWebRTCFixForWeb();
_getCurrentProfile(activeClient);
CallKeepManager().initialize();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (mounted) {
Matrix.of(context).backgroundPush?.setupPush();
await matrixState.retrievePersistedActiveClient();
_getCurrentProfile(activeClient);
}
});

_checkTorBrowser();
super.initState();
}
Expand Down
18 changes: 10 additions & 8 deletions lib/pages/multiple_accounts/multiple_accounts_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ class MultipleAccountsPickerController {
);
}

void _setActiveClient(Client newClient) {
context.go(
'/rooms',
extra: SwitchActiveAccountBodyArgs(
newActiveClient: newClient,
),
);
Matrix.of(context).setActiveClient(newClient);
void _setActiveClient(Client newClient) async {
final result = await _matrixState.setActiveClient(newClient);
if (result.isSuccess) {
context.go(
'/rooms',
extra: SwitchActiveAccountBodyArgs(
newActiveClient: newClient,
),
);
}
}
}
29 changes: 18 additions & 11 deletions lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ import '../utils/background_push.dart';
import '../utils/famedlysdk_store.dart';
import 'local_notifications_extension.dart';

enum SetActiveClientResult {
success,
unknownClient;

bool get isSuccess => this == SetActiveClientResult.success;
}

class Matrix extends StatefulWidget {
final Widget? child;

Expand Down Expand Up @@ -120,16 +127,18 @@ class MatrixState extends State<Matrix>
late String currentClientSecret;
RequestTokenResponse? currentThreepidCreds;

void setActiveClient(Client? newClient) {
_checkHomeserverExists(newClient);
Future<SetActiveClientResult> setActiveClient(Client? newClient) async {
final index = widget.clients.indexWhere((client) => client == newClient);
if (index != -1) {
_activeClient = index;
// TODO: Multi-client VoiP support
createVoipPlugin();
_storePersistActiveAccount(newClient!);
_setTwakeSupport(newClient);
await _storePersistActiveAccount(newClient!);
return SetActiveClientResult.success;
} else {
Logs().w('Tried to set an unknown client ${newClient!.userID} as active');
return SetActiveClientResult.unknownClient;
}
}

Expand Down Expand Up @@ -188,7 +197,7 @@ class MatrixState extends State<Matrix>
.stream
.where((l) => l == LoginState.loggedIn)
.first
.then((_) {
.then((_) async {
Logs().d(
'MatrixState::getLoginClient() Login successful - Client ${_loginClientCandidate!.clientName}',
);
Expand Down Expand Up @@ -302,7 +311,6 @@ class MatrixState extends State<Matrix>
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
await _retrievePersistedActiveClient();
WidgetsBinding.instance.addObserver(this);
initMatrix();
initReceiveSharingIntent();
Expand Down Expand Up @@ -387,8 +395,8 @@ class MatrixState extends State<Matrix>
TwakeApp.routerKey.currentContext!,
L10n.of(context)!.oneClientLoggedOut,
);
setActiveClient(widget.clients.first);
if (state != LoginState.loggedIn) {
final result = await setActiveClient(widget.clients.first);
if (state != LoginState.loggedIn && result.isSuccess) {
TwakeApp.router.go(
'/rooms',
extra: LogoutBodyArgs(
Expand Down Expand Up @@ -666,7 +674,7 @@ class MatrixState extends State<Matrix>
}
}

void _checkHomeserverExists(Client? client) async {
void _setTwakeSupport(Client? client) async {
Logs().d(
'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported',
);
Expand All @@ -690,7 +698,7 @@ class MatrixState extends State<Matrix>
);
}

Future<void> _retrievePersistedActiveClient() async {
Future<void> retrievePersistedActiveClient() async {
try {
final multipleAccountRepository = getIt.get<MultipleAccountRepository>();
final persistActiveAccount =
Expand All @@ -701,8 +709,7 @@ class MatrixState extends State<Matrix>
} else {
final newActiveClient = getClientByUserId(persistActiveAccount);
if (newActiveClient != null) {
_activeClient =
widget.clients.indexWhere((client) => client == newActiveClient);
setActiveClient(newActiveClient);
}
}
} catch (e) {
Expand Down

0 comments on commit b451aa9

Please sign in to comment.