-
Notifications
You must be signed in to change notification settings - Fork 30
/
MainActivity.java
200 lines (159 loc) · 5.58 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package it.feio.android.checklistview.demo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import it.feio.android.checklistview.Settings;
import it.feio.android.checklistview.exceptions.ViewNotSupportedException;
import it.feio.android.checklistview.interfaces.CheckListChangedListener;
import it.feio.android.checklistview.interfaces.Constants;
import it.feio.android.checklistview.models.ChecklistManager;
public class MainActivity extends AppCompatActivity implements CheckListChangedListener {
View switchView;
private SharedPreferences prefs;
boolean isChecklist;
private ChecklistManager mChecklistManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
prefs = PreferenceManager.getDefaultSharedPreferences(this);
switchView = findViewById(R.id.edittext);
((EditText)switchView).setText(prefs.getString("text", getString(R.string.template_phrase)));
if (prefs.getBoolean("isChecklist", false)) {
isChecklist = false;
toggleCheckList();
}
initTextView();
}
private void initTextView() {
TextView textview = findViewById(R.id.bottom_banner);
textview.setText(Html.fromHtml(getString(R.string.omninotes)));
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.omninotes_link))));
}
});
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("refresh", false)) {
if (!isChecklist) {
toggleCheckList();
}
prefs.edit().putBoolean("refresh", false).apply();
}
}
@Override
protected void onPause() {
save();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_toggle_checklist:
toggleCheckList();
break;
case R.id.action_move_to_bottom:
mChecklistManager.moveCheckedToBottom();
break;
case R.id.settings:
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
break;
}
return super.onOptionsItemSelected(item);
}
private void toggleCheckList() {
View newView;
/*
* Here is where the job is done. By simply calling an instance of the
* ChecklistManager we can call its methods.
*/
try {
// Getting instance
mChecklistManager = mChecklistManager == null ? new ChecklistManager(getApplicationContext()) : mChecklistManager;
/*
* These method are useful when converting from EditText to
* ChecklistView (but can be set anytime, they'll be used at
* appropriate moment)
*/
// Setting new entries hint text (if not set no hint
// will be used)
mChecklistManager.newEntryHint(prefs.getString("settings_hint", ""));
// Let checked items are moved on bottom
mChecklistManager.moveCheckedOnBottom(Integer.parseInt(prefs.getString("settings_checked_items_behavior",
String.valueOf(Settings.CHECKED_HOLD))));
// Is also possible to set a general changes listener
mChecklistManager.setCheckListChangedListener(this);
/*
* These method are useful when converting from ChecklistView to
* EditText (but can be set anytime, they'll be used at appropriate
* moment)
*/
// Decide if keep or remove checked items when converting
// back to simple text from checklist
mChecklistManager.linesSeparator(prefs.getString("settings_lines_separator", Constants.LINES_SEPARATOR));
// Decide if keep or remove checked items when converting
// back to simple text from checklist
mChecklistManager.keepChecked(prefs.getBoolean("settings_keep_checked", Constants.KEEP_CHECKED));
// I want to make checks symbols visible when converting
// back to simple text from checklist
mChecklistManager.showCheckMarks(prefs.getBoolean("settings_show_checks", Constants.SHOW_CHECKS));
// Enable or disable drag & drop
mChecklistManager.dragEnabled(true);
mChecklistManager.dragVibrationEnabled(true);
// Converting actual EditText into a View that can
// replace the source or viceversa
newView = mChecklistManager.convert(switchView);
// Replacing view in the layout
mChecklistManager.replaceViews(switchView, newView);
// Updating the instance of the pointed view for
// eventual reverse conversion
switchView = newView;
isChecklist = !isChecklist;
} catch (ViewNotSupportedException e) {
// This exception is fired if the source view class is not supported
e.printStackTrace();
}
}
private void save(){
String text = "";
if (isChecklist) {
try {
text = ((EditText)mChecklistManager.convert(switchView)).getText().toString();
} catch (ViewNotSupportedException e) {
e.printStackTrace();
}
} else {
text = ((EditText)switchView).getText().toString();
}
prefs.edit()
.putString("text", text)
.putBoolean("isChecklist", isChecklist)
.apply();
}
@Override
public void onCheckListChanged() {
Log.v(Constants.TAG, "Some text is changed!!");
}
}