Created
November 20, 2018 12:25
-
-
Save ruan65/b41c57a957baa5bb22a0b69d3a9e1321 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package premiumapp.org.jtimer.dialogs; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.graphics.drawable.ColorDrawable; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.DialogFragment; | |
import android.view.Window; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
import premiumapp.org.jtimer.JeyTimerActivity; | |
import premiumapp.org.jtimer.R; | |
import premiumapp.org.jtimer.data.LocalDialogListAdapter; | |
import premiumapp.org.jtimer.data.LocalListData; | |
import premiumapp.org.jtimer.utils.Util; | |
public class LocalDialog extends DialogFragment { | |
protected Context mCtx; | |
protected ListView mIntervalList; | |
protected String[] mData; | |
protected AdapterView.OnItemClickListener onItemClickListener; | |
protected String mHeaderText; | |
public static LocalDialog newInstance(Context ctx, int stringArrayId, String header) { | |
LocalDialog ld = new LocalDialog(); | |
ld.mData = ctx.getResources().getStringArray(stringArrayId); | |
ld.mCtx = ctx; | |
ld.mHeaderText = header; | |
return ld; | |
} | |
@NonNull | |
@Override | |
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { | |
Dialog dialog = new Dialog(mCtx); | |
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
dialog.setContentView(R.layout.local_dialog); | |
Window window = dialog.getWindow(); | |
window.setBackgroundDrawable(new ColorDrawable(0)); | |
((TextView) window.findViewById(R.id.tv_dialog_header)).setText(mHeaderText); | |
mIntervalList = (ListView) window.findViewById(R.id.interval_list); | |
LocalDialogListAdapter adapter = prepareAdapter(); | |
mIntervalList.setAdapter(adapter); | |
mIntervalList.setOnItemClickListener(onItemClickListener); | |
setRetainInstance(true); // to avoid app crash if return to foreground while dialog is open | |
return dialog; | |
} | |
public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) { | |
this.onItemClickListener = onItemClickListener; | |
} | |
private LocalDialogListAdapter prepareAdapter() { | |
List<LocalListData> data = new ArrayList<>(); | |
LocalDialogListAdapter adapter = new LocalDialogListAdapter(mCtx, data); | |
updateAdapter(adapter); | |
return adapter; | |
} | |
private void updateAdapter(LocalDialogListAdapter adapter) { | |
adapter.clear(); | |
adapter.setNotifyOnChange(false); | |
JeyTimerActivity jta = (JeyTimerActivity) mCtx; | |
if (mHeaderText.equals(getString(R.string.notify_every))) { | |
int current = jta.mTimerNotifyIntervalPrefs.getInt(jta.getCurrentTimer() + "", 60_000); | |
for (String s : mData) { | |
LocalListData d = new LocalListData(s, s.equals(Util.getIntervalString(mCtx, current))); | |
adapter.add(d); | |
} | |
} else if (mHeaderText.equals(getString(R.string.sounds))) { | |
String currentSound = jta.mTimerSoundPrefs.getString(jta.getCurrentTimer() + "", ""); | |
for (String s : mData) { | |
LocalListData d = new LocalListData(s, s.equals(currentSound)); | |
adapter.add(d); | |
} | |
} | |
adapter.notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment