Skip to content

Commit b4f9d36

Browse files
author
Aman
committed
heavy refactoring & moved content reciever to viewer activity
1 parent 7af38f7 commit b4f9d36

16 files changed

+1026
-951
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<provider android:authorities="aman.loghub.provider" />
1010
</queries>
1111

12-
1312
<application
1413
android:name="Log"
1514
android:largeHeap="true"
@@ -28,8 +27,14 @@
2827
<action android:name="android.intent.action.MAIN" />
2928
<category android:name="android.intent.category.LAUNCHER" />
3029
</intent-filter>
31-
32-
<!-- Intent filter to handle JSON files -->
30+
</activity>
31+
32+
<activity
33+
android:name=".ViewerActivity"
34+
android:exported="true"
35+
android:screenOrientation="portrait"
36+
android:parentActivityName=".MainActivity">
37+
3338
<intent-filter>
3439
<action android:name="android.intent.action.VIEW" />
3540
<category android:name="android.intent.category.DEFAULT" />
@@ -39,8 +44,7 @@
3944
<data android:mimeType="application/json" />
4045
<data android:mimeType="text/json" />
4146
</intent-filter>
42-
43-
<!-- Additional intent filter for .json file extensions -->
47+
4448
<intent-filter>
4549
<action android:name="android.intent.action.VIEW" />
4650
<category android:name="android.intent.category.DEFAULT" />
@@ -53,14 +57,9 @@
5357
<data android:pathPattern=".*\\..*\\..*\\.json" />
5458
<data android:pathPattern=".*\\..*\\..*\\..*\\.json" />
5559
</intent-filter>
56-
</activity>
5760

58-
<activity
59-
android:name=".ViewerActivity"
60-
android:exported="false"
61-
android:screenOrientation="portrait"
62-
android:parentActivityName=".MainActivity" />
61+
</activity>
6362

6463
</application>
6564

66-
</manifest>
65+
</manifest>

app/src/main/java/aman/jsonviewer/Fragments/CardViewFragment.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class CardViewFragment extends Fragment implements ViewerActivity.Searcha
3232
private CardAdapter adapter;
3333
private List<CardItem> items = new ArrayList<>();
3434
private String jsonData;
35-
private ExecutorService executor = Executors.newSingleThreadExecutor();
36-
private Handler mainHandler = new Handler(Looper.getMainLooper());
35+
private final ExecutorService executor = Executors.newSingleThreadExecutor();
36+
private final Handler mainHandler = new Handler(Looper.getMainLooper());
3737

3838
public static CardViewFragment newInstance() {
3939
return new CardViewFragment();
@@ -64,11 +64,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
6464
@Override
6565
public void onResume() {
6666
super.onResume();
67-
// Apply current search query when fragment becomes visible
6867
if (getActivity() instanceof ViewerActivity) {
6968
String currentQuery = ((ViewerActivity) getActivity()).getCurrentSearchQuery();
7069
if (currentQuery != null && !currentQuery.isEmpty() && adapter != null) {
71-
// Post with delay to ensure data is loaded
7270
recyclerView.postDelayed(() -> {
7371
if (adapter != null && !items.isEmpty()) {
7472
adapter.filter(currentQuery);
@@ -105,7 +103,6 @@ private void parseJsonAsync() {
105103
progressBar.setVisibility(View.GONE);
106104
recyclerView.setVisibility(View.VISIBLE);
107105

108-
// Apply search filter if there's an active search
109106
if (getActivity() instanceof ViewerActivity) {
110107
String currentQuery = ((ViewerActivity) getActivity()).getCurrentSearchQuery();
111108
if (currentQuery != null && !currentQuery.isEmpty()) {
@@ -181,7 +178,6 @@ public void onSearch(String query) {
181178
}
182179
}
183180

184-
// NEW: Public method to allow ViewerActivity to access adapter for navigation
185181
public CardAdapter getAdapter() {
186182
return adapter;
187183
}
@@ -196,7 +192,7 @@ static class CardItem {
196192
String key;
197193
String value;
198194
String type;
199-
boolean matchesSearch = false; // NEW: Track search matches
195+
boolean matchesSearch = false;
200196

201197
CardItem(String key, String value, String type) {
202198
this.key = key;
@@ -209,17 +205,15 @@ class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
209205
private List<CardItem> displayItems;
210206
private List<CardItem> allItems;
211207

212-
// NEW: Search navigation fields
213208
private String currentSearchQuery = "";
214-
private List<Integer> searchMatches = new ArrayList<>();
209+
private final List<Integer> searchMatches = new ArrayList<>();
215210
private int currentMatchIndex = -1;
216211

217212
CardAdapter(List<CardItem> items) {
218213
this.allItems = items;
219214
this.displayItems = items;
220215
}
221216

222-
// Method to update all items (called after async loading)
223217
void updateAllItems(List<CardItem> newItems) {
224218
this.allItems = newItems;
225219
if (currentSearchQuery.isEmpty()) {
@@ -246,7 +240,6 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
246240
int color = getTypeColor(item.type);
247241
holder.typeText.setTextColor(color);
248242

249-
// NEW: Highlight current match differently
250243
if (!searchMatches.isEmpty() && currentMatchIndex >= 0 &&
251244
currentMatchIndex < searchMatches.size() &&
252245
searchMatches.get(currentMatchIndex) == position) {
@@ -291,15 +284,13 @@ void filter(String query) {
291284
currentSearchQuery = query;
292285

293286
if (query.isEmpty()) {
294-
// Clear search
295287
for (CardItem item : allItems) {
296288
item.matchesSearch = false;
297289
}
298290
displayItems = allItems;
299291
searchMatches.clear();
300292
currentMatchIndex = -1;
301293
} else {
302-
// Filter and mark matches
303294
List<CardItem> filtered = new ArrayList<>();
304295
String lowerQuery = query.toLowerCase();
305296
for (CardItem item : allItems) {
@@ -312,7 +303,6 @@ void filter(String query) {
312303
}
313304
displayItems = filtered;
314305

315-
// NEW: Update search matches and scroll to first
316306
updateSearchMatches();
317307
if (!searchMatches.isEmpty()) {
318308
currentMatchIndex = 0;
@@ -322,7 +312,6 @@ void filter(String query) {
322312
notifyDataSetChanged();
323313
}
324314

325-
// NEW: Update search matches list
326315
private void updateSearchMatches() {
327316
searchMatches.clear();
328317
for (int i = 0; i < displayItems.size(); i++) {
@@ -332,7 +321,6 @@ private void updateSearchMatches() {
332321
}
333322
}
334323

335-
// NEW: Scroll to current match
336324
private void scrollToCurrentMatch() {
337325
if (currentMatchIndex >= 0 && currentMatchIndex < searchMatches.size()) {
338326
int position = searchMatches.get(currentMatchIndex);
@@ -344,14 +332,12 @@ private void scrollToCurrentMatch() {
344332
}
345333
}
346334

347-
// NEW: Navigate to next match
348335
public void nextMatch() {
349336
if (searchMatches.isEmpty()) return;
350337
currentMatchIndex = (currentMatchIndex + 1) % searchMatches.size();
351338
scrollToCurrentMatch();
352339
}
353340

354-
// NEW: Navigate to previous match
355341
public void previousMatch() {
356342
if (searchMatches.isEmpty()) return;
357343
currentMatchIndex--;
@@ -361,12 +347,10 @@ public void previousMatch() {
361347
scrollToCurrentMatch();
362348
}
363349

364-
// NEW: Get current match index
365350
public int getCurrentMatchIndex() {
366351
return currentMatchIndex;
367352
}
368353

369-
// NEW: Get total matches
370354
public int getTotalMatches() {
371355
return searchMatches.size();
372356
}
@@ -386,4 +370,4 @@ class ViewHolder extends RecyclerView.ViewHolder {
386370
}
387371
}
388372
}
389-
}
373+
}

0 commit comments

Comments
 (0)