Skip to content

Commit

Permalink
1.修复每第一次登录需要重启应用才可以同步数据的bug;
Browse files Browse the repository at this point in the history
2.保存上一次的搜索笔记记录;
3.规范refreshData()方法。
  • Loading branch information
BlueOcean1998 committed Nov 2, 2020
1 parent eb7508a commit 39f7016
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode Integer.valueOf(getDate())
versionName "2.14.3"
versionName "2.14.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -44,7 +44,7 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0-alpha03'
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha06'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01'
implementation 'androidx.preference:preference:1.1.1'
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/cn/zerokirby/note/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -198,7 +199,7 @@ public boolean handleMessage(@NonNull Message msg) {
drawerLayout.closeDrawers();
Toast.makeText(getContext(), "同步成功!", Toast.LENGTH_SHORT).show();//显示解析到的内容
userDataHelper.updateSyncTime();
refreshData("");
refreshData();
break;
case UPLOAD:
Toast.makeText(getContext(), "上传成功!", Toast.LENGTH_SHORT).show();//上传头像成功
Expand Down Expand Up @@ -289,10 +290,13 @@ public void onClick(DialogInterface dialogInterface, int i) {
});

userDataHelper.getInfo();
refreshData("");
refreshData();
}

//刷新数据
public void refreshData() {
refreshData("");
}
public void refreshData(String s) {
recyclerView.startAnimation(adapterAlpha1);
//初始化笔记数据
Expand All @@ -317,7 +321,7 @@ public void run() {
@Override
public void run() {
//清空搜索内容
refreshData("");
refreshData();
searchText = "";
swipeRefreshLayout.setRefreshing(false);
}
Expand Down Expand Up @@ -411,6 +415,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View searchView = layoutInflater.inflate(R.layout.search_view, null);
EditText searchEt = searchView.findViewById(R.id.search_et);
if (!TextUtils.isEmpty(searchText)) searchEt.setText(searchText);
builder.setView(searchView);

builder.setPositiveButton("查找", new DialogInterface.OnClickListener() {
Expand All @@ -422,10 +427,10 @@ public void onClick(DialogInterface dialogInterface, int i) {//点击确定则
"找到" + noteList.size() + "条笔记", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//什么也不做
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//清空搜索信息
@Override
public void onClick(DialogInterface dialogInterface, int i) {

searchText = "";
}
});
builder.show();
Expand Down Expand Up @@ -453,6 +458,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
return super.onOptionsItemSelected(item);
}

@SuppressLint("UseCompatLoadingForDrawables")
public void checkLoginStatus() {//检查登录状态,以调整文字并确定按钮是否显示
AvatarDataHelper avatarDataHelper = new AvatarDataHelper();
isLogin = userDataHelper.getUserInfo().getUserId();
Expand Down Expand Up @@ -635,7 +641,7 @@ public void onReceive(Context context, Intent intent) {
modifyNote(note);
break;
case NoteChangeConstant.REFRESH_DATA:
refreshData("");
refreshData();
break;
case NoteChangeConstant.CHECK_LOGIN_STATUS:
checkLoginStatus();
Expand Down
13 changes: 3 additions & 10 deletions app/src/main/java/cn/zerokirby/note/data/NoteDataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class NoteDataHelper {

private final SimpleDateFormat simpleDateFormat;//简化日期

private final Intent intent;//本地广播发送
private final static String LOCAL_BROADCAST = "cn.zerokirby.note.LOCAL_BROADCAST";
private final LocalBroadcastManager localBroadcastManager;//本地广播管理器

public NoteDataHelper() {
Expand All @@ -40,7 +40,6 @@ public NoteDataHelper() {

simpleDateFormat = new SimpleDateFormat(getContext().getString(R.string.formatDate), Locale.getDefault());

intent = new Intent("cn.zerokirby.note.LOCAL_BROADCAST");
localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
}

Expand Down Expand Up @@ -126,6 +125,7 @@ public int saveChange(Note note) {
values.put("content", note.getContent());
values.put("time", System.currentTimeMillis());

Intent intent = new Intent(LOCAL_BROADCAST);
if (note.getId() == 0) {
//数据库插入操作
db.insert("Note", null, values);
Expand Down Expand Up @@ -155,10 +155,6 @@ public int saveChange(Note note) {
if (values != null) values.clear();
if (cursor != null) cursor.close();
if (db != null) db.close();

//清除intent中的extras
Bundle bundle = intent.getExtras();
if (bundle != null) bundle.clear();
}
return noteId;
}
Expand All @@ -174,17 +170,14 @@ public void deleteNote(int noteId) {
db.delete("Note", "id = ?", new String[]{String.valueOf(noteId)});

//发送本地广播通知MainActivity删除笔记
Intent intent = new Intent(LOCAL_BROADCAST);
intent.putExtra("operation_type", NoteChangeConstant.DELETE_NOTE_BY_ID);
intent.putExtra("note_id", noteId);
localBroadcastManager.sendBroadcast(intent);

Toast.makeText(getContext(), R.string.deleteSuccess, Toast.LENGTH_SHORT).show();
} finally {
if (db != null) db.close();

//清除intent中的extras
Bundle bundle = intent.getExtras();
if (bundle != null) bundle.clear();
}
}

Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/cn/zerokirby/note/data/UserDataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -30,15 +31,9 @@ public class UserDataHelper {
private Cursor cursor;

private String responseData;
private final int userId;
private int noteId;
private long time;
private String title;
private String content;

public UserDataHelper() {
databaseHelper = new DatabaseHelper("ProgressNote.db", null, 1);
userId = getUserInfo().getUserId();
}

/**
Expand All @@ -52,9 +47,14 @@ public void sendRequestWithOkHttpSC(Handler handler) {
public void run() {//在子线程中进行网络操作
Response response = null;
try {
OkHttpClient client = new OkHttpClient();//利用OkHttp发送HTTP请求调用服务器到客户端的同步servlet
RequestBody requestBody = new FormBody.Builder().add("userId", String.valueOf(userId)).build();
Request request = new Request.Builder().url("https://zerokirby.cn:8443/progress_note_server/SyncServlet_SC").post(requestBody).build();
//利用OkHttp发送HTTP请求调用服务器到客户端的同步servlet
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("userId", String.valueOf(getUserInfo().getUserId())).build();
Log.d("Foxizz_Test", String.valueOf(getUserInfo().getUserId()));
Request request = new Request.Builder()
.url("https://zerokirby.cn:8443/progress_note_server/SyncServlet_SC")
.post(requestBody).build();
response = client.newCall(request).execute();
responseData = Objects.requireNonNull(response.body()).string();
parseJSONWithJSONArray(responseData);//处理JSON
Expand Down Expand Up @@ -82,7 +82,7 @@ public void run() {//在子线程中进行网络操作
Response response = null;
try {
OkHttpClient client = new OkHttpClient();//利用OkHttp发送HTTP请求调用客户端到服务器的同步servlet
RequestBody requestBody = new FormBody.Builder().add("userId", String.valueOf(userId))
RequestBody requestBody = new FormBody.Builder().add("userId", String.valueOf(getUserInfo().getUserId()))
.add("json", Objects.requireNonNull(makeJSONArray())).build();
Request request = new Request.Builder().url("https://zerokirby.cn:8443/progress_note_server/SyncServlet_CS").post(requestBody).build();
response = client.newCall(request).execute();
Expand Down Expand Up @@ -111,10 +111,10 @@ private void parseJSONWithJSONArray(String jsonData) {//处理JSON
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
noteId = jsonObject.getInt("NoteId");
time = jsonObject.getLong("Time");
title = jsonObject.getString("Title");
content = jsonObject.getString("Content");
int noteId = jsonObject.getInt("NoteId");
long time = jsonObject.getLong("Time");
String title = jsonObject.getString("Title");
String content = jsonObject.getString("Content");

ContentValues values = new ContentValues();//将笔记ID、标题、修改时间和内容存储到本地
values.put("id", noteId);
Expand Down

0 comments on commit 39f7016

Please sign in to comment.