-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/io/github/storagereloaded/android/ui/TagEditActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters