Remember active group tab

This commit is contained in:
Sylvia van Os
2020-11-17 18:30:21 +01:00
parent dec85d8fa9
commit 3c797b5ec9
5 changed files with 61 additions and 36 deletions

View File

@@ -1,5 +1,11 @@
# Changelog
## Unreleased
Changes:
- Remember active group tab between screens and sessions
## v1.2.1 (2020-11-17)
Changes:

View File

@@ -13,8 +13,8 @@ android {
applicationId "me.hackerchick.catima"
minSdkVersion 16
targetSdkVersion 29
versionCode 44
versionName "1.2.0"
versionCode 45
versionName "1.2.1"
}
buildTypes {
release {

View File

@@ -6,6 +6,7 @@ import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
@@ -63,6 +64,14 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O
public void onTabSelected(TabLayout.Tab tab) {
selectedTab = tab.getPosition();
updateLoyaltyCardList(filter, tab.getTag());
// Store active tab in Shared Preference to restore next app launch
SharedPreferences activeTabPref = getApplicationContext().getSharedPreferences(
getString(R.string.sharedpreference_active_tab),
Context.MODE_PRIVATE);
SharedPreferences.Editor activeTabPrefEditor = activeTabPref.edit();
activeTabPrefEditor.putInt(getString(R.string.sharedpreference_active_tab), selectedTab);
activeTabPrefEditor.apply();
}
@Override
@@ -107,15 +116,22 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O
}
}
// Start of active tab logic
TabLayout groupsTabLayout = findViewById(R.id.groups);
boolean hasReset = updateTabGroups(groupsTabLayout);
updateTabGroups(groupsTabLayout);
// Restore active tab from Shared Preference
SharedPreferences activeTabPref = getApplicationContext().getSharedPreferences(
getString(R.string.sharedpreference_active_tab),
Context.MODE_PRIVATE);
selectedTab = activeTabPref.getInt(getString(R.string.sharedpreference_active_tab), 0);
Object group = null;
if (groupsTabLayout.getTabCount() != 0) {
TabLayout.Tab tab = groupsTabLayout.getTabAt(0);
if (!hasReset) {
if (selectedTab < groupsTabLayout.getTabCount()) {
tab = groupsTabLayout.getTabAt(selectedTab);
}
@@ -123,6 +139,7 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O
group = tab.getTag();
}
updateLoyaltyCardList(filter, group);
// End of active tab logic
FloatingActionButton addButton = findViewById(R.id.fabAdd);
addButton.setOnClickListener(new View.OnClickListener() {
@@ -241,7 +258,7 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O
});
}
public boolean updateTabGroups(TabLayout groupsTabLayout)
public void updateTabGroups(TabLayout groupsTabLayout)
{
final DBHelper db = new DBHelper(this);
@@ -250,42 +267,24 @@ public class MainActivity extends AppCompatActivity implements GestureDetector.O
if (newGroups.size() == 0) {
groupsTabLayout.removeAllTabs();
groupsTabLayout.setVisibility(View.GONE);
return true;
return;
}
// -1 because there is an "All" tab
boolean isChanged = groupsTabLayout.getTabCount() - 1 != newGroups.size();
groupsTabLayout.removeAllTabs();
if (!isChanged) {
for (int i = 0; i < newGroups.size(); i++) {
if (!((Group) groupsTabLayout.getTabAt(i + 1).getTag())._id.equals(newGroups.get(i)._id)) {
isChanged = true;
break;
}
}
TabLayout.Tab allTab = groupsTabLayout.newTab();
allTab.setText(R.string.all);
allTab.setTag(null);
groupsTabLayout.addTab(allTab, false);
for (Group group : newGroups) {
TabLayout.Tab tab = groupsTabLayout.newTab();
tab.setText(group._id);
tab.setTag(group);
groupsTabLayout.addTab(tab, false);
}
if (isChanged) {
groupsTabLayout.removeAllTabs();
TabLayout.Tab allTab = groupsTabLayout.newTab();
allTab.setText(R.string.all);
allTab.setTag(null);
groupsTabLayout.addTab(allTab);
for (Group group : newGroups) {
TabLayout.Tab tab = groupsTabLayout.newTab();
tab.setText(group._id);
tab.setTag(group);
groupsTabLayout.addTab(tab);
}
groupsTabLayout.setVisibility(View.VISIBLE);
return true;
}
return false;
groupsTabLayout.setVisibility(View.VISIBLE);
}
@Override

View File

@@ -1,6 +1,8 @@
package protect.card_locker;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
@@ -88,6 +90,16 @@ public class ManageGroupsActivity extends AppCompatActivity
registerForContextMenu(groupList);
}
private void invalidateHomescreenActiveTab()
{
SharedPreferences activeTabPref = getApplicationContext().getSharedPreferences(
getString(R.string.sharedpreference_active_tab),
Context.MODE_PRIVATE);
SharedPreferences.Editor activeTabPrefEditor = activeTabPref.edit();
activeTabPrefEditor.putInt(getString(R.string.sharedpreference_active_tab), 0);
activeTabPrefEditor.apply();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
@@ -119,6 +131,8 @@ public class ManageGroupsActivity extends AppCompatActivity
public void onClick(DialogInterface dialog, int which) {
db.updateGroup(groupName, input.getText().toString());
updateGroupList();
// Rename may change ordering, so invalidate
invalidateHomescreenActiveTab();
}
});
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@@ -147,6 +161,8 @@ public class ManageGroupsActivity extends AppCompatActivity
public void onClick(DialogInterface dialog, int which) {
db.deleteGroup(groupName);
updateGroupList();
// Delete may change ordering, so invalidate
invalidateHomescreenActiveTab();
}
});
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@@ -171,6 +187,8 @@ public class ManageGroupsActivity extends AppCompatActivity
public void onClick(DialogInterface dialog, int which) {
db.insertGroup(input.getText().toString());
updateGroupList();
// Create may change ordering, so invalidate
invalidateHomescreenActiveTab();
}
});
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

View File

@@ -117,6 +117,8 @@
<string name="settings_lock_barcode_orientation">Lock barcode orientation</string>
<string name="settings_key_lock_barcode_orientation" translatable="false">pref_lock_barcode_orientation</string>
<string name="sharedpreference_active_tab" translatable="false">sharedpreference_active_tab</string>
<string name="intent_import_card_from_url_share_text">I want to share a card with you</string>
<string name="intent_import_card_from_url_host" translatable="false">thelastproject.github.io</string>
<string name="intent_import_card_from_url_path_prefix" translatable="false">/Catima/share</string>