Last active
May 31, 2018 18:13
-
-
Save mikeuduc/553b48b2707cce1c9769bb4a953590a5 to your computer and use it in GitHub Desktop.
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
package com.simplecrew.simplecrewapp.company; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.support.v4.widget.SwipeRefreshLayout; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Base64; | |
import android.view.View; | |
import android.widget.Toast; | |
import java.util.List; | |
import java.util.Random; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
import com.mixpanel.android.mpmetrics.MixpanelAPI; | |
import com.simplecrew.sdk.coreapi.ScSdk; | |
import com.simplecrew.sdk.coreapi.listener.ScCallback; | |
import com.simplecrew.sdk.coreapi.model.ScCompany; | |
import com.simplecrew.sdk.coreapi.model.ScError; | |
import com.simplecrew.sdk.coreui.DialogAlert; | |
import com.simplecrew.sdk.coreui.RecyclerItemClickListener; | |
import com.simplecrew.simplecrewapp.AppConfig; | |
import com.simplecrew.simplecrewapp.BuildConfig; | |
import com.simplecrew.simplecrewapp.OwnActivity; | |
import com.simplecrew.simplecrewapp.R; | |
import com.simplecrew.simplecrewapp.campaign.CampaignActivity; | |
import com.simplecrew.simplecrewapp.user.LoginActivity; | |
import com.simplecrew.simplecrewapp.utils.DialogError; | |
import com.simplecrew.simplecrewapp.utils.UiUtils; | |
public class CompanyActivity extends OwnActivity implements RecyclerItemClickListener.OnItemClickListener { | |
private static final String MIXPANEL_DISTINCT_ID_NAME = "Mixpanel Example $distinctid"; | |
private MixpanelAPI mixpanel; | |
@BindView(R.id.recycler_view) | |
RecyclerView recyclerView; | |
@BindView(R.id.layout_swipe) | |
SwipeRefreshLayout swipeRefreshLayout; | |
CompanyAdapter companyAdapter = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_company_list); | |
initMixpanel(); | |
ButterKnife.bind(this); | |
onApplyViews(); | |
onApplyData(); | |
} | |
@OnClick(R.id.image_logout) | |
public void confirmLogout() { | |
showAlert(getString(R.string.str_confirm_logout), false, new DialogAlert.OnConfirmClickListener() { | |
@Override | |
public void onClick() { | |
logout(new ScCallback() { | |
@Override | |
public void onSuccess() { | |
startActivity(new Intent(CompanyActivity.this, LoginActivity.class)); | |
finish(); | |
} | |
}); | |
} | |
}); | |
} | |
@Override | |
public void onApplyViews() { | |
super.onApplyViews(); | |
// Recycler View | |
UiUtils.onApplyCompanyRecyclerView(CompanyActivity.this, recyclerView); | |
companyAdapter = new CompanyAdapter(); | |
recyclerView.setAdapter(companyAdapter); | |
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, this)); | |
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
swipeRefreshLayout.setEnabled( | |
((LinearLayoutManager) recyclerView.getLayoutManager()) | |
.findFirstCompletelyVisibleItemPosition() == 0); | |
} | |
}); | |
UiUtils.onApplySwipeColor(swipeRefreshLayout); | |
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
onApplyData(); | |
} | |
}); | |
} | |
@Override | |
public void onApplyData() { | |
super.onApplyData(); | |
if (!isNetworkConnected()) { | |
showDialogNoNet(); | |
} else getData(); | |
} | |
private void showProgress(boolean isShow) { | |
swipeRefreshLayout.setRefreshing(isShow); | |
} | |
private void getData() { | |
ScSdk.ScCompanyExecutor.getCompanies(new ScCallback.With<List<ScCompany>, ScError>() { | |
@Override | |
public void onSuccessful(List<ScCompany> data) { | |
showProgress(false); | |
if (data != null && data.size() > 0) | |
companyAdapter.replaceAll(data); | |
} | |
@Override | |
public void onFailure(ScError error, List<ScCompany> data) { | |
showProgress(false); | |
failure(error, new DialogError.OnDismissClickListener() { | |
@Override | |
public void onClick() { | |
logout(new ScCallback() { | |
@Override | |
public void onSuccess() { | |
startActivity(new Intent(CompanyActivity.this, LoginActivity.class)); | |
finish(); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
@Override | |
public void onItemClick(View view, int position) { | |
if (!isNetworkConnected()) { | |
showDialogNoNet(); | |
} else { | |
ScCompany scCompany = companyAdapter.get(position); | |
if (scCompany != null && scCompany.isSuspended()) { | |
failure(new ScError("Company account has been suspended!")); | |
} else { | |
AppConfig.getInstance().setCurrentCompany(scCompany); | |
startActivity(new Intent(CompanyActivity.this, CampaignActivity.class)); | |
finish(); | |
} | |
} | |
} | |
private void initMixpanel() { | |
String mixPanelToken; | |
if (BuildConfig.DEBUG) { | |
mixPanelToken = REMOVED_FOR_PUBLIC_GIST | |
} else { | |
mixPanelToken = REMOVED_FOR_PUBLIC_GIST | |
} | |
mixpanel = MixpanelAPI.getInstance(this, mixPanelToken); | |
MixpanelAPI.People people = mixpanel.getPeople(); | |
people.identify(ScSdk.ScUserExecutor.getUid()); | |
people.initPushHandling(REMOVED_FOR_PUBLIC_GIST); | |
mixpanel.getPeople().showNotificationIfAvailable(this); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
mixpanel.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment