Skip to content

Commit

Permalink
搭好grep框架
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane96 committed Dec 3, 2017
1 parent 59d8d99 commit 475f317
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 30 deletions.
15 changes: 0 additions & 15 deletions fairy-client/src/main/java/me/zane/fairy/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
*/
package me.zane.fairy;

import android.content.Context;
import android.graphics.Point;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.view.WindowManager;

import java.io.File;
import java.util.Calendar;
Expand All @@ -34,18 +31,6 @@

public class Utils {

public static float fromDPtoPix(Context context, int dp) {
return context.getResources().getDisplayMetrics().density * dp;
}

public static int getScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
windowManager.getDefaultDisplay().getSize(size);

return size.x;
}

public static File getDiskCacheDir(String uniqueName) {
String cachePath;
if(!"mounted".equals(Environment.getExternalStorageState()) && Environment.isExternalStorageRemovable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

public class EditBindingAdapter {
@BindingAdapter("android:hint")
@BindingAdapter("judgehint")
public static void setHint(EditText view, CharSequence hint) {
switch (view.getId()) {
case R.id.edit_options_logcat:
Expand All @@ -46,6 +46,13 @@ public static void setHint(EditText view, CharSequence hint) {
}
view.setHint(filter);
break;
case R.id.edit_grep_logcat:
String grep = "[grep]";
if (!hint.equals("")) {
grep = hint.toString();
}
view.setHint(grep);
break;
default:
ZLog.e("no match");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Email: [email protected]
*/

@Database(entities = {LogcatItem.class, LogcatContent.class}, version = 3)
@Database(entities = {LogcatItem.class, LogcatContent.class}, version = 4)
public abstract class LogcatDatabase extends RoomDatabase{
private static final class SingletonHolder {
private static final LogcatDatabase instance = Room.databaseBuilder(App.getInstance(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.zane.fairy.repository;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;

import me.zane.fairy.vo.LogcatContent;

/**
* Grep进行过滤
* Created by Zane on 2017/11/30.
* Email: [email protected]
*/

public class GrepFilter {

static LiveData<LogcatContent> grepData(LiveData<LogcatContent> rawData, String grep) {

return Transformations.map(rawData, logcatData -> {

// TODO: 2017/11/30 grep logic
return new LogcatContent(0, "");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;
import android.util.ArrayMap;

import me.zane.fairy.api.LiveNetService;
import me.zane.fairy.api.ServiceProvider;
Expand Down Expand Up @@ -59,10 +60,17 @@ public static LogcatContentRepository getInstance(@NonNull AppExecutors executor
return instance;
}

public LiveData<LogcatContent> getLogcatContent(int id) {
/**
* 向上层抛出LiveData
* 根据grep来决策是否过滤数据
* @param id
* @param grep
* @return
*/
public LiveData<LogcatContent> getLogcatContent(int id, String grep) {
source.init(id);
source.initData();
return source.asLiveData();
return DataCreator.creat(source.asLiveData(), grep);
}

public void fetchData(String options, String filter) {
Expand Down Expand Up @@ -90,4 +98,24 @@ public void clearContent(LogcatContent content) {
source.clearContent();
});
}

private static class DataCreator {
private static final ArrayMap<String, LiveData<LogcatContent>> dataCache;

static {
dataCache = new ArrayMap<>();
}

static LiveData<LogcatContent> creat (LiveData<LogcatContent> rawData, String grep) {
if (grep.equals("")) {
return rawData;
}
LiveData<LogcatContent> grepData = dataCache.get(grep);
if (grepData == null) {
grepData = GrepFilter.grepData(rawData, grep);
dataCache.put(grep, grepData);
}
return grepData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public class LogcatActivity extends AppCompatActivity{
public static final String INDEX_KEY = "index_key";
public static final String OPTIONS = "options";
public static final String FILTER = "filter";
public static final String GREP = "grrp";

private ActivityLogcatBinding binding;
private LogcatContentViewModel viewModel;

private String options;
private String filter;
private String grep;
private int id;

private boolean isFirstLoad = true;
Expand All @@ -60,6 +62,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
id = getIntent().getIntExtra(INDEX_KEY, -1);
options = getIntent().getStringExtra(OPTIONS);
filter = getIntent().getStringExtra(FILTER);
grep = getIntent().getStringExtra(GREP);

init();
}
Expand All @@ -68,10 +71,11 @@ private void init() {
//绑定
viewModel = ViewModelProviders.of(this, ViewModelFactory.getInstance()).get(LogcatContentViewModel.class);
binding.setModel(viewModel);
viewModel.init(id, binding);
viewModel.init(id, grep, binding);

viewModel.onFilterChanged(filter);
viewModel.onOptionsChanged(options);
viewModel.onGrepChanged(grep);

binding.scrollviewLogcat.setSmoothScrollingEnabled(true);
viewModel.getData().observe(this, content -> {
Expand Down Expand Up @@ -103,6 +107,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
protected void onStop() {
super.onStop();
LogcatItemViewModel itemViewModel = ViewModelProviders.of(this, ViewModelFactory.getInstance()).get(LogcatItemViewModel.class);
itemViewModel.updateItem(new LogcatItem(id, binding.getModel().options.get(), binding.getModel().filter.get()));
itemViewModel.updateItem(new LogcatItem(id, binding.getModel().options.get(), binding.getModel().filter.get(), binding.getModel().grep.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package me.zane.fairy.view.content;

import android.app.Application;
import android.arch.core.util.Function;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
Expand All @@ -24,6 +25,7 @@
import android.support.annotation.NonNull;

import me.zane.fairy.MySharedPre;
import me.zane.fairy.ZLog;
import me.zane.fairy.databinding.ActivityLogcatBinding;
import me.zane.fairy.repository.LogcatContentRepository;
import me.zane.fairy.vo.LogcatContent;
Expand All @@ -40,22 +42,23 @@ public class LogcatContentViewModel extends AndroidViewModel{

public final ObservableField<String> filter = new ObservableField<>();
public final ObservableField<String> options = new ObservableField<>();
public final ObservableField<String> grep = new ObservableField<>();
public final ObservableField<Boolean> isStartFetch = new ObservableField<>();

private final MutableLiveData<Boolean> isStartLiveData = new MutableLiveData<>();
private final MutableLiveData<String> isStartLiveData = new MutableLiveData<>();
private final LiveData<LogcatContent> contentLiveData;

public LogcatContentViewModel(@NonNull Application application, LogcatContentRepository repository) {
super(application);
this.repository = repository;
contentLiveData = Transformations.switchMap(isStartLiveData, isStart -> repository.getLogcatContent(id));
contentLiveData = Transformations.switchMap(isStartLiveData, grep -> repository.getLogcatContent(id, grep));
}

//---------------------------------action binding---------------------------------
void init(int id, ActivityLogcatBinding binding) {
void init(int id, String grep, ActivityLogcatBinding binding) {
this.binding = binding;
this.id = id;
isStartLiveData.setValue(true);
isStartLiveData.setValue(grep);
insertIfNotExits(new LogcatContent(id, "init fairy"));
}

Expand All @@ -67,6 +70,12 @@ public void onFilterChanged(CharSequence s) {
filter.set(s.toString());
}

public void onGrepChanged(CharSequence s) {
grep.set(s.toString());
//replace the data from repository (grep or not grep)
isStartLiveData.setValue(s.toString());
}

public void onStartFetch() {
isStartFetch.set(true);
repository.fetchData(options.get(), filter.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private void initView() {
intent.putExtra(LogcatActivity.OPTIONS, item.getOptions());
intent.putExtra(LogcatActivity.INDEX_KEY, item.getId());
intent.putExtra(LogcatActivity.FILTER, item.getFilter());
intent.putExtra(LogcatActivity.GREP, item.getGrep());
startActivity(intent);
});
}
Expand All @@ -98,7 +99,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_main_bar:
viewModel.insertItem(new LogcatItem(0, "", ""));
viewModel.insertItem(new LogcatItem(0, "", "", ""));
break;
}
return super.onOptionsItemSelected(item);
Expand Down
17 changes: 15 additions & 2 deletions fairy-client/src/main/java/me/zane/fairy/vo/LogcatItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ public class LogcatItem {
@ColumnInfo(name = "logcat_filter")
private String filter;

public LogcatItem(int id, String options, String filter) {
@ColumnInfo(name = "logcat_grep")
private String grep;

public LogcatItem(int id, String options, String filter, String grep) {
this.id = id;
this.options = options;
this.filter = filter;
this.grep = grep;
}

public int getId() {
Expand All @@ -67,10 +71,19 @@ public void setFilter(String filter) {
this.filter = filter;
}

public String getGrep() {
return grep;
}

public void setGrep(String grep) {
this.grep = grep;
}

@Override
public String toString() {
return "id: " + id +
" options: " + options +
" filter: " + filter;
" filter: " + filter +
" grep: " + grep;
}
}
10 changes: 8 additions & 2 deletions fairy-client/src/main/res/layout/activity_logcat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@
<EditText
android:id="@+id/edit_options_logcat"
android:onTextChanged="@{(s, start, before, count) -> model.onOptionsChanged(s)}"
android:hint="@{model.options}"
app:judgehint="@{model.options}"
style="@style/MyEditStyle"/>

<EditText
android:id="@+id/edit_filter_logcat"
android:onTextChanged="@{(s, start, before, count) -> model.onFilterChanged(s)}"
android:hint="@{model.filter}"
app:judgehint="@{model.filter}"
style="@style/MyEditStyle"/>

<EditText
android:id="@+id/edit_grep_logcat"
android:onTextChanged="@{(s, start, before, count) -> model.onGrepChanged(s)}"
app:judgehint="@{model.grep}"
style="@style/MyEditStyle"/>
</LinearLayout>

Expand Down
4 changes: 4 additions & 0 deletions fairy-client/src/test/java/me/zane/fairy/ExampleUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public void addition_isCorrect() throws Exception {
//assertEquals("10-26 16:40:36.000", LiveNetService.getInstance().currentTimeLine("10-26 16:40:35.999"));

assertEquals("-v threadtime", extractOptions("-v threadtime -t \"11-5 8:2:21.121\""));

String rawString = "<p>sss</p><p>bbb</p>";
String[] strs = rawString.split("<p>");
assertEquals("sss", strs[2]);
}

private String extractOptions(String rawOptions) {
Expand Down

0 comments on commit 475f317

Please sign in to comment.