Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
update magnetic searchMark
Browse files Browse the repository at this point in the history
  • Loading branch information
7449 committed Jun 27, 2017
1 parent 365513b commit fb6eae0
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 2 deletions.
45 changes: 45 additions & 0 deletions magnetic/src/main/java/com/magnetic/manager/DBManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.magnetic.manager;

import com.framework.utils.UIUtils;
import com.magnetic.mvp.model.DaoMaster;
import com.magnetic.mvp.model.DaoSession;
import com.magnetic.mvp.model.SearchModel;
import com.magnetic.mvp.model.SearchModelDao;

import java.util.List;

/**
* by y on 2017/6/21.
*/

public class DBManager {
private DBManager() {
}

private static final String SQL_NAME = "magnetic";

private static class SessionHolder {
private static final DaoSession daoSession = new DaoMaster(
new DaoMaster.DevOpenHelper(UIUtils.getContext(), SQL_NAME, null).getWritableDatabase()).newSession();
}

public static boolean isEmpty(String key) {
return SessionHolder.daoSession.getSearchModelDao().queryBuilder().where(SearchModelDao.Properties.SearchContent.eq(key)).unique() == null;
}

public static List<SearchModel> getSearchContent() {
return SessionHolder.daoSession.getSearchModelDao().loadAll();
}

public static void insert(String markName) {
SessionHolder.daoSession.getSearchModelDao().insert(new SearchModel(markName));
}

public static void clear(String key) {
SessionHolder.daoSession.getSearchModelDao().deleteByKey(key);
}

public static void clear() {
SessionHolder.daoSession.getSearchModelDao().deleteAll();
}
}
32 changes: 32 additions & 0 deletions magnetic/src/main/java/com/magnetic/mvp/model/SearchModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.magnetic.mvp.model;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
* by y on 27/06/2017.
*/
@Entity
public class SearchModel {
@Id
private String searchContent;

@Generated(hash = 947289422)
public SearchModel(String searchContent) {
this.searchContent = searchContent;
}

@Generated(hash = 506184495)
public SearchModel() {
}

public String getSearchContent() {
return this.searchContent;
}

public void setSearchContent(String searchContent) {
this.searchContent = searchContent;
}

}
54 changes: 52 additions & 2 deletions magnetic/src/main/java/com/magnetic/ui/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@
import com.framework.base.BaseActivity;
import com.framework.utils.UIUtils;
import com.magnetic.R;
import com.magnetic.manager.DBManager;
import com.magnetic.mvp.model.MagneticModel;
import com.magnetic.mvp.model.SearchModel;
import com.magnetic.mvp.presenter.MainPresenterImpl;
import com.magnetic.mvp.view.ViewManager;
import com.xadapter.OnXBindListener;
import com.xadapter.adapter.XRecyclerViewAdapter;
import com.xadapter.holder.XViewHolder;

public class MainActivity extends BaseActivity<MainPresenterImpl> implements ViewManager.MainView {
import java.util.List;

public class MainActivity extends BaseActivity<MainPresenterImpl> implements ViewManager.MainView, OnXBindListener<SearchModel> {

private Toolbar mToolbar;
private XRecyclerViewAdapter<SearchModel> adapter;
private MaterialDialog markDialog;

@Override
protected void initCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -56,9 +65,35 @@ public boolean onOptionsItemSelected(MenuItem item) {
.input(
UIUtils.getString(R.string.search_dialog_hint),
null,
(dialog, input) -> mPresenter.startSearch(String.valueOf(input)))
((dialog, input) -> {
if (DBManager.isEmpty(String.valueOf(input))) {
DBManager.insert(String.valueOf(input));
}
mPresenter.startSearch(String.valueOf(input));
}))
.show();
break;
case R.id.mark:
List<SearchModel> fictionMarkAll = DBManager.getSearchContent();
if (fictionMarkAll == null || fictionMarkAll.isEmpty()) {
UIUtils.snackBar(mStatusView, R.string.mark_null);
} else {
if (adapter == null) {
adapter = new XRecyclerViewAdapter<>();
}
markDialog = new MaterialDialog
.Builder(this)
.title(R.string.search_content)
.adapter(
adapter
.initXData(fictionMarkAll)
.setLayoutId(R.layout.item_mark)
.onXBind(this)
.setOnItemClickListener((view, position, info) -> mPresenter.startSearch(String.valueOf(info.getSearchContent())))
, null)
.show();
}
break;
}
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -93,4 +128,19 @@ public void hideProgress() {
public FragmentActivity getMainActivity() {
return this;
}

@Override
public void onXBind(XViewHolder holder, int position, SearchModel searchModel) {
holder.setTextView(R.id.tv_name, searchModel.getSearchContent());
holder.getView(R.id.iv_delete).setOnClickListener(v -> {
DBManager.clear(searchModel.getSearchContent());
adapter.remove(position);
if (DBManager.getSearchContent() == null || DBManager.getSearchContent().isEmpty()) {
if (markDialog != null) {
markDialog.dismiss();
UIUtils.snackBar(mStatusView, R.string.mark_null);
}
}
});
}
}
10 changes: 10 additions & 0 deletions magnetic/src/main/res/drawable/ic_bookmark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#fff"
android:pathData="M17,3H7c-1.1,0 -1.99,0.9 -1.99,2L5,21l7,-3 7,3V5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>
10 changes: 10 additions & 0 deletions magnetic/src/main/res/drawable/ic_delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#000"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z" />
</vector>
23 changes: 23 additions & 0 deletions magnetic/src/main/res/layout/item_mark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="?android:selectableItemBackground">

<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp" />

<android.support.v7.widget.AppCompatImageView
android:id="@+id/iv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_delete" />

</RelativeLayout>
6 changes: 6 additions & 0 deletions magnetic/src/main/res/menu/search_menu.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/mark"
android:icon="@drawable/ic_bookmark"
android:title="@string/search_content"
app:showAsAction="ifRoom" />
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/search_title"
app:showAsAction="ifRoom" />

</menu>
2 changes: 2 additions & 0 deletions magnetic/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
<string name="copy">只复制</string>
<string name="xl_null">没有安装迅雷</string>
<string name="copy_success">磁力已经粘贴到复制板</string>
<string name="search_content">搜索记录</string>
<string name="mark_null">没有搜索记录</string>
</resources>

0 comments on commit fb6eae0

Please sign in to comment.