Skip to content

Commit

Permalink
1.0.1:add search to chara list; minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wxwang committed Jan 12, 2018
1 parent 0efb363 commit b009c96
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 90 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.lazyeraser.imas.derehelper"
minSdkVersion 17
targetSdkVersion 25
versionCode 10
versionName "1.0.0"
versionCode 11
versionName "1.0.1"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/com/lazyeraser/imas/cgss/utils/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,8 @@ public List<String> getAll(String tableName, String column){
}
}

public boolean insertData(String tableName, ContentValues contentValues) {
try {
getWritableDatabase().replace(tableName, null, contentValues);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
public void insertData(String tableName, ContentValues contentValues) {
getWritableDatabase().replace(tableName, null, contentValues);
}

public boolean updateData(String tableName, ContentValues contentValues, String where, String[] value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class UpdateManager {
private int curVersionCode;
private int newVersionCode;
private String updateInfo;
private String md5;
private UpdateCallback callback;
private Context mContext;

Expand Down Expand Up @@ -105,7 +106,7 @@ private void getCurVersion() {
//检查更新
private OnUpdateCheckedListener onUpdateCheckedListener;
public interface OnUpdateCheckedListener{
void onChecked();
void onChecked(boolean haveUpdate);
}
public void checkUpdate(boolean needNoUpdateHint){
checkUpdate(needNoUpdateHint, null);
Expand Down Expand Up @@ -137,6 +138,7 @@ public void onResponse(Call call, Response response) throws IOException {
try {
newVersionCode = Integer.parseInt(obj.getString("verCode"));
newVersion = obj.getString("verName");
md5 = obj.getString("md5");
String verInfoKey;
if (SStaticR.isCn){
verInfoKey = "verInfo";
Expand Down Expand Up @@ -195,7 +197,13 @@ public void run() {

if(ApkFile.exists())
{
ApkFile.delete();
String existMd5 = Utils.getFileMD5(ApkFile);
if (!TextUtils.isEmpty(md5) && !TextUtils.isEmpty(existMd5) && existMd5.equals(md5)){
updateHandler.sendEmptyMessage(UPDATE_DOWNLOAD_COMPLETED);
return;
}else {
ApkFile.delete();
}
}


Expand Down Expand Up @@ -270,12 +278,10 @@ public void cancelDownload()
});

public interface UpdateCallback {
public void checkUpdateCompleted(Boolean hasUpdate,
CharSequence updateInfo);

public void downloadProgressChanged(float progress, int maxlength);
public void downloadCanceled();
public void downloadCompleted(Boolean sucess, CharSequence errorMsg);
void checkUpdateCompleted(Boolean hasUpdate, CharSequence updateInfo);
void downloadProgressChanged(float progress, int maxlength);
void downloadCanceled();
void downloadCompleted(Boolean sucess, CharSequence errorMsg);
}

private UpdateManager.UpdateCallback defaultCallBack = new UpdateManager.UpdateCallback()
Expand Down Expand Up @@ -318,7 +324,7 @@ public void downloadCanceled()
public void checkUpdateCompleted(Boolean hasUpdate,
CharSequence updateInfo) {
if (onUpdateCheckedListener != null){
onUpdateCheckedListener.onChecked();
onUpdateCheckedListener.onChecked(hasUpdate);
}
if (hasUpdate) {
new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE)
Expand Down
38 changes: 34 additions & 4 deletions app/src/main/java/com/lazyeraser/imas/cgss/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.AnyRes;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
Expand All @@ -20,16 +18,18 @@
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lazyeraser.imas.derehelper.R;
import com.lazyeraser.imas.main.SStaticR;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -204,4 +204,34 @@ public static boolean saveImageToGallery(Context context, Bitmap bmp) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
return true;
}

public static String getFileMD5(File f) {
BigInteger bi = null;
try {
byte[] buffer = new byte[8192];
int len = 0;
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(f);
while ((len = fis.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
fis.close();
byte[] b = md.digest();
bi = new BigInteger(1, b);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bi == null ? "" : bi.toString(16);
}

public static String getFileMD5(String path) {
return getFileMD5(new File(path));
}

public static boolean igCaseContain(String s1, String s2){
return s2.toLowerCase().contains(s1.toLowerCase());
}

}
28 changes: 23 additions & 5 deletions app/src/main/java/com/lazyeraser/imas/cgss/view/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ public class MainActivity extends BaseActivity {

private BroadcastReceiver broadcastReceiver;

private MainViewModel mainViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setActionBarTitle(R.string.card_list);
initActionBar(ACTIONBAR_TYPE_CUSTOM, R.drawable.umi_ic_menu_white, R.drawable.ic_filter_list_white_24dp);
actionBarStartAction(menu -> umi.moveDrawer(drawerLayout, Gravity.START));

MainViewModel mainViewModel = new MainViewModel(this);
mainViewModel = new MainViewModel(this);
setBinding(R.layout.activity_main).setVariable(com.lazyeraser.imas.derehelper.BR.viewModel, mainViewModel);
updateManager = new UpdateManager(mContext);
drawerLayout = (DrawerLayout)getBView(R.id.drawerLayout);
Expand Down Expand Up @@ -112,7 +114,7 @@ protected void onCreate(Bundle savedInstanceState) {
return true;
});

initDialog(mainViewModel); // 初始化数据更新对话框
initDialog(); // 初始化数据更新对话框

fragments = new HashMap<>();
cardListFrag = new CardListFrag();
Expand All @@ -128,6 +130,8 @@ protected void onCreate(Bundle savedInstanceState) {
switchFrag(cardListFrag);
if (umi.getSP(SharedHelper.KEY_AUTO_APP)){
checkUpdate(false);
}else if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){
mainViewModel.checkDataUpdate();
}
// 监听语言设置改变
if (broadcastReceiver == null){
Expand Down Expand Up @@ -174,7 +178,14 @@ private void checkUpdate(boolean hint){
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else {
updateManager.checkUpdate(hint, () -> umi.dismissLoading());
updateManager.checkUpdate(hint, b -> {
umi.dismissLoading();
if(!b){
if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){
mainViewModel.checkDataUpdate();
}
}
});
}
}

Expand All @@ -183,14 +194,21 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
updateManager.checkUpdate(needUpdateHint, () -> umi.dismissLoading());
updateManager.checkUpdate(needUpdateHint, b -> {
umi.dismissLoading();
if(!b){
if (umi.getSP(SharedHelper.KEY_AUTO_DATA)){
mainViewModel.checkDataUpdate();
}
}
});
}else {
umi.makeToast(R.string.permission_denied_hint);
}
}
}

private void initDialog(MainViewModel mainViewModel){
private void initDialog(){
upToDateDialog = new SweetAlertDialog(mContext, SweetAlertDialog.SUCCESS_TYPE)
.setTitleText(getString(R.string.check_hint))
.setConfirmClickListener(Dialog::dismiss);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ private void onTranSwitchChanged(boolean check){
ContentValues contentValues = new ContentValues();
contentValues.put("origin", key);
contentValues.put("translate", stringStringMap.get(key));
subscriber.onNext(DBHelper.with(mContext).insertData(DBHelper.TABLE_NAME_Translation, contentValues));
DBHelper.with(mContext).insertData(DBHelper.TABLE_NAME_Translation, contentValues);
}
subscriber.onNext(true);
subscriber.onCompleted();
}).subscribeOn(Schedulers.io())
.compose(((ActivityLifecycleProvider) mContext).bindToLifecycle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.reflect.TypeToken;
Expand All @@ -15,6 +16,7 @@
import com.lazyeraser.imas.cgss.entity.Chara;
import com.lazyeraser.imas.cgss.utils.DBHelper;
import com.lazyeraser.imas.cgss.utils.JsonUtils;
import com.lazyeraser.imas.cgss.utils.Utils;
import com.lazyeraser.imas.cgss.view.CharaDetailActivity;
import com.lazyeraser.imas.cgss.view.MainActivity;
import com.lazyeraser.imas.cgss.view.fragments.CharaListFrag;
Expand All @@ -39,7 +41,7 @@
* Created by lazyeraser on 2017/9/19.
*/

public class CharaListViewModel extends BaseViewModel{
public class CharaListViewModel extends BaseViewModel {

public final ObservableList<CharaViewModel> itemViewModel = new ObservableArrayList<>();
public final ItemView itemView = ItemView.of(com.lazyeraser.imas.derehelper.BR.viewModel, R.layout.item_list_chara);
Expand All @@ -50,7 +52,7 @@ public class CharaListViewModel extends BaseViewModel{
public final ReplyCommand<Pair<Integer, View>> onListItemClickCommand = new ReplyCommand<>(pair -> {
ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, pair.second.findViewById(R.id.chara_icon), "chara_icon");
Bundle bundle = transitionActivityOptions.toBundle();
if (bundle == null){
if (bundle == null) {
bundle = new Bundle();
}
bundle.putString("theChara", JsonUtils.getJsonFromBean(itemViewModel.get(pair.first).chara.get()));
Expand All @@ -61,10 +63,20 @@ public class CharaListViewModel extends BaseViewModel{
});

private List<String> typeFilter = new ArrayList<>();

public final ReplyCommand<List<String>> onTypeSelCommand = new ReplyCommand<>(strings -> typeFilter = strings);

public final ReplyCommand filterCardsCommand = new ReplyCommand(() ->{

private String search = "";

public final ReplyCommand<List<String>> onTypeSelCommand = new ReplyCommand<>(strings -> {
typeFilter = strings;
filterChara();
});

public final ReplyCommand<String> onSearchCommand = new ReplyCommand<>(string -> {
search = string;
filterChara();
});

public final ReplyCommand filterCardsCommand = new ReplyCommand(() -> {
filterChara();
Messenger.getDefault().sendNoMsg(CharaListFrag.TOKEN_CLOSE_FILTER);

Expand All @@ -74,23 +86,23 @@ public class CharaListViewModel extends BaseViewModel{
initFilter();
Messenger.getDefault().sendNoMsg(CharaListFrag.TOKEN_RESET_FILTER);
});

public CharaListViewModel(BaseActivity mContext) {
super(mContext);
initFilter();
loadData();
Messenger.getDefault().register(mContext, MainActivity.TOKEN_DATA_UPDATED, this::loadData);
}

private void loadData(){
private void loadData() {
itemViewModel.clear();
umi.showLoading();
Observable.just(DBHelper.with(mContext)
.getAll(DBHelper.TABLE_NAME_Chara_Detail, "json"))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(js -> Observable.create((Observable.OnSubscribe<Map<Chara, CharaViewModel>>) subscriber -> {
if (js.size() == 0){
if (js.size() == 0) {
subscriber.onNext(new HashMap<Chara, CharaViewModel>());
subscriber.onCompleted();
}
Expand All @@ -102,7 +114,8 @@ private void loadData(){
}
charaJsonBuilder.delete(charaJsonBuilder.length() - 1, charaJsonBuilder.length());
charaJsonBuilder.append("]");
List<Chara> charas = JsonUtils.getArrayFromJson(charaJsonBuilder.toString(), new TypeToken<List<Chara>>(){});
List<Chara> charas = JsonUtils.getArrayFromJson(charaJsonBuilder.toString(), new TypeToken<List<Chara>>() {
});
Map<Chara, CharaViewModel> map = new HashMap<>();
for (Chara chara : charas) {
map.put(chara, new CharaViewModel(mContext, chara));
Expand All @@ -115,23 +128,27 @@ private void loadData(){
}), ExceptionHandler::handleException);
}

private void initFilter(){
private void initFilter() {
typeFilter.clear();
typeFilter.addAll(SStaticR.typeMap.values());
}

private void filterChara(){


private boolean checkSearch(Chara chara) {
return TextUtils.isEmpty(search) || Utils.igCaseContain(search, chara.getConventional()) || Utils.igCaseContain(search, chara.getVoice()) || Utils.igCaseContain(search, chara.getName()) || Utils.igCaseContain(search, chara.getName_kana());
}

private void filterChara() {

for (Chara chara : charaDataList.keySet()) {
CharaViewModel vm = charaDataList.get(chara);
if (typeFilter.contains(chara.getType().toUpperCase())) {
if (typeFilter.contains(chara.getType().toUpperCase()) && checkSearch(chara)) {
// 符合条件 如不在当前显示的列表中则加入
if (!itemViewModel.contains(vm)){
if (!itemViewModel.contains(vm)) {
itemViewModel.add(vm);
}
}else {
} else {
// 不符合 如果存在则remove
if (itemViewModel.contains(vm)){
if (itemViewModel.contains(vm)) {
itemViewModel.remove(vm);
}
}
Expand All @@ -141,9 +158,9 @@ private void filterChara(){
Chara charaA = a.chara.get();
Chara charaB = b.chara.get();
boolean desc = false;
if (charaB.getChara_id() < charaA.getChara_id()){
if (charaB.getChara_id() < charaA.getChara_id()) {
return desc ? -1 : 1;
}else {
} else {
return desc ? 1 : -1;
}
});
Expand Down
Loading

0 comments on commit b009c96

Please sign in to comment.