Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions app/src/main/java/com/adgad/kboard/AddWordDialogFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by arjun on 03/01/16.
*/
public class AddWordDialogFragment extends DialogFragment {

private static final String TAG = AddWordDialogFragment.class.getName();


public interface AddWordDialogListener {
void onDialogPositiveClick(DialogFragment dialog, int index);
Expand Down Expand Up @@ -55,28 +59,70 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
((TextView)view.findViewById(R.id.word)).setText(existingWord);
}
final int index = getArguments().getInt("index");
final boolean isEditing = (index > -1);

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setTitle("Edit Key")
.setNeutralButton("Move up", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNeutralClick(index);
}
})
.setPositiveButton(index > -1 ? "OK" : "Add", new DialogInterface.OnClickListener() {
.setTitle(isEditing ? "Edit Key" : "Add Key")
.setPositiveButton(isEditing ? "OK" : "Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(AddWordDialogFragment.this, index);
}
})
.setNegativeButton(index > -1 ? "Delete" : "Cancel", new DialogInterface.OnClickListener() {
.setNegativeButton(isEditing ? "Delete" : "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(AddWordDialogFragment.this, index);
}
});

if (isEditing) {
builder.setNeutralButton("Move up", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNeutralClick(index);
}
});
}

return builder.create();
}

@Override
public void onStart() {
super.onStart();

AlertDialog dialog = (AlertDialog) getDialog();
if (dialog == null) {
Log.e(TAG, "dialog is null, skipping button disabling");
return;
}

final int index = getArguments().getInt("index");
final int keyCount = getArguments().getInt("keyCount");
final boolean isEditing = (index > -1);

if (isEditing) {
if (keyCount <= 1) {
// Disable the "Delete" button, so that the last key will not be deleted
Button deleteButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
if (deleteButton != null) {
deleteButton.setEnabled(false);
} else {
Log.e(TAG, "deleteButton is null, cannot disable");
}
}
if (index == 0) {
// Disable the "Move up" button, as the topmost key cannot be moved up
Button moveUpButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if (moveUpButton != null) {
moveUpButton.setEnabled(false);
} else {
Log.e(TAG, "moveUpButton is null, cannot disable");
}
}
}
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/com/adgad/kboard/CustomKeysActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ public void onClick(View v) {


private void showAddDialog(int index, String word) {
final int keyCount = adapter.getItemCount();

DialogFragment newFragment = new AddWordDialogFragment();
Bundle args = new Bundle();
args.putInt("keyCount", keyCount);
args.putInt("index", index);
args.putString("word", word);
newFragment.setArguments(args);
Expand All @@ -113,7 +116,7 @@ public void onDialogPositiveClick(DialogFragment dialog, int index) {

@Override
public void onDialogNegativeClick(DialogFragment dialog, int index) {
if(index > 0) {
if(index >= 0) {
adapter.remove(index);
}
dialog.dismiss();
Expand Down