代码优化

master
liuyx 2 years ago
parent 1c70e3bfa3
commit f3d8513083

@ -1,5 +1,6 @@
package cc.liuyx.note.activity; package cc.liuyx.note.activity;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -21,6 +22,7 @@ import com.githang.statusbar.StatusBarCompat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Objects;
public abstract class BaseActivity extends AppCompatActivity { public abstract class BaseActivity extends AppCompatActivity {
@ -59,7 +61,6 @@ public abstract class BaseActivity extends AppCompatActivity {
public void setNightMode(){ public void setNightMode(){
if(isNightMode()) this.setTheme(R.style.NightTheme); if(isNightMode()) this.setTheme(R.style.NightTheme);
else setTheme(R.style.DayTheme); else setTheme(R.style.DayTheme);
} }
protected abstract void needRefresh(); protected abstract void needRefresh();
@ -71,8 +72,7 @@ public abstract class BaseActivity extends AppCompatActivity {
} }
public long calStrToSec(String date) throws ParseException {//decode calender date to second public long calStrToSec(String date) throws ParseException {//decode calender date to second
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long secTime = format.parse(date).getTime(); return Objects.requireNonNull(format.parse(date)).getTime();
return secTime;
} }
} }

@ -24,6 +24,7 @@ import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects;
import cc.liuyx.note.db.NoteDatabase; import cc.liuyx.note.db.NoteDatabase;
@ -47,7 +48,7 @@ public class EditActivity extends BaseActivity {
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar); setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true); Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);

@ -171,40 +171,40 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
add_tag = customView.findViewById(R.id.add_tag); add_tag = customView.findViewById(R.id.add_tag);
add_tag.setOnClickListener(new OnClickListener() { add_tag.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (sharedPreferences.getString("tagListString", "").split("_").length < 8) { if (sharedPreferences.getString("tagListString", "").split("_").length < 8) {
final EditText et = new EditText(context); final EditText et = new EditText(context);
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setMessage("输入标签名") .setMessage("输入标签名")
.setView(et) .setView(et)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
List<String> tagList = Arrays.asList(sharedPreferences.getString("tagListString", null).split("_")); //获取tags List<String> tagList = Arrays.asList(sharedPreferences.getString("tagListString", null).split("_")); //获取tags
String name = et.getText().toString(); String name = et.getText().toString();
if (!tagList.contains(name)) { if (!tagList.contains(name)) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String oldTagListString = sharedPreferences.getString("tagListString", null); String oldTagListString = sharedPreferences.getString("tagListString", null);
String newTagListString = oldTagListString + "_" + name; String newTagListString = oldTagListString + "_" + name;
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tagListString", newTagListString); editor.putString("tagListString", newTagListString);
editor.apply(); editor.apply();
refreshTagList(); refreshTagList();
} else } else
Toast.makeText(context, "Repeated tag!", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Repeated tag!", Toast.LENGTH_SHORT).show();
} }
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
} }
}).create().show(); }).create().show();
} else { } else {
Toast.makeText(context, "自定义的标签够多了!", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "自定义的标签够多了!", Toast.LENGTH_SHORT).show();
}
} }
}
}); });
//final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); //final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
@ -481,8 +481,8 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
else myToolbar.setTitle("所有笔记"); else myToolbar.setTitle("所有笔记");
} }
@SuppressLint("InflateParams")
public void initPopupView() { public void initPopupView() {
//instantiate the popup.xml layout file
layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = (ViewGroup) layoutInflater.inflate(R.layout.setting_layout, null); customView = (ViewGroup) layoutInflater.inflate(R.layout.setting_layout, null);
coverView = (ViewGroup) layoutInflater.inflate(R.layout.setting_cover, null); coverView = (ViewGroup) layoutInflater.inflate(R.layout.setting_cover, null);
@ -496,8 +496,6 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
private void initPrefs() { private void initPrefs() {
//initialize all useful SharedPreferences for the first time the app runs
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
if (!sharedPreferences.contains("nightMode")) { if (!sharedPreferences.contains("nightMode")) {

@ -35,15 +35,12 @@ public class AlarmReceiver extends BroadcastReceiver {
manager.createNotificationChannel(mChannel); manager.createNotificationChannel(mChannel);
} }
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setContentTitle(title).setContentText(content).setSmallIcon(R.drawable.red_alarm_24dp) .setContentTitle(title).setContentText(content).setSmallIcon(R.drawable.red_alarm_24dp)
.setContentIntent(pendingIntent).setAutoCancel(true).setFullScreenIntent(pendingIntent, true); .setContentIntent(pendingIntent).setAutoCancel(true).setFullScreenIntent(pendingIntent, true);
Notification notification = builder.build(); Notification notification = builder.build();
manager.notify(1, notification); manager.notify(1, notification);
} }
} }

Loading…
Cancel
Save