Skip to content

Commit 4542c40

Browse files
committed
优化 BusDataManager 和 FeatureDAO,新增根据 ID 批量获取特征功能,改进历史记录数据处理
1 parent 37c483a commit 4542c40

File tree

7 files changed

+83
-21
lines changed

7 files changed

+83
-21
lines changed

app/src/main/java/com/gg/busStation/function/BusDataManager.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.Date;
22+
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Locale;
25+
import java.util.Map;
2426
import java.util.concurrent.TimeUnit;
2527

2628
public class BusDataManager {
@@ -139,12 +141,29 @@ private static String getCompanyCode(Feature feature) {
139141

140142
public static List<ListItemData> routesToListItemData(List<Route> routes, FeatureDAO featureDAO) {
141143
String language = Locale.getDefault().getLanguage();
142-
143144
List<ListItemData> data = new ArrayList<>();
145+
146+
if (routes == null || routes.isEmpty()) {
147+
return data;
148+
}
149+
150+
List<Integer> routeIds = new ArrayList<>();
151+
for (Route route : routes) {
152+
routeIds.add(route.id());
153+
}
154+
List<Feature> features = featureDAO.getFeaturesByIds(routeIds);
155+
156+
Map<Integer, Feature> featureMap = new HashMap<>();
157+
for (Feature feature : features) {
158+
featureMap.put(feature.getRouteId(), feature);
159+
}
160+
144161
for (Route route : routes) {
145-
Feature feature = featureDAO.getFeature(route.id());
146-
ListItemData listItemData = createListItemData(feature, route.routeSeq(), language);
147-
data.add(listItemData);
162+
Feature feature = featureMap.get(route.id());
163+
if (feature != null) {
164+
ListItemData listItemData = createListItemData(feature, route.routeSeq(), language);
165+
data.add(listItemData);
166+
}
148167
}
149168

150169
return data;

app/src/main/java/com/gg/busStation/function/database/dao/FeatureDAO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public interface FeatureDAO {
1818

1919
List<Feature> fuzzySearchFeature(String routeName);
2020

21+
List<Feature> getFeaturesByIds(List<Integer> ids);
22+
2123
List<String> getFeatureNthCharacters(String routeName, int index);
2224
}
2325

app/src/main/java/com/gg/busStation/function/database/dao/FeatureDAOImpl.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.gg.busStation.function.feature.CompanyManager;
1212

1313
import java.util.ArrayList;
14+
import java.util.Collections;
1415
import java.util.List;
1516

1617
public class FeatureDAOImpl implements FeatureDAO {
@@ -93,7 +94,7 @@ public List<Feature> getFeatures(CompanyManager.CompanyEnum company) {
9394
@Override
9495
public List<Feature> fuzzySearchFeature(String routeName) {
9596
if (routeName.isEmpty()) {
96-
return new ArrayList<>();
97+
return Collections.emptyList();
9798
}
9899

99100
String args = routeName + "%";
@@ -117,6 +118,42 @@ public List<Feature> fuzzySearchFeature(String routeName) {
117118
return features;
118119
}
119120

121+
@Override
122+
public List<Feature> getFeaturesByIds(List<Integer> ids) {
123+
if (ids.isEmpty()) {
124+
return Collections.emptyList();
125+
}
126+
127+
List<String> args = new ArrayList<>(ids.size());
128+
StringBuilder placeholders = new StringBuilder();
129+
for (int i = 0; i < ids.size(); i++) {
130+
args.add(String.valueOf(ids.get(i)));
131+
if (i > 0) {
132+
placeholders.append(",");
133+
}
134+
placeholders.append("?");
135+
}
136+
137+
Cursor cursor = db.query(SQLConstants.featureDBName, null,
138+
"routeId IN (" + placeholders + ")",
139+
args.toArray(new String[0]),
140+
null, null, null);
141+
142+
List<Feature> features = new ArrayList<>();
143+
if (!cursor.moveToFirst()) {
144+
cursor.close();
145+
return features;
146+
}
147+
148+
do {
149+
Feature feature = convertToFeature(cursor);
150+
features.add(feature);
151+
} while (cursor.moveToNext());
152+
153+
cursor.close();
154+
return features;
155+
}
156+
120157
@Override
121158
public List<String> getFeatureNthCharacters(String routeName, int index) {
122159
Cursor cursor = db.query(SQLConstants.featureDBName,

app/src/main/java/com/gg/busStation/function/internet/HttpClientHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
import okhttp3.ResponseBody;
1212

1313
public class HttpClientHelper {
14+
private static final OkHttpClient client = new OkHttpClient().newBuilder()
15+
.build();
16+
1417
private HttpClientHelper() {
1518
}
1619

1720
public static void getDataAsync(String url, Callback callback) {
18-
OkHttpClient client = new OkHttpClient();
19-
2021
Request request = new Request.Builder()
2122
.url(url)
2223
.build();
@@ -26,8 +27,6 @@ public static void getDataAsync(String url, Callback callback) {
2627
}
2728

2829
public static ResponseBody getBody(String url) throws IOException {
29-
OkHttpClient client = new OkHttpClient();
30-
3130
Request request = new Request.Builder()
3231
.url(url)
3332
.build();

app/src/main/java/com/gg/busStation/ui/activity/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gg.busStation.ui.activity;
22

33
import android.Manifest;
4+
import android.annotation.SuppressLint;
45
import android.content.Intent;
56
import android.content.pm.PackageManager;
67
import android.net.Uri;
@@ -61,6 +62,7 @@ public void start() {
6162
runOnUiThread(loadingDialog::show);
6263
}
6364

65+
@SuppressLint("SetTextI18n")
6466
@Override
6567
public void progress(int now, int max, String tip) {
6668
runOnUiThread(() -> {

app/src/main/java/com/gg/busStation/ui/adapter/MainAdapter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ public boolean areContentsTheSame(@NonNull ListItemData oldItem, @NonNull ListIt
4545
};
4646
private final FragmentActivity mActivity;
4747
private final boolean isSearch;
48+
private final HistoryDAO historyDAO;
4849

4950
public MainAdapter(FragmentActivity context, boolean isSearch) {
5051
super(DIFF_CALLBACK);
5152
this.mActivity = context;
5253
this.isSearch = isSearch;
54+
55+
DataBaseHelper dbHelper = DataBaseHelper.getInstance(mActivity);
56+
historyDAO = new HistoryDAOImpl(dbHelper.getDatabase());
5357
}
5458

5559
@NonNull
@@ -72,8 +76,6 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
7276
holder.itemView.setOnClickListener(view -> {
7377
new StopBottomSheetDialog(listItemData).show(mActivity.getSupportFragmentManager(), StopBottomSheetDialog.TAG);
7478
// DataBaseManager.addRoutesHistory(listItemData.getCo(), listItemData.getStopNumber(), listItemData.getBound(), listItemData.getService_type());
75-
DataBaseHelper dbHelper = DataBaseHelper.getInstance(mActivity);
76-
HistoryDAO historyDAO = new HistoryDAOImpl(dbHelper.getDatabase());
7779
historyDAO.insert(listItemData.getRouteId(), listItemData.getRouteSeq(), false);
7880
});
7981

@@ -101,9 +103,6 @@ private void showMenu(View moreButtion, ListItemData listItemData) {
101103
int routeId = listItemData.getRouteId();
102104
int routeSeq = listItemData.getRouteSeq();
103105

104-
SQLiteDatabase database = DataBaseHelper.getInstance(mActivity).getDatabase();
105-
HistoryDAO historyDAO = new HistoryDAOImpl(database);
106-
107106
if (historyDAO.getPinnedIndex(routeId, routeSeq) != 0) {
108107
popupMenu.getMenu().findItem(R.id.bus_menu_pin).setVisible(false);
109108
popupMenu.getMenu().findItem(R.id.bus_menu_unpin).setVisible(true);
@@ -136,7 +135,6 @@ private void showMenu(View moreButtion, ListItemData listItemData) {
136135

137136
public void refreshList() {
138137
SQLiteDatabase database = DataBaseHelper.getInstance(mActivity).getDatabase();
139-
HistoryDAO historyDAO = new HistoryDAOImpl(database);
140138
FeatureDAO featureDAO = new FeatureDAOImpl(database);
141139
List<Route> allHistory = historyDAO.getAllHistory();
142140
List<ListItemData> data = BusDataManager.routesToListItemData(allHistory, featureDAO);

app/src/main/java/com/gg/busStation/ui/fragment/HomeFragment.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
4343
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
4444
super.onViewCreated(view, savedInstanceState);
4545

46-
SQLiteDatabase database = DataBaseHelper.getInstance(requireContext()).getDatabase();
47-
HistoryDAO historyDAO = new HistoryDAOImpl(database);
48-
FeatureDAO featureDAO = new FeatureDAOImpl(database);
49-
List<Route> allHistory = historyDAO.getAllHistory();
50-
List<ListItemData> data = BusDataManager.routesToListItemData(allHistory, featureDAO);
51-
initView(data);
46+
new Thread(() -> {
47+
SQLiteDatabase database = DataBaseHelper.getInstance(requireContext()).getDatabase();
48+
HistoryDAO historyDAO = new HistoryDAOImpl(database);
49+
FeatureDAO featureDAO = new FeatureDAOImpl(database);
50+
List<Route> allHistory = historyDAO.getAllHistory();
51+
List<ListItemData> data = BusDataManager.routesToListItemData(allHistory, featureDAO);
52+
FragmentActivity activity = getActivity();
53+
if (activity != null) {
54+
activity.runOnUiThread(() -> initView(data));
55+
}
56+
}).start();
5257
}
5358

5459
@Override

0 commit comments

Comments
 (0)