Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Collect 541 #1259

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2017 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.odk.collect.android.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import org.odk.collect.android.bundle.CollectDialogBundle;
import org.odk.collect.android.fragments.CollectDialogFragment;

public abstract class CollectAbstractActivity extends AppCompatActivity {
protected FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager = getSupportFragmentManager();
}

public void buildDialog(CollectDialogBundle collectDialogBundle) {
if (collectDialogBundle != null) {
createAndShowDialog(collectDialogBundle);
}
}

private void createAndShowDialog(CollectDialogBundle collectDialogBundle) {
CollectDialogFragment dialogFragment = CollectDialogFragment.newInstance(collectDialogBundle);
dialogFragment.show(fragmentManager, collectDialogBundle.getDialogTag());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2017 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.odk.collect.android.bundle;

import java.io.Serializable;

public class CollectDialogBundle implements Serializable {

private static final String COLLECT_DIALOG_TAG = "collectDialogTag";

private String dialogTag;
private String dialogTitle;
private String dialogMessage;
private String negativeButtonText;
private String positiveButtonText;
private String neutralButtonText;

private boolean cancelable;

private int icon;
private int actionTag;

CollectDialogBundle(Builder builder) {
dialogTag = builder.dialogTag;
dialogTitle = builder.dialogTitle;
dialogMessage = builder.dialogMessage;
negativeButtonText = builder.negativeButtonText;
positiveButtonText = builder.positiveButtonText;
neutralButtonText = builder.neutralButtonText;
cancelable = builder.cancelable;
icon = builder.icon;
actionTag = builder.actionTag;
}

public String getDialogTag() {
return dialogTag;
}

public String getDialogTitle() {
return dialogTitle;
}

public String getDialogMessage() {
return dialogMessage;
}

public String getNegativeButtonText() {
return negativeButtonText;
}

public String getPositiveButtonText() {
return positiveButtonText;
}

public String getNeutralButtonText() {
return neutralButtonText;
}

public boolean isCancelable() {
return cancelable;
}

public Integer getIcon() {
return icon;
}

public int getActionTag() {
return actionTag;
}

public static class Builder {

private String dialogTag;
private String dialogTitle;
private String dialogMessage;
private String negativeButtonText;
private String positiveButtonText;
private String neutralButtonText;

private boolean cancelable;

private int icon;
private int actionTag;

public Builder() {
this(COLLECT_DIALOG_TAG);
}

public Builder(String dialogTag) {
this.dialogTag = dialogTag;
}

public Builder setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
return this;
}

public Builder setDialogMessage(String dialogMessage) {
this.dialogMessage = dialogMessage;
return this;
}

public Builder setNegativeButtonText(String negativeButtonText) {
this.negativeButtonText = negativeButtonText;
return this;
}

public Builder setPositiveButtonText(String positiveButtonText) {
this.positiveButtonText = positiveButtonText;
return this;
}

public Builder setNeutralButtonText(String neutralButtonText) {
this.neutralButtonText = neutralButtonText;
return this;
}

public Builder setCancelable(Boolean cancelable) {
this.cancelable = cancelable;
return this;
}

public Builder setIcon(int icon) {
this.icon = icon;
return this;
}

public Builder setActionTag(int actionTag) {
this.actionTag = actionTag;
return this;
}

public CollectDialogBundle build() {
return new CollectDialogBundle(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2017 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.odk.collect.android.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import org.odk.collect.android.bundle.CollectDialogBundle;

public class CollectDialogFragment extends DialogFragment {

public static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle";

public interface DialogButtonCallbacks {
void onNegativeButtonClick(DialogInterface dialog, int actionTag);

void onPositiveButtonClick(DialogInterface dialog, int actionTag);

void onNeutralButtonClick(DialogInterface dialog, int actionTag);
}

private DialogButtonCallbacks callback;

public static CollectDialogFragment newInstance(CollectDialogBundle collectDialogBundle) {
CollectDialogFragment dialogFragment = new CollectDialogFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle);
dialogFragment.setArguments(bundle);
return dialogFragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final CollectDialogBundle collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

if (collectDialogBundle != null) {
final int actionTag = collectDialogBundle.getActionTag();

builder
.setTitle(collectDialogBundle.getDialogTitle())
.setMessage(collectDialogBundle.getDialogMessage())
.setCancelable(collectDialogBundle.isCancelable());

if (collectDialogBundle.getNegativeButtonText() != null) {
builder.setNegativeButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (callback != null) {
callback.onNegativeButtonClick(dialog, actionTag);
}
}
});
}

if (collectDialogBundle.getPositiveButtonText() != null) {
builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (callback != null) {
callback.onPositiveButtonClick(dialog, actionTag);
}
}
});
}

if (collectDialogBundle.getNeutralButtonText() != null) {
builder.setNeutralButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (callback != null) {
callback.onNeutralButtonClick(dialog, actionTag);
}
}
});
}

if (collectDialogBundle.getIcon() != null) {
builder.setIcon(collectDialogBundle.getIcon());
}

setCancelable(collectDialogBundle.isCancelable());
}

return builder.create();
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
callback = (DialogButtonCallbacks) context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package org.odk.collect.android.preferences;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import org.odk.collect.android.R;
import org.odk.collect.android.activities.CollectAbstractActivity;
import org.odk.collect.android.fragments.CollectDialogFragment;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -36,10 +38,12 @@
* @author Thomas Smyth, Sassafras Tech Collective (tom@sassafrastech.com; constraint behavior
* option)
*/
public class AdminPreferencesActivity extends AppCompatActivity {
public class AdminPreferencesActivity extends CollectAbstractActivity implements CollectDialogFragment.DialogButtonCallbacks {
public static final String ADMIN_PREFERENCES = "admin_prefs";
public static final String TAG = "AdminPreferencesFragment";

public static final int FINISH_RESET_SETTINGS = 1;

public static boolean saveSharedPreferencesToFile(File dst, Context context) {
// this should be in a thread if it gets big, but for now it's tiny
boolean res = false;
Expand Down Expand Up @@ -82,4 +86,24 @@ protected void onCreate(Bundle savedInstanceState) {
.commit();
}
}

@Override
public void onNegativeButtonClick(DialogInterface dialog, int actionTag) {

}

@Override
public void onPositiveButtonClick(DialogInterface dialog, int actionTag) {
switch (actionTag) {
case FINISH_RESET_SETTINGS:
dialog.dismiss();
recreate();
break;
}
}

@Override
public void onNeutralButtonClick(DialogInterface dialog, int actionTag) {

}
}
Loading