1 package com.moiseum.wolnelektury.view.main;
3 import android.app.ProgressDialog;
4 import android.content.ComponentName;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.pm.ApplicationInfo;
8 import android.content.pm.PackageManager;
9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.support.annotation.StringRes;
12 import android.support.customtabs.CustomTabsClient;
13 import android.support.customtabs.CustomTabsIntent;
14 import android.support.customtabs.CustomTabsServiceConnection;
15 import android.support.v4.app.FragmentManager;
16 import android.support.v4.content.ContextCompat;
17 import android.support.v4.view.GravityCompat;
18 import android.support.v4.widget.DrawerLayout;
19 import android.support.v7.app.ActionBarDrawerToggle;
20 import android.support.v7.app.AlertDialog;
21 import android.support.v7.widget.LinearLayoutManager;
22 import android.support.v7.widget.RecyclerView;
23 import android.view.MenuItem;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.TextView;
27 import android.widget.Toast;
29 import com.moiseum.wolnelektury.R;
30 import com.moiseum.wolnelektury.base.AbstractIntent;
31 import com.moiseum.wolnelektury.base.mvp.PresenterActivity;
32 import com.moiseum.wolnelektury.view.book.BookActivity;
33 import com.moiseum.wolnelektury.view.book.BookType;
34 import com.moiseum.wolnelektury.view.supportus.SupportUsActivity;
36 import org.greenrobot.eventbus.EventBus;
37 import org.greenrobot.eventbus.Subscribe;
39 import java.util.List;
41 import butterknife.BindView;
42 import butterknife.OnClick;
44 import static com.moiseum.wolnelektury.view.main.MainActivity.MainIntent.RELAUNCH_MESSAGE_KEY;
46 public class MainActivity extends PresenterActivity<MainPresenter> implements MainView {
48 private ProgressDialog progressDialog;
50 public static class MainIntent extends AbstractIntent {
52 static final String RELAUNCH_MESSAGE_KEY = "RelaunchMessageKey";
54 public MainIntent(Context context) {
55 super(context, MainActivity.class);
58 public MainIntent(@StringRes int relaunchMessageResId, Context context) {
59 super(context, MainActivity.class);
60 this.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
61 this.putExtra(RELAUNCH_MESSAGE_KEY, relaunchMessageResId);
65 public static class ChangeNavigationEvent {
66 private final NavigationElement element;
68 public ChangeNavigationEvent(NavigationElement element) {
69 if (element == NavigationElement.SEPARATOR || element == NavigationElement.SUPPORT_US) {
70 throw new IllegalArgumentException("Unsupported navigation element");
72 this.element = element;
75 public NavigationElement getElement() {
80 @BindView(R.id.drawer_layout)
81 DrawerLayout drawerLayout;
82 @BindView(R.id.rvNavigation)
83 RecyclerView rvNavigation;
84 @BindView(R.id.btnLogin)
86 @BindView(R.id.llLoggedInContainer)
87 View llLoggedInContainer;
88 @BindView(R.id.tvUsername)
91 private ActionBarDrawerToggle drawerToggle;
92 private NavigationAdapter navigationAdapter;
93 private NavigationElement currentNavigationElement;
96 public int getLayoutResourceId() {
97 return R.layout.activity_main;
101 public void prepareView(Bundle savedInstanceState) {
102 setBackButtonEnable(true);
104 EventBus.getDefault().register(this);
106 if (getIntent().hasExtra(RELAUNCH_MESSAGE_KEY)) {
107 Toast.makeText(this, getIntent().getIntExtra(RELAUNCH_MESSAGE_KEY, 0), Toast.LENGTH_SHORT).show();
112 protected MainPresenter createPresenter() {
113 return new MainPresenter(this);
117 public void onNewIntent(Intent intent) {
118 super.onNewIntent(intent);
119 String action = intent.getAction();
120 String data = intent.getDataString();
121 if (Intent.ACTION_VIEW.equals(action) && data != null) {
122 getPresenter().onBrowserCallback(data);
127 public void onDestroy() {
129 EventBus.getDefault().unregister(this);
132 private void initDrawer() {
133 navigationAdapter = new NavigationAdapter(this, () -> {
135 drawerLayout.closeDrawers();
137 navigationAdapter.setOnItemClickListener((item, view, position) -> {
138 if (item != NavigationElement.SEPARATOR) {
142 navigationAdapter.selectItem(NavigationElement.LIBRARY);
143 rvNavigation.setLayoutManager(new LinearLayoutManager(this));
144 rvNavigation.setAdapter(navigationAdapter);
146 drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) {
148 public void onDrawerOpened(View drawerView) {
149 super.onDrawerOpened(drawerView);
153 public void onDrawerClosed(View drawerView) {
154 super.onDrawerClosed(drawerView);
157 drawerLayout.addDrawerListener(drawerToggle);
159 selectItem(NavigationElement.LIBRARY);
162 public void selectItem(NavigationElement navigationElement) {
163 if (NavigationElement.PREMIUM == navigationElement) {
164 getPresenter().checkForPremiumBook();
166 currentNavigationElement = navigationElement;
167 FragmentManager fragmentManager = getSupportFragmentManager();
168 fragmentManager.beginTransaction().replace(R.id.content, navigationElement.getFragment()).commit();
169 setTitle(navigationElement.getTitle());
171 drawerLayout.closeDrawers();
175 protected void onPostCreate(Bundle savedInstanceState) {
176 super.onPostCreate(savedInstanceState);
177 drawerToggle.syncState();
181 public void onBackPressed() {
182 if (NavigationElement.LIBRARY != currentNavigationElement) {
183 selectItem(NavigationElement.LIBRARY);
185 super.onBackPressed();
190 public boolean onOptionsItemSelected(MenuItem item) {
191 return drawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
195 protected void onHomeClicked() {
196 drawerLayout.openDrawer(GravityCompat.START);
199 @SuppressWarnings("unused")
201 public void onChangeNavigation(ChangeNavigationEvent event) {
202 selectItem(event.getElement());
203 navigationAdapter.selectItem(event.getElement());
206 @OnClick(R.id.btnLogin)
207 public void onLoginClicked() {
208 getPresenter().onLoginClicked();
211 @OnClick(R.id.btnLogout)
212 public void onLogoutClicked() {
213 getPresenter().onLogoutClicked();
217 public void setLoggedIn(boolean loggedIn) {
219 btnLogin.setVisibility(View.GONE);
220 llLoggedInContainer.setVisibility(View.VISIBLE);
222 btnLogin.setVisibility(View.VISIBLE);
223 llLoggedInContainer.setVisibility(View.GONE);
228 public void setLoggedUsername(String username) {
229 tvUsername.setText(username);
233 public void setProgressDialogVisibility(boolean visible) {
234 if (visible && progressDialog == null) {
235 String dialogMessage = getString(R.string.main_view_progress);
236 progressDialog = ProgressDialog.show(this, null, dialogMessage, true, false);
237 } else if (!visible && progressDialog != null) {
238 progressDialog.hide();
239 progressDialog = null;
244 public void showToastMessage(int messageResId) {
245 Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();
249 public void showCustomTabsAuthentication(Uri authorizationUrl) {
250 showBrowserView(authorizationUrl);
254 public void showPremiumBook(String slug) {
255 startActivity(new BookActivity.BookIntent(slug, BookType.TYPE_PREMIUM, this));
259 public void showNoPremiumBookAvailable(boolean userLoggedIn) {
260 AlertDialog.Builder builder = new AlertDialog.Builder(this)
261 .setTitle(R.string.no_prapremiere_title);
264 builder.setMessage(R.string.no_prapremiere_message)
265 .setPositiveButton(R.string.become_a_friend, (dialog, which) -> getPresenter().onBecomeAFriendClick())
266 .setNegativeButton(R.string.no_thanks, (dialog, which) -> {
270 builder.setMessage(R.string.no_prapremiere_message_logged)
271 .setPositiveButton(R.string.OK, (dialog, which) -> {
280 public void showPremiumForm() {
285 public void showLoginFirst() {
286 new AlertDialog.Builder(this)
287 .setTitle(R.string.login)
288 .setMessage(R.string.login_first)
289 .setPositiveButton(R.string.login, (dialog, which) -> {
290 getPresenter().onLoginClicked();
292 .setNegativeButton(R.string.no_thanks, (dialog, which) -> {
300 public void relaunch(int relaunchMessageResId) {
301 MainIntent intent = new MainIntent(relaunchMessageResId, this);
302 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
303 startActivity(intent);