-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'smarak/development' into products_v5
- Loading branch information
Showing
11 changed files
with
300 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enum ConnectivityStatus { WiFi, Cellular, Offline } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:connectivity/connectivity.dart'; | ||
import 'package:gogrocy/core/enums/connectivity_status.dart'; | ||
|
||
class ConnectivityService { | ||
StreamController<ConnectivityStatus> _connectionStatusController; | ||
|
||
ConnectivityService() { | ||
_connectionStatusController = StreamController<ConnectivityStatus>(); | ||
_initNetworkStatusListener(); | ||
} | ||
|
||
StreamController<ConnectivityStatus> get connectionStatusController => | ||
_connectionStatusController; | ||
|
||
void _initNetworkStatusListener() { | ||
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { | ||
_connectionStatusController.add(_getStatusFromResult(result)); | ||
}); | ||
} | ||
|
||
ConnectivityStatus _getStatusFromResult(ConnectivityResult result) { | ||
switch (result) { | ||
case ConnectivityResult.wifi: | ||
return ConnectivityStatus.WiFi; | ||
case ConnectivityResult.mobile: | ||
return ConnectivityStatus.Cellular; | ||
case ConnectivityResult.none: | ||
return ConnectivityStatus.Offline; | ||
} | ||
} | ||
|
||
void disposeStream(){ | ||
_connectionStatusController.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
import 'dart:io'; | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:gogrocy/core/enums/connectivity_status.dart'; | ||
import 'package:gogrocy/core/enums/viewstate.dart'; | ||
import 'package:gogrocy/core/services/connectivity_service.dart'; | ||
|
||
class BaseModel extends ChangeNotifier { | ||
ViewState _state = ViewState.Idle; | ||
|
||
ViewState get state => _state; | ||
|
||
StreamController<ConnectivityStatus> _connectivityStatusController = | ||
ConnectivityService().connectionStatusController; | ||
Stream<ConnectivityStatus> connectivityStatus = | ||
ConnectivityService().connectionStatusController.stream; | ||
bool hasConnection = true; | ||
|
||
BaseModel() { | ||
connectivityStatus.listen((ConnectivityStatus status) { | ||
switch (status) { | ||
case ConnectivityStatus.WiFi: | ||
this.hasConnection = true; | ||
print(hasConnection); | ||
notifyListeners(); | ||
break; | ||
case ConnectivityStatus.Cellular: | ||
this.hasConnection = true; | ||
notifyListeners(); | ||
break; | ||
case ConnectivityStatus.Offline: | ||
this.hasConnection = false; | ||
notifyListeners(); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
void setState(ViewState viewState) { | ||
_state = viewState; | ||
notifyListeners(); | ||
} | ||
|
||
Future<bool> checkInternetStatus() async { | ||
try { | ||
final result = await InternetAddress.lookup('google.com'); | ||
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { | ||
print('connected'); | ||
return true; | ||
} | ||
} on SocketException catch (_) { | ||
print('not connected'); | ||
return false; | ||
} | ||
return false; | ||
@override | ||
void dispose() { | ||
// TODO: implement dispose | ||
_connectivityStatusController.close(); | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,4 +114,8 @@ class LoginModel extends BaseModel { | |
} | ||
return user; | ||
} | ||
|
||
checkInternetStatus() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.