Skip to content

Commit

Permalink
-Solved constructor issue
Browse files Browse the repository at this point in the history
-Adding factory pattern
  • Loading branch information
MohitAndroid committed Apr 4, 2019
1 parent c60b2df commit eb00581
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ public class BeaconHelper<T> {
private boolean isOnlyBeaconStuff;
private List<IBeacon> iBeacons;

public BeaconHelper(Activity context) {
private BeaconHelper(Activity context) {
this.context = context;
beaconKeySerializer = new BeaconKeySerializer();
}

public static BeaconHelper getInstance(Activity context) {
return new BeaconHelper(context);
}

public void startBeaconUpdates(List<T> data, long timeInterval, BeaconResultListener beaconResultListener) {
this.isOnlyBeaconStuff = false;
this.beaconResultListener = beaconResultListener;
this.data = data;

beaconKeySerializer = BeaconKeySerializer.getInstance();
beaconResultEntities = new ArrayList<>();
bluetoothManager =
(BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
Expand All @@ -44,15 +55,8 @@ public BeaconHelper(Activity context) {
oldKeys = new ArrayList<>();
newKeys = new ArrayList<>();
mHandler = new Handler();
iBeacons = new ArrayList<>();
timer = new Timer();
isOnlyBeaconStuff = false;
}

public void startBeaconUpdates(List<T> data, long timeInterval, BeaconResultListener beaconResultListener) {
this.isOnlyBeaconStuff = false;
this.beaconResultListener = beaconResultListener;
this.data = data;
try {
String validationErrorMsg = checkValidation();
if (!TextUtils.isEmpty(validationErrorMsg)) {
Expand Down Expand Up @@ -88,6 +92,14 @@ public void run() {
public void startBeaconUpdates(long timeInterval, BeaconListener beaconListener) {
this.isOnlyBeaconStuff = true;
this.beaconListener = beaconListener;

bluetoothManager =
(BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mHandler = new Handler();
iBeacons = new ArrayList<>();
timer = new Timer();

try {
String validationErrorMsg = checkValidation();
if (!TextUtils.isEmpty(validationErrorMsg)) {
Expand Down Expand Up @@ -178,7 +190,7 @@ public void run() {
};

private void getOnlyBeaconData(IBeacon iBeacon) {
try{
try {
boolean isAdd = true;
for (int i = 0; i < iBeacons.size(); i++) {
if (iBeacons.get(i).getBluetoothAddress().equals(iBeacon.getBluetoothAddress())) {
Expand All @@ -195,7 +207,7 @@ private void getOnlyBeaconData(IBeacon iBeacon) {
if (isAdd) {
iBeacons.add(iBeacon);
}
}catch (Exception e){
} catch (Exception e) {
displayBeaconError(e.getMessage());
}

Expand Down Expand Up @@ -249,6 +261,7 @@ private void getBeaconFilteredData(IBeacon iBeacon) {
private void displayError(String msg) {
this.beaconResultListener.onError(msg);
}

private void displayBeaconError(String msg) {
this.beaconListener.onError(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
public class BeaconKeySerializer {
List<FieldTypeEntity> fieldTypeEntities;

public BeaconKeySerializer() {
private BeaconKeySerializer() {
fieldTypeEntities = new ArrayList<>();
}

public static BeaconKeySerializer getInstance(){
return new BeaconKeySerializer();
}

public List<FieldTypeEntity> getBeaconAnnotationDetails(Object object) throws BeaconKeySerializeException {
try {
if (object == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public class BLEBeaconWrapper<T> {

public BLEBeaconWrapper(Activity context) {
this.context = context;
networkManager = new NetworkManager();
parserListClass = new ParserListClass();
beaconHelper = new BeaconHelper(this.context);
networkManager = NetworkManager.getInstance();
parserListClass = ParserListClass.getInstance();
beaconHelper = BeaconHelper.getInstance(this.context);
}


public void getBeaconData(String url, Class<T> t, Map<String, String> headerData,
long timeInterval, BleBeaconListener<T> tBleBeaconListener) {

Expand Down Expand Up @@ -118,7 +119,7 @@ public void onError(String errorMsg) {
}

private void BeaconOnlyWrapper() {
beaconHelper.startBeaconUpdates( timeInterval, new BeaconListener() {
beaconHelper.startBeaconUpdates(timeInterval, new BeaconListener() {
@Override
public void onResult(List<IBeacon> beaconResultEntities) {
tBeaconListener.onResult(beaconResultEntities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
public class NetworkManager {


public NetworkManager() {
private NetworkManager() {
}

public static NetworkManager getInstance(){
return new NetworkManager();
}

public void getRequest(String baseUrl, Map<String, String> headerData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ public class ParserListClass<T> {
private static final String JSON_OBJECT = "jsonObject";
private static final String JSON_ARRAY = "jsonArray";
private int count = 0;
private List<T> parableObjects;
private String jsonString;
private FilterListener<T> filterListener;

public ParserListClass() {
rootFields = new ArrayList<>();
count = 0;
gson = new Gson();
parableObjects = new ArrayList<>();
private ParserListClass() {
}

public static ParserListClass getInstance(){
return new ParserListClass();
}

private void setFields() throws ParseFilterException {
rootFields = new ArrayList<>();
try {
for (Field field : t.getDeclaredFields()) {
field.setAccessible(true);
Expand Down Expand Up @@ -80,6 +79,9 @@ public void parseData(Class<T> t, String jsonString, FilterListener<T> filterLis
this.filterListener = filterListener;
this.jsonString = jsonString;

count = 0;
gson = new Gson();

if (!TextUtils.isEmpty(this.jsonString) && this.t != null &&
this.filterListener != null) {
setFields();
Expand Down

0 comments on commit eb00581

Please sign in to comment.