Skip to content

Commit 55c0460

Browse files
author
davedega
committed
Move EditText into Toolbar
Sort in alphabetical order
1 parent 56f81f1 commit 55c0460

File tree

12 files changed

+134
-56
lines changed

12 files changed

+134
-56
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
<activity
3131
android:name=".RnDActivity"
32-
android:label="@string/title_activity_maps"
32+
android:theme="@style/AppTheme"
33+
android:label="@string/app_name"
3334
android:screenOrientation="portrait">
3435
<intent-filter>
3536
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/dega/backbase/RnDActivity.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import android.os.Bundle;
44
import android.support.v4.app.FragmentManager;
5-
import android.support.v4.app.NavUtils;
65
import android.support.v7.app.ActionBar;
76
import android.support.v7.app.AppCompatActivity;
7+
import android.support.v7.widget.Toolbar;
88
import android.util.Log;
9+
import android.view.Menu;
10+
import android.view.MenuInflater;
911
import android.view.MenuItem;
12+
import android.view.View;
13+
import android.widget.EditText;
1014

1115
import com.dega.backbase.model.Entry;
1216
import com.google.android.gms.maps.CameraUpdateFactory;
@@ -23,28 +27,19 @@ public class RnDActivity extends AppCompatActivity implements OnMapReadyCallback
2327

2428
private RnDPresenter presenter;
2529
private GoogleMap mMap;
30+
31+
//to center the map en the city selected
2632
private LatLng citySelected;
2733
private Entry entrySelected;
2834

35+
2936
@Override
3037
protected void onCreate(Bundle savedInstanceState) {
3138
super.onCreate(savedInstanceState);
3239
setContentView(R.layout.activity_wrapper);
3340
attachListFragment();
3441
}
3542

36-
@Override
37-
public boolean onOptionsItemSelected(MenuItem item) {
38-
switch (item.getItemId()) {
39-
case android.R.id.home:
40-
onBackPressed();
41-
return true;
42-
default:
43-
return super.onOptionsItemSelected(item);
44-
}
45-
}
46-
47-
4843
private void attachListFragment() {
4944
RnDFragment entriesFragment = (RnDFragment) getSupportFragmentManager()
5045
.findFragmentById(R.id.content_frame);
@@ -57,16 +52,11 @@ private void attachListFragment() {
5752
}
5853
}
5954

60-
6155
@Override
6256
public void onMapReady(GoogleMap googleMap) {
6357
mMap = googleMap;
6458
mMap.addMarker(new MarkerOptions().position(citySelected).title(entrySelected.toString()));
6559
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(citySelected, 10));
66-
ActionBar actionBar = getSupportActionBar();
67-
actionBar.setDisplayHomeAsUpEnabled(true);
68-
actionBar.setHomeButtonEnabled(true);
69-
7060
}
7161

7262
@Override

app/src/main/java/com/dega/backbase/RnDContract.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface Presenter {
2222

2323
Set<Entry> searchByPrefix(final Set<Entry> entries, final String prefix);
2424

25+
void onSearchCity();
2526
}
2627

2728
interface View {
@@ -32,5 +33,6 @@ interface View {
3233

3334
void showErrorMessage(int message);
3435

36+
void showSearchCity();
3537
}
3638
}

app/src/main/java/com/dega/backbase/RnDFragment.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import android.text.TextWatcher;
1212
import android.util.Log;
1313
import android.view.LayoutInflater;
14+
import android.view.Menu;
15+
import android.view.MenuInflater;
1416
import android.view.MotionEvent;
1517
import android.view.View;
1618
import android.view.ViewGroup;
@@ -38,9 +40,9 @@ public class RnDFragment extends Fragment implements RnDContract.View {
3840

3941
private RnDContract.Presenter presenter;
4042

43+
private LinearLayout loadingContainer;
4144
private ListView listView;
4245
private ProgressBar progressBar;
43-
private LinearLayout loadingContainer;
4446

4547
private EditText searchEditText;
4648

@@ -55,10 +57,15 @@ public static RnDFragment newInstance() {
5557
return fragment;
5658
}
5759

60+
@Override
61+
public void onCreate(@Nullable Bundle savedInstanceState) {
62+
super.onCreate(savedInstanceState);
63+
setHasOptionsMenu(true);
64+
}
5865

5966
@Nullable
6067
@Override
61-
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
68+
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
6269

6370
rootView = inflater.inflate(R.layout.entries_fragment, container, false);
6471
listView = rootView.findViewById(R.id.entriesListView);
@@ -74,8 +81,15 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
7481
});
7582
progressBar = rootView.findViewById(R.id.progressBar);
7683
loadingContainer = rootView.findViewById(R.id.loadingContainer);
77-
searchEditText = rootView.findViewById(R.id.searchEditText);
7884

85+
rootView.findViewById(R.id.clearImageView).setOnClickListener(new View.OnClickListener() {
86+
@Override
87+
public void onClick(View view) {
88+
searchEditText.setText("");
89+
}
90+
});
91+
92+
searchEditText = rootView.findViewById(R.id.searchEditText);
7993
searchEditText.addTextChangedListener(new TextWatcher() {
8094
Handler handler = new Handler(Looper.getMainLooper());
8195
Runnable workRunnable;
@@ -120,28 +134,16 @@ public void run() {
120134
}
121135
});
122136

123-
searchEditText.setOnTouchListener(new View.OnTouchListener() {
124-
125-
@Override
126-
public boolean onTouch(View v, MotionEvent event) {
127-
final int DRAWABLE_LEFT = 0;
128-
final int DRAWABLE_TOP = 1;
129-
final int DRAWABLE_RIGHT = 2;
130-
final int DRAWABLE_BOTTOM = 3;
131-
132-
if (event.getAction() == MotionEvent.ACTION_UP) {
133-
if (event.getRawX() >= (searchEditText.getRight() - searchEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
134-
// your action here
135-
searchEditText.setText("");
136-
return true;
137-
}
138-
}
139-
return false;
140-
}
141-
});
142137
return rootView;
143138
}
144139

140+
141+
@Override
142+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
143+
inflater.inflate(R.menu.menu, menu);
144+
super.onCreateOptionsMenu(menu, inflater);
145+
}
146+
145147
@Override
146148
public void setPresenter(RnDContract.Presenter presenter) {
147149
this.presenter = presenter;
@@ -164,6 +166,11 @@ public void showErrorMessage(int message) {
164166
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
165167
}
166168

169+
@Override
170+
public void showSearchCity() {
171+
172+
}
173+
167174
// The Adapter lives within the view since is the only class who access it
168175
class EntriesAdapter extends ArrayAdapter<Entry> {
169176

app/src/main/java/com/dega/backbase/RnDPresenter.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.InputStream;
1313
import java.io.InputStreamReader;
1414
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.Comparator;
1517
import java.util.HashSet;
1618
import java.util.List;
1719
import java.util.Set;
@@ -26,6 +28,10 @@ public class RnDPresenter implements RnDContract.Presenter {
2628
private List<Entry> entries;
2729

2830

31+
public List<Entry> getEntries() {
32+
return entries;
33+
}
34+
2935
public void setEntries(List<Entry> entries) {
3036
this.entries = entries;
3137
}
@@ -68,6 +74,11 @@ public Set<Entry> searchByPrefix(Set<Entry> entries, String prefix) {
6874
return entriesWithPrefix;
6975
}
7076

77+
@Override
78+
public void onSearchCity() {
79+
view.showSearchCity();
80+
}
81+
7182
// the purpose of this class is to load the file in background
7283
class EntriesTask extends AsyncTask<Void, Integer, List<Entry>> {
7384
@Override
@@ -87,7 +98,9 @@ protected List<Entry> doInBackground(Void... voids) {
8798
reader.endArray();
8899
reader.close();
89100
setEntries(entries);
90-
return entries;
101+
102+
List<Entry> sorted = sortAlphabetical(entries);
103+
return sorted;
91104

92105
} catch (IOException ex) {
93106
ex.printStackTrace();
@@ -105,4 +118,18 @@ protected void onPostExecute(List<Entry> entries) {
105118
}
106119
}
107120
}
121+
122+
123+
private List<Entry> sortAlphabetical(List<Entry> entries) {
124+
Collections.sort(entries, new Comparator<Entry>() {
125+
@Override
126+
public int compare(Entry entry1, Entry entry2) {
127+
String s1 = entry1.getName();
128+
String s2 = entry2.getName();
129+
return s1.compareToIgnoreCase(s2);
130+
}
131+
});
132+
133+
return entries;
134+
}
108135
}

app/src/main/java/com/dega/backbase/model/Entry.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.dega.backbase.model;
22

3+
import android.support.annotation.NonNull;
4+
5+
import java.util.Comparator;
6+
37
/**
48
* Created by davedega on 25/03/18.
59
*/
@@ -49,9 +53,10 @@ public void setCoord(Coord coord) {
4953

5054
@Override
5155
public String toString() {
52-
return name + ", "+country;
56+
return name + ", " + country;
5357
}
5458

59+
5560
@Override
5661
public boolean equals(Object o) {
5762
if (this == o) return true;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/toolbar"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content"
7+
android:background="?attr/colorPrimary"
8+
android:minHeight="?attr/actionBarSize"
9+
android:theme="?attr/actionBarTheme"
10+
app:titleTextColor="@color/white">
11+
12+
13+
<LinearLayout
14+
android:id="@+id/searchContainer"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:gravity="center"
18+
android:orientation="horizontal"
19+
android:visibility="visible">
20+
21+
<EditText
22+
android:id="@+id/searchEditText"
23+
android:layout_width="0dp"
24+
android:layout_height="wrap_content"
25+
android:layout_weight="1"
26+
android:backgroundTint="@color/white"
27+
android:hint="@string/search_city"
28+
android:textColor="@color/white"
29+
android:textColorHint="@color/white" />
30+
31+
<ImageView
32+
android:id="@+id/clearImageView"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:layout_marginRight="8dp"
36+
android:src="@android:drawable/ic_menu_close_clear_cancel" />
37+
38+
</LinearLayout>
39+
40+
41+
</android.support.v7.widget.Toolbar>

app/src/main/res/layout/entries_fragment.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
android:layout_height="match_parent"
66
android:orientation="vertical">
77

8+
<include layout="@layout/action_bar_layout" />
9+
810
<LinearLayout
911
android:id="@+id/loadingContainer"
1012
android:layout_width="wrap_content"
@@ -29,13 +31,6 @@
2931
android:visibility="visible" />
3032
</LinearLayout>
3133

32-
<EditText
33-
android:id="@+id/searchEditText"
34-
android:layout_width="match_parent"
35-
android:layout_height="wrap_content"
36-
android:drawableRight="@android:drawable/ic_menu_close_clear_cancel" />
37-
38-
3934
<ListView
4035
android:id="@+id/entriesListView"
4136
android:layout_width="match_parent"

app/src/main/res/menu/menu.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/search_city"
6+
android:icon="@android:drawable/ic_menu_search"
7+
android:title="@string/search_city"
8+
app:showAsAction="always" />
9+
</menu>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<resources>
2-
3-
<string name="app_name">Backbase</string>
2+
<string name="app_name">Assignment RnD</string>
43
<string name="cities">Cities</string>
54
<string name="title_activity_maps">Map</string>
65
<string name="entries_not_loaded">Entries not loaded</string>
7-
6+
<string name="search_city">Search city</string>
7+
<string name="search">Search</string>
88
</resources>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
55
<!-- Customize your theme here. -->
66
<item name="colorPrimary">@color/colorPrimary</item>
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

app/src/test/java/com/dega/backbase/PresenterTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.HashSet;
99
import java.util.LinkedHashSet;
1010
import java.util.Set;
11+
import java.util.TreeSet;
1112

1213
import static org.junit.Assert.*;
1314
import static org.junit.Assert.assertTrue;
@@ -18,8 +19,8 @@
1819
public class PresenterTest {
1920

2021
RnDPresenter presenter = new RnDPresenter();
21-
HashSet<Entry> entries = new HashSet<>();
22-
HashSet<Entry> expected = new HashSet<>();
22+
TreeSet<Entry> entries = new TreeSet<>();
23+
TreeSet<Entry> expected = new TreeSet<>();
2324
Set<Entry> result;
2425

2526

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy