Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Sabrina/loading error (#66)
Browse files Browse the repository at this point in the history
* add loading status if load failed on launch, show error, prompt to kill and relaunch the app

* add developer backdoor to pint the url for internal debugging purposes
  • Loading branch information
sabrina-li authored May 28, 2020
1 parent 1540ec3 commit 14b0ca7
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.fullstorydev.shoppedemo.data;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.fullstory.FS;
import com.fullstorydev.shoppedemo.utilities.Constants;

public class CustomerInfo {
Expand Down Expand Up @@ -44,9 +47,12 @@ private CustomerInfo(OrderBuilder builder) {
this.securityCode = builder.securityCode;
}


public boolean validateOrder() throws NullPointerException, IllegalArgumentException{
this.isValid = true;

//backdoor for printing out the fullstory URL for internal debugging
if(this.firstName.equals("give me fullstory")) Log.i("CustomerInfo", "FullStory URL is: " + FS.getCurrentSessionURL());

if (firstName == null || firstName.length()<1) {
this.firstNameError.postValue("first name can not be null");
this.isValid = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.lifecycle.MutableLiveData;

import com.fullstorydev.shoppedemo.R;
import com.fullstorydev.shoppedemo.utilities.Constants;
import com.fullstorydev.shoppedemo.utilities.JsonHelper;
import com.fullstorydev.shoppedemo.utilities.NetworkUtils;
import com.google.gson.JsonParseException;
Expand All @@ -15,10 +16,17 @@

public class ProductRepository {
private MutableLiveData<List<Item>> mAllProducts; //products are sold in shop, fetched from API
private MutableLiveData<Constants.Status> mStatus;

public ProductRepository(Application application) {
mStatus = new MutableLiveData<>(Constants.Status.LOADING);
mAllProducts = new MutableLiveData<>();

reload(application);
}
public void reload(Application application){
mStatus.postValue(Constants.Status.LOADING);

// get from API every time
//TODO: save the product locally and call API in the backend, only update DB when it changes and notify and handle change
new Thread(() -> {
Expand All @@ -27,11 +35,14 @@ public ProductRepository(Application application) {
String resStr = NetworkUtils.getProductListFromURL(hostURLStr);
List<Item> list = JsonHelper.getProductListFromJsonString(resStr);
mAllProducts.postValue(list);
mStatus.postValue(Constants.Status.SUCCESS);
} catch (IOException | JsonParseException e) {
mStatus.postValue(Constants.Status.ERROR);
e.printStackTrace();
}
}).start();
}

public LiveData<List<Item>> getAllFromAPI() { return mAllProducts; }
public MutableLiveData<Constants.Status> getStatus() { return mStatus; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fullstorydev.shoppedemo.R;
import com.fullstorydev.shoppedemo.adapters.MarketProductAdapter;
import com.fullstorydev.shoppedemo.data.Item;
import com.fullstorydev.shoppedemo.utilities.Constants;

public class MarketFragment extends Fragment implements MarketEventHandlers {

Expand Down Expand Up @@ -48,6 +49,23 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

marketViewModel = new ViewModelProvider(this).get(MarketViewModel.class);
marketViewModel.getProductList().observe(this.getViewLifecycleOwner(), products -> mMarketProductAdapter.setProductList(products));

marketViewModel.getStatus().observe(this.getViewLifecycleOwner(), status ->{
View v = getView();
if(v != null){
if (status == Constants.Status.LOADING) {
v.findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
} else {
v.findViewById(R.id.progressbar).setVisibility(View.GONE);
}
//TODO: add network listener to detect network change and auto reload
if (status == Constants.Status.ERROR) {
v.findViewById(R.id.tv_error).setVisibility(View.VISIBLE);
} else {
v.findViewById(R.id.tv_error).setVisibility(View.GONE);
}
}
});
}

public void onClickAddToCart(Item item){ marketViewModel.increaseQuantityInCart(item); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@
import com.fullstorydev.shoppedemo.data.Item;
import com.fullstorydev.shoppedemo.data.ItemRepository;
import com.fullstorydev.shoppedemo.data.ProductRepository;
import com.fullstorydev.shoppedemo.utilities.Constants;

import java.util.List;

public class MarketViewModel extends AndroidViewModel {
private ProductRepository mProductRepository;
private ItemRepository mItemRepository;
private LiveData<List<Item>> mProductList;
private LiveData<Constants.Status> mStatus;
private Application application;

public MarketViewModel(Application application) {
super(application);
mProductRepository = new ProductRepository(application);
mItemRepository = new ItemRepository(application);
mProductList = mProductRepository.getAllFromAPI();
mStatus = mProductRepository.getStatus();
this.application = application;
}

public LiveData<List<Item>> getProductList() {
LiveData<List<Item>> getProductList() {
return mProductList;
}

public void increaseQuantityInCart(Item item){ mItemRepository.increaseQuantityInCart(item); }
LiveData<Constants.Status> getStatus() { return mStatus; }

void increaseQuantityInCart(Item item){ mItemRepository.increaseQuantityInCart(item); }
void reload(){ mProductRepository.reload(this.application); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ static public Integer[] getMonths() {
public static final String SECURITY_CODE = "securityCode";

public static final String PREFERENCE_FILE_KEY = "com.fullstorydev.shoppedemo.PREFERENCE_FILE_KEY";

public enum Status { SUCCESS, LOADING, ERROR };
}
14 changes: 14 additions & 0 deletions java/app/src/main/res/layout/fragment_market.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
android:id="@+id/progressbar"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_error"
android:visibility="gone"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_error"/>

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions java/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
<string name="security_code">Security Code</string>
<string name="subtotal">Subtotal:</string>
<string name="purchase">Purchase</string>

<string name="load_error">Error, please close the app and retry later!</string>
</resources>

0 comments on commit 14b0ca7

Please sign in to comment.