talkplus_flutter_sdk 0.6.1 copy "talkplus_flutter_sdk: ^0.6.1" to clipboard
talkplus_flutter_sdk: ^0.6.1 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;
  }

  void uploadPhoto() async {
    debugPrint("uploadPhoto");
    final ImagePicker picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.camera);
    String path = "${image?.path}";

    if (image?.path == null) {
      debugPrint("image path is null");
      return;
    }

    debugPrint("path: $path");
    File file = File(path);

    final sendMessageParams = TPMessageSendParams(contentType: TPMessageContentType.TPMessageContentFile,
        messageType: TPMessageType.TPMessageText,
        channel: channel!)
      ..file = file
      ..textMessage = "";

    TPMessage? message = await TalkPlusAPI.sendTPMessage(sendMessageParams);
    if(message != null) {
      debugPrint("success: ${message?.getMessageId()}");
    } else {
      debugPrint("failed in sending files");
    }
  }

  // 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'),
          ),
        ),
      ),
    );
  }
}