Skip to content

Commit

Permalink
Add TagEditActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
teccheck committed Jun 17, 2021
1 parent 934184a commit 10c68a7
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.StoRe">
<activity android:name=".ui.TagListActivity"></activity>
<activity android:name=".ui.TagEditActivity"></activity>
<activity android:name=".ui.TagListActivity" />
<activity android:name=".ui.ItemEditActivity" />
<activity android:name=".ui.ItemViewActivity" />
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public LiveData<List<TagEntity>> getTags() {
return appDatabase.tagDao().getTags();
}

public LiveData<TagEntity> getTag(int tagId) {
return appDatabase.tagDao().getTag(tagId);
}

public void saveItem(ItemEntity item) {
appDatabase.saveItem(item);
}
Expand All @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,12 @@ public interface TagDao {
@Query("SELECT * FROM tags ORDER BY name")
LiveData<List<TagEntity>> getTags();

@Query("SELECT * FROM tags WHERE id=:tagId")
LiveData<TagEntity> getTag(int tagId);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<TagEntity> tags);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(TagEntity tag);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class TagViewModel extends AndroidViewModel {

private final DataRepository repository;
public boolean loaded = false;

public TagViewModel(@NonNull Application application) {
super(application);
Expand All @@ -24,4 +25,12 @@ public TagViewModel(@NonNull Application application) {
public LiveData<List<TagEntity>> getTags() {
return repository.getTags();
}

public LiveData<TagEntity> getTag(int tagId) {
return repository.getTag(tagId);
}

public void saveTag(TagEntity tag) {
repository.saveTag(tag);
}
}
47 changes: 47 additions & 0 deletions app/src/main/res/layout/activity_tag_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="@menu/menu_database_settings"
app:navigationIcon="@drawable/ic_baseline_close_24"
app:title="@string/title_activity_tag_edit" />

</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tag_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:hint="@string/tag_name">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="?attr/colorOnBackground" />

</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/menu/menu_tag_edit_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/save"
android:title="@string/save"
app:showAsAction="always" />
<item
android:id="@+id/delete"
android:title="@string/delete" />

</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@
<!-- Tag List Activity -->
<string name="title_activity_tag_list">Tags</string>
<string name="edit_tag">Tag Bearbeiten</string>

<!-- Tag Edit Activity -->
<string name="title_activity_tag_edit">Tag bearbeiten</string>
<string name="tag_name">Tag Name</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@
<!-- Tag List Activity -->
<string name="title_activity_tag_list">Tags</string>
<string name="edit_tag">Edit Tag</string>

<!-- Tag Edit Activity -->
<string name="title_activity_tag_edit">Edit Tag</string>
<string name="tag_name">Tag Name</string>
</resources>

0 comments on commit 10c68a7

Please sign in to comment.