Skip to content

Commit

Permalink
Integrate database in FamilyTreeActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
teinvdlugt committed Feb 24, 2016
1 parent e47322c commit 92f9f3f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,89 @@
package com.teinvdlugt.android.greekgods;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.teinvdlugt.android.greekgods.models.Person;

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

public class FamilyTreeActivity extends AppCompatActivity {

FamilyTreeLayout treeLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_family_tree);

FamilyTreeLayout treeLayout = (FamilyTreeLayout) findViewById(R.id.family_tree_layout);
Person person = new Person();
person.setName("Zeus");
person.setShortDescription("Oppergod van Olympus");
Person parent1 = new Person();
parent1.setName("Rhea");
parent1.setShortDescription("Titaan");
Person parent2 = new Person();
parent2.setName("Kronos");
parent2.setShortDescription("Titaan");
person.setParent1(parent1);
person.setParent2(parent2);
treeLayout.setPerson(person);
treeLayout = (FamilyTreeLayout) findViewById(R.id.family_tree_layout);
loadPerson(64);
}

@SuppressLint("DefaultLocale")
private void loadPerson(final int personId) {
new AsyncTask<Void, Void, Person>() {
@Override
protected Person doInBackground(Void... params) {
Person person = getBasicPersonData(personId);
List<Integer> parentIds = getParentIds(personId);
List<Person> parents = new ArrayList<>();
for (int parentId : parentIds) {
parents.add(getBasicPersonData(parentId));
}
person.setParents(parents);
return person;
}

@Deprecated
private List<Integer> getParentIds(int personId) {
List<Integer> result = new ArrayList<>();
Cursor c = null;

try {
SQLiteDatabase db = openOrCreateDatabase("data", 0, null);
String query = String.format(DBUtils.PARENTS_QUERY, personId);
c = db.rawQuery(query, null);
c.moveToFirst();
do {
result.add(c.getInt(c.getColumnIndex("personId")));
} while (c.moveToNext());
} catch (CursorIndexOutOfBoundsException ignored) {
} finally { if (c != null) c.close(); }

return result;
}

private Person getBasicPersonData(int id) {
Person person = new Person(id);
person.setId(id);
Cursor c = null;
try {
SQLiteDatabase db = openOrCreateDatabase("data", 0, null);
String[] columns = {"name", "shortDescription"};
String[] selectionArgs = {String.valueOf(id)};
c = db.query("people", columns, "personId=?", selectionArgs, null, null, null);
c.moveToFirst();
person.setName(c.getString(c.getColumnIndex("name")));
person.setShortDescription(c.getString(c.getColumnIndex("shortDescription")));
} finally {
if (c != null) c.close();
}

return person;
}

@Override
protected void onPostExecute(Person person) {
treeLayout.setPerson(person);
}
}.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@
public class FamilyTreeLayout extends LinearLayout {

private Person person;
private FamilyTreeNode personView, parent1View, parent2View;

public void setPerson(Person person) {
this.person = person;
removeAllViews();
personView = new FamilyTreeNode(getContext());
parent1View = new FamilyTreeNode(getContext());
parent2View = new FamilyTreeNode(getContext());
FamilyTreeNode personView = new FamilyTreeNode(getContext());
personView.setPerson(person);
parent1View.setPerson(person.getParent1());
parent2View.setPerson(person.getParent2());

LayoutParams wrapParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams marginParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
marginParams.bottomMargin = marginParams.topMargin = marginParams.leftMargin = marginParams.rightMargin = 16;

LinearLayout parentsLayout = new LinearLayout(getContext());
parentsLayout.addView(parent1View, marginParams);
parentsLayout.addView(parent2View, marginParams);

for (Person parent : person.getParents()) {
FamilyTreeNode parentView = new FamilyTreeNode(getContext());
parentView.setPerson(parent);
parentsLayout.addView(parentView, marginParams);
}

addView(parentsLayout, wrapParams);
addView(personView, marginParams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class Person {
private String name;
private String description, shortDescription;
private List<Relation> relations;
private Person parent1, parent2;
private List<Person> children;
private List<Person> parents;
private int id;

public Person(String name, List<Relation> relations) {
Expand All @@ -33,6 +34,10 @@ public Person(String name, List<Relation> relations) {
public Person() {
}

public Person(int id) {
this.id = id;
}

public int getId() {
return id;
}
Expand Down Expand Up @@ -65,20 +70,20 @@ public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}

public Person getParent1() {
return parent1;
public List<Person> getParents() {
return parents;
}

public void setParent1(Person parent1) {
this.parent1 = parent1;
public void setParents(List<Person> parents) {
this.parents = parents;
}

public Person getParent2() {
return parent2;
public List<Person> getChildren() {
return children;
}

public void setParent2(Person parent2) {
this.parent2 = parent2;
public void setChildren(List<Person> children) {
this.children = children;
}

public List<Relation> getRelations() {
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_family_tree.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FamilyTreeActivity">

<com.teinvdlugt.android.greekgods.FamilyTreeLayout
android:id="@+id/family_tree_layout"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</FrameLayout>
android:layout_gravity="center_vertical|center_horizontal" />
</HorizontalScrollView>

0 comments on commit 92f9f3f

Please sign in to comment.