talkplus_flutter_sdk 0.5.4 copy "talkplus_flutter_sdk: ^0.5.4" to clipboard
talkplus_flutter_sdk: ^0.5.4 copied to clipboard

TalkPlus Flutter SDK

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:talkplus_flutter_sdk/entity/channel/TPChannel.dart';
import 'package:talkplus_flutter_sdk/entity/channel/TPMessage.dart';
import 'package:talkplus_flutter_sdk/entity/user/TPUser.dart';
import 'package:talkplus_flutter_sdk/talkplus_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  TPChannel? channel;
  TPUser? user;

  void test() async {
    await TalkPlusAPI.init("3ee30362-8459-4ff8-b69c-091be04cfd7a");

    final loginParams = TPLoginParams(loginType: TPLoginType.TPLoginAnonymous, userId: "test1")
    ..userName = "test1"
    ..translationLanguage = "ko";

    /// login
    user ??= await TalkPlusAPI.login(loginParams);

    /// join chat channel
    channel ??= await TalkPlusAPI.joinChannel("demo_channel");
    if(channel == null) {
      debugPrint("failed in joining the channel");
      return;
    }

    /// Check that there is(are) un-read message(s)
    final createdAt = channel?.mLastMessage?.getCreatedAt() ?? 0;
    final lastReadAt = channel?.getLastReadAt() ?? 0;
    debugPrint("Last Message CreateAt: $createdAt");
    debugPrint("LastReadAt: $lastReadAt, Channel ID: ${channel?.getChannelId()}");

    if(lastReadAt < createdAt) {
      debugPrint("unread message(s) left");
    } else {
      debugPrint("No unread message(s)");
    }

    /// get messages from the chat channel
    final messageParams = TPMessageRetrievalParams(channel: channel!)
    ..lastMessage = null
    ..translationLanguage = "ko";

    await TalkPlusAPI.getTPMessages(messageParams, (messages, hasNext) async {
      for (TPMessage message in messages.reversed) {
        debugPrint("Messages in channels, Text: ${message.getText()}, TranslatedText: ${message.getTranslatedText()}}");
      }
    });

    /// send message to the chat channel
    final sendMessageParams = TPMessageSendParams(contentType: TPMessageContentType.TPMessageContentText,
        messageType: TPMessageType.TPMessageText,
        channel: channel!)
      ..textMessage = "say Hello"
      ..translationLanguages = ["ko"];

    TPMessage? messageSent = await TalkPlusAPI.sendTPMessage(sendMessageParams);
    if(messageSent != null){
      debugPrint("Message Sent, Text: ${messageSent!.getText()}, TranslatedText: ${messageSent!.getTranslatedText()}}");
    }

    //await TalkPlusAPI.logout();
    //channel = null;
    //user = null;
  }

  XFile? image;
  final ImagePicker picker = ImagePicker();

  void uploadPhoto() async {
    debugPrint("uploadPhoto");

    var img = await picker.pickImage(source: ImageSource.gallery);
    image = img;

    String path = "${image?.path}";
    debugPrint("path: $path");
    if (image?.path != null) {
      File file = File(path);
      try {
        TPMessage? message = await TalkPlusAPI.sendFileMessage(
            channel!, "", TPMessage.TYPE_TEXT, file, null, null, null);
        debugPrint("sendFileMessage success: ${message?.getMessageId()}");
      } catch (err) {
        debugPrint("sendFileMessage err: $err");
      }
    }
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      // TalkPlusAPI.init(appId);
      // platformVersion =
      //     await _talkplusFlutterSdkPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              test();
            },
            child: const Text('Join Demo Channel'),
          ),
        ),
      ),
    );
  }
}