From da641515fa1e16604909f3fdbd7f12209141b2dd Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 20 Apr 2020 13:19:49 +0200 Subject: [PATCH] Display the name of the call --- lib/helpers/conversations_helper.dart | 15 +++++- lib/ui/screens/call_screen.dart | 68 ++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 47a3eb1..2b7781f 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -143,6 +143,19 @@ class ConversationsHelper { return _conversationsDatabaseHelper.get(id); } + /// Get information about a conversation. The method throws an [Exception] in + /// case of failure + /// + /// Return value of this method is never null. + Future getSingleOrThrow(int id, {bool force = false}) async { + final conv = await this.getSingle(id, force: force); + + if (conv == null) + throw Exception("Could not get information about the conversation!"); + + return conv; + } + /// Get the name of a [conversation]. This requires information /// about the users of this conversation static String getConversationName( @@ -187,7 +200,7 @@ class ConversationsHelper { } } - /// Asynchronously get the name fo the conversation + /// Asynchronously get the name of the conversation /// /// Unlike the synchronous method, this method does not need information /// about the members of the conversation diff --git a/lib/ui/screens/call_screen.dart b/lib/ui/screens/call_screen.dart index b3866ac..14ddd4e 100644 --- a/lib/ui/screens/call_screen.dart +++ b/lib/ui/screens/call_screen.dart @@ -1,3 +1,9 @@ +import 'package:comunic/helpers/conversations_helper.dart'; +import 'package:comunic/models/conversation.dart'; +import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; +import 'package:comunic/ui/widgets/safe_state.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Call screen @@ -16,9 +22,67 @@ class CallScreen extends StatefulWidget { _CallScreenState createState() => _CallScreenState(); } -class _CallScreenState extends State { +class _CallScreenState extends SafeState { + // Widget properties + int get convID => widget.convID; + + // State properties + Conversation _conversation; + String _convName; + var _error = false; + + @override + void initState() { + super.initState(); + _initCall(); + } + + @override + void dispose() { + super.dispose(); + _endCall(); + } + + void _initCall() async { + try { + setState(() => _error = false); + + // First, load information about the conversation + _conversation = await ConversationsHelper().getSingleOrThrow(convID); + _convName = + await ConversationsHelper.getConversationNameAsync(_conversation); + assert(_convName != null); + + setState(() {}); + } catch (e, stack) { + print("Could not initialize call! $e\n$stack"); + setState(() => _error = true); + } + } + + void _endCall() async {} + @override Widget build(BuildContext context) { - return Container(); + return Scaffold( + appBar: AppBar( + leading: ComunicBackButton(), + title: + _convName == null ? CircularProgressIndicator() : Text(_convName), + ), + body: _buildBody(), + ); + } + + /// Build widget body + Widget _buildBody() { + if (_error) + // Handle errors + return buildErrorCard(tr("Could not initialize call!"), actions: [ + MaterialButton( + onPressed: () => _initCall(), + child: Text(tr("Try again").toUpperCase()), + ) + ]); } }