Added navigation drawer and the option to set the current content screen, added settings.
lbs-gitea/maptology/master This commit looks good

This commit is contained in:
Sarah Theußen
2017-12-14 17:10:58 +01:00
parent 1235172db7
commit ffb9c99085
19 changed files with 467 additions and 49 deletions
@@ -0,0 +1,43 @@
package com.maptology.android.app;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* Customized ArrayAdapter to display menu options with icons.
*
* @author sarah
*/
public class CustomArrayAdapter extends ArrayAdapter<NavigationDataModel> {
Context mContext;
int layoutResourceId;
NavigationDataModel data[] = null;
public CustomArrayAdapter(Context mContext, int layoutResourceId, NavigationDataModel[] data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View listItem = inflater.inflate(layoutResourceId, parent, false);
android.widget.ImageView imageViewIcon = (android.widget.ImageView) listItem.findViewById(R.id.navViewIcon);
TextView textViewName = (TextView) listItem.findViewById(R.id.navViewName);
NavigationDataModel folder = data[position];
imageViewIcon.setImageResource(folder.icon);
textViewName.setText(folder.name);
return listItem;
}
}
@@ -1,13 +1,16 @@
package com.maptology.android.app;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import com.maptology.android.app.R;
import com.maptology.android.app.notice.NoticeFragment;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* Demonstrates how to draw the map.
@@ -22,10 +25,32 @@ public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
/**
* A reference to the current fragment.
* Names of all available fragments.
*/
private Fragment fragment;
private static final String MAP_FRAGMENT = "map_fragment";
private static final String SHARE_FRAGMENT = "share_fragment";
private static final String SETTINGS_FRAGMENT = "settings_fragment";
/**
* References to the fragments.
*/
private Fragment mapFragment;
private Fragment shareFragment;
private Fragment settingsFragment;
private Screen previousScreen;
/**
* Navigation Drawer menu.
*/
private NavigationDataModel[] drawerMenu = {new NavigationDataModel(R.drawable.ic_map_black_24dp, "Map"),
new NavigationDataModel(R.drawable.ic_share_black_24dp, "Share location"),
new NavigationDataModel(R.drawable.ic_settings_black_24dp, "Settings")};
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView drawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
/* Start the Service */
@@ -33,18 +58,135 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setTitle(R.string.application_short);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setContentView(R.layout.activity);
if (savedInstanceState != null) {
fragment = (MapFragment)getSupportFragmentManager().findFragmentByTag(TAG);
} else {
setScreen(Screen.MAP);
createNavigationDrawer();
}
private void createNavigationDrawer() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// toggle with burger icon
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
setTitle(R.string.application_short);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
setTitle(R.string.drawer_title);
}
};
drawerToggle.setDrawerIndicatorEnabled(true); // show burger icon
drawerToggle.syncState(); // show burger / back arrow
drawerLayout.addDrawerListener(drawerToggle);
// populate drawer menu
drawerList = (ListView) findViewById(R.id.navigation);
drawerList.setAdapter(new CustomArrayAdapter(this, R.layout.view_nav_item, drawerMenu));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
setScreen(Screen.MAP);
break;
case 1:
setScreen(Screen.SHARE);
break;
case 2:
setScreen(Screen.SETTINGS);
break;
default:
break;
}
drawerLayout.closeDrawer(drawerList);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// override method so toggling with burger icon is possible
switch (item.getItemId()) {
case android.R.id.home:
if (drawerLayout.isDrawerOpen(drawerList)) {
drawerLayout.closeDrawer(drawerList);
} else {
drawerLayout.openDrawer(drawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Method to set the current screen (content fragment).
*
* @param view screen to set
*/
public void setScreen(@NonNull Screen view) {
if (previousScreen != view) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new MapFragment();
//fragment = new NoticeFragment();
ft.add(R.id.container, fragment, TAG);
mapFragment = getSupportFragmentManager().findFragmentByTag(MAP_FRAGMENT);
shareFragment = getSupportFragmentManager().findFragmentByTag(SHARE_FRAGMENT);
settingsFragment = getSupportFragmentManager().findFragmentByTag(SETTINGS_FRAGMENT);
switch (view) {
case MAP:
if (mapFragment == null) {
mapFragment = new MapFragment();
ft.replace(R.id.container, mapFragment, TAG);
}
previousScreen = Screen.MAP;
break;
case SHARE:
if (shareFragment == null) {
shareFragment = new ShareFragment();
ft.replace(R.id.container, shareFragment, TAG);
}
previousScreen = Screen.SHARE;
break;
case SETTINGS:
if (settingsFragment == null) {
settingsFragment = new SettingsFragment();
ft.replace(R.id.container, settingsFragment, TAG);
}
previousScreen = Screen.SETTINGS;
break;
default:
// default to map
if (mapFragment == null) {
mapFragment = new MapFragment();
ft.replace(R.id.container, mapFragment, TAG);
}
previousScreen = Screen.MAP;
break;
}
ft.commit();
}
}
/**
* Available screens to display as content.
*/
private enum Screen {
MAP,
SHARE,
SETTINGS
}
}
@@ -0,0 +1,16 @@
package com.maptology.android.app;
/**
* Data model for the navigation items (necessary for custom ArrayAdapter).
*
* @author sarah
*/
public class NavigationDataModel {
public int icon;
public String name;
public NavigationDataModel(int icon, String name) {
this.icon = icon;
this.name = name;
}
}
@@ -0,0 +1,78 @@
package com.maptology.android.app;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* A fragment that displays the app settings and offers developer options.
*
* @author sarah
*/
public class SettingsFragment extends AbstractFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* Creates the view and initializes the references to the main elements.
*
* @param inflater The layout inflater.
* @param container The container.
* @param savedInstanceState The instance state.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
// set app version
String versionName = "";
try {
versionName = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
TextView versionText = (TextView) view.findViewById(R.id.app_version);
versionText.setText(versionName);
// set allowance and store it in shared preferences (via repository)
final CheckBox permissionCheck = (CheckBox) view.findViewById(R.id.permission_checkbox);
permissionCheck.setChecked(getRepository().getResearchAllowed(getContext()));
permissionCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean permissionGiven = permissionCheck.isChecked();
getRepository().setResearchAllowed(getContext(), permissionGiven);
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
@@ -1,6 +1,7 @@
package com.maptology.android.app.repository;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
/**
@@ -13,37 +14,61 @@ import android.location.Location;
*/
public class Repository {
/**
* This value indicates whether the notices are currently refreshed.
* It is set by the NoticeSyncTask and used to prevent duplicate
* simultaneous syncing. The NoticeFragment uses this value to show
* the refresh animation in the SwipeRefeshView.
*/
public final Value<Boolean> loadingNotices = new Value<>(this, Boolean.FALSE);
/**
* This value indicates whether the notices are currently refreshed.
* It is set by the NoticeSyncTask and used to prevent duplicate
* simultaneous syncing. The NoticeFragment uses this value to show
* the refresh animation in the SwipeRefeshView.
*/
public final Value<Boolean> loadingNotices = new Value<>(this, Boolean.FALSE);
public final ActiveValue<Location> gpsPosition = new LocationValue(this, null);
/**
* The context.
*/
protected Context context;
/**
* Tag to get and store the user has allowed that his positions can be used for research in shared preferences.
*/
private String researchAllowedTag = "RESEARCH_ALLOWED";
public final ActiveValue<Location> gpsPosition = new LocationValue(this, null);
/**
* Creates a new repository with the specified context.
*
* @param context The context.
*/
public Repository(Context context) {
this.context = context;
}
/**
* The context.
*/
protected Context context;
/**
* Returns the repository's context.
*
* @return The context.
*/
public Context getContext() {
return context;
}
/**
* Creates a new repository with the specified context.
*
* @param context The context.
*/
public Repository(Context context) {
this.context = context;
}
/**
* Sets whether the user has allowed that his positions can be used for research.
*
* @param context context
* @param allowed allowance
*/
public void setResearchAllowed(Context context, boolean allowed) {
SharedPreferences preferences = context.getSharedPreferences(researchAllowedTag, Context.MODE_PRIVATE);
preferences.edit().putBoolean(researchAllowedTag, allowed).apply();
}
/**
* Returns the repository's context.
*
* @return The context.
*/
public Context getContext() {
return context;
}
/**
* Get whether the user has allowed that his positions can be used for research.
*
* @param context context
* @return allowance
*/
public boolean getResearchAllowed(Context context) {
SharedPreferences preferences = context.getSharedPreferences(researchAllowedTag, Context.MODE_PRIVATE);
return preferences.getBoolean(researchAllowedTag, false);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 310 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 322 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 827 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

+28 -7
View File
@@ -1,9 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:orientation="vertical" >
</LinearLayout>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/coordinator">
<android.support.v4.widget.DrawerLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:orientation="vertical">
</FrameLayout>
<ListView
android:id="@+id/navigation"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/app_version_desc"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:text="@string/app_version_desc"/>
<TextView
android:id="@+id/app_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_toRightOf="@id/app_version_desc"
android:text="version"/>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/app_version_desc"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/app_permission_desc"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@id/divider"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:text="@string/app_permission_desc"/>
<CheckBox
android:id="@+id/permission_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/divider"
android:layout_toRightOf="@id/app_permission_desc"
android:checked="false"/>
</RelativeLayout>
</LinearLayout>
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="16dp">
<ImageView
android:id="@+id/navViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/navViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/navViewIcon"
android:textColor="@color/primary_text_default_material_light"
android:textSize="@dimen/abc_text_size_medium_material"
android:text="Item Name"/>
</RelativeLayout>
</RelativeLayout>
+10 -1
View File
@@ -18,4 +18,13 @@
<!-- Title of the spawned Notification -->
<string name="task_notification_title">Maptology</string>
</resources>
<!-- Navigation Drawer -->
<string name="drawer_open">Open navigation</string>
<string name="drawer_close">Close navigation</string>
<string name="drawer_title">Navigation</string>
<!-- Settings Page -->
<string name="app_version_desc">App Version</string>
<string name="app_permission_desc">Do you want to share your location anonymously for research?</string>
</resources>