This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added discount codes changed nav icon indentation changes indentation changes indentation changes refactor changes minor change * indentation changes * used string template
- Loading branch information
1 parent
2acd5c6
commit d41e69a
Showing
14 changed files
with
574 additions
and
1 deletion.
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
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
152 changes: 152 additions & 0 deletions
152
android/app/src/main/java/org/fossasia/openevent/core/discount/DiscountCodeFragment.java
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,152 @@ | ||
package org.fossasia.openevent.core.discount; | ||
|
||
import android.arch.lifecycle.ViewModelProviders; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v4.widget.SwipeRefreshLayout; | ||
import android.support.v7.widget.DefaultItemAnimator; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import org.fossasia.openevent.R; | ||
import org.fossasia.openevent.common.network.NetworkUtils; | ||
import org.fossasia.openevent.common.ui.base.BaseFragment; | ||
import org.fossasia.openevent.common.utils.Utils; | ||
import org.fossasia.openevent.core.auth.AuthUtil; | ||
import org.fossasia.openevent.core.auth.LoginActivity; | ||
import org.fossasia.openevent.data.DiscountCode; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import butterknife.BindView; | ||
import timber.log.Timber; | ||
|
||
public class DiscountCodeFragment extends BaseFragment { | ||
|
||
@BindView(R.id.discount_refresh_layout) | ||
protected SwipeRefreshLayout swipeRefreshLayout; | ||
@BindView(R.id.txt_no_discount_codes) | ||
protected TextView noDiscountCodeView; | ||
@BindView(R.id.discount_code_header) | ||
protected TextView discountCodeHeader; | ||
@BindView(R.id.list_discount_codes) | ||
protected RecyclerView discountCodesRecyclerView; | ||
|
||
private List<DiscountCode> discountCodes = new ArrayList<>(); | ||
private DiscountCodesListAdapter discountCodesListAdapter; | ||
private DiscountFragmentViewModel discountFragmentViewModel; | ||
|
||
@Nullable | ||
@Override | ||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
setHasOptionsMenu(true); | ||
final View view = super.onCreateView(inflater, container, savedInstanceState); | ||
setUpRecyclerView(); | ||
Utils.registerIfUrlValid(swipeRefreshLayout, this, this::refresh); | ||
discountFragmentViewModel = ViewModelProviders.of(this).get(DiscountFragmentViewModel.class); | ||
if (AuthUtil.isUserLoggedIn()) { | ||
if (NetworkUtils.haveNetworkConnection(getContext())) { | ||
swipeRefreshLayout.setRefreshing(true); | ||
downloadDiscountCodes(); | ||
} | ||
loadData(); | ||
} else { | ||
redirectToLogin(); | ||
} | ||
return view; | ||
} | ||
|
||
private void redirectToLogin() { | ||
Toast.makeText(getContext(), "User need to be logged in!", Toast.LENGTH_SHORT).show(); | ||
Intent intent = new Intent(getActivity(), LoginActivity.class); | ||
startActivity(intent); | ||
} | ||
|
||
private void downloadDiscountCodes() { | ||
discountFragmentViewModel.downloadDiscountCodes().observe(this, this::onDiscountCodeDownloadDone); | ||
} | ||
|
||
private void setUpRecyclerView() { | ||
discountCodesRecyclerView.setVisibility(View.VISIBLE); | ||
discountCodesListAdapter = new DiscountCodesListAdapter(discountCodes); | ||
discountCodesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); | ||
discountCodesRecyclerView.setNestedScrollingEnabled(false); | ||
discountCodesRecyclerView.setAdapter(discountCodesListAdapter); | ||
discountCodesRecyclerView.setItemAnimator(new DefaultItemAnimator()); | ||
} | ||
|
||
private void handleVisibility() { | ||
if (!discountCodes.isEmpty()) { | ||
discountCodeHeader.setVisibility(View.VISIBLE); | ||
discountCodesRecyclerView.setVisibility(View.VISIBLE); | ||
noDiscountCodeView.setVisibility(View.GONE); | ||
} else { | ||
discountCodeHeader.setVisibility(View.GONE); | ||
discountCodesRecyclerView.setVisibility(View.GONE); | ||
} | ||
} | ||
|
||
private void loadData() { | ||
discountFragmentViewModel.getDiscountCodes().observe(this, discountCodes -> { | ||
this.discountCodes.clear(); | ||
this.discountCodes.addAll(discountCodes); | ||
discountCodesListAdapter.notifyDataSetChanged(); | ||
handleVisibility(); | ||
}); | ||
} | ||
|
||
public void onDiscountCodeDownloadDone(boolean status) { | ||
if (!status) { | ||
Timber.d("Discount Codes Download failed"); | ||
if (getActivity() != null && swipeRefreshLayout != null) { | ||
Snackbar.make(swipeRefreshLayout, getActivity().getString(R.string.refresh_failed), Snackbar.LENGTH_LONG) | ||
.setAction(R.string.retry_download, view -> refresh()).show(); | ||
} | ||
} | ||
if (swipeRefreshLayout != null) | ||
swipeRefreshLayout.setRefreshing(false); | ||
} | ||
|
||
private void refresh() { | ||
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() { | ||
|
||
@Override | ||
public void networkAvailable() { | ||
if (AuthUtil.isUserLoggedIn()) { | ||
downloadDiscountCodes(); | ||
} else { | ||
redirectToLogin(); | ||
if (swipeRefreshLayout != null) | ||
swipeRefreshLayout.setRefreshing(false); | ||
} | ||
} | ||
|
||
@Override | ||
public void networkUnavailable() { | ||
onDiscountCodeDownloadDone(false); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected int getLayoutResource() { | ||
return R.layout.list_discount_codes; | ||
} | ||
|
||
@Override | ||
public void onDestroyView() { | ||
super.onDestroyView(); | ||
Utils.unregisterIfUrlValid(this); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
android/app/src/main/java/org/fossasia/openevent/core/discount/DiscountCodesListAdapter.java
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,29 @@ | ||
package org.fossasia.openevent.core.discount; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import org.fossasia.openevent.R; | ||
import org.fossasia.openevent.common.ui.base.BaseRVAdapter; | ||
import org.fossasia.openevent.data.DiscountCode; | ||
|
||
import java.util.List; | ||
|
||
public class DiscountCodesListAdapter extends BaseRVAdapter<DiscountCode, DiscountViewHolder> { | ||
|
||
public DiscountCodesListAdapter(List<DiscountCode> discountCodes) { | ||
super(discountCodes); | ||
} | ||
|
||
@Override | ||
public DiscountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_discount_code, parent, false); | ||
return new DiscountViewHolder(view); | ||
} | ||
|
||
public void onBindViewHolder(DiscountViewHolder holder, int position) { | ||
holder.bindDiscountCode(getItem(position)); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...oid/app/src/main/java/org/fossasia/openevent/core/discount/DiscountFragmentViewModel.java
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,68 @@ | ||
package org.fossasia.openevent.core.discount; | ||
|
||
import android.arch.lifecycle.LiveData; | ||
import android.arch.lifecycle.MutableLiveData; | ||
import android.arch.lifecycle.Transformations; | ||
import android.arch.lifecycle.ViewModel; | ||
|
||
import org.fossasia.openevent.common.api.APIClient; | ||
import org.fossasia.openevent.common.arch.LiveRealmData; | ||
import org.fossasia.openevent.data.DiscountCode; | ||
import org.fossasia.openevent.data.repository.RealmDataRepository; | ||
|
||
import java.util.List; | ||
|
||
import io.reactivex.android.schedulers.AndroidSchedulers; | ||
import io.reactivex.disposables.CompositeDisposable; | ||
import io.reactivex.schedulers.Schedulers; | ||
import timber.log.Timber; | ||
|
||
public class DiscountFragmentViewModel extends ViewModel { | ||
|
||
private LiveData<List<DiscountCode>> discountCodes; | ||
private RealmDataRepository realmRepo; | ||
private MutableLiveData<Boolean> discountCodesDownloadResponse; | ||
private CompositeDisposable compositeDisposable; | ||
|
||
public DiscountFragmentViewModel() { | ||
realmRepo = RealmDataRepository.getDefaultInstance(); | ||
compositeDisposable = new CompositeDisposable(); | ||
} | ||
|
||
|
||
public LiveData<List<DiscountCode>> getDiscountCodes() { | ||
if (discountCodes == null) { | ||
LiveRealmData<DiscountCode> discountCodeLiveRealmData = RealmDataRepository.asLiveData(realmRepo.getDiscountCodes()); | ||
discountCodes = Transformations.map(discountCodeLiveRealmData, input -> input); | ||
} | ||
return discountCodes; | ||
} | ||
|
||
public LiveData<Boolean> downloadDiscountCodes() { | ||
if (discountCodesDownloadResponse == null) | ||
discountCodesDownloadResponse = new MutableLiveData<>(); | ||
try { | ||
compositeDisposable.add(APIClient.getOpenEventAPI().getDiscountCodes() | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe(discountList -> { | ||
Timber.i("Downloaded Discount Codes"); | ||
realmRepo.saveDiscountCodes(discountList).subscribe(); | ||
discountCodesDownloadResponse.setValue(true); | ||
}, throwable -> { | ||
Timber.i("Discount Codes download failed"); | ||
discountCodesDownloadResponse.setValue(false); | ||
})); | ||
} catch (Exception e) { | ||
Timber.e(e); | ||
} | ||
|
||
return discountCodesDownloadResponse; | ||
} | ||
|
||
@Override | ||
protected void onCleared() { | ||
super.onCleared(); | ||
compositeDisposable.dispose(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
android/app/src/main/java/org/fossasia/openevent/core/discount/DiscountViewHolder.java
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,86 @@ | ||
package org.fossasia.openevent.core.discount; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import org.fossasia.openevent.R; | ||
import org.fossasia.openevent.common.utils.Utils; | ||
import org.fossasia.openevent.data.DiscountCode; | ||
|
||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
|
||
public class DiscountViewHolder extends RecyclerView.ViewHolder { | ||
|
||
@BindView(R.id.discount_code) | ||
protected TextView discountCodeTextView; | ||
|
||
@BindView(R.id.discount_code_value) | ||
protected TextView discountCodeValue; | ||
|
||
@BindView(R.id.discount_is_active) | ||
protected TextView discountIsActive | ||
; | ||
@BindView(R.id.discount_max_quantity) | ||
protected TextView discountMaxQuantity; | ||
|
||
@BindView(R.id.discount_min_quantity) | ||
protected TextView discountMinQuantity; | ||
|
||
@BindView(R.id.discount_tickets) | ||
protected TextView discountTicket; | ||
|
||
@BindView(R.id.discount_url) | ||
protected TextView discountUrl; | ||
|
||
@BindView(R.id.discount_used_for) | ||
protected TextView discountUsedFor; | ||
|
||
private Context context; | ||
|
||
public DiscountViewHolder(View itemView) { | ||
super(itemView); | ||
ButterKnife.bind(this, itemView); | ||
context = itemView.getContext(); | ||
} | ||
|
||
public void bindDiscountCode(DiscountCode discountCode) { | ||
String code = Utils.checkStringEmpty(discountCode.getCode()); | ||
String discountUrl = Utils.checkStringEmpty(discountCode.getDiscount_url()); | ||
String tickets = Utils.checkStringEmpty(discountCode.getTickets()); | ||
String usedFor = Utils.checkStringEmpty(discountCode.getUsed_for()); | ||
|
||
Resources res = context.getResources(); | ||
String minQuantity = String.format(res.getString(R.string.discount_code_min_quantity), discountCode.getMin_quantity()); | ||
String maxQuantity = String.format(res.getString(R.string.discount_code_max_quantity), discountCode.getMax_quantity()); | ||
String value = String.format(res.getString(R.string.discount_code_value), discountCode.getValue()); | ||
|
||
setStringFieldWithPrefix(discountCodeTextView, code, null); | ||
setStringFieldWithPrefix(discountCodeValue, value, null); | ||
setStringFieldWithPrefix(discountMinQuantity, minQuantity, null); | ||
setStringFieldWithPrefix(discountMaxQuantity, maxQuantity, null); | ||
setStringFieldWithPrefix(discountTicket, tickets, "Tickets : "); | ||
setStringFieldWithPrefix(discountUsedFor, usedFor, "Used For : "); | ||
setStringFieldWithPrefix(this.discountUrl, discountUrl, "Discount URL : "); | ||
} | ||
|
||
|
||
private void setStringFieldWithPrefix(TextView textView, String field, String prefix) { | ||
if (textView == null) | ||
return; | ||
|
||
if (!TextUtils.isEmpty(field.trim())) { | ||
textView.setVisibility(View.VISIBLE); | ||
if(prefix == null) | ||
textView.setText(field); | ||
else | ||
textView.setText(prefix + field); | ||
} else { | ||
textView.setVisibility(View.GONE); | ||
} | ||
} | ||
} |
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.