-
Notifications
You must be signed in to change notification settings - Fork 0
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
リアルタイム通知画面のサンプル実装 #11
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
56c92e5
リアルタイム通知のサンプル実装
Yoshida-koshi 7f4c561
Merge remote-tracking branch 'origin/develop' into feature/debug-real…
Yoshida-koshi 7b7708c
Merge remote-tracking branch 'origin/develop' into feature/debug-real…
Yoshida-koshi 5735729
Podfileを更新
Yoshida-koshi 8e02a67
Merge remote-tracking branch 'origin/develop' into feature/debug-real…
Yoshida-koshi d472ef9
APIをMastodonRepositoryを使うように修正
Yoshida-koshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:pg_mobile/repository/mastodon_repository.dart'; | ||
import 'package:web_socket_channel/io.dart'; | ||
import 'package:web_socket_channel/web_socket_channel.dart'; | ||
import 'package:webview_flutter/webview_flutter.dart'; | ||
|
||
class SignInPage extends StatefulWidget { | ||
const SignInPage({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<SignInPage> createState() => _SignInPageState(); | ||
} | ||
|
||
class _SignInPageState extends State<SignInPage> { | ||
late final WebViewController controller; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
controller = WebViewController() | ||
..setJavaScriptMode(JavaScriptMode.unrestricted) | ||
..loadRequest(Uri.parse(MastodonRepository.instance.url)) | ||
..setNavigationDelegate( | ||
NavigationDelegate( | ||
onPageFinished: (String url) async { | ||
final uri = Uri.parse(url); | ||
if (uri.queryParameters['code'] != null) { | ||
final accessToken = await MastodonRepository.instance.signIn(uri); | ||
if (accessToken != null) { | ||
if (!mounted) return; | ||
Navigator.push( | ||
context, | ||
MaterialPageRoute( | ||
builder: (_) => | ||
DebugRealTimeNotificationPage(accessToken: accessToken), | ||
), | ||
); | ||
} | ||
} | ||
}, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("サインイン"), | ||
centerTitle: true, | ||
), | ||
body: Center( | ||
child: ElevatedButton( | ||
onPressed: () { | ||
showModalBottomSheet( | ||
isScrollControlled: true, | ||
enableDrag: true, | ||
backgroundColor: Colors.transparent, | ||
context: context, | ||
builder: (BuildContext context) { | ||
return SizedBox( | ||
height: MediaQuery.of(context).size.height * 0.75, | ||
child: LoginView( | ||
controller: controller, | ||
), | ||
); | ||
}, | ||
); | ||
}, | ||
child: const Text("サインインする"), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class DebugRealTimeNotificationPage extends StatefulWidget { | ||
final String accessToken; | ||
|
||
const DebugRealTimeNotificationPage({Key? key, required this.accessToken}) | ||
: super(key: key); | ||
|
||
@override | ||
State<DebugRealTimeNotificationPage> createState() => | ||
_DebugRealTimeNotificationPageState(); | ||
} | ||
|
||
class _DebugRealTimeNotificationPageState | ||
extends State<DebugRealTimeNotificationPage> { | ||
List<String> nameList = []; | ||
late final WebSocketChannel channel; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
// WebSocketの接続を行う | ||
channel = IOWebSocketChannel.connect( | ||
Uri.parse( | ||
"wss://community.4nonome.com/api/v1/streaming/?stream=user:notification&access_token=${widget.accessToken}"), | ||
); | ||
// リアルタイムで返ってきた通知を処理する | ||
channel.stream.listen((data) { | ||
Map<String, dynamic> body = json.decode(data); | ||
Map<String, dynamic> payloadData = json.decode(body["payload"]); | ||
if (mounted) { | ||
setState(() { | ||
nameList.add(payloadData['account']['username']); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("通知リスト"), | ||
centerTitle: true, | ||
), | ||
body: ListView.builder( | ||
itemCount: nameList.length, | ||
itemBuilder: (BuildContext context, int index) { | ||
return Text(nameList[index]); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class LoginView extends StatelessWidget { | ||
final WebViewController controller; | ||
const LoginView({Key? key, required this.controller}) : super(key: key); | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Login"), | ||
), | ||
body: WebViewWidget(controller: controller), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
天才か!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yoくんでもできるよ笑笑