hycop_multi_platform 0.6.4 hycop_multi_platform: ^0.6.4 copied to clipboard
Hycop Framework
import 'package:creta_studio_model/model/frame_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:hycop_multi_platform/hycop.dart';
//import '/Project/hycop_multi_platform/lib/hycop.dart';
//아래 다섯개는 잘 빌드 됨 의미 없음
// import 'package:creta_common/creta_common.dart';
// import 'package:creta_studio_io/creta_studio_io.dart';
// import 'package:creta_studio_model/creta_studio_model.dart';
// import 'package:creta_user_io/creta_user_io.dart';
// import 'package:creta_user_model/creta_user_model.dart';
// //문제를 이르키는 코드 실제로 import 하는 구문
// //creta_common **
// import 'package:creta_common/common/creta_common_utils.dart';
// import 'package:creta_common/common/cross_common_job_stub.dart';
// //creta_studio_io
// //creta_studio_model **
// import 'package:creta_studio_model/model/contents_model.dart';
// //creta_user_io
//creta_user_model
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hycop Multi Platform',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Hycop Multi Platform'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _hycopInitResult = 'not initialize';
String _createAccountResult = '[empty]';
final String _loginAccountResult = '[empty]';
String _deleteAccountResult = '[empty]';
String _setDataResult = '[empty]';
String _getDataResult = '[empty]';
String _deleteDataResult = '[empty]';
String _uploadStorageResult = '[empty]';
String _downloadStorageResult = '[empty]';
Uint8List? _downloadUint8List;
FrameModel realtimeModel = FrameModel('', '');
bool _uploadToRealtime = false;
bool _downloadFromRealtime = false;
double _mouseX = 0;
double _mouseY = 0;
int _lastRealtime = DateTime.now().millisecondsSinceEpoch;
String _getNowString() {
return DateTime.now().toString();
}
Future<void> _hycopInit() async {
setupLogger();
Logger.root.level = Level.INFO;
HycopFactory.serverType = ServerType.firebase;
setState(() {
_hycopInitResult = '[${_getNowString()}] initializing...';
});
await HycopFactory.initAll();
setState(() {
_hycopInitResult = '[${_getNowString()}] initialized';
});
}
Future<void> _createAccount() async {
Map<String, dynamic> userData = {
'accountSignUpType': AccountSignUpType.hycop.index,
'password': '-507263a',
'email': 'HycopTestUser@nomail.com',
};
AccountManager.createAccount(userData).then((value) {
// success
setState(() {
_createAccountResult = '[${_getNowString()}] ${userData['email']} account created';
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_createAccountResult =
'[${_getNowString()}] fail to create account !!!\n${error.toString()}';
});
},
);
}
Future<void> _loginAccount() async {
String password = '-507263a';
String email = 'HycopTestUser@nomail.com';
AccountManager.login(email, password).then((value) {
// success
setState(() {
_createAccountResult = '[${_getNowString()}] $email logined';
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_createAccountResult = '[${_getNowString()}] fail to login !!!\n${error.toString()}';
});
},
);
}
Future<void> _deleteAccount() async {
String email = AccountManager.currentLoginUser.email;
HycopFactory.account!.deleteAccount(deleteNow: true).then((value) {
// success
setState(() {
_deleteAccountResult = '[${_getNowString()}] $email account deleted';
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_deleteAccountResult =
'[${_getNowString()}] fail to delete account !!!\n${error.toString()}';
});
},
);
}
Future<void> _getDataFromDB() async {
var userDataList = await HycopFactory.dataBase
?.queryData('hycop_users', where: {'email': 'test'}, orderBy: 'email');
if (userDataList?.isEmpty ?? true) {
setState(() {
_getDataResult = '[${_getNowString()}] no data';
});
return;
}
setState(() {
_getDataResult = '[${_getNowString()}] ';
for (var userData in userDataList!) {
_getDataResult += 'users=${userData['email']?.toString()},';
}
});
}
Future<void> _setDataToDB() async {
Map<String, dynamic> userData = {'userId': 'test', 'email': 'test', 'accountSignUpType': 0};
await HycopFactory.dataBase!.createData('hycop_users', 'user=test', userData).then((value) {
// success
setState(() {
_setDataResult = '[${_getNowString()}] create successfully';
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_setDataResult = '[${_getNowString()}] fail to create !!!\n${error.toString()}';
});
},
);
}
Future<void> _deleteDataFromDB() async {
await HycopFactory.dataBase!.removeData('hycop_users', 'user=test').then((value) {
// success
setState(() {
_deleteDataResult = '[${_getNowString()}] delete successfully';
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_deleteDataResult = '[${_getNowString()}] fail to delete !!!\n${error.toString()}';
});
},
);
}
Future<void> _downloadDataFromStorage() async {
HycopFactory.storage!.setBucket();
String fileId = 'HycopTest/content/image/a6de38b7f004ea361acfbc9ed98a447eHycopTestImage.png';
setState(() {
_downloadUint8List = null;
_downloadStorageResult = 'downloading...';
});
HycopFactory.storage!.getFileBytes(fileId).then((value) {
// success
setState(() {
_downloadUint8List = value;
if (_downloadUint8List == null) {
_downloadStorageResult = '[${_getNowString()}] fail to download !!!';
}
});
}).catchError(
(error, stackTrace) {
// error
setState(() {
_deleteDataResult = '[${_getNowString()}] fail to delete !!!\n${error.toString()}';
});
},
);
}
Future<void> _uploadDataToStorage() async {
setState(() {
_uploadStorageResult = 'uploading...';
});
HycopFactory.storage!.setBucket();
String fileName = 'HycopTestImage.png';
String fileType = 'image';
final ByteData bytes = await rootBundle.load('assets/logo.png');
final Uint8List fileBytes = bytes.buffer.asUint8List();
FileModel? fileModel = await HycopFactory.storage!.uploadFile(
fileName,
fileType,
fileBytes,
makeThumbnail: true,
bucketId: 'HycopTest',
);
setState(() {
_uploadStorageResult =
(fileModel == null) ? '[${_getNowString()}] fail to upload !!!' : fileModel.id;
});
}
void _updateLocation(PointerEvent details) {
if (_uploadToRealtime) {
int nowMilliseconds = DateTime.now().millisecondsSinceEpoch;
setState(() {
_mouseX = details.position.dx;
_mouseY = details.position.dy;
});
if (nowMilliseconds - _lastRealtime > 1000) {
_lastRealtime = nowMilliseconds;
realtimeModel.posX.set(details.position.dx,
save: false, noUndo: true, dontChangeBookTime: true, dontRealTime: true);
realtimeModel.posY.set(details.position.dy,
save: false, noUndo: true, dontChangeBookTime: true, dontRealTime: true);
HycopFactory.realtime!
.setDelta(directive: 'set', mid: realtimeModel.mid, delta: realtimeModel.toMap());
}
}
}
Future<void> _uploadDataToRealtime() async {
if (_uploadToRealtime == false && _downloadFromRealtime == false) {
_uploadToRealtime = true;
FrameModel tmpModel = FrameModel('page=hycoptest', 'book=hycoptest');
realtimeModel.copyFrom(tmpModel, newMid: 'frame=hycoptest', pMid: 'page=hycoptest');
HycopFactory.realtime!.startTemp(realtimeModel.mid);
//HycopFactory.realtime!.setPrefix('hycop'); // ==> 필요없음
}
}
void realTimeCallback(
String listenerId, String directive, String userId, Map<String, dynamic> dataMap) {
//logger.finest('realTimeCallback invoker($directive, $userId)');
if (directive == 'set') {
String mid = dataMap["mid"] ?? '';
if (mid.isEmpty) {
return;
}
if (realtimeModel.mid == mid) {
realtimeModel.fromMap(dataMap);
setState(() {
_mouseX = realtimeModel.posX.value;
_mouseY = realtimeModel.posY.value;
});
}
}
}
Future<void> _downloadDataFromRealtime() async {
if (_uploadToRealtime == false && _downloadFromRealtime == false) {
_downloadFromRealtime = true;
FrameModel tmpModel = FrameModel('page=hycoptest', 'book=hycoptest');
realtimeModel.copyFrom(tmpModel, newMid: 'frame=hycoptest', pMid: 'page=hycoptest');
HycopFactory.realtime!.start();
HycopFactory.realtime!.addListener("page=hycoptest", "hycop_frame", realTimeCallback);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: SingleChildScrollView(
child: MouseRegion(
onHover: _updateLocation,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// init hycop
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _hycopInit();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Hycop init'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_hycopInitResult),
),
// create account
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _createAccount();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Create account'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_createAccountResult),
),
// login account
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _loginAccount();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Login account'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_loginAccountResult),
),
// delete account
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _deleteAccount();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Create account'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_deleteAccountResult),
),
// get db
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _getDataFromDB();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Get data from DB'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_getDataResult),
),
// set db
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _setDataToDB();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Set data To DB'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_setDataResult),
), // get
// delete db
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _deleteDataFromDB();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Delete data To DB'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_deleteDataResult),
),
// download from storage
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _downloadDataFromStorage();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Download data from Storage'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: _downloadUint8List == null
? Text(_downloadStorageResult)
: Container(
color: Colors.black,
child: Image.memory(
_downloadUint8List!,
width: 200,
)),
),
// upload to storage
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _uploadDataToStorage();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Upload data to Storage'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(_uploadStorageResult),
),
// upload realtime
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _uploadDataToRealtime();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Upload data to Realtime'),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text('X:$_mouseX, Y:$_mouseY'),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.grey[300])),
onPressed: () async {
await _downloadDataFromRealtime();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Download data from Realtime'),
),
),
),
],
),
),
),
),
);
}
}