Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Hide search buttons when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocipriani01 committed Jan 8, 2021
1 parent a46987b commit a2f9cbd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
Expand Down Expand Up @@ -44,6 +46,7 @@ public class ControlPanelFragment extends Fragment
private ViewPager2 viewPager;
private TabLayout tabLayout;
private Context context;
private MenuItem searchMenu;

@Override
public void onAttach(@NonNull Context context) {
Expand Down Expand Up @@ -119,12 +122,12 @@ public void onDestroy() {

@Override
public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) {
MenuItem item = menu.add(R.string.mount_goto);
item.setIcon(R.drawable.search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
searchMenu = menu.add(R.string.search);
searchMenu.setIcon(R.drawable.search);
searchMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView searchView = new SearchView(context);
searchView.setOnQueryTextListener(this);
item.setActionView(searchView);
searchMenu.setActionView(searchView);
}

@Override
Expand All @@ -145,11 +148,13 @@ private void noDevices() {
noDevicesText.post(() -> noDevicesText.setVisibility(View.VISIBLE));
controlLayout.post(() -> controlLayout.setVisibility(View.GONE));
viewPager.post(() -> fragmentAdapter.notifyDataSetChanged());
new Handler(Looper.getMainLooper()).post(() -> searchMenu.setVisible(false));
}

private void devices() {
noDevicesText.post(() -> noDevicesText.setVisibility(View.GONE));
controlLayout.post(() -> controlLayout.setVisibility(View.VISIBLE));
new Handler(Looper.getMainLooper()).post(() -> searchMenu.setVisible(true));
}

@Override
Expand Down
68 changes: 37 additions & 31 deletions app/src/main/java/org/indilib/i4j/iparcos/GoToFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
Expand Down Expand Up @@ -35,7 +37,6 @@
import org.indilib.i4j.iparcos.catalog.Coordinates;
import org.indilib.i4j.iparcos.prop.PropUpdater;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -60,6 +61,7 @@ public class GoToFragment extends ListFragment
private static ArrayAdapter<CatalogEntry> entriesAdapter;
private ConnectionManager connectionManager;
private Context context;
private MenuItem searchMenu;
// INDI properties
private INDINumberProperty telescopeCoordP = null;
private INDINumberElement telescopeCoordRA = null;
Expand Down Expand Up @@ -129,34 +131,12 @@ public void onAttach(@NonNull Context context) {
this.context = context;
}

@Override
public void onStart() {
super.onStart();
// Set up INDI connection
connectionManager = IPARCOSApp.getConnectionManager();
connectionManager.addListener(this);
// Enumerate existing properties
if (connectionManager.isConnected()) {
List<INDIDevice> list = connectionManager.getConnection().getDevicesAsList();
if (list != null) {
for (INDIDevice device : list) {
device.addINDIDeviceListener(this);
for (INDIProperty<?> property : device.getPropertiesAsList()) {
newProperty(device, property);
}
}
}
} else {
clearVars();
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText(getString(R.string.empty_catalog));
setHasOptionsMenu(true);
ArrayList<CatalogEntry> entries = catalog.getEntries();
List<CatalogEntry> entries = catalog.getEntries();
entriesAdapter = new ArrayAdapter<CatalogEntry>(context,
android.R.layout.simple_list_item_2, android.R.id.text1, entries) {
@NonNull
Expand All @@ -171,22 +151,47 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
}
};
setListAdapter(entriesAdapter);
if (!catalog.isReady()) {
if (catalog.isReady()) {
new Handler(Looper.getMainLooper()).post(() -> searchMenu.setVisible(true));
} else {
// List loading
setListShown(false);
catalog.setListener(this);
if (!catalog.isLoading()) new Thread(catalog::load).start();
}
}

@Override
public void onStart() {
super.onStart();
// Set up INDI connection
connectionManager = IPARCOSApp.getConnectionManager();
connectionManager.addListener(this);
// Enumerate existing properties
if (connectionManager.isConnected()) {
List<INDIDevice> list = connectionManager.getConnection().getDevicesAsList();
if (list != null) {
for (INDIDevice device : list) {
device.addINDIDeviceListener(this);
for (INDIProperty<?> property : device.getPropertiesAsList()) {
newProperty(device, property);
}
}
}
} else {
clearVars();
}
}

@Override
public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) {
MenuItem item = menu.add(R.string.mount_goto);
item.setIcon(R.drawable.search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
searchMenu = menu.add(R.string.mount_goto);
searchMenu.setIcon(R.drawable.search);
searchMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
searchMenu.setVisible(false);
SearchView searchView = new SearchView(context);
searchView.setOnQueryTextListener(this);
item.setActionView(searchView);
searchMenu.setActionView(searchView);
}

@Override
Expand Down Expand Up @@ -231,7 +236,7 @@ public boolean onQueryTextSubmit(String query) {

@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
ArrayList<CatalogEntry> entries = catalog.getEntries();
List<CatalogEntry> entries = catalog.getEntries();
final Coordinates coord = entries.get(position).getCoordinates();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(entries.get(position).createDescription(context)).setTitle(entries.get(position).getName());
Expand Down Expand Up @@ -356,8 +361,9 @@ public void newMessage(INDIServerConnection connection, Date timestamp, String m

@Override
public void onLoaded(boolean success) {
getActivity().runOnUiThread(() -> {
requireActivity().runOnUiThread(() -> {
if (success) {
searchMenu.setVisible(true);
entriesAdapter.notifyDataSetChanged();
if (isResumed()) {
setListShown(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* A catalog of astronomical objects.
Expand All @@ -21,7 +22,7 @@ public class Catalog {
/**
* Catalog objects.
*/
private final ArrayList<CatalogEntry> entries = new ArrayList<>();
private final List<CatalogEntry> entries = new ArrayList<>();
private boolean ready = false;
private boolean loading = false;
private CatalogLoadingListener listener = null;
Expand Down Expand Up @@ -68,7 +69,7 @@ public boolean isReady() {
/**
* @return an {@link ArrayList} containing all the entries of this catalog.
*/
public ArrayList<CatalogEntry> getEntries() {
public List<CatalogEntry> getEntries() {
return entries;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* Represents a deep sky object. This class also contains a loader to fetch DSO from the app's catalog.
Expand Down Expand Up @@ -78,7 +78,7 @@ private DSOEntry(String data) {
coord = new Coordinates(raString, decString);
}

public static void loadToList(ArrayList<CatalogEntry> list, Resources resources) throws IOException {
public static void loadToList(List<CatalogEntry> list, Resources resources) throws IOException {
// Open and read the catalog file
InputStream resourceStream = resources.openRawResource(RESOURCE);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* Represents a star. This class also contains a loader to fetch stars from the app's catalog.
Expand Down Expand Up @@ -52,7 +52,7 @@ private StarEntry(String data) {
}
}

public static void loadToList(ArrayList<CatalogEntry> list, Resources resources) throws IOException {
public static void loadToList(List<CatalogEntry> list, Resources resources) throws IOException {
// Open and read the catalog file
InputStream resourceStream = resources.openRawResource(RESOURCE);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@
<string name="telescope_parking">Parcheggiamento montatura</string>
<string name="telescope_parking_confirm">Sei sicuro di voler cambiare lo stato della montatura?</string>
<string name="dark_nebula">Nebulosa oscura</string>
<string name="search">Cerca</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@
<string name="telescope_parking">Telescope parking</string>
<string name="telescope_parking_confirm">Are you sure you want change the parking state?</string>
<string name="dark_nebula">Dark nebula</string>
<string name="search">Search</string>
</resources>

0 comments on commit a2f9cbd

Please sign in to comment.