Skip to content

Commit

Permalink
fixed db @ services
Browse files Browse the repository at this point in the history
  • Loading branch information
agenthun committed Dec 19, 2015
1 parent 1d9afd2 commit 7ab5379
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.agenthun.readingroutine.datastore.UserData;
import com.agenthun.readingroutine.fragments.MenuFragment;
import com.agenthun.readingroutine.fragments.SettingsFragment;
import com.agenthun.readingroutine.services.AlarmNoiser;
import com.agenthun.readingroutine.services.AlarmNoiserIntentService;
import com.agenthun.readingroutine.transitionmanagers.TActivity;
import com.agenthun.readingroutine.utils.Avatar;
Expand Down Expand Up @@ -236,8 +237,8 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
}
});

//AlarmNoiser.startAlarmNoiserService(this, 5, AlarmNoiserIntentService.class, AlarmNoiserIntentService.ACTION_NOTIFICATION);
AlarmNoiserIntentService.startActionNotification(this, "test", "");
AlarmNoiser.startAlarmNoiserService(this, 5, AlarmNoiserIntentService.class, AlarmNoiserIntentService.ACTION_NOTIFICATION);
//AlarmNoiserIntentService.startActionNotification(this, "test", "");
Log.d(TAG, "onCreate() returned: startAlarmNoiserService");
}

Expand Down Expand Up @@ -347,7 +348,7 @@ public void onBackPressed() {
@Override
protected void onDestroy() {
super.onDestroy();
//AlarmNoiser.stopAlarmNoiserService(this, AlarmNoiserIntentService.class, AlarmNoiserIntentService.ACTION_NOTIFICATION);
AlarmNoiser.stopAlarmNoiserService(this, AlarmNoiserIntentService.class, AlarmNoiserIntentService.ACTION_NOTIFICATION);
Log.d(TAG, "onDestroy() returned: stopAlarmNoiserService");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ private BookDatabaseUtil(Context context) {
mContext = context;
}

public synchronized static BookDatabaseUtil getInstance(Context context) {
/* public synchronized static BookDatabaseUtil getInstance(Context context) {
if (instance == null) {
instance = new BookDatabaseUtil(context);
}
return instance;
}*/

public static BookDatabaseUtil getInstance(Context context) {
if (instance == null) {
synchronized (BookDatabaseUtil.class) {
if (instance == null) {
instance = new BookDatabaseUtil(context);
}
}
}
return instance;
}

public void destory() {
public static void destory() {
if (instance != null) {
instance.onDestory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public abstract class DBHelper extends SQLiteOpenHelper {

private SQLiteDatabase mDb;
protected SQLiteDatabase mDb;

public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ private NoteDatabaseUtil(Context context) {

public synchronized static NoteDatabaseUtil getInstance(Context context) {
if (instance == null) {
instance = new NoteDatabaseUtil(context);
synchronized (BookDatabaseUtil.class) {
if (instance == null) {
instance = new NoteDatabaseUtil(context);
}
}
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public class NotesFragment extends TFragment implements RevealBackgroundView.OnS

private ArrayList<NoteInfo> mDataSet;
private int itemPosition = 1;
NoteDatabaseUtil databaseUtil;

public NotesFragment() {
// Required empty public constructor
Expand All @@ -91,8 +90,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

private void setupDatabase() {
databaseUtil = NoteDatabaseUtil.getInstance(getContext());
mDataSet = databaseUtil.queryNoteInfos();
mDataSet = NoteDatabaseUtil.getInstance(getContext()).queryNoteInfos();
if (mDataSet == null && getIsTrial() != true) {
BmobQuery<NoteInfo> bmobQuery = new BmobQuery<>();
bmobQuery.setLimit(10);
Expand All @@ -106,7 +104,7 @@ private void setupDatabase() {
bmobQuery.findObjects(getContext(), new FindListener<NoteInfo>() {
@Override
public void onSuccess(List<NoteInfo> list) {
mDataSet = (ArrayList<NoteInfo>) databaseUtil.setNotes(list);
mDataSet = (ArrayList<NoteInfo>) NoteDatabaseUtil.getInstance(getContext()).setNotes(list);
}

@Override
Expand Down Expand Up @@ -297,17 +295,17 @@ private void addItem(String title, String compose, String time, int colorIndex)
public void onSuccess() {
Log.i(TAG, "上传服务器成功");
Log.i(TAG, noteInfo.getObjectId());
databaseUtil.insertNote(noteInfo);
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "上传服务器失败: " + s);
databaseUtil.insertNote(noteInfo, noteInfo, true); //无效invalid ObjectId
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfo, true); //无效invalid ObjectId
}
});
} else {
databaseUtil.insertNote(noteInfo, noteInfo, true); //无效invalid ObjectId
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfo, true); //无效invalid ObjectId
}
mDataSet.add(0, noteInfo);
notesAdapter.notifyDataSetChanged();
Expand All @@ -322,17 +320,17 @@ private void deleteItem(int position, boolean setAnimator) {
@Override
public void onSuccess() {
Log.i(TAG, "删除成功");
databaseUtil.deleteNote(noteInfo);
NoteDatabaseUtil.getInstance(getContext()).deleteNote(noteInfo);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "删除失败: " + s);
databaseUtil.deleteNote(noteInfo, true);
NoteDatabaseUtil.getInstance(getContext()).deleteNote(noteInfo, true);
}
});
} else {
databaseUtil.deleteNote(noteInfo, true);
NoteDatabaseUtil.getInstance(getContext()).deleteNote(noteInfo, true);
}

int size = mDataSet.size();
Expand Down Expand Up @@ -367,25 +365,25 @@ private void updateItem(final int position, String title, String compose, String
public void onSuccess() {
Log.i(TAG, "上传服务器成功");
Log.i(TAG, noteInfo.getObjectId());
databaseUtil.insertNote(noteInfo, noteInfoOld, true);
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfoOld, true);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "上传服务器失败: " + s);
databaseUtil.insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
}
});
} else {
databaseUtil.insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
}
} else {
//服务器
noteInfo.update(getContext(), noteInfo.getObjectId(), new UpdateListener() {
@Override
public void onSuccess() {
Log.i(TAG, "更新服务器成功");
databaseUtil.insertNote(noteInfo);
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo);
}

@Override
Expand All @@ -394,7 +392,7 @@ public void onFailure(int i, String s) {
switch (i) {
case 9010:
case 9016:
databaseUtil.insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
NoteDatabaseUtil.getInstance(getContext()).insertNote(noteInfo, noteInfoOld, true); //无效invalid ObjectId
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class RoutinesFragment extends TFragment implements RevealBackgroundView.
private boolean pendingIntro;
private ArrayList<BookInfo> mDataSet;
private int itemPosition = 1;
private BookDatabaseUtil databaseUtil;

public RoutinesFragment() {
// Required empty public constructor
Expand All @@ -86,8 +85,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

private void setupDatabase() {
databaseUtil = BookDatabaseUtil.getInstance(getContext());
mDataSet = databaseUtil.queryBookInfos();
mDataSet = BookDatabaseUtil.getInstance(getContext()).queryBookInfos();
if (mDataSet == null && getIsTrial() != true) {
BmobQuery<BookInfo> bmobQuery = new BmobQuery<>();
bmobQuery.setLimit(10);
Expand All @@ -101,7 +99,7 @@ private void setupDatabase() {
bmobQuery.findObjects(getContext(), new FindListener<BookInfo>() {
@Override
public void onSuccess(List<BookInfo> list) {
mDataSet = (ArrayList<BookInfo>) databaseUtil.setBookInfos(list);
mDataSet = (ArrayList<BookInfo>) BookDatabaseUtil.getInstance(getContext()).setBookInfos(list);
}

@Override
Expand Down Expand Up @@ -244,7 +242,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onDestroy() {
databaseUtil.destory();
BookDatabaseUtil.destory();
super.onDestroy();
}

Expand All @@ -256,24 +254,25 @@ private void addItem(String name, int colorIndex, String time) {
bookInfo.setBookName(name);
bookInfo.setBookColor(colorIndex);
bookInfo.setBookAlarmTime(time);

if (!getIsTrial()) {
//服务器
bookInfo.save(getContext(), new SaveListener() {
@Override
public void onSuccess() {
Log.i(TAG, "上传服务器成功");
Log.i(TAG, bookInfo.getObjectId());
databaseUtil.insertBookInfo(bookInfo);
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "上传服务器失败: " + s);
databaseUtil.insertBookInfo(bookInfo, bookInfo, true); //无效invalid ObjectId
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfo, true); //无效invalid ObjectId
}
});
} else {
databaseUtil.insertBookInfo(bookInfo, bookInfo, true); //无效invalid ObjectId
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfo, true); //无效invalid ObjectId
}

mDataSet.add(0, bookInfo);
Expand All @@ -283,23 +282,24 @@ public void onFailure(int i, String s) {
//删除
private void deleteItem(int position, boolean setAnimator) {
final BookInfo bookInfo = mDataSet.get(position);

if (!getIsTrial()) {
//服务器
bookInfo.delete(getContext(), bookInfo.getObjectId(), new DeleteListener() {
@Override
public void onSuccess() {
Log.i(TAG, "删除成功");
databaseUtil.deleteBookInfo(bookInfo);
BookDatabaseUtil.getInstance(getContext()).deleteBookInfo(bookInfo);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "删除失败: " + s);
databaseUtil.deleteBookInfo(bookInfo, true);
BookDatabaseUtil.getInstance(getContext()).deleteBookInfo(bookInfo, true);
}
});
} else {
databaseUtil.deleteBookInfo(bookInfo, true);
BookDatabaseUtil.getInstance(getContext()).deleteBookInfo(bookInfo, true);
}

int size = mDataSet.size();
Expand All @@ -324,6 +324,7 @@ private void updateItem(final int position, String name, int colorIndex, String
bookInfo.setBookAlarmTime(time);

Log.i(TAG, "test id = " + bookInfo.getObjectId());

if (bookInfo.getObjectId() == null) {
Log.i(TAG, "into : test id = null");
if (!getIsTrial()) {
Expand All @@ -332,25 +333,25 @@ private void updateItem(final int position, String name, int colorIndex, String
public void onSuccess() {
Log.i(TAG, "上传服务器成功");
Log.i(TAG, bookInfo.getObjectId());
databaseUtil.insertBookInfo(bookInfo, bookInfoOld, true);
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfoOld, true);
}

@Override
public void onFailure(int i, String s) {
Log.i(TAG, "上传服务器失败: " + s);
databaseUtil.insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
}
});
} else {
databaseUtil.insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
}
} else {
//服务器
bookInfo.update(getContext(), bookInfo.getObjectId(), new UpdateListener() {
@Override
public void onSuccess() {
Log.i(TAG, "更新服务器成功");
databaseUtil.insertBookInfo(bookInfo);
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo);
}

@Override
Expand All @@ -359,7 +360,7 @@ public void onFailure(int i, String s) {
switch (i) {
case 9010:
case 9016:
databaseUtil.insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
BookDatabaseUtil.getInstance(getContext()).insertBookInfo(bookInfo, bookInfoOld, true); //无效invalid ObjectId
break;
}
}
Expand Down
Loading

0 comments on commit 7ab5379

Please sign in to comment.