diff --git a/app/src/main/java/me/tsukanov/counter/activities/MainActivity.java b/app/src/main/java/me/tsukanov/counter/activities/MainActivity.java index 34308741..84dd8334 100644 --- a/app/src/main/java/me/tsukanov/counter/activities/MainActivity.java +++ b/app/src/main/java/me/tsukanov/counter/activities/MainActivity.java @@ -6,23 +6,21 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; -import android.view.View; import android.view.WindowManager.LayoutParams; import android.widget.FrameLayout; + import androidx.annotation.NonNull; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceManager; +import com.google.android.material.appbar.MaterialToolbar; import me.tsukanov.counter.CounterApplication; import me.tsukanov.counter.R; import me.tsukanov.counter.SharedPrefKeys; @@ -39,9 +37,8 @@ public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); - private ActionBar actionBar; + private MaterialToolbar toolbar; private DrawerLayout navigationLayout; - private ActionBarDrawerToggle navigationToggle; private SharedPreferences sharedPrefs; private FrameLayout menuFrame; private String selectedCounterName; @@ -50,23 +47,20 @@ public class MainActivity extends AppCompatActivity { @Override public void onCreate(final Bundle savedInstanceState) { - actionBar = getSupportActionBar(); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); registerIntentReceivers(getApplicationContext()); super.onCreate(savedInstanceState); - // Enable ActionBar home button to behave as action to toggle navigation drawer - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setHomeButtonEnabled(true); + setContentView(R.layout.layout_main); - setContentView(R.layout.drawer_layout); + toolbar = findViewById(R.id.mainToolbar); + setSupportActionBar(toolbar); - navigationLayout = findViewById(R.id.drawer_layout); - navigationToggle = generateActionBarToggle(this.actionBar, navigationLayout); - navigationLayout.addDrawerListener(navigationToggle); + toolbar.setNavigationOnClickListener(view -> openDrawer()); - menuFrame = findViewById(R.id.menu_frame); + navigationLayout = findViewById(R.id.mainNavigationLayout); + menuFrame = findViewById(R.id.mainMenuFrame); } private void registerIntentReceivers(@NonNull final Context context) { @@ -77,25 +71,7 @@ private void registerIntentReceivers(@NonNull final Context context) { context, new CounterChangeReceiver(), counterSelectionFilter, - ContextCompat.RECEIVER_NOT_EXPORTED); - } - - private ActionBarDrawerToggle generateActionBarToggle( - @NonNull final ActionBar actionBar, @NonNull final DrawerLayout drawerLayout) { - - return new ActionBarDrawerToggle( - this, drawerLayout, null, R.string.drawer_open, R.string.drawer_close) { - - public void onDrawerClosed(View view) { - actionBar.setTitle(selectedCounterName); - supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() - } - - public void onDrawerOpened(View drawerView) { - actionBar.setTitle(getTitle()); - supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() - } - }; + ContextCompat.RECEIVER_EXPORTED); } @Override @@ -116,7 +92,7 @@ protected void onResume() { private void initCountersList() { final FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.menu_frame, new CountersListFragment()); + transaction.replace(R.id.mainMenuFrame, new CountersListFragment()); transaction.commit(); } @@ -130,14 +106,14 @@ private void switchCounter(@NonNull final String counterName) { selectedCounterFragment.setArguments(bundle); getSupportFragmentManager() .beginTransaction() - .replace(R.id.content_frame, selectedCounterFragment) + .replace(R.id.mainContentFrame, selectedCounterFragment) .commitAllowingStateLoss(); - actionBar.setTitle(counterName); + toolbar.setTitle(counterName); - if (isNavigationOpen()) { - closeNavigation(); - } + if (isNavigationOpen()) { + closeNavigation(); + } } /** @@ -179,12 +155,6 @@ public boolean onCreateOptionsMenu(Menu menu) { return true; } - @Override - protected void onPostCreate(Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - navigationToggle.syncState(); - } - @SuppressLint("ApplySharedPref") @Override protected void onPause() { @@ -195,43 +165,36 @@ protected void onPause() { prefsEditor.commit(); } - @Override - public void onConfigurationChanged(@NonNull final Configuration newConfig) { - super.onConfigurationChanged(newConfig); - navigationToggle.onConfigurationChanged(newConfig); - } - @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - if (isNavigationOpen()) { - closeNavigation(); - } else { - openDrawer(); - } - return true; - - case R.id.menu_settings: - startActivity(new Intent(this, SettingsActivity.class)); - return true; - - default: - return super.onOptionsItemSelected(item); + return switch (item.getItemId()) { + case android.R.id.home -> { + if (isNavigationOpen()) { + closeNavigation(); + } else { + openDrawer(); + } + yield true; + } + case R.id.menu_settings -> { + startActivity(new Intent(this, SettingsActivity.class)); + yield true; + } + default -> super.onOptionsItemSelected(item); + }; + } + + public boolean isNavigationOpen() { + return navigationLayout.isDrawerOpen(menuFrame); } - } - - public boolean isNavigationOpen() { - return navigationLayout.isDrawerOpen(menuFrame); - } - private void closeNavigation() { - navigationLayout.closeDrawer(menuFrame); - } + private void closeNavigation() { + navigationLayout.closeDrawer(menuFrame); + } - private void openDrawer() { - navigationLayout.openDrawer(menuFrame); - } + private void openDrawer() { + navigationLayout.openDrawer(menuFrame); + } private class CounterChangeReceiver extends BroadcastReceiver { diff --git a/app/src/main/java/me/tsukanov/counter/activities/SettingsActivity.java b/app/src/main/java/me/tsukanov/counter/activities/SettingsActivity.java index 3ed6f6bb..c8f60b2d 100644 --- a/app/src/main/java/me/tsukanov/counter/activities/SettingsActivity.java +++ b/app/src/main/java/me/tsukanov/counter/activities/SettingsActivity.java @@ -15,6 +15,9 @@ import androidx.preference.Preference.OnPreferenceChangeListener; import androidx.preference.Preference.OnPreferenceClickListener; import androidx.preference.PreferenceManager; + +import com.google.android.material.appbar.MaterialToolbar; + import java.io.IOException; import me.tsukanov.counter.CounterApplication; import me.tsukanov.counter.R; @@ -40,6 +43,11 @@ public class SettingsActivity extends AppCompatActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.layout_settings); + + MaterialToolbar actionBar = findViewById(R.id.settingsToolbar); + setSupportActionBar(actionBar); + sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); Themes.initCurrentTheme(sharedPrefs); @@ -57,7 +65,7 @@ private void initSettingsFragment() { getSupportFragmentManager() .beginTransaction() - .replace(android.R.id.content, settingsFragment) + .replace(R.id.settingsFrame, settingsFragment) .commit(); } @@ -109,7 +117,7 @@ private OnPreferenceClickListener getOnTipClickListener() { protected void onPostCreate(final Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); getDelegate().onPostCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); +// getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override diff --git a/app/src/main/java/me/tsukanov/counter/infrastructure/Actions.java b/app/src/main/java/me/tsukanov/counter/infrastructure/Actions.java index f4625a15..2ab0c16a 100644 --- a/app/src/main/java/me/tsukanov/counter/infrastructure/Actions.java +++ b/app/src/main/java/me/tsukanov/counter/infrastructure/Actions.java @@ -9,6 +9,6 @@ public enum Actions { @NonNull public String getActionName() { - return String.format("%s.%s", CounterApplication.PACKAGE_NAME, this.toString()); + return String.format("%s.%s", CounterApplication.PACKAGE_NAME, this); } } diff --git a/app/src/main/java/me/tsukanov/counter/view/CounterFragment.java b/app/src/main/java/me/tsukanov/counter/view/CounterFragment.java index f3405af4..4b3f271d 100644 --- a/app/src/main/java/me/tsukanov/counter/view/CounterFragment.java +++ b/app/src/main/java/me/tsukanov/counter/view/CounterFragment.java @@ -17,22 +17,24 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; -import android.widget.FrameLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.preference.PreferenceManager; +import java.util.Locale; +import java.util.Map; import java.util.Objects; import me.tsukanov.counter.CounterApplication; import me.tsukanov.counter.R; import me.tsukanov.counter.SharedPrefKeys; -import me.tsukanov.counter.activities.MainActivity; import me.tsukanov.counter.domain.IntegerCounter; import me.tsukanov.counter.repository.CounterStorage; import me.tsukanov.counter.repository.exceptions.MissingCounterException; import me.tsukanov.counter.view.dialogs.DeleteDialog; import me.tsukanov.counter.view.dialogs.EditDialog; +import org.apache.commons.text.StringSubstitutor; +import org.joda.time.format.DateTimeFormat; public class CounterFragment extends Fragment { @@ -50,8 +52,9 @@ public class CounterFragment extends Fragment { private SharedPreferences sharedPrefs; private Vibrator vibrator; - private FrameLayout counterFrame; + private TextView counterLabel; + private TextView updateTimestampLabel; private Button incrementButton; private Button decrementButton; @@ -133,14 +136,15 @@ public View onCreateView( decrementButton.setOnClickListener(v -> decrement()); counterLabel = view.findViewById(R.id.counterLabel); + updateTimestampLabel = view.findViewById(R.id.updateTimestampLabel); - counterFrame = view.findViewById(R.id.counterFrame); - counterFrame.setOnClickListener( - v -> { - if (sharedPrefs.getBoolean(SharedPrefKeys.LABEL_CONTROL_ON.getName(), true)) { - increment(); - } - }); + view.findViewById(R.id.counterFrame) + .setOnClickListener( + v -> { + if (sharedPrefs.getBoolean(SharedPrefKeys.LABEL_CONTROL_ON.getName(), true)) { + increment(); + } + }); updateInterface(); @@ -152,20 +156,6 @@ public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInf inflater.inflate(R.menu.counter_menu, menu); } - @Override - public void onPrepareOptionsMenu(@NonNull final Menu menu) { - boolean isDrawerOpen = ((MainActivity) requireActivity()).isNavigationOpen(); - - MenuItem editItem = menu.findItem(R.id.menu_edit); - editItem.setVisible(!isDrawerOpen); - - MenuItem deleteItem = menu.findItem(R.id.menu_delete); - deleteItem.setVisible(!isDrawerOpen); - - MenuItem resetItem = menu.findItem(R.id.menu_reset); - resetItem.setVisible(!isDrawerOpen); - } - public boolean onKeyDown(int keyCode) { switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: @@ -264,6 +254,20 @@ private void reset() { private void updateInterface() { counterLabel.setText(Integer.toString(counter.getValue())); + if (counter.getLastUpdatedDate() != null) { + final String formattedTimestamp = + counter + .getLastUpdatedDate() + .toString( + DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.getDefault())); + updateTimestampLabel.setText( + StringSubstitutor.replace( + getResources().getText(R.string.last_update_timestamp), + Map.of("timestamp", formattedTimestamp), + "{", + "}")); + } + incrementButton.setEnabled(counter.getValue() < IntegerCounter.MAX_VALUE); decrementButton.setEnabled(counter.getValue() > IntegerCounter.MIN_VALUE); } diff --git a/app/src/main/java/me/tsukanov/counter/view/CountersListFragment.java b/app/src/main/java/me/tsukanov/counter/view/CountersListFragment.java index 8444dd36..df1b1ea0 100644 --- a/app/src/main/java/me/tsukanov/counter/view/CountersListFragment.java +++ b/app/src/main/java/me/tsukanov/counter/view/CountersListFragment.java @@ -58,7 +58,7 @@ public void onActivityCreated(Bundle savedInstanceState) { requireActivity().getApplication(), new UpdateReceiver(), counterSetChangeFilter, - ContextCompat.RECEIVER_NOT_EXPORTED); + ContextCompat.RECEIVER_EXPORTED); } @Override diff --git a/app/src/main/res/drawable/ic_back.xml b/app/src/main/res/drawable/ic_back.xml new file mode 100644 index 00000000..77191eaa --- /dev/null +++ b/app/src/main/res/drawable/ic_back.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu.xml b/app/src/main/res/drawable/ic_menu.xml new file mode 100644 index 00000000..24ccd115 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_phone_android.xml b/app/src/main/res/drawable/ic_phone.xml similarity index 100% rename from app/src/main/res/drawable/ic_phone_android.xml rename to app/src/main/res/drawable/ic_phone.xml diff --git a/app/src/main/res/drawable/ic_exposure_plus_1.xml b/app/src/main/res/drawable/ic_plus_one.xml similarity index 100% rename from app/src/main/res/drawable/ic_exposure_plus_1.xml rename to app/src/main/res/drawable/ic_plus_one.xml diff --git a/app/src/main/res/drawable/ic_volume_up.xml b/app/src/main/res/drawable/ic_volume.xml similarity index 100% rename from app/src/main/res/drawable/ic_volume_up.xml rename to app/src/main/res/drawable/ic_volume.xml diff --git a/app/src/main/res/layout-land/counter.xml b/app/src/main/res/layout-land/counter.xml index 09705a97..67bf6ba9 100644 --- a/app/src/main/res/layout-land/counter.xml +++ b/app/src/main/res/layout-land/counter.xml @@ -1,56 +1,78 @@ - - + android:layout_marginLeft="16dp" + android:layout_marginTop="56dp" + android:layout_marginRight="16dp" + android:layout_marginBottom="16dp" + android:orientation="horizontal"> - - + android:layout_weight="0.91" + android:orientation="vertical"> + + + + + + + + + + + android:layout_width="150dp" + android:layout_height="match_parent" + android:orientation="vertical">