Skip to content

Commit

Permalink
Merge pull request maoruibin#53 from 70kg/develop
Browse files Browse the repository at this point in the history
添加单词完成自动提示
  • Loading branch information
maoruibin committed Jun 3, 2017
2 parents 9be9c2d 85f3c0f commit dddb469
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 66 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 191,10 @@ dependencies {
exclude module: 'recyclerview-v7'
}

compile 'net.grandcentrix.tray:tray:0.11.0'
compile "com.readystatesoftware.sqliteasset:sqliteassethelper:${libs.sqliteassethelper}"

compile 'com.facebook.stetho:stetho:1.4.2'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
compile "net.grandcentrix.tray:tray:${libs.tray}"

compile "com.facebook.stetho:stetho:${libs.stetho}"
compile "com.facebook.stetho:stetho-okhttp3:${libs.stetho_okhttp3}"
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 77,13 @@ public String wrapAmPhonetic() {

@Override
public String wrapEnMp3() {
if(getBasic() == null)return "";
return getBasic().getUkSpeech();
}

@Override
public String wrapAmMp3() {
if(getBasic() == null)return "";
return getBasic().getUsSpeech();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 66,7 @@ public class BasePresenter<V extends IBaseView> {

protected FileManager mFileManager = new FileManager();

public BasePresenter(LiteOrm liteOrm, WarpAipService apiService,Context context) {
public BasePresenter(LiteOrm liteOrm, WarpAipService apiService, Context context) {
mLiteOrm = liteOrm;
mWarpApiService = apiService;
mContext = context;
Expand All @@ -79,21 79,23 @@ public BasePresenter(LiteOrm liteOrm, WarpAipService apiService, SingleRequestSe
mContext = context;
}

public void onCreate(){}
public void onCreate() {
}

/**
* attach IBaseView to Presenter
*
* @param view view
*/
public void attachView(V view){
public void attachView(V view) {
this.mView = view;
}

public void onDestroy(){
public void onDestroy() {
this.mView = null;
}

protected Context getContext(){
protected Context getContext() {
return mContext;
}

Expand All @@ -106,81 108,75 @@ protected Context getContext(){
public Result isFavorite(String word) {
QueryBuilder queryBuilder = new QueryBuilder(Result.class);
queryBuilder = queryBuilder.whereEquals("query ", word);
List<Result>list = mLiteOrm.query(queryBuilder);
if(list.isEmpty()){
List<Result> list = mLiteOrm.query(queryBuilder);
if (list.isEmpty()) {
return null;
}
return list.get(0);
}

protected long insertResultToDb(Result entity){
protected long insertResultToDb(Result entity) {
return mLiteOrm.insert(entity);
}

protected int deleteResultFromDb(Result entity){
protected int deleteResultFromDb(Result entity) {
return mLiteOrm.delete(entity);
}

public void playSound(String fileName,String mp3Url) {
Observable.just(mp3Url)
.subscribe(new Action1<String>() {
@Override
public void call(String entity) {
File cacheFile = mFileManager.getCacheFileByUrl(getContext(), fileName);
if (cacheFile != null && cacheFile.exists()) {
playSound(cacheFile);
return;
}
Call<ResponseBody> call = mSingleRequestService.downloadSoundFile(mp3Url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
cacheAndPlaySound(getContext(), fileName, response.body().bytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Logger.e(t.getMessage());
t.printStackTrace();
}
});
public void playSound(String fileName, String mp3Url) {
File cacheFile = mFileManager.getCacheFileByUrl(getContext(), fileName);
if (cacheFile != null && cacheFile.exists()) {
playSound(cacheFile);
return;
}
Call<ResponseBody> call = mSingleRequestService.downloadSoundFile(mp3Url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
cacheAndPlaySound(getContext(), fileName, response.body().bytes());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Logger.e(t.getMessage());
t.printStackTrace();
}
});
}

public void startSoundAnim(View view){
addScaleAnim(view,1000,null);
public void startSoundAnim(View view) {
addScaleAnim(view, 1000, null);
}

public void startFavoriteAnim(View view,AnimationEndListener listener){
addScaleAnim(view,500,listener);
public void startFavoriteAnim(View view, AnimationEndListener listener) {
addScaleAnim(view, 500, listener);
}

private void addScaleAnim(View view, long duration, AnimationEndListener listener) {
ObjectAnimator animY = ObjectAnimator.ofFloat(view, "scaleY", 1f,0.5f, 1f, 1.2f,1f);
ObjectAnimator animX = ObjectAnimator.ofFloat(view, "scaleX", 1f,0.5f, 1f, 1.2f,1f);
ObjectAnimator animY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.5f, 1f, 1.2f, 1f);
ObjectAnimator animX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.5f, 1f, 1.2f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animX,animY);
animatorSet.playTogether(animX, animY);
animatorSet.setDuration(duration);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if(listener != null){
if (listener != null) {
listener.onAnimationEnd(animation);
}
}
});
animatorSet.start();
}

public interface AnimationEndListener{
public interface AnimationEndListener {
void onAnimationEnd(Animator animation);
}

Expand All @@ -197,9 193,9 @@ public void call(File file) {
}

private void playSound(File file) {
if(file == null)return;
if (file == null) return;
Uri myUri = Uri.fromFile(file);
Logger.i("播放 " file.getAbsolutePath());
Logger.i("播放 " file.getAbsolutePath());
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Expand All @@ -212,7 208,6 @@ private void playSound(File file) {
}



private Callable<File> cacheFileObservable(Context context, String fileName, byte[] data) {
return new Callable<File>() {
@Override
Expand All @@ -224,11 219,12 @@ public File call() throws Exception {

/**
* make a operation observable
*
* @param func
* @param <T>
* @return the Observable
*/
protected <T> Observable<T> makeObservable(final Callable<T> func) {
protected <T> Observable<T> makeObservable(final Callable<T> func) {
return Observable.create(new Observable.OnSubscribe<T>() {
@Override
public void call(Subscriber<? super T> subscriber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 51,6 @@
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static android.support.v7.widget.StaggeredGridLayoutManager.TAG;

/**
* Created by GuDong on 2/28/16 17:02.
Expand All @@ -60,6 59,7 @@
public class BookPresenter extends BasePresenter<IBookView> {
private static final String KEY_TIP_OF_RECITE_OPEN = "TIP_OF_RECITE_OPEN";
private static final String KEY_RECITE_MODE_SWITCH = "RECITE_MODE_SWITCH";
private static final String TAG = "BOOK_PRESENTER";
@Inject
public BookPresenter(LiteOrm liteOrm, WarpAipService apiService, SingleRequestService singleRequestService, Context context) {
super(liteOrm, apiService, singleRequestService,context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 55,7 @@
import name.gudong.translate.mvp.views.IMainView;
import name.gudong.translate.ui.activitys.MainActivity;
import name.gudong.translate.util.DialogUtil;
import name.gudong.translate.util.LocalDicHelper;
import name.gudong.translate.util.SpUtils;
import name.gudong.translate.util.Utils;
import rx.Observable;
Expand Down Expand Up @@ -91,6 92,21 @@ public void checkIntentFromClickTipView(Intent intent) {
}
}

public void analysisLocalDic() {
makeObservable(new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
return LocalDicHelper.getLocalDic(mContext);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<String>>() {
@Override
public void call(List<String> strings) {
mView.attachLocalDic(strings);
}
});
}
public boolean hasExtraResult(Intent intent) {
return intent.hasExtra(KEY_RESULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,8 @@

package name.gudong.translate.mvp.views;

import java.util.List;

import name.gudong.translate.mvp.model.entity.dayline.IDayLine;
import name.gudong.translate.mvp.model.entity.translate.Result;
import name.gudong.translate.mvp.model.type.ETranslateFrom;
Expand Down Expand Up @@ -58,4 60,7 @@ public interface IMainView extends IBaseView {
void initWithNotFavorite();

void fillDayline(IDayLine entity);

void attachLocalDic(List<String> dic);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 41,8 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
Expand All @@ -53,6 53,7 @@
import com.umeng.analytics.MobclickAgent;

import java.net.UnknownHostException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -79,7 80,7 @@
public class MainActivity extends BaseActivity<MainPresenter> implements IMainView {
private static final String TAG = "MainActivity";
@BindView(android.R.id.input)
EditText mInput;
AutoCompleteTextView mInput;
@BindView(R.id.list_result)
LinearLayout mList;
@BindView(R.id.sp_translate_way)
Expand Down Expand Up @@ -132,7 133,7 @@ protected void onCreate(Bundle savedInstanceState) {

private void setUpDayline(boolean isOpenDayLine) {
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet_view);
if(!isOpenDayLine){
if (!isOpenDayLine) {
bottomSheet.setVisibility(View.GONE);
return;
}
Expand All @@ -145,7 146,6 @@ private void setUpDayline(boolean isOpenDayLine) {
}



private void checkIntent() {
mPresenter.checkIntentFromClickTipView(getIntent());
//每日一句
Expand Down Expand Up @@ -209,6 209,7 @@ private void checkTranslateWay() {

private void initConfig() {
//mPresenter.clearSoundCache();
mPresenter.analysisLocalDic();
}

private void checkVersion() {
Expand Down Expand Up @@ -306,6 307,7 @@ private void addListener() {
mInput.setOnKeyListener((v, keyCode, event) -> {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
MobclickAgent.onEvent(this, "action_translate_by_keyboard");
mInput.dismissDropDown();
translate();
return true;
}
Expand Down Expand Up @@ -386,7 388,7 @@ private boolean isEmptyWord(String input, boolean withEmptyPoint) {
}

private void translate() {
Log.i(TAG,"execute translate");
Log.i(TAG, "execute translate");
closeKeyboard();
final String input = mInput.getText().toString().trim();
if (checkInput(input)) {
Expand Down Expand Up @@ -569,6 571,17 @@ public void fillDayline(IDayLine entity) {
mIvSoundDayline.setTag(entity);
}

@Override
public void attachLocalDic(List<String> dic) {
ArrayAdapter<String> wordAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
dic);
mInput.setAdapter(wordAdapter);
mInput.setThreshold(1);
mInput.setOnItemClickListener((parent, view, position, id) -> translate());
}

@OnClick(R.id.iv_sound_dayline)
public void onClickDaylineSound(View view) {
MobclickAgent.onEvent(getApplicationContext(), "sound_dayline_activity");
Expand Down Expand Up @@ -606,7 619,7 @@ private void initSpinner() {
}

public void onClickBottomSheet() {
if(mBottomSheetBehavior == null){
if (mBottomSheetBehavior == null) {
return;
}
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
Expand All @@ -627,7 640,7 @@ public void onClickInput(View view) {
* @return
*/
private boolean checkBottomSheetIsExpandedAndReset() {
if(mBottomSheetBehavior == null){
if (mBottomSheetBehavior == null) {
return false;
}
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
Expand Down
Loading

0 comments on commit dddb469

Please sign in to comment.