From 10c68a7617c917dd24935004a448291ef93f4167 Mon Sep 17 00:00:00 2001 From: TecCheck Date: Thu, 17 Jun 2021 20:42:53 +0200 Subject: [PATCH] Add TagEditActivity --- app/src/main/AndroidManifest.xml | 3 +- .../android/DataRepository.java | 10 ++ .../android/db/AppDatabase.java | 4 + .../android/db/dao/TagDao.java | 7 ++ .../android/ui/TagEditActivity.java | 105 ++++++++++++++++++ .../android/ui/TagListActivity.java | 8 +- .../android/viewmodel/TagViewModel.java | 9 ++ app/src/main/res/layout/activity_tag_edit.xml | 47 ++++++++ .../main/res/menu/menu_tag_edit_activity.xml | 13 +++ app/src/main/res/values-de/strings.xml | 4 + app/src/main/res/values/strings.xml | 4 + 11 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/github/storagereloaded/android/ui/TagEditActivity.java create mode 100644 app/src/main/res/layout/activity_tag_edit.xml create mode 100644 app/src/main/res/menu/menu_tag_edit_activity.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d231a2a..b0c193d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.StoRe"> - + + > getTags() { return appDatabase.tagDao().getTags(); } + public LiveData getTag(int tagId) { + return appDatabase.tagDao().getTag(tagId); + } + public void saveItem(ItemEntity item) { appDatabase.saveItem(item); } @@ -81,7 +85,13 @@ public void saveDatabase(DatabaseEntity database) { appDatabase.saveDatabase(database); } + public void saveTag(TagEntity tag) { + appDatabase.saveTag(tag); + } + public void deleteItem(int itemId) { appDatabase.deleteItem(itemId); } + + } \ No newline at end of file diff --git a/app/src/main/java/io/github/storagereloaded/android/db/AppDatabase.java b/app/src/main/java/io/github/storagereloaded/android/db/AppDatabase.java index 2ecf0aa..65bfcfd 100644 --- a/app/src/main/java/io/github/storagereloaded/android/db/AppDatabase.java +++ b/app/src/main/java/io/github/storagereloaded/android/db/AppDatabase.java @@ -132,6 +132,10 @@ public void saveDatabase(DatabaseEntity database) { appExecutors.diskIO().execute(() -> databaseDao().insert(database)); } + public void saveTag(TagEntity tag) { + appExecutors.diskIO().execute(() -> tagDao().insert(tag)); + } + public void deleteItem(int itemId) { appExecutors.diskIO().execute(() -> itemDao().deleteItem(itemId)); } diff --git a/app/src/main/java/io/github/storagereloaded/android/db/dao/TagDao.java b/app/src/main/java/io/github/storagereloaded/android/db/dao/TagDao.java index 6da2a4f..5646166 100644 --- a/app/src/main/java/io/github/storagereloaded/android/db/dao/TagDao.java +++ b/app/src/main/java/io/github/storagereloaded/android/db/dao/TagDao.java @@ -8,6 +8,7 @@ import java.util.List; +import io.github.storagereloaded.android.db.entity.DatabaseEntity; import io.github.storagereloaded.android.db.entity.TagEntity; @Dao @@ -18,6 +19,12 @@ public interface TagDao { @Query("SELECT * FROM tags ORDER BY name") LiveData> getTags(); + @Query("SELECT * FROM tags WHERE id=:tagId") + LiveData getTag(int tagId); + @Insert(onConflict = OnConflictStrategy.REPLACE) void insertAll(List tags); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(TagEntity tag); } diff --git a/app/src/main/java/io/github/storagereloaded/android/ui/TagEditActivity.java b/app/src/main/java/io/github/storagereloaded/android/ui/TagEditActivity.java new file mode 100644 index 0000000..b9b8724 --- /dev/null +++ b/app/src/main/java/io/github/storagereloaded/android/ui/TagEditActivity.java @@ -0,0 +1,105 @@ +package io.github.storagereloaded.android.ui; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; + +import android.content.Intent; +import android.os.Bundle; +import android.view.MenuItem; +import android.widget.EditText; + +import com.google.android.material.appbar.MaterialToolbar; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.textfield.TextInputLayout; + +import io.github.storagereloaded.android.R; +import io.github.storagereloaded.android.db.entity.TagEntity; +import io.github.storagereloaded.android.viewmodel.TagViewModel; + +public class TagEditActivity extends AppCompatActivity { + + public static final String TAG_NAME = "tag_name"; + + EditText name; + + TagViewModel model; + TagEntity tag; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_tag_edit); + + MaterialToolbar toolbar = findViewById(R.id.toolbar); + toolbar.setOnMenuItemClickListener(this::onOptionsItemSelected); + toolbar.setNavigationOnClickListener(v -> onBackPressed()); + + name = ((TextInputLayout) findViewById(R.id.tag_name)).getEditText(); + + model = new ViewModelProvider(this).get(TagViewModel.class); + + Intent intent = getIntent(); + if (intent.hasExtra(TagListActivity.EXTRA_TAG_ID)) { + // Existing tag should be edited + model.getTag(intent.getIntExtra(TagListActivity.EXTRA_TAG_ID, 0)).observe(this, tag -> { + if (tag == null) + return; + + this.tag = tag; + + if (!model.loaded) { + name.setText(tag.getName()); + model.loaded = true; + } + }); + } + + if (savedInstanceState != null) + name.setText(savedInstanceState.getString(TAG_NAME)); + } + + @Override + public void onBackPressed() { + if (name.getText().toString().equals("")) + finish(); + else + showUnsavedDialog(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.save) { + saveDatabase(); + finish(); + return true; + } + + return super.onOptionsItemSelected(item); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(TAG_NAME, name.getText().toString()); + } + + private void showUnsavedDialog() { + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this); + builder.setTitle(R.string.unsaved_dialog_title); + builder.setMessage(R.string.unsaved_dialog_description); + builder.setPositiveButton(android.R.string.ok, (dialog, which) -> finish()); + builder.setNegativeButton(android.R.string.cancel, null); + + builder.create().show(); + } + + private void saveDatabase() { + if(tag == null) { + tag = new TagEntity(); + tag.setId(0); + } + tag.setName(name.getText().toString()); + model.saveTag(tag); + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/storagereloaded/android/ui/TagListActivity.java b/app/src/main/java/io/github/storagereloaded/android/ui/TagListActivity.java index 32c4381..d9c1f09 100644 --- a/app/src/main/java/io/github/storagereloaded/android/ui/TagListActivity.java +++ b/app/src/main/java/io/github/storagereloaded/android/ui/TagListActivity.java @@ -43,9 +43,9 @@ protected void onCreate(Bundle savedInstanceState) { intent.putExtra(EXTRA_TAG_ID, tagId); setResult(RESULT_OK, intent); } else { - //Intent intent = new Intent(getApplicationContext(), TagEditActivity.class); - //intent.putExtra(EXTRA_TAG_ID, tagId); - //startActivity(intent); + Intent intent = new Intent(getApplicationContext(), TagEditActivity.class); + intent.putExtra(EXTRA_TAG_ID, tagId); + startActivity(intent); } }); @@ -54,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) { FloatingActionButton itemAddButton = findViewById(R.id.fab); itemAddButton.setOnClickListener(v -> { - //startActivity(new Intent(this, TagEditActivity.class)); + startActivity(new Intent(this, TagEditActivity.class)); }); setResult(RESULT_CANCELED); diff --git a/app/src/main/java/io/github/storagereloaded/android/viewmodel/TagViewModel.java b/app/src/main/java/io/github/storagereloaded/android/viewmodel/TagViewModel.java index d300e15..8b3086f 100644 --- a/app/src/main/java/io/github/storagereloaded/android/viewmodel/TagViewModel.java +++ b/app/src/main/java/io/github/storagereloaded/android/viewmodel/TagViewModel.java @@ -15,6 +15,7 @@ public class TagViewModel extends AndroidViewModel { private final DataRepository repository; + public boolean loaded = false; public TagViewModel(@NonNull Application application) { super(application); @@ -24,4 +25,12 @@ public TagViewModel(@NonNull Application application) { public LiveData> getTags() { return repository.getTags(); } + + public LiveData getTag(int tagId) { + return repository.getTag(tagId); + } + + public void saveTag(TagEntity tag) { + repository.saveTag(tag); + } } diff --git a/app/src/main/res/layout/activity_tag_edit.xml b/app/src/main/res/layout/activity_tag_edit.xml new file mode 100644 index 0000000..a40e1b6 --- /dev/null +++ b/app/src/main/res/layout/activity_tag_edit.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_tag_edit_activity.xml b/app/src/main/res/menu/menu_tag_edit_activity.xml new file mode 100644 index 0000000..25fc838 --- /dev/null +++ b/app/src/main/res/menu/menu_tag_edit_activity.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index f4efc2c..3c06e8d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -72,4 +72,8 @@ Tags Tag Bearbeiten + + + Tag bearbeiten + Tag Name \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7d5ba10..8bb863a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -76,4 +76,8 @@ Tags Edit Tag + + + Edit Tag + Tag Name \ No newline at end of file