Skip to content

Commit

Permalink
added stop functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yusera-at-dwao committed Dec 10, 2024
1 parent 6442a62 commit 230a433
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 65 deletions.
2 changes: 2 additions & 0 deletions alium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ dependencies {
implementation 'com.android.volley:volley:1.2.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.squareup.retrofit2:retrofit:2.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.11.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
8 changes: 8 additions & 0 deletions alium/src/main/java/com/dwao/alium/listeners/Observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dwao.alium.listeners;

public interface Observer {


public void stop();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dwao.alium.listeners;

import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;

public interface SurveyConfigResponseListener {
@GET
Call<JSONObject> configResponse(@Url String anEmptyString);

}
10 changes: 10 additions & 0 deletions alium/src/main/java/com/dwao/alium/listeners/SurveyLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dwao.alium.listeners;

import com.dwao.alium.survey.AliumSurveyLoader;

public interface SurveyLoader extends Observer{
public void stop();
public void addSurveyLoader(AliumSurveyLoader loader);
public boolean isStopped();
public String getScreenName();
}
16 changes: 12 additions & 4 deletions alium/src/main/java/com/dwao/alium/network/VolleyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
import org.json.JSONObject;

public class VolleyService {
private RequestQueue queue;
public static final String SURVEY_REQUEST_TAG="SURVEY_REQ";
public RequestQueue getSurveyQueue(){
return this.queue;
}

public void loadRequestWithVolley(Context context, String url){
Log.d("post_url", url);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
RequestQueue config_queue = Volley.newRequestQueue(context);
StringRequest stringRequest=new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
@Override
Expand All @@ -32,11 +38,12 @@ public void onErrorResponse(VolleyError error) {
Log.e("trackWithAlium", "error: "+error.toString());
}
}) ;
queue.add(stringRequest);
config_queue.add(stringRequest);

}
public void callVolley(Context context, String url, VolleyResponseListener volleyResponseListener){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
queue = Volley.newRequestQueue(context);
Log.d("fetch-main", url);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
Expand All @@ -59,8 +66,9 @@ public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});

stringRequest.addMarker("SURVEY_REQ");
// Add the request to the RequestQueue.
queue.add(stringRequest);

}
}
85 changes: 47 additions & 38 deletions alium/src/main/java/com/dwao/alium/survey/Alium.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.dwao.alium.survey;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;
//import androidx.lifecycle.ProcessLifecycleOwner;

import com.dwao.alium.listeners.SurveyLoader;
import com.dwao.alium.listeners.VolleyResponseListener;
import com.dwao.alium.models.SurveyConfig;
import com.dwao.alium.network.VolleyService;
Expand All @@ -20,10 +16,8 @@
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class Alium {
Expand All @@ -35,7 +29,7 @@ public class Alium {
static boolean isAppInForeground(){
return appState;
}

static Map<String, SurveyLoader> surveyLoaderMap=new HashMap<>();
private static VolleyService volleyService;
private static String configURL;
private Alium(){
Expand Down Expand Up @@ -82,34 +76,34 @@ public static void config(Application application,String url){
// ProcessLifecycleOwner.get().getLifecycle().addObserver(appLifeCycleListener);
}

public static void trigger(Activity activity, SurveyParameters parameters){
public static SurveyLoader trigger(Activity activity, SurveyParameters parameters){
if (configURL == null) {
throw new IllegalStateException("Configuration URL not set. Call configure() method first.");
}
SurveyLoader surveyLoader=new SurveyLoaderObserver(parameters.screenName);
if(surveyConfigMap.isEmpty()) {
volleyService.callVolley(activity, configURL, new ConfigURLResponseListener(activity, parameters));
volleyService.callVolley(activity, configURL, new ConfigURLResponseListener(activity, parameters, surveyLoader,
new ConfigURLResponseListener.Callback() {
@Override
public void onCall() {
AliumSurveyLoader aliumSurveyLoader= new AliumSurveyLoader(activity, parameters, surveyConfigMap);
aliumSurveyLoader.showSurvey();
surveyLoader.addSurveyLoader(aliumSurveyLoader);
}
}));
Log.d("Alium-initialized", "calling survey on" + parameters.screenName);
}else{
Log.d("helllo", surveyConfigJSON.toString());
new AliumSurveyLoader(activity, parameters, surveyConfigMap)
.showSurvey();

}

Log.d("helllo", surveyConfigJSON.toString());
AliumSurveyLoader aliumSurveyLoader= new AliumSurveyLoader(activity, parameters, surveyConfigMap);
aliumSurveyLoader.showSurvey();
surveyLoader.addSurveyLoader(aliumSurveyLoader);
return surveyLoader;


}
// public static void trigger(FragmentActivity activity, SurveyParameters parameters){
// if (configURL == null) {
// throw new IllegalStateException("Configuration URL not set. Call configure() method first.");
// }
// if(surveyConfigMap.isEmpty()) {
// volleyService.callVolley(activity, configURL, new ConfigURLResponseListener(activity, parameters));
// Log.d("Alium-initialized", "calling survey on" + parameters.screenName);
// }else{
// Log.d("helllo", surveyConfigJSON.toString());
// new AliumSurveyLoader(activity, parameters, surveyConfigMap)
// .showSurvey();
// }
//
// }

public static void trigger( Fragment fragment, SurveyParameters parameters){
if (configURL == null) {
throw new IllegalStateException("Configuration URL not set. Call configure() method first.");
Expand All @@ -133,8 +127,8 @@ public static void trigger(android.app.Fragment fragment, SurveyParameters param
Log.d("Alium-initialized", "calling survey on" + parameters.screenName);
}else{
Log.d("helllo", surveyConfigJSON.toString());
new AliumSurveyLoader(fragment, parameters, surveyConfigMap)
.showSurvey();
new AliumSurveyLoader(fragment, parameters, surveyConfigMap).showSurvey();

}

}
Expand All @@ -143,9 +137,16 @@ private static class ConfigURLResponseListener implements VolleyResponseListener
SurveyParameters surveyParameters;
Fragment xfragment;
android.app.Fragment fragment;
ConfigURLResponseListener(Activity activity,SurveyParameters parameters){
SurveyLoader surveyLoader;
Callback callback;
interface Callback{
public void onCall();
}
ConfigURLResponseListener(Activity activity,SurveyParameters parameters, SurveyLoader surveyLoader, Callback callback){
this.surveyLoader=surveyLoader;
this.activity=activity;
surveyParameters=parameters;
this.callback=callback;
}
ConfigURLResponseListener(Fragment xfragment,SurveyParameters parameters){
this.xfragment=xfragment;
Expand All @@ -166,12 +167,20 @@ public void onResponseReceived(JSONObject jsonObject) {


Log.d("SurveyConfig", surveyConfigMap.toString());
if(activity!=null){
new AliumSurveyLoader(activity, surveyParameters, surveyConfigMap)
.showSurvey();
}else if(fragment!=null){
new AliumSurveyLoader(fragment, surveyParameters, surveyConfigMap)
.showSurvey();
if(xfragment!=null){
trigger(xfragment, surveyParameters);
// new AliumSurveyLoader(fragment, surveyParameters, surveyConfigMap)
// .showSurvey();
} else if(fragment!=null){
trigger(fragment, surveyParameters);
// new AliumSurveyLoader(fragment, surveyParameters, surveyConfigMap)
// .showSurvey();
}
else if(activity!=null) {
callback.onCall();
// trigger(activity, surveyParameters);
// new AliumSurveyLoader(activity, surveyParameters, surveyConfigMap)
// .showSurvey();
}

}
Expand Down
Loading

0 comments on commit 230a433

Please sign in to comment.