From abb5c33d02f9cb10fee9a71af1ee227d5ef7e23c Mon Sep 17 00:00:00 2001 From: Vlad Golubev Date: Wed, 4 Feb 2015 22:25:42 +0200 Subject: [PATCH 1/8] Remove fragment 'caching' --- .../ua/samosfator/moduleok/FragmentUtils.java | 34 ------------------- .../ua/samosfator/moduleok/MainActivity.java | 6 ++-- .../sections/SectionClickListener.java | 13 ++++--- 3 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java diff --git a/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java b/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java deleted file mode 100644 index 39cb7cc..0000000 --- a/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -package ua.samosfator.moduleok; - -import ua.samosfator.moduleok.fragment.LoginFragment; -import ua.samosfator.moduleok.fragment.LogoutFragment; -import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; -import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; - -public class FragmentUtils { - - private static LoginFragment loginFragment; - private static LogoutFragment logoutFragment; - private static LastTotalFragment lastTotalFragment; - private static ModulesFragment modulesFragment; - - public static LoginFragment getLoginFragment() { - if (loginFragment == null) loginFragment = new LoginFragment(); - return loginFragment; - } - - public static LogoutFragment getLogoutFragment() { - if (logoutFragment == null) logoutFragment = new LogoutFragment(); - return logoutFragment; - } - - public static LastTotalFragment getLastTotalFragment() { - if (lastTotalFragment == null) lastTotalFragment = new LastTotalFragment(); - return lastTotalFragment; - } - - public static ModulesFragment getModulesFragment() { - if (modulesFragment == null) modulesFragment = new ModulesFragment(); - return modulesFragment; - } -} diff --git a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java index 1198734..1343965 100644 --- a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java +++ b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java @@ -19,6 +19,8 @@ import ua.samosfator.moduleok.event.LoginEvent; import ua.samosfator.moduleok.event.LogoutEvent; import ua.samosfator.moduleok.event.RefreshEvent; +import ua.samosfator.moduleok.fragment.LoginFragment; +import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; import ua.samosfator.moduleok.fragment.navigation_drawer_fragment.NavigationDrawerFragment; import ua.samosfator.moduleok.notification.ScoreCheckerService; @@ -98,13 +100,13 @@ private void initAndSetAccountInfo() { private void openLastTotalFragment() { getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getLastTotalFragment()) + .replace(R.id.main_container, new LastTotalFragment()) .commit(); } private void openLoginFragment() { getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getLoginFragment()) + .replace(R.id.main_container, new LoginFragment()) .commit(); } diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java index d9bc598..00c7628 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java @@ -15,8 +15,11 @@ import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.Auth; -import ua.samosfator.moduleok.FragmentUtils; import ua.samosfator.moduleok.R; +import ua.samosfator.moduleok.fragment.LoginFragment; +import ua.samosfator.moduleok.fragment.LogoutFragment; +import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; +import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; import ua.samosfator.moduleok.recyclerview.RecyclerItemClickListener; public class SectionClickListener implements RecyclerItemClickListener.OnItemClickListener { @@ -37,7 +40,7 @@ public void onItemClick(View view, int position) { switch (clickedSection) { case LAST_TOTAL: { mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getLastTotalFragment()) + .replace(R.id.main_container, new LastTotalFragment()) .commit(); mDrawerLayout.closeDrawers(); highlightSelectedSection(view); @@ -45,7 +48,7 @@ public void onItemClick(View view, int position) { } case MODULES: { mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getModulesFragment()) + .replace(R.id.main_container, new ModulesFragment()) .commit(); mDrawerLayout.closeDrawers(); highlightSelectedSection(view); @@ -54,11 +57,11 @@ public void onItemClick(View view, int position) { case LOG_IN: { if (Auth.isLoggedIn()) { mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getLogoutFragment()) + .replace(R.id.main_container, new LogoutFragment()) .commit(); } else { mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, FragmentUtils.getLoginFragment()) + .replace(R.id.main_container, new LoginFragment()) .commit(); } mDrawerLayout.closeDrawers(); From 3dbc8e59acdab18cb26d803e00106e36d3809e8f Mon Sep 17 00:00:00 2001 From: Vlad Golubev Date: Thu, 5 Feb 2015 00:38:23 +0200 Subject: [PATCH 2/8] Optimize Modules tab --- .../moduleok/LoadPageAsyncTask.java | 4 ++-- .../modules_fragment/ModuleFragment.java | 23 +++++++++++++++--- .../modules_fragment/ModulesFragment.java | 9 +++++-- .../modules_fragment/ModulesPagerAdapter.java | 24 ++++++++++++------- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java b/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java index fb2c811..95901ea 100644 --- a/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java +++ b/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java @@ -18,8 +18,8 @@ class LoadPageAsyncTask extends AsyncTask { protected String doInBackground(Void... params) { Document mainPage = Jsoup.parse("
"); try { -// mainPage = getFromCustomRemoteSource(); - mainPage = getFromRemoteSource(); + mainPage = getFromCustomRemoteSource(); +// mainPage = getFromRemoteSource(); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java index 63ae166..b946550 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java @@ -8,6 +8,9 @@ import android.view.View; import android.view.ViewGroup; +import java.util.HashMap; +import java.util.Map; + import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.R; @@ -19,6 +22,10 @@ public class ModuleFragment extends Fragment { private ModuleSubjectItemAdapter moduleSubjectItemAdapter; + /////////////////// + private Map cacheRecyclerView = new HashMap<>(); + //////////////////// + public ModuleFragment() { // Required empty public constructor } @@ -29,11 +36,20 @@ public void onResume() { super.onResume(); } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + initModuleSubjectItemAdapter(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_module, container, false); - initModuleSubjectItemAdapter(); - initRecyclerView(rootView); + if (cacheRecyclerView.get(getArguments().getInt("module")) == null) { + initRecyclerView(rootView); + } else { + cacheRecyclerView.get(getArguments().getInt("module")); + } return rootView; } @@ -43,10 +59,11 @@ private void initModuleSubjectItemAdapter() { } private void initRecyclerView(View rootView) { - if (moduleSubjectItemAdapter == null) initModuleSubjectItemAdapter(); RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.modules_subjects_recycler_view); recyclerView.setAdapter(moduleSubjectItemAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + + cacheRecyclerView.put(getArguments().getInt("module"), recyclerView); } @SuppressWarnings("UnusedDeclaration") diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java index 20a4538..06f173e 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java @@ -30,6 +30,8 @@ public class ModulesFragment extends Fragment { private static int maxModulesCount; static List mSubjects = new ArrayList<>(); private View rootView; + private ModulesPagerAdapter modulesPagerAdapter; + public ModulesFragment() { // Required empty public constructor @@ -55,8 +57,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa private void initTabStrip(View rootView) { ViewPager pager = (ViewPager) rootView.findViewById(R.id.modules_viewpager); - ModulesPagerAdapter modulesPagerAdapter = new ModulesPagerAdapter(getChildFragmentManager(), maxModulesCount); + modulesPagerAdapter = new ModulesPagerAdapter(getChildFragmentManager(), maxModulesCount); pager.setAdapter(modulesPagerAdapter); + pager.setOffscreenPageLimit(maxModulesCount - 1); PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.modules_tabs); tabs.setViewPager(pager); @@ -82,7 +85,9 @@ private static void openLoginFragment() { @SuppressWarnings("UnusedDeclaration") public void onEvent(RefreshEndEvent event) { - initTabStrip(rootView); + fragmentManager.beginTransaction() + .replace(R.id.main_container, new ModulesFragment()) + .commit(); } @Override diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesPagerAdapter.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesPagerAdapter.java index 42e3275..fc4c859 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesPagerAdapter.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesPagerAdapter.java @@ -6,6 +6,9 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import java.util.ArrayList; +import java.util.List; + import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.R; @@ -21,6 +24,7 @@ class ModulesPagerAdapter extends FragmentPagerAdapter { appResources.getString(R.string.module_3_name), appResources.getString(R.string.module_4_name), appResources.getString(R.string.module_5_name), appResources.getString(R.string.module_6_name) }; + private List moduleFragmentList = new ArrayList<>(maxModulesCount); public ModulesPagerAdapter(FragmentManager fm, int maxModulesCount) { super(fm); @@ -39,13 +43,17 @@ public int getCount() { @Override public Fragment getItem(int position) { - ModuleFragment moduleFragment = new ModuleFragment(); - - Bundle bundle = new Bundle(); - bundle.putInt("module", position); - - moduleFragment.setArguments(bundle); - - return moduleFragment; + if (position >= moduleFragmentList.size()) { + ModuleFragment moduleFragment = new ModuleFragment(); + Bundle bundle = new Bundle(); + bundle.putInt("module", position); + moduleFragment.setArguments(bundle); + + moduleFragmentList.add(moduleFragment); + + return moduleFragment; + } else { + return moduleFragmentList.get(position); + } } } From af31cdbd0257f67e92dfe13b03d51f6df3ff357c Mon Sep 17 00:00:00 2001 From: Vlad Golubev Date: Thu, 5 Feb 2015 01:20:58 +0200 Subject: [PATCH 3/8] Optimize Refreshing --- app/src/main/AndroidManifest.xml | 2 +- .../moduleok/LoadPageAsyncTask.java | 5 ---- .../ua/samosfator/moduleok/MainActivity.java | 7 +++++ .../LastTotalFragment.java | 9 +++---- .../modules_fragment/ModuleFragment.java | 10 +------ .../sections/SectionClickListener.java | 26 ++++++++++--------- 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e27ffdb..c23ab3b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ EventBus.getDefault().post(new RefreshEndEvent())); - } - private Document getFromRemoteSource() throws IOException { return Jsoup.connect("http://mod.tanet.edu.te.ua/ratings/index") .cookie("PHPSESSID", Preferences.read("SESSIONID", "")).get(); diff --git a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java index 1343965..9bd9ab3 100644 --- a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java +++ b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java @@ -18,6 +18,7 @@ import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.event.LoginEvent; import ua.samosfator.moduleok.event.LogoutEvent; +import ua.samosfator.moduleok.event.RefreshEndEvent; import ua.samosfator.moduleok.event.RefreshEvent; import ua.samosfator.moduleok.fragment.LoginFragment; import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; @@ -147,6 +148,12 @@ public void onEvent(LogoutEvent event) { eraseAccountInfo(); } + @SuppressWarnings("UnusedDeclaration") + public void onEvent(RefreshEvent event) { + StudentKeeper.refreshStudent(); + EventBus.getDefault().post(new RefreshEndEvent()); + } + @Override protected void onStop() { EventBus.getDefault().unregister(this); diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java index af72a4d..ca387bb 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java @@ -19,7 +19,7 @@ import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.R; import ua.samosfator.moduleok.StudentKeeper; -import ua.samosfator.moduleok.event.RefreshEvent; +import ua.samosfator.moduleok.event.RefreshEndEvent; import ua.samosfator.moduleok.event.SemesterChangedEvent; import ua.samosfator.moduleok.fragment.LoginFragment; import ua.samosfator.moduleok.parser.Subject; @@ -63,9 +63,9 @@ private void initRecyclerView(View rootView) { private void initSubjects() { try { - int semester = StudentKeeper.getCurrentSemesterIndex(); + int semesterIndex = StudentKeeper.getCurrentSemesterIndex(); mSubjects.clear(); - mSubjects.addAll(StudentKeeper.getCurrentStudent().getSemesters().get(semester).getSubjects()); + mSubjects.addAll(StudentKeeper.getCurrentStudent().getSemesters().get(semesterIndex).getSubjects()); } catch (IllegalArgumentException e) { openLoginFragment(); } @@ -82,8 +82,7 @@ private void openLoginFragment() { } @SuppressWarnings("UnusedDeclaration") - public void onEvent(RefreshEvent event) { - StudentKeeper.refreshStudent(); + public void onEvent(RefreshEndEvent event) { initSubjects(); reRenderSubjectsList(); } diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java index b946550..4b7e3f2 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java @@ -14,8 +14,7 @@ import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.R; -import ua.samosfator.moduleok.StudentKeeper; -import ua.samosfator.moduleok.event.RefreshEvent; +import ua.samosfator.moduleok.event.RefreshEndEvent; import ua.samosfator.moduleok.event.SemesterChangedEvent; public class ModuleFragment extends Fragment { @@ -72,13 +71,6 @@ public void onEvent(SemesterChangedEvent event) { reRenderModuleSubjectsList(); } - @SuppressWarnings("UnusedDeclaration") - public void onEvent(RefreshEvent event) { - StudentKeeper.refreshStudent(); - ModulesFragment.initSubjects(); - reRenderModuleSubjectsList(); - } - private void reRenderModuleSubjectsList() { moduleSubjectItemAdapter.notifyItemRangeChanged(0, moduleSubjectItemAdapter.getItemCount()); } diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java index 00c7628..526ab2e 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.content.res.Resources; import android.net.Uri; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.RecyclerView; @@ -39,31 +40,26 @@ public void onItemClick(View view, int position) { SectionsEnum clickedSection = SectionsEnum.getSectionById(position); switch (clickedSection) { case LAST_TOTAL: { - mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new LastTotalFragment()) - .commit(); + openFragment(new LastTotalFragment()); + mDrawerLayout.closeDrawers(); highlightSelectedSection(view); break; } case MODULES: { - mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new ModulesFragment()) - .commit(); + openFragment(new ModulesFragment()); + mDrawerLayout.closeDrawers(); highlightSelectedSection(view); break; } case LOG_IN: { if (Auth.isLoggedIn()) { - mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new LogoutFragment()) - .commit(); + openFragment(new LogoutFragment()); } else { - mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new LoginFragment()) - .commit(); + openFragment(new LoginFragment()); } + mDrawerLayout.closeDrawers(); highlightSelectedSection(view); break; @@ -89,6 +85,12 @@ public void onItemClick(View view, int position) { } } + private void openFragment(Fragment fragment) { + mFragmentActivity.getSupportFragmentManager().beginTransaction() + .replace(R.id.main_container, fragment) + .commit(); + } + private void highlightSelectedSection(View view) { removeHighlightFromSections(); From 4cd84a0a237b203cb3f7646c3d4caa1bc5c9f056 Mon Sep 17 00:00:00 2001 From: Vlad Golubev Date: Thu, 5 Feb 2015 01:40:49 +0200 Subject: [PATCH 4/8] Optimize section highlight --- .../sections/SectionClickListener.java | 33 ++----------- .../sections/SectionHighlighter.java | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionHighlighter.java diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java index 526ab2e..0366bc1 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java @@ -7,6 +7,7 @@ import android.support.v4.app.FragmentActivity; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; @@ -43,14 +44,14 @@ public void onItemClick(View view, int position) { openFragment(new LastTotalFragment()); mDrawerLayout.closeDrawers(); - highlightSelectedSection(view); + SectionHighlighter.highlightSection(mRecyclerView, view); break; } case MODULES: { openFragment(new ModulesFragment()); mDrawerLayout.closeDrawers(); - highlightSelectedSection(view); + SectionHighlighter.highlightSection(mRecyclerView, view); break; } case LOG_IN: { @@ -61,7 +62,7 @@ public void onItemClick(View view, int position) { } mDrawerLayout.closeDrawers(); - highlightSelectedSection(view); + SectionHighlighter.highlightSection(mRecyclerView, view); break; } case EMPTY: { @@ -90,30 +91,4 @@ private void openFragment(Fragment fragment) { .replace(R.id.main_container, fragment) .commit(); } - - private void highlightSelectedSection(View view) { - removeHighlightFromSections(); - - view = ((FrameLayout) view).getChildAt(0); - TextView sectionTextView = view instanceof MaterialRippleLayout ? - ((TextView) ((MaterialRippleLayout) view).getChildAt(0)) : (TextView) view; - sectionTextView.setTextColor(App.getContext().getResources().getColor(R.color.colorAccent)); - sectionTextView.setBackgroundColor(App.getContext().getResources().getColor(R.color.grey_300)); - } - - private void removeHighlightFromSections() { - Resources appResources = App.getContext().getResources(); - int textColorPrimaryDark = appResources.getColor(R.color.textColorPrimaryDark); - int colorGrey200 = appResources.getColor(R.color.grey_200); - - for (int i = 0; i < mRecyclerView.getChildCount(); i++) { - if (i != SectionsEnum.EMPTY.INDEX) { - View recyclerViewChild = ((FrameLayout) mRecyclerView.getChildAt(i)).getChildAt(0); - TextView otherSectionTextView = recyclerViewChild instanceof MaterialRippleLayout ? - ((TextView) (((MaterialRippleLayout) recyclerViewChild).getChildAt(0))) : (TextView) recyclerViewChild; - otherSectionTextView.setTextColor(textColorPrimaryDark); - otherSectionTextView.setBackgroundColor(colorGrey200); - } - } - } } diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionHighlighter.java b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionHighlighter.java new file mode 100644 index 0000000..a22dcea --- /dev/null +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionHighlighter.java @@ -0,0 +1,47 @@ +package ua.samosfator.moduleok.fragment.navigation_drawer_fragment.sections; + +import android.content.res.Resources; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.TextView; + +import com.balysv.materialripple.MaterialRippleLayout; + +import ua.samosfator.moduleok.App; +import ua.samosfator.moduleok.R; + +public class SectionHighlighter { + + private final static Resources appResources = App.getContext().getResources(); + private final static int textColorPrimaryDark = appResources.getColor(R.color.textColorPrimaryDark); + private final static int colorGrey200 = appResources.getColor(R.color.grey_200); + + public static void highlightSection(RecyclerView sectionRecyclerView, View sectionToHighlight) { + removeHighlightFromSections(sectionRecyclerView); + highlightCurrentSection(sectionToHighlight); + } + + private static void highlightCurrentSection(View sectionToHighlight) { + sectionToHighlight = ((FrameLayout) sectionToHighlight).getChildAt(0); + TextView sectionTextView = sectionToHighlight instanceof MaterialRippleLayout ? + ((TextView) ((MaterialRippleLayout) sectionToHighlight).getChildAt(0)) : (TextView) sectionToHighlight; + sectionTextView.setTextColor(App.getContext().getResources().getColor(R.color.colorAccent)); + sectionTextView.setBackgroundColor(App.getContext().getResources().getColor(R.color.grey_300)); + } + + private static void removeHighlightFromSections(RecyclerView recyclerView) { + final int mRecyclerViewChildCount = recyclerView.getChildCount(); + + for (int i = 0; i < mRecyclerViewChildCount; i++) { + if (i != SectionsEnum.EMPTY.INDEX) { + View recyclerViewChild = ((FrameLayout) recyclerView.getChildAt(i)).getChildAt(0); + TextView otherSectionTextView = recyclerViewChild instanceof MaterialRippleLayout ? + ((TextView) (((MaterialRippleLayout) recyclerViewChild).getChildAt(0))) : (TextView) recyclerViewChild; + otherSectionTextView.setTextColor(textColorPrimaryDark); + otherSectionTextView.setBackgroundColor(colorGrey200); + } + } + } +} From 01c2ce7e4aa70d07ed136e1fe96a4a95b5b6aa59 Mon Sep 17 00:00:00 2001 From: vlad Date: Mon, 9 Feb 2015 15:00:26 +0200 Subject: [PATCH 5/8] Significant fragments optimization --- .idea/inspectionProfiles/Project_Default.xml | 16 +++++ .../inspectionProfiles/profiles_settings.xml | 7 ++ MODULE.OK.iml | 6 +- app/app.iml | 12 ++-- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 2 +- .../ua/samosfator/moduleok/FragmentUtils.java | 61 ++++++++++++++++ .../samosfator/moduleok/FragmentsKeeper.java | 56 +++++++++++++++ .../moduleok/LoadPageAsyncTask.java | 4 +- .../ua/samosfator/moduleok/MainActivity.java | 69 ++++++++++--------- .../moduleok/fragment/LoginFragment.java | 15 ++-- .../moduleok/fragment/LogoutFragment.java | 32 --------- .../LastTotalFragment.java | 7 +- .../modules_fragment/ModulesFragment.java | 10 ++- .../sections/SectionClickListener.java | 52 ++++++++------ app/src/main/res/layout/fragment_logout.xml | 6 -- 16 files changed, 234 insertions(+), 122 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java create mode 100644 app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java delete mode 100644 app/src/main/java/ua/samosfator/moduleok/fragment/LogoutFragment.java delete mode 100644 app/src/main/res/layout/fragment_logout.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..bc06c6e --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/MODULE.OK.iml b/MODULE.OK.iml index 8ffc04a..156d3d2 100644 --- a/MODULE.OK.iml +++ b/MODULE.OK.iml @@ -1,5 +1,5 @@ - + @@ -7,9 +7,7 @@ - - - + diff --git a/app/app.iml b/app/app.iml index e819d53..3d00c8e 100644 --- a/app/app.iml +++ b/app/app.iml @@ -1,5 +1,5 @@ - + @@ -25,6 +25,7 @@ + @@ -86,16 +87,17 @@ - + - + + + + - - diff --git a/app/build.gradle b/app/build.gradle index fcf26b5..74610b2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,6 +32,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:1.6.0' + compile 'net.sourceforge.streamsupport:streamsupport:1.1.3' compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:recyclerview-v7:21.0.3' compile 'org.jsoup:jsoup:1.8.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c23ab3b..1a75b94 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,7 @@ android:name=".App" android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java b/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java new file mode 100644 index 0000000..16cd813 --- /dev/null +++ b/app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java @@ -0,0 +1,61 @@ +package ua.samosfator.moduleok; + +import android.os.Handler; +import android.os.Looper; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; + +public class FragmentUtils { + + private static List addedFragments = new ArrayList<>(); + private static String TAG = "FragmentUtils"; + + public static void showFragment(FragmentTransaction fragmentTransaction, Fragment fragment) { + + hideFragments(fragmentTransaction); + + if (fragment.isAdded()) { + Log.d(TAG, "showing... " + fragment.getClass().getSimpleName()); + + fragmentTransaction.show(fragment); + } else { + Log.d(TAG, "adding... " + fragment.getClass().getSimpleName()); + + removeOldSameFragments(fragmentTransaction, fragment); + + fragmentTransaction.add(R.id.main_container, fragment); + fragmentTransaction.show(fragment); + + addedFragments.add(fragment); + } + fragmentTransaction.commit(); + } + + private static void hideFragments(FragmentTransaction fragmentTransaction) { + for (Fragment addedFragment : addedFragments) { + fragmentTransaction.hide(addedFragment); + } + } + + private static void removeOldSameFragments(FragmentTransaction fragmentTransaction, Fragment targetFragment) { + for (Fragment addedFragment : addedFragments) { + if (addedFragment.getClass().getSimpleName().equals(targetFragment.getClass().getSimpleName())) { + Log.d(TAG, "removing old same... " + addedFragment.getClass().getSimpleName()); + fragmentTransaction.remove(addedFragment); + } + } + } + + public static void removeFragment(FragmentManager fragmentManager, Fragment fragment) { + Log.d(TAG, "removing... " + fragment.getClass().getSimpleName()); + new Handler(Looper.getMainLooper()).post(() -> { + fragmentManager.beginTransaction().remove(fragment).hide(fragment).commit(); + fragmentManager.executePendingTransactions(); + }); + } +} \ No newline at end of file diff --git a/app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java b/app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java new file mode 100644 index 0000000..3af4a46 --- /dev/null +++ b/app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java @@ -0,0 +1,56 @@ +package ua.samosfator.moduleok; + +import android.support.v4.app.Fragment; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import ua.samosfator.moduleok.fragment.LoginFragment; +import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; +import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; + +public class FragmentsKeeper { + + private static List all = Collections.synchronizedList(new ArrayList<>()); + + private static Fragment lastTotal = new LastTotalFragment(); + private static Fragment modules = new ModulesFragment(); + private static LoginFragment login = new LoginFragment(); + + public static Fragment getLastTotal() { + return lastTotal; + } + + public static void setLastTotal(Fragment lastTotal) { + FragmentsKeeper.lastTotal = lastTotal; + } + + public static Fragment getModules() { + return modules; + } + + public static void setModules(Fragment modules) { + FragmentsKeeper.modules = modules; + } + + public static LoginFragment getLogin() { + return login; + } + + public static void setLogin(LoginFragment login) { + FragmentsKeeper.login = login; + } + + public static List getAll() { + populateFragmentsList(); + return all; + } + + private static void populateFragmentsList() { + all.clear(); + all.add(lastTotal); + all.add(modules); + all.add(login); + } +} diff --git a/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java b/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java index 9a03e48..a9ff019 100644 --- a/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java +++ b/app/src/main/java/ua/samosfator/moduleok/LoadPageAsyncTask.java @@ -18,8 +18,8 @@ class LoadPageAsyncTask extends AsyncTask { protected String doInBackground(Void... params) { Document mainPage = Jsoup.parse("
"); try { - mainPage = getFromCustomRemoteSource(); -// mainPage = getFromRemoteSource(); +// mainPage = getFromCustomRemoteSource(); + mainPage = getFromRemoteSource(); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java index 9bd9ab3..819f935 100644 --- a/app/src/main/java/ua/samosfator/moduleok/MainActivity.java +++ b/app/src/main/java/ua/samosfator/moduleok/MainActivity.java @@ -1,5 +1,6 @@ package ua.samosfator.moduleok; +import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.os.Handler; @@ -20,8 +21,8 @@ import ua.samosfator.moduleok.event.LogoutEvent; import ua.samosfator.moduleok.event.RefreshEndEvent; import ua.samosfator.moduleok.event.RefreshEvent; -import ua.samosfator.moduleok.fragment.LoginFragment; import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; +import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; import ua.samosfator.moduleok.fragment.navigation_drawer_fragment.NavigationDrawerFragment; import ua.samosfator.moduleok.notification.ScoreCheckerService; @@ -66,20 +67,10 @@ private void initNavigationDrawer() { drawerFragment.setup(R.id.navigation_drawer_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar); } - private void eraseAccountInfo() { - final TextView studentName_TextView = (TextView) findViewById(R.id.student_name_txt); - final TextView studentGroup_TextView = (TextView) findViewById(R.id.student_group_txt); - - runOnUiThread(() -> { - studentName_TextView.setText(getString(R.string.sampleStudentName)); - studentGroup_TextView.setText(getString(R.string.sampleStudentGroup)); - openLoginFragment(); - }); - } - private void initAndSetAccountInfo() { if (!Auth.isLoggedIn()) { eraseAccountInfo(); + openLoginFragment(); return; } @@ -99,16 +90,42 @@ private void initAndSetAccountInfo() { }); } + private void eraseAccountInfo() { + final TextView studentName_TextView = (TextView) findViewById(R.id.student_name_txt); + final TextView studentGroup_TextView = (TextView) findViewById(R.id.student_group_txt); + + runOnUiThread(() -> { + studentName_TextView.setText(getString(R.string.sampleStudentName)); + studentGroup_TextView.setText(getString(R.string.sampleStudentGroup)); + }); + } + + @SuppressLint("CommitTransaction") private void openLastTotalFragment() { - getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new LastTotalFragment()) - .commit(); + FragmentUtils.showFragment(getSupportFragmentManager().beginTransaction(), FragmentsKeeper.getLastTotal()); } + @SuppressLint("CommitTransaction") private void openLoginFragment() { - getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, new LoginFragment()) - .commit(); + FragmentUtils.showFragment(getSupportFragmentManager().beginTransaction(), FragmentsKeeper.getLogin()); + } + + @SuppressLint("CommitTransaction") + @SuppressWarnings("UnusedDeclaration") + public void onEvent(LoginEvent event) { + EventBus.getDefault().post(new RefreshEvent()); + initAndSetAccountInfo(); + } + + @SuppressWarnings("UnusedDeclaration") + public void onEvent(LogoutEvent event) { + eraseAccountInfo(); + } + + @SuppressWarnings("UnusedDeclaration") + public void onEvent(RefreshEvent event) { + StudentKeeper.refreshStudent(); + EventBus.getDefault().post(new RefreshEndEvent()); } @Override @@ -138,22 +155,6 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - @SuppressWarnings("UnusedDeclaration") - public void onEvent(LoginEvent event) { - initAndSetAccountInfo(); - } - - @SuppressWarnings("UnusedDeclaration") - public void onEvent(LogoutEvent event) { - eraseAccountInfo(); - } - - @SuppressWarnings("UnusedDeclaration") - public void onEvent(RefreshEvent event) { - StudentKeeper.refreshStudent(); - EventBus.getDefault().post(new RefreshEndEvent()); - } - @Override protected void onStop() { EventBus.getDefault().unregister(this); diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/LoginFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/LoginFragment.java index 321b847..7f425b7 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/LoginFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/LoginFragment.java @@ -28,6 +28,8 @@ public class LoginFragment extends Fragment { private MaterialEditText login_txt; private MaterialEditText password_txt; + private View rootView; + public LoginFragment() { // Required empty public constructor } @@ -41,9 +43,8 @@ public void onSaveInstanceState(Bundle outState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View rootView = inflater.inflate(R.layout.fragment_login, container, false); - initViews(rootView); - login_button = (CircularProgressButton) rootView.findViewById(R.id.btnWithText); + rootView = inflater.inflate(R.layout.fragment_login, container, false); + initViews(); password_txt.setOnEditorActionListener((v, actionId, event) -> { if (actionId == EditorInfo.IME_ACTION_DONE) { validateAndStartLogin(); @@ -105,14 +106,14 @@ private void doLogin(final String login, final String password) { }).start(); } - private void initViews(View rootView) { + private void initViews() { login_txt = (MaterialEditText) rootView.findViewById(R.id.login_editText); password_txt = (MaterialEditText) rootView.findViewById(R.id.password_editText); + login_button = (CircularProgressButton) rootView.findViewById(R.id.btnWithText); } - private void restoreViews(Bundle savedInstanceState) { - login_txt.setText(savedInstanceState.getString("login")); - password_txt.setText(savedInstanceState.getString("pass")); + public LoginFragment restoreView() { + return new LoginFragment(); } private void showInternetConnectionError() { diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/LogoutFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/LogoutFragment.java deleted file mode 100644 index 473dd42..0000000 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/LogoutFragment.java +++ /dev/null @@ -1,32 +0,0 @@ -package ua.samosfator.moduleok.fragment; - -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.splunk.mint.Mint; -import com.splunk.mint.MintLogLevel; - -import de.greenrobot.event.EventBus; -import ua.samosfator.moduleok.Preferences; -import ua.samosfator.moduleok.R; -import ua.samosfator.moduleok.event.LogoutEvent; - -public class LogoutFragment extends Fragment { - - public LogoutFragment() { - // Required empty public constructor - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_logout, container, false); - Preferences.save("SESSIONID", ""); - Preferences.save("mainPageHtml", ""); - EventBus.getDefault().post(new LogoutEvent()); - Mint.logEvent("log out", MintLogLevel.Info); - return rootView; - } -} diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java index ca387bb..b0b1a62 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/last_total_fragment/LastTotalFragment.java @@ -17,11 +17,12 @@ import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.App; +import ua.samosfator.moduleok.FragmentUtils; +import ua.samosfator.moduleok.FragmentsKeeper; import ua.samosfator.moduleok.R; import ua.samosfator.moduleok.StudentKeeper; import ua.samosfator.moduleok.event.RefreshEndEvent; import ua.samosfator.moduleok.event.SemesterChangedEvent; -import ua.samosfator.moduleok.fragment.LoginFragment; import ua.samosfator.moduleok.parser.Subject; import ua.samosfator.moduleok.recyclerview.RecyclerItemClickListener; @@ -76,9 +77,7 @@ private void reRenderSubjectsList() { } private void openLoginFragment() { - getFragmentManager().beginTransaction() - .replace(R.id.main_container, new LoginFragment()) - .commit(); + FragmentUtils.showFragment(getFragmentManager().beginTransaction(), FragmentsKeeper.getLogin()); } @SuppressWarnings("UnusedDeclaration") diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java index 06f173e..a92805e 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java @@ -17,6 +17,8 @@ import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.App; +import ua.samosfator.moduleok.FragmentUtils; +import ua.samosfator.moduleok.FragmentsKeeper; import ua.samosfator.moduleok.R; import ua.samosfator.moduleok.StudentKeeper; import ua.samosfator.moduleok.event.RefreshEndEvent; @@ -78,16 +80,12 @@ static void initSubjects() { } private static void openLoginFragment() { - fragmentManager.beginTransaction() - .replace(R.id.main_container, new LoginFragment()) - .commit(); + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); } @SuppressWarnings("UnusedDeclaration") public void onEvent(RefreshEndEvent event) { - fragmentManager.beginTransaction() - .replace(R.id.main_container, new ModulesFragment()) - .commit(); + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); } @Override diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java index 0366bc1..4e7d104 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/navigation_drawer_fragment/sections/SectionClickListener.java @@ -1,54 +1,63 @@ package ua.samosfator.moduleok.fragment.navigation_drawer_fragment.sections; +import android.annotation.SuppressLint; import android.content.Intent; -import android.content.res.Resources; import android.net.Uri; -import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.View; -import android.widget.FrameLayout; -import android.widget.TextView; import android.widget.Toast; -import com.balysv.materialripple.MaterialRippleLayout; +import com.splunk.mint.Mint; +import com.splunk.mint.MintLogLevel; +import de.greenrobot.event.EventBus; import ua.samosfator.moduleok.App; import ua.samosfator.moduleok.Auth; +import ua.samosfator.moduleok.FragmentUtils; +import ua.samosfator.moduleok.FragmentsKeeper; +import ua.samosfator.moduleok.Preferences; import ua.samosfator.moduleok.R; +import ua.samosfator.moduleok.event.LogoutEvent; import ua.samosfator.moduleok.fragment.LoginFragment; -import ua.samosfator.moduleok.fragment.LogoutFragment; -import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; -import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; import ua.samosfator.moduleok.recyclerview.RecyclerItemClickListener; public class SectionClickListener implements RecyclerItemClickListener.OnItemClickListener { - private FragmentActivity mFragmentActivity; + private FragmentManager fragmentManager; private DrawerLayout mDrawerLayout; private RecyclerView mRecyclerView; public SectionClickListener(FragmentActivity activity, DrawerLayout drawerLayout, RecyclerView recyclerView) { - mFragmentActivity = activity; + fragmentManager = activity.getSupportFragmentManager(); mDrawerLayout = drawerLayout; mRecyclerView = recyclerView; } + @SuppressLint("CommitTransaction") @Override public void onItemClick(View view, int position) { SectionsEnum clickedSection = SectionsEnum.getSectionById(position); switch (clickedSection) { case LAST_TOTAL: { - openFragment(new LastTotalFragment()); + if (Auth.isLoggedIn()) { + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLastTotal()); + } else { + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); + } mDrawerLayout.closeDrawers(); SectionHighlighter.highlightSection(mRecyclerView, view); break; } case MODULES: { - openFragment(new ModulesFragment()); + if (Auth.isLoggedIn()) { + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getModules()); + } else { + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); + } mDrawerLayout.closeDrawers(); SectionHighlighter.highlightSection(mRecyclerView, view); @@ -56,9 +65,16 @@ public void onItemClick(View view, int position) { } case LOG_IN: { if (Auth.isLoggedIn()) { - openFragment(new LogoutFragment()); + Preferences.save("SESSIONID", ""); + Preferences.save("mainPageHtml", ""); + + FragmentsKeeper.setLogin(new LoginFragment()); + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); + + EventBus.getDefault().post(new LogoutEvent()); + Mint.logEvent("log out", MintLogLevel.Info); } else { - openFragment(new LoginFragment()); + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); } mDrawerLayout.closeDrawers(); @@ -85,10 +101,4 @@ public void onItemClick(View view, int position) { } } } - - private void openFragment(Fragment fragment) { - mFragmentActivity.getSupportFragmentManager().beginTransaction() - .replace(R.id.main_container, fragment) - .commit(); - } } diff --git a/app/src/main/res/layout/fragment_logout.xml b/app/src/main/res/layout/fragment_logout.xml deleted file mode 100644 index 3c31e52..0000000 --- a/app/src/main/res/layout/fragment_logout.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - From 9322793d778297540ef84450d1c5b67beebe6294 Mon Sep 17 00:00:00 2001 From: vlad Date: Mon, 9 Feb 2015 15:08:34 +0200 Subject: [PATCH 6/8] Fix showing Login form on refresh --- .../moduleok/fragment/modules_fragment/ModuleFragment.java | 3 --- .../moduleok/fragment/modules_fragment/ModulesFragment.java | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java index 4b7e3f2..cc54849 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModuleFragment.java @@ -20,10 +20,7 @@ public class ModuleFragment extends Fragment { private ModuleSubjectItemAdapter moduleSubjectItemAdapter; - - /////////////////// private Map cacheRecyclerView = new HashMap<>(); - //////////////////// public ModuleFragment() { // Required empty public constructor diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java index a92805e..c5beec8 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java @@ -85,7 +85,7 @@ private static void openLoginFragment() { @SuppressWarnings("UnusedDeclaration") public void onEvent(RefreshEndEvent event) { - FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getLogin()); + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getModules()); } @Override From 3b374d85b0e7235f17e85be3b317e867ba9ed597 Mon Sep 17 00:00:00 2001 From: vlad Date: Mon, 9 Feb 2015 15:12:51 +0200 Subject: [PATCH 7/8] Fix always showing modules fragments on refresh --- .../moduleok/fragment/modules_fragment/ModulesFragment.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java index c5beec8..98f8c09 100644 --- a/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java +++ b/app/src/main/java/ua/samosfator/moduleok/fragment/modules_fragment/ModulesFragment.java @@ -22,7 +22,6 @@ import ua.samosfator.moduleok.R; import ua.samosfator.moduleok.StudentKeeper; import ua.samosfator.moduleok.event.RefreshEndEvent; -import ua.samosfator.moduleok.fragment.LoginFragment; import ua.samosfator.moduleok.parser.Semester; import ua.samosfator.moduleok.parser.Subject; @@ -85,7 +84,9 @@ private static void openLoginFragment() { @SuppressWarnings("UnusedDeclaration") public void onEvent(RefreshEndEvent event) { - FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getModules()); + if (FragmentsKeeper.getModules().isVisible()) { + FragmentUtils.showFragment(fragmentManager.beginTransaction(), FragmentsKeeper.getModules()); + } } @Override From b435990f7af84126e79d17517b0612f8911b2ad9 Mon Sep 17 00:00:00 2001 From: vlad Date: Mon, 9 Feb 2015 16:42:09 +0200 Subject: [PATCH 8/8] Get back background service --- app/app.iml | 9 ++++----- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 2 +- .../moduleok/notification/ScoreCheckerService.java | 3 +++ 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/app.iml b/app/app.iml index 3d00c8e..42735ed 100644 --- a/app/app.iml +++ b/app/app.iml @@ -87,17 +87,16 @@
- + + - - - - + +
diff --git a/app/build.gradle b/app/build.gradle index 74610b2..33cf6cf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:1.6.0' - compile 'net.sourceforge.streamsupport:streamsupport:1.1.3' +// compile 'net.sourceforge.streamsupport:streamsupport:1.1.3' compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:recyclerview-v7:21.0.3' compile 'org.jsoup:jsoup:1.8.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1a75b94..c23ab3b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,7 @@ android:name=".App" android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/ua/samosfator/moduleok/notification/ScoreCheckerService.java b/app/src/main/java/ua/samosfator/moduleok/notification/ScoreCheckerService.java index 6648476..66d8c5a 100644 --- a/app/src/main/java/ua/samosfator/moduleok/notification/ScoreCheckerService.java +++ b/app/src/main/java/ua/samosfator/moduleok/notification/ScoreCheckerService.java @@ -82,10 +82,13 @@ public void onEvent(InternetConnectionAbsent event) { private void startServiceTimer() { moduleDatesUpdateTask = new ModuleDatesUpdateTask(); timer.schedule(moduleDatesUpdateTask, 200, TimeUnit.HOURS.toMillis(12)); + updateTimeTask = new UpdateTimeTask(); ModuleDatesUpdateTask.updatePendingModulesCount(); if (pendingModulesCount > 0) { timer.schedule(updateTimeTask, 0, TimeUnit.HOURS.toMillis(2)); + + Log.d(TAG, "" + pendingModulesCount + " modules in the near 2 days"); } else { Log.d(TAG, "No modules dates in the near 2 days"); }