mirror of
https://github.com/Linloir/Simple-TCP-Client.git
synced 2025-12-17 00:38:11 +08:00
Fix Bug
- Data base concurrency cause constraint failed when updating userinfo
This commit is contained in:
parent
0bb71791da
commit
2a78af4885
@ -1,6 +1,44 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-13 14:03:56
|
||||
* @LastEditTime : 2022-10-13 14:03:56
|
||||
* @LastEditTime : 2022-10-14 13:47:33
|
||||
* @Description :
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:tcp_client/chat/cubit/chat_state.dart';
|
||||
import 'package:tcp_client/repositories/common_models/message.dart';
|
||||
import 'package:tcp_client/repositories/local_service_repository/local_service_repository.dart';
|
||||
import 'package:tcp_client/repositories/tcp_repository/models/tcp_response.dart';
|
||||
import 'package:tcp_client/repositories/tcp_repository/tcp_repository.dart';
|
||||
|
||||
class ChatCubit extends Cubit<ChatState> {
|
||||
ChatCubit({
|
||||
required this.localServiceRepository,
|
||||
required this.tcpRepository
|
||||
}): super(ChatState.empty()) {
|
||||
subscription = tcpRepository.responseStreamBroadcast.listen(_onResponse);
|
||||
}
|
||||
|
||||
final LocalServiceRepository localServiceRepository;
|
||||
final TCPRepository tcpRepository;
|
||||
late final StreamSubscription subscription;
|
||||
|
||||
void addMessage(Message message) {
|
||||
//Store locally
|
||||
//Send to server
|
||||
//Emit new state
|
||||
}
|
||||
|
||||
void _onResponse(TCPResponse response) {
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
subscription.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,23 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-13 14:03:52
|
||||
* @LastEditTime : 2022-10-13 14:03:52
|
||||
* @LastEditTime : 2022-10-14 13:42:46
|
||||
* @Description :
|
||||
*/
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:tcp_client/repositories/common_models/message.dart';
|
||||
|
||||
enum ChatStatus { fetching, partial, full }
|
||||
|
||||
class ChatState extends Equatable {
|
||||
final ChatStatus status;
|
||||
final List<Message> chatHistory;
|
||||
|
||||
const ChatState({required this.chatHistory, required this.status});
|
||||
|
||||
static ChatState empty() => const ChatState(chatHistory: [], status: ChatStatus.fetching);
|
||||
|
||||
@override
|
||||
List<Object> get props => [chatHistory];
|
||||
}
|
||||
|
||||
6
lib/chat/view/in_message_box/in_message_box.dart
Normal file
6
lib/chat/view/in_message_box/in_message_box.dart
Normal file
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-14 13:49:47
|
||||
* @LastEditTime : 2022-10-14 13:49:47
|
||||
* @Description :
|
||||
*/
|
||||
6
lib/chat/view/out_message_box/out_message_box.dart
Normal file
6
lib/chat/view/out_message_box/out_message_box.dart
Normal file
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-14 13:49:28
|
||||
* @LastEditTime : 2022-10-14 13:49:28
|
||||
* @Description :
|
||||
*/
|
||||
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-12 09:56:04
|
||||
* @LastEditTime : 2022-10-12 17:54:35
|
||||
* @LastEditTime : 2022-10-14 14:24:11
|
||||
* @Description :
|
||||
*/
|
||||
|
||||
@ -22,6 +22,7 @@ class InitializationCubit extends Cubit<InitializationState> {
|
||||
TCPRepository? tcpRepository;
|
||||
LocalServiceRepository? localServiceRepository;
|
||||
Future(() async {
|
||||
print('${(await getApplicationDocumentsDirectory()).path}/.data/database.db');
|
||||
localServiceRepository = await LocalServiceRepository.create(databaseFilePath: '${(await getApplicationDocumentsDirectory()).path}/.data/database.db');
|
||||
}).then((_) {
|
||||
emit(state.copyWith(
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-11 10:56:02
|
||||
* @LastEditTime : 2022-10-12 15:35:30
|
||||
* @LastEditTime : 2022-10-14 14:30:11
|
||||
* @Description : Local Service Repository
|
||||
*/
|
||||
|
||||
@ -231,27 +231,29 @@ class LocalServiceRepository {
|
||||
Future<void> storeUserInfo({
|
||||
required UserInfo userInfo
|
||||
}) async {
|
||||
//check if exist
|
||||
var queryResult = await _database.query(
|
||||
'users',
|
||||
where: 'userid = ?',
|
||||
whereArgs: [userInfo.userID]
|
||||
);
|
||||
if(queryResult.isEmpty) {
|
||||
_database.insert(
|
||||
await _database.transaction((txn) async {
|
||||
//check if exist
|
||||
var queryResult = await txn.query(
|
||||
'users',
|
||||
userInfo.jsonObject
|
||||
);
|
||||
}
|
||||
else {
|
||||
_database.update(
|
||||
'users',
|
||||
userInfo.jsonObject,
|
||||
where: 'userid = ?',
|
||||
whereArgs: [userInfo.userID]
|
||||
);
|
||||
}
|
||||
_userInfoChangeStreamController.add(userInfo);
|
||||
if(queryResult.isEmpty) {
|
||||
txn.insert(
|
||||
'users',
|
||||
userInfo.jsonObject
|
||||
);
|
||||
}
|
||||
else {
|
||||
txn.update(
|
||||
'users',
|
||||
userInfo.jsonObject,
|
||||
where: 'userid = ?',
|
||||
whereArgs: [userInfo.userID]
|
||||
);
|
||||
}
|
||||
_userInfoChangeStreamController.add(userInfo);
|
||||
});
|
||||
}
|
||||
|
||||
Future<UserInfo?> fetchUserInfoViaID({required int userid}) async {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user