Skip to content

Commit

Permalink
Show update timestamps
Browse files Browse the repository at this point in the history
Plus, a bunch of layout fixes throughout the app.

Fixes #34.
  • Loading branch information
gentlecat committed Dec 28, 2024
1 parent eb46179 commit eca1d0c
Show file tree
Hide file tree
Showing 29 changed files with 487 additions and 492 deletions.
119 changes: 41 additions & 78 deletions app/src/main/java/me/tsukanov/counter/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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();
}

Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -57,7 +65,7 @@ private void initSettingsFragment() {

getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, settingsFragment)
.replace(R.id.settingsFrame, settingsFragment)
.commit();
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
52 changes: 28 additions & 24 deletions app/src/main/java/me/tsukanov/counter/view/CounterFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;

Expand Down Expand Up @@ -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();

Expand All @@ -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:
Expand Down Expand 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
requireActivity().getApplication(),
new UpdateReceiver(),
counterSetChangeFilter,
ContextCompat.RECEIVER_NOT_EXPORTED);
ContextCompat.RECEIVER_EXPORTED);
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorPrimary"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorPrimary"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z" />
</vector>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit eca1d0c

Please sign in to comment.