mirror of
https://github.com/oliexdev/openScale.git
synced 2025-09-01 04:22:26 +02:00
Merge branch 'context' of git://github.com/erijo/openScale into erijo-context
This commit is contained in:
@@ -94,7 +94,7 @@ public class OpenScale {
|
|||||||
|
|
||||||
public static OpenScale getInstance(Context context) {
|
public static OpenScale getInstance(Context context) {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new OpenScale(context);
|
instance = new OpenScale(context.getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
@@ -461,6 +461,10 @@ public class OpenScale {
|
|||||||
fragment.updateOnView(scaleMeasurementList);
|
fragment.updateOnView(scaleMeasurementList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void unregisterFragment(FragmentUpdateListener fragment) {
|
||||||
|
fragmentList.remove(fragment);
|
||||||
|
}
|
||||||
|
|
||||||
public void updateScaleData() {
|
public void updateScaleData() {
|
||||||
int selectedUserId = getSelectedScaleUserId();
|
int selectedUserId = getSelectedScaleUserId();
|
||||||
|
|
||||||
|
@@ -37,6 +37,7 @@ import android.support.design.widget.BottomNavigationView;
|
|||||||
import android.support.design.widget.NavigationView;
|
import android.support.design.widget.NavigationView;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.support.v4.content.FileProvider;
|
import android.support.v4.content.FileProvider;
|
||||||
import android.support.v4.view.GravityCompat;
|
import android.support.v4.view.GravityCompat;
|
||||||
import android.support.v4.widget.DrawerLayout;
|
import android.support.v4.widget.DrawerLayout;
|
||||||
@@ -76,11 +77,10 @@ public class MainActivity extends AppCompatActivity
|
|||||||
private static boolean firstAppStart = true;
|
private static boolean firstAppStart = true;
|
||||||
private static boolean valueOfCountModified = false;
|
private static boolean valueOfCountModified = false;
|
||||||
private static int bluetoothStatusIcon = R.drawable.ic_bluetooth_disabled;
|
private static int bluetoothStatusIcon = R.drawable.ic_bluetooth_disabled;
|
||||||
private static MenuItem bluetoothStatus;
|
private MenuItem bluetoothStatus;
|
||||||
|
|
||||||
private static final int IMPORT_DATA_REQUEST = 100;
|
private static final int IMPORT_DATA_REQUEST = 100;
|
||||||
|
|
||||||
private Fragment currentFragment;
|
|
||||||
private DrawerLayout drawerLayout;
|
private DrawerLayout drawerLayout;
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
private NavigationView navDrawer;
|
private NavigationView navDrawer;
|
||||||
@@ -110,8 +110,6 @@ public class MainActivity extends AppCompatActivity
|
|||||||
|
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
currentFragment = null;
|
|
||||||
|
|
||||||
// Set a Toolbar to replace the ActionBar.
|
// Set a Toolbar to replace the ActionBar.
|
||||||
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
@@ -288,28 +286,23 @@ public class MainActivity extends AppCompatActivity
|
|||||||
Class fragmentClass;
|
Class fragmentClass;
|
||||||
String fragmentTitle;
|
String fragmentTitle;
|
||||||
|
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
switch (menuItemId) {
|
||||||
|
default:
|
||||||
switch(menuItemId) {
|
|
||||||
case R.id.nav_overview:
|
case R.id.nav_overview:
|
||||||
fragmentClass = OverviewFragment.class;
|
fragmentClass = OverviewFragment.class;
|
||||||
fragmentTitle = getResources().getString(R.string.title_overview);
|
fragmentTitle = getResources().getString(R.string.title_overview);
|
||||||
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
|
||||||
break;
|
break;
|
||||||
case R.id.nav_graph:
|
case R.id.nav_graph:
|
||||||
fragmentClass = GraphFragment.class;
|
fragmentClass = GraphFragment.class;
|
||||||
fragmentTitle = getResources().getString(R.string.title_graph);
|
fragmentTitle = getResources().getString(R.string.title_graph);
|
||||||
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
|
||||||
break;
|
break;
|
||||||
case R.id.nav_table:
|
case R.id.nav_table:
|
||||||
fragmentClass = TableFragment.class;
|
fragmentClass = TableFragment.class;
|
||||||
fragmentTitle = getResources().getString(R.string.title_table);
|
fragmentTitle = getResources().getString(R.string.title_table);
|
||||||
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
|
||||||
break;
|
break;
|
||||||
case R.id.nav_statistic:
|
case R.id.nav_statistic:
|
||||||
fragmentClass = StatisticsFragment.class;
|
fragmentClass = StatisticsFragment.class;
|
||||||
fragmentTitle = getResources().getString(R.string.title_statistics);
|
fragmentTitle = getResources().getString(R.string.title_statistics);
|
||||||
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
|
||||||
break;
|
break;
|
||||||
case R.id.nav_settings:
|
case R.id.nav_settings:
|
||||||
Intent settingsIntent = new Intent(this, SettingsActivity.class);
|
Intent settingsIntent = new Intent(this, SettingsActivity.class);
|
||||||
@@ -322,35 +315,45 @@ public class MainActivity extends AppCompatActivity
|
|||||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/oliexdev/openScale/wiki")));
|
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/oliexdev/openScale/wiki")));
|
||||||
drawerLayout.closeDrawers();
|
drawerLayout.closeDrawers();
|
||||||
return;
|
return;
|
||||||
default:
|
|
||||||
fragmentClass = OverviewFragment.class;
|
|
||||||
fragmentTitle = getResources().getString(R.string.title_overview);
|
|
||||||
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
|
||||||
|
|
||||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
// hide previous fragment if it available
|
// Make sure that any pending transaction completes so that added fragments are
|
||||||
if (currentFragment != null) {
|
// actually added and won't get added again (may happen during activity creation
|
||||||
fragmentManager.beginTransaction().hide(currentFragment).commit();
|
// when this method is called twice).
|
||||||
|
fragmentManager.executePendingTransactions();
|
||||||
|
|
||||||
|
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||||
|
final String tag = String.valueOf(menuItemId);
|
||||||
|
|
||||||
|
boolean found = false;
|
||||||
|
for (Fragment fragment : fragmentManager.getFragments()) {
|
||||||
|
if (fragment.getTag().equals(tag)) {
|
||||||
|
// Show selected fragment if already added
|
||||||
|
transaction.show(fragment);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
else if (!fragment.isHidden()) {
|
||||||
|
// Hide currently shown fragment
|
||||||
|
transaction.hide(fragment);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to find selected fragment
|
// If fragment isn't found then add it
|
||||||
currentFragment = fragmentManager.findFragmentByTag(""+menuItemId);
|
if (!found) {
|
||||||
|
|
||||||
// if fragment not found then add the fragment
|
|
||||||
if (currentFragment == null) {
|
|
||||||
try {
|
try {
|
||||||
currentFragment = (Fragment) fragmentClass.newInstance();
|
transaction.add(R.id.fragment_content, (Fragment) fragmentClass.newInstance(), tag);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
fragmentManager.beginTransaction().add(R.id.fragment_content, currentFragment, "" + menuItemId).commit();
|
|
||||||
} else { // otherwise show fragment
|
|
||||||
fragmentManager.beginTransaction().show(currentFragment).commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transaction.commit();
|
||||||
|
|
||||||
// Set action bar title
|
// Set action bar title
|
||||||
setTitle(fragmentTitle);
|
setTitle(fragmentTitle);
|
||||||
|
|
||||||
@@ -654,7 +657,6 @@ public class MainActivity extends AppCompatActivity
|
|||||||
R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
currentFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -226,6 +226,12 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
return graphView;
|
return graphView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
OpenScale.getInstance(getContext()).unregisterFragment(this);
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
|
@@ -148,6 +148,12 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
return overviewView;
|
return overviewView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
OpenScale.getInstance(getContext()).unregisterFragment(this);
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
|
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
|
||||||
if (scaleMeasurementList.isEmpty()) {
|
if (scaleMeasurementList.isEmpty()) {
|
||||||
|
@@ -156,6 +156,12 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
|||||||
return statisticsView;
|
return statisticsView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
OpenScale.getInstance(getContext()).unregisterFragment(this);
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
|
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
|
||||||
currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
|
currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
|
||||||
|
@@ -97,6 +97,12 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
return tableView;
|
return tableView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
OpenScale.getInstance(getContext()).unregisterFragment(this);
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
|
Reference in New Issue
Block a user