Skip to content

Commit

Permalink
updated to version 1.0.1+28
Browse files Browse the repository at this point in the history
  • Loading branch information
nixrajput committed Dec 29, 2022
1 parent 1a6ecd6 commit 6639937
Show file tree
Hide file tree
Showing 28 changed files with 699 additions and 355 deletions.
5 changes: 3 additions & 2 deletions lib/apis/providers/api_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:social_media_app/app_services/network_controller.dart';
import 'package:social_media_app/app_services/route_service.dart';
Expand Down Expand Up @@ -28,7 +29,7 @@ class ApiProvider {

String? baseUrl;

final _network = NetworkController.find;
final _networkService = NetworkController.instance;

/// This is the method that is called from the service class.
Future<dynamic> _catchAsyncApiError({
Expand All @@ -42,7 +43,7 @@ class ApiProvider {
}) async {
AppUtility.log('$feature Request');

if (_network.isConnected == false) {
if (_networkService.isConnected == false) {
AppUtility.log('Error: No network connection', tag: 'error');
RouteService.set(RouteStatus.noNetwork);
return;
Expand Down
52 changes: 40 additions & 12 deletions lib/apis/providers/socket_api_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:social_media_app/app_services/auth_service.dart';
import 'package:social_media_app/constants/urls.dart';
import 'package:social_media_app/utils/utility.dart';

Expand All @@ -21,32 +23,43 @@ class SocketApiProvider {
/// All the private and public variables
static WebSocket? _socket;
StreamController<dynamic>? _socketEventStream;
var _isDisposed = false;

Stream<dynamic>? get socketEventStream =>
_socketEventStream?.stream.asBroadcastStream();

WebSocket? get socket => _socket;

bool get isConnected => _socket?.readyState == WebSocket.open;

bool get isDisconnected => _socket?.readyState == WebSocket.closed;

bool get isConnecting => _socket?.readyState == WebSocket.connecting;

bool get isClosing => _socket?.readyState == WebSocket.closing;

bool get isNotConnected => _socket?.readyState != WebSocket.open;

bool get isDisposed => _isDisposed;

// All socket related functions.
Future<void> init(String token) async {
Future<void> init() async {
final _authService = AuthService.find;
AppUtility.log("Socket initializing...");

if (_socket != null && isConnected) {
AppUtility.log("Socket already connected.");
return;
}

if (token.isEmpty) {
AppUtility.log("Socket token is empty", tag: 'warning');
if (_authService.token.isEmpty) {
AppUtility.log("Socket token is empty");
return;
}

try {
_socket = await WebSocket.connect('${AppUrls.webSocketUrl}?token=$token');
_socket = await WebSocket.connect(
'${AppUrls.webSocketUrl}?token=${_authService.token}');

if (_socket != null && isConnected) {
AppUtility.log("Socket connection established");
Expand All @@ -56,12 +69,20 @@ class SocketApiProvider {
_socket!.listen(
_socketEventHandler,
onError: (error) {
_socket?.close();
_socketEventStream?.close();
_socketEventStream = null;
_socket = null;
AppUtility.log("Socket error: $error", tag: 'error');
reconnect(token);
reconnect(_authService.token);
},
onDone: () {
_socket?.close();
_socketEventStream?.close();
_socketEventStream = null;
_socket = null;
AppUtility.log("Socket done");
reconnect(token);
reconnect(_authService.token);
},
cancelOnError: true,
);
Expand All @@ -77,17 +98,24 @@ class SocketApiProvider {
_socketApi._socketEventStream?.add(event);
}

void close() {
_socketApi._socketEventStream?.close();
void dispose() {
AppUtility.log("Socket disposing...");
_socket?.close();
_socketApi._socketEventStream = null;
_socketEventStream?.close();
_socketEventStream = null;
_socket = null;
AppUtility.log("Socket closed");
_isDisposed = true;
AppUtility.log("Socket disposed");
}

void reconnect(String token) {
close();
init(token);
if (isDisposed) {
AppUtility.log("Socket is disposed");
return;
}

AppUtility.log("Socket reconnecting...");
init();
}

void send(String message) {
Expand Down
29 changes: 18 additions & 11 deletions lib/app_services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:math' show Random;

import 'package:device_info_plus/device_info_plus.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -125,14 +126,28 @@ class AuthService extends GetxService {
}
}

Future<void> deleteAllLocalDataAndCache() async {
await StorageService.remove('loginData');
await StorageService.remove('profileData');
await StorageService.remove("fcmToken");
await HiveService.deleteAllBoxes();
AppUtility.log('Local Data Removed');
}

Future<void> _logout() async {
final _socket = SocketApiProvider();
final _chatController = ChatController.find;
AppUtility.log("Logout Request");
setToken = '';
setExpiresAt = 0;
_isLoggedIn = false;
SocketApiProvider().close();
await ChatController.find.close();

if (_socket.isConnected) {
_socket.dispose();
await _chatController.close();
}
await FirebaseMessaging.instance.deleteToken();
await deleteAllLocalDataAndCache();
_isLoggedIn = false;
AppUtility.log("Logout Success");
AppUtility.showSnackBar(
'Logout Successfully',
Expand Down Expand Up @@ -168,14 +183,6 @@ class AuthService extends GetxService {
AppUtility.log('Login Data Saved to Local Storage');
}

Future<void> deleteAllLocalDataAndCache() async {
await StorageService.remove('loginData');
await StorageService.remove('profileData');
await StorageService.remove("fcmToken");
await HiveService.deleteAllBoxes();
AppUtility.log('Local Data Removed');
}

Future<void> saveFcmTokenToLocalStorage(String fcmToken) async {
if (fcmToken.isEmpty) {
AppUtility.log('Fcm Token is empty', tag: 'error');
Expand Down
31 changes: 31 additions & 0 deletions lib/app_services/background_services.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:social_media_app/app_services/network_controller.dart';
import 'package:social_media_app/utils/utility.dart';
import 'package:workmanager/workmanager.dart';

const backgroundMessageTask = "backgroundMessageTask";
const backgroundNotificationTask = "backgroundNotificationTask";

@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
switch (task) {
case backgroundMessageTask:
final networkService = NetworkController.instance;
if (!networkService.isInitialized) {
await networkService.init();
}
if (networkService.isConnected) {
AppUtility.log('Network is connected');
}
AppUtility.log('Background Message Task');
break;
case backgroundNotificationTask:
AppUtility.log('Background Notification Task');
break;
case Workmanager.iOSBackgroundTask:
AppUtility.log('iOS Background Task');
break;
}
return Future.value(true);
});
}
Loading

0 comments on commit 6639937

Please sign in to comment.