import 'dart:async';

import 'package:chat/data/constants.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:keyboard_utils/keyboard_listener.dart';
import 'package:keyboard_utils/keyboard_utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

class KeyboardBloc {
  KeyboardUtils _keyboardUtils = KeyboardUtils();
  StreamController<double> _streamController = StreamController<double>();
  Stream<double> get stream => _streamController.stream;

  KeyboardUtils get keyboardUtils => _keyboardUtils;

  void start() {
    _keyboardUtils.add(listener:
        KeyboardListener(willShowKeyboard: (double keyboardHeight) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();

      if (keyboardHeight > 10) {
        _streamController.sink.add(keyboardHeight);
        prefs.setDouble(Constants.KeyboardHeight, keyboardHeight);
      }
    },willHideKeyboard: () async{
      print('~~~~willHideKeyboard~~~~~~');
        MessageMgr().emit('Keyboard Hide');
    }));
  }

  void dispose() {
    _keyboardUtils.dispose();
    _streamController.close();
  }
}
