Skip to content

Commit

Permalink
Merge pull request #25 from samosfator/memory
Browse files Browse the repository at this point in the history
Significant reduce memory consumption
  • Loading branch information
Vlad Golubev committed Feb 9, 2015
2 parents 1ea42de + b435990 commit dbdc280
Show file tree
Hide file tree
Showing 20 changed files with 316 additions and 186 deletions.
16 changes: 16 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions MODULE.OK.iml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
Expand Down
3 changes: 2 additions & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="MODULE.OK" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand All @@ -25,6 +25,7 @@
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<application
android:allowBackup="true"
android:largeHeap="true"
android:largeHeap="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="false"
Expand Down
69 changes: 48 additions & 21 deletions app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
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;
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 LoginFragment loginFragment;
private static LogoutFragment logoutFragment;
private static LastTotalFragment lastTotalFragment;
private static ModulesFragment modulesFragment;
private static List<Fragment> 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);

public static LoginFragment getLoginFragment() {
if (loginFragment == null) loginFragment = new LoginFragment();
return loginFragment;
addedFragments.add(fragment);
}
fragmentTransaction.commit();
}

public static LogoutFragment getLogoutFragment() {
if (logoutFragment == null) logoutFragment = new LogoutFragment();
return logoutFragment;
private static void hideFragments(FragmentTransaction fragmentTransaction) {
for (Fragment addedFragment : addedFragments) {
fragmentTransaction.hide(addedFragment);
}
}

public static LastTotalFragment getLastTotalFragment() {
if (lastTotalFragment == null) lastTotalFragment = new LastTotalFragment();
return lastTotalFragment;
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 ModulesFragment getModulesFragment() {
if (modulesFragment == null) modulesFragment = new ModulesFragment();
return modulesFragment;
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();
});
}
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java
Original file line number Diff line number Diff line change
@@ -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<Fragment> 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<Fragment> getAll() {
populateFragmentsList();
return all;
}

private static void populateFragmentsList() {
all.clear();
all.add(lastTotal);
all.add(modules);
all.add(login);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ protected String doInBackground(Void... params) {
} catch (IOException e) {
e.printStackTrace();
}
postRefreshEndEvent();
return mainPage.html();
}

private void postRefreshEndEvent() {
new Handler(Looper.getMainLooper()).post(() -> 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();
Expand Down
64 changes: 37 additions & 27 deletions app/src/main/java/ua/samosfator/moduleok/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.samosfator.moduleok;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -18,7 +19,10 @@
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.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;

Expand Down Expand Up @@ -63,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;
}

Expand All @@ -96,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, FragmentUtils.getLastTotalFragment())
.commit();
FragmentUtils.showFragment(getSupportFragmentManager().beginTransaction(), FragmentsKeeper.getLastTotal());
}

@SuppressLint("CommitTransaction")
private void openLoginFragment() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, FragmentUtils.getLoginFragment())
.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
Expand Down Expand Up @@ -135,16 +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();
}

@Override
protected void onStop() {
EventBus.getDefault().unregister(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit dbdc280

Please sign in to comment.