Skip to content

Commit

Permalink
Add remove all history
Browse files Browse the repository at this point in the history
  • Loading branch information
lmovse committed Oct 5, 2018
1 parent 9577ea6 commit ce04744
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 122 deletions.
7 changes: 6 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ android {
defaultConfig {
resConfigs "zh"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
*/

public class AppDbRepository implements AppDbSource.TranslateDbSource, AppDbSource.HistoryDbSource, AppDbSource.CollectDbSource {

private final LocalDbSource mLocalDbSource;
public static AppDbRepository instance = null;

private static AppDbRepository instance = null;

private AppDbRepository(Context context) {
mLocalDbSource = LocalDbSource.getInstance(context);
Expand Down Expand Up @@ -82,15 +84,20 @@ public History getHistory(String original) {
}

@Override
public ArrayList<History> getHistorys() {
return mLocalDbSource.getHistorys();
public ArrayList<History> getHistories() {
return mLocalDbSource.getHistories();
}

@Override
public void deleteHistory(String original) {
mLocalDbSource.deleteHistory(original);
}

@Override
public void deleteAllHistory() {
mLocalDbSource.deleteAllHistory();
}

@Override
public Collect getCollect(String original) {
return mLocalDbSource.getCollect(original);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ interface HistoryDbSource {

History getHistory(String original);

ArrayList<History> getHistorys();
ArrayList<History> getHistories();

void deleteHistory(String original);

void deleteAllHistory();
}

interface CollectDbSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LocalDbOpenHelper extends SQLiteOpenHelper {
+ AppDbSchema.HistoryTable._ID + " integer" + " primary key autoincrement,"
+ AppDbSchema.HistoryTable.ORIGINAL + " text,"
+ AppDbSchema.HistoryTable.RESULT + " text, "
+ AppDbSchema.HistoryTable.COLLECTED + " tinyint unsigned default 0" + ")";
+ AppDbSchema.HistoryTable.COLLECTED + "tinyint unsigned default 0" + ")";

private static final String CREATE_COLLECT = "CREATE TABLE "
+ AppDbSchema.CollectTable.TABLE_NAME + " ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@
import android.database.sqlite.SQLiteDatabase;

import com.example.jooff.shuyi.constant.TransSource;
import com.example.jooff.shuyi.data.AppDbSchema;
import com.example.jooff.shuyi.data.AppDbSource;
import com.example.jooff.shuyi.data.entity.Collect;
import com.example.jooff.shuyi.data.entity.History;
import com.example.jooff.shuyi.data.entity.Translate;

import java.util.ArrayList;

import static com.example.jooff.shuyi.data.AppDbSchema.CollectTable;
import static com.example.jooff.shuyi.data.AppDbSchema.HistoryTable;

/**
* Created by Jooff on 2016/8/14.
* Tomorrow is a nice day
*/

public class LocalDbSource implements AppDbSource.HistoryDbSource, AppDbSource.CollectDbSource, AppDbSource.TranslateDbSource {
private static LocalDbSource sLocalDbSource = null;

private volatile static LocalDbSource sLocalDbSource = null;

private SQLiteDatabase db;

/**
* 构造方法私有化,创建单例类
*/
public static final String DELETE_ALL_HISTORY_SQL = "DELETE FROM " + HistoryTable.TABLE_NAME;

private LocalDbSource(Context context) {
LocalDbOpenHelper mHelper = new LocalDbOpenHelper(context);
db = mHelper.getWritableDatabase();
}

/**
* 获取单例的实例,使用双重校验锁,保证线程安全与速率
*/
public static LocalDbSource getInstance(Context context) {
if (sLocalDbSource== null) {
if (sLocalDbSource == null) {
synchronized (LocalDbSource.class) {
if (sLocalDbSource == null) {
sLocalDbSource = new LocalDbSource(context);
Expand Down Expand Up @@ -72,46 +72,46 @@ public void getTrans(int transFrom, String original, AppDbSource.TranslateCallba
public void saveHistory(History history) {
if (getHistory(history.getOriginal()) == null) {
ContentValues values = new ContentValues();
values.put(AppDbSchema.HistoryTable.ORIGINAL, history.getOriginal());
values.put(AppDbSchema.HistoryTable.RESULT, history.getResult());
db.insert(AppDbSchema.HistoryTable.TABLE_NAME, null, values);
values.put(HistoryTable.ORIGINAL, history.getOriginal());
values.put(HistoryTable.RESULT, history.getResult());
db.insert(HistoryTable.TABLE_NAME, null, values);
} else {
ContentValues values = new ContentValues();
String updateClause = AppDbSchema.HistoryTable.ORIGINAL + " == ? ";
String updateClause = HistoryTable.ORIGINAL + " == ? ";
String[] updateArgs = {history.getOriginal()};
values.put(AppDbSchema.HistoryTable.RESULT, history.getResult());
db.update(AppDbSchema.HistoryTable.TABLE_NAME, values, updateClause, updateArgs);
values.put(HistoryTable.RESULT, history.getResult());
db.update(HistoryTable.TABLE_NAME, values, updateClause, updateArgs);
}
}

@Override
public void collectHistory(History history) {
ContentValues values = new ContentValues();
values.put(AppDbSchema.CollectTable.ORIGINAL, history.getOriginal());
values.put(AppDbSchema.CollectTable.RESULT, history.getResult());
db.insert(AppDbSchema.CollectTable.TABLE_NAME, null, values);
values.put(CollectTable.ORIGINAL, history.getOriginal());
values.put(CollectTable.RESULT, history.getResult());
db.insert(CollectTable.TABLE_NAME, null, values);
Object[] args = {1, history.getOriginal()};
db.execSQL("UPDATE " + AppDbSchema.HistoryTable.TABLE_NAME + " SET collected = ? where original = ?", args);
db.execSQL("UPDATE " + HistoryTable.TABLE_NAME + " SET collected = ? where original = ?", args);
}

@Override
public void cancelCollect(String original) {
deleteCollect(original);
Object[] args = {0, original};
db.execSQL("UPDATE " + AppDbSchema.HistoryTable.TABLE_NAME + " SET collected = ? where original = ?", args);
db.execSQL("UPDATE " + HistoryTable.TABLE_NAME + " SET collected = ? where original = ?", args);
}

@Override
public History getHistory(String original) {
History item = null;
String queryLogic = AppDbSchema.HistoryTable.ORIGINAL + " == ? ";
String queryLogic = HistoryTable.ORIGINAL + " == ? ";
String[] queryArgs = {original};
Cursor cursor = db.query(AppDbSchema.HistoryTable.TABLE_NAME, null, queryLogic, queryArgs, null, null, null);
Cursor cursor = db.query(HistoryTable.TABLE_NAME, null, queryLogic, queryArgs, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String textOriginal = cursor.getString(cursor.getColumnIndex(AppDbSchema.HistoryTable.ORIGINAL));
String textResult = cursor.getString(cursor.getColumnIndex(AppDbSchema.HistoryTable.RESULT));
int collected = cursor.getInt(cursor.getColumnIndex(AppDbSchema.HistoryTable.COLLECTED));
String textOriginal = cursor.getString(cursor.getColumnIndex(HistoryTable.ORIGINAL));
String textResult = cursor.getString(cursor.getColumnIndex(HistoryTable.RESULT));
int collected = cursor.getInt(cursor.getColumnIndex(HistoryTable.COLLECTED));
item = new History(textOriginal, textResult, collected);
}
if (cursor != null) {
Expand All @@ -121,14 +121,14 @@ public History getHistory(String original) {
}

@Override
public ArrayList<History> getHistorys() {
public ArrayList<History> getHistories() {
ArrayList<History> items = new ArrayList<>();
Cursor cursor = db.query(AppDbSchema.HistoryTable.TABLE_NAME, null, null, null, null, null, null);
Cursor cursor = db.query(HistoryTable.TABLE_NAME, null, null, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String hisOrigin = cursor.getString(cursor.getColumnIndex(AppDbSchema.HistoryTable.ORIGINAL));
String hisResult = cursor.getString(cursor.getColumnIndex(AppDbSchema.HistoryTable.RESULT));
int collected = cursor.getInt(cursor.getColumnIndex(AppDbSchema.HistoryTable.COLLECTED));
String hisOrigin = cursor.getString(cursor.getColumnIndex(HistoryTable.ORIGINAL));
String hisResult = cursor.getString(cursor.getColumnIndex(HistoryTable.RESULT));
int collected = cursor.getInt(cursor.getColumnIndex(HistoryTable.COLLECTED));
History item = new History(hisOrigin, hisResult, collected);
items.add(0, item);
}
Expand All @@ -141,23 +141,28 @@ public ArrayList<History> getHistorys() {

@Override
public void deleteHistory(String original) {
String deleteClause = AppDbSchema.HistoryTable.ORIGINAL + " == ? ";
String deleteClause = HistoryTable.ORIGINAL + " == ? ";
String[] deleteArgs = {original};
db.delete(AppDbSchema.HistoryTable.TABLE_NAME, deleteClause, deleteArgs);
db.delete(HistoryTable.TABLE_NAME, deleteClause, deleteArgs);
}

@Override
public void deleteAllHistory() {
db.execSQL(DELETE_ALL_HISTORY_SQL);
}

//--------------------------------------- Collect Source -------------------------------------------

@Override
public Collect getCollect(String original) {
Collect item = null;
String queryLogic = AppDbSchema.CollectTable.ORIGINAL + " == ? ";
String queryLogic = CollectTable.ORIGINAL + " == ? ";
String[] queryArgs = {original};
Cursor cursor = db.query(AppDbSchema.CollectTable.TABLE_NAME, null, queryLogic, queryArgs, null, null, null);
Cursor cursor = db.query(CollectTable.TABLE_NAME, null, queryLogic, queryArgs, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String textOriginal = cursor.getString(cursor.getColumnIndex(AppDbSchema.CollectTable.ORIGINAL));
String textResult = cursor.getString(cursor.getColumnIndex(AppDbSchema.CollectTable.RESULT));
String textOriginal = cursor.getString(cursor.getColumnIndex(CollectTable.ORIGINAL));
String textResult = cursor.getString(cursor.getColumnIndex(CollectTable.RESULT));
item = new Collect(textOriginal, textResult);
}
if (cursor != null) {
Expand All @@ -169,11 +174,11 @@ public Collect getCollect(String original) {
@Override
public ArrayList<Collect> getCollects() {
ArrayList<Collect> items = new ArrayList<>();
Cursor cursor = db.query(AppDbSchema.CollectTable.TABLE_NAME, null, null, null, null, null, null);
Cursor cursor = db.query(CollectTable.TABLE_NAME, null, null, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String hisOrigin = cursor.getString(cursor.getColumnIndex(AppDbSchema.CollectTable.ORIGINAL));
String hisResult = cursor.getString(cursor.getColumnIndex(AppDbSchema.CollectTable.RESULT));
String hisOrigin = cursor.getString(cursor.getColumnIndex(CollectTable.ORIGINAL));
String hisResult = cursor.getString(cursor.getColumnIndex(CollectTable.RESULT));
Collect item = new Collect(hisOrigin, hisResult);
items.add(0, item);
}
Expand All @@ -186,9 +191,9 @@ public ArrayList<Collect> getCollects() {

@Override
public void deleteCollect(String original) {
String deleteClause = AppDbSchema.CollectTable.ORIGINAL + " == ? ";
String deleteClause = CollectTable.ORIGINAL + " == ? ";
String[] deleteArgs = {original};
db.delete(AppDbSchema.CollectTable.TABLE_NAME, deleteClause, deleteArgs);
db.delete(CollectTable.TABLE_NAME, deleteClause, deleteArgs);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.example.jooff.shuyi.data.remote;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.jooff.shuyi.common.MyApp;
import com.example.jooff.shuyi.constant.TransSource;
import com.example.jooff.shuyi.data.AppDbSource;
import com.example.jooff.shuyi.util.EntityFormat;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Created by Jooff on 2017/2/1.
Expand All @@ -32,33 +29,25 @@ public static RemoteJsonSource getInstance() {

@Override
public void getTrans(final int source, final String original, final AppDbSource.TranslateCallback callback) {
JsonObjectRequest request = new JsonObjectRequest(original, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject s) {
try {
switch (source) {
case TransSource.FROM_BAUDU:
EntityFormat.getBeanFromBaidu(s, callback);
break;
case TransSource.FROM_YIYUN:
EntityFormat.getBeanFromYiyun(s, callback);
break;
case TransSource.FROM_SHANBEI:
EntityFormat.getBeanFromShanBei(s, callback);
break;
case TransSource.FROM_YOUDAO:
EntityFormat.getBeanFromYoudao(s, callback);
}
} catch (JSONException e) {
e.printStackTrace();
JsonObjectRequest request = new JsonObjectRequest(original, s -> {
try {
switch (source) {
case TransSource.FROM_BAUDU:
EntityFormat.getBeanFromBaidu(s, callback);
break;
case TransSource.FROM_YIYUN:
EntityFormat.getBeanFromYiyun(s, callback);
break;
case TransSource.FROM_SHANBEI:
EntityFormat.getBeanFromShanBei(s, callback);
break;
case TransSource.FROM_YOUDAO:
EntityFormat.getBeanFromYoudao(s, callback);
}
} catch (JSONException e) {
e.printStackTrace();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
callback.onError(1);
}
});
}, volleyError -> callback.onError(1));
MyApp.sRequestQueue.add(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public HistoryPresenter(AppDbSource.HistoryDbSource historyDbSource, HistoryCont

@Override
public void loadData() {
ArrayList<History> historys = mHistoryDbSource.getHistorys();
ArrayList<History> historys = mHistoryDbSource.getHistories();
if (!historys.isEmpty()) {
mHistoryView.showHistory(historys);
}
Expand All @@ -34,27 +34,27 @@ public void initTheme() {}

@Override
public void collectHistory(int position) {
mHistoryDbSource.collectHistory(mHistoryDbSource.getHistorys().get(position));
mHistoryDbSource.collectHistory(mHistoryDbSource.getHistories().get(position));
}

@Override
public void unCollectHistory(int position) {
String original = mHistoryDbSource.getHistorys().get(position).getOriginal();
String original = mHistoryDbSource.getHistories().get(position).getOriginal();
Log.d("取消收藏项", "unCollectHistory: " + original);
mHistoryDbSource.cancelCollect(original);
}

@Override
public void deleteHistoryItem(int position) {
String original = mHistoryDbSource.getHistorys().get(position).getOriginal();
String original = mHistoryDbSource.getHistories().get(position).getOriginal();
Log.d("删除项", "deleteHistory: " + original);
mHistoryDbSource.deleteHistory(original);
mHistoryView.showHistoryDeleted();
}

@Override
public void beginTranslate(int position) {
History item = mHistoryDbSource.getHistorys().get(position);
History item = mHistoryDbSource.getHistories().get(position);
String original = item.getOriginal();
mHistoryView.showTranslate(original);
}
Expand Down
Loading

0 comments on commit ce04744

Please sign in to comment.