Commit 0790c83e authored by Adrien Oliva's avatar Adrien Oliva

feat(database) Use database to show quests

Switch ArrayAdapter to CursorAdapter to retrieve data directly from
database.
Populate witch data from Main Quest act 1

GTLAB #1Signed-off-by: Adrien Oliva's avatarAdrien Oliva <olivaa+gitlab@yapbreak.fr>
parent 763b968a
Pipeline #462 passed with stage
in 0 seconds
package fr.yapbreak.skyrimquest;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
......@@ -14,11 +11,8 @@ import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.ArrayList;
import fr.yapbreak.skyrimquest.data.DataHelper;
import fr.yapbreak.skyrimquest.quests.Adapter;
import fr.yapbreak.skyrimquest.quests.Quest;
import fr.yapbreak.skyrimquest.quests.SkyrimCursorAdapter;
public class QuestList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
......@@ -28,6 +22,9 @@ public class QuestList extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: remove this line when database is ready
this.deleteDatabase("quest_database");
quest_data = new DataHelper(this);
quest_data.populate();
......@@ -44,14 +41,10 @@ public class QuestList extends AppCompatActivity
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ArrayList<Quest> questlist = new ArrayList<>();
questlist.add(new Quest("Unbound", "Escape imperial custody and a dragon raid"));
questlist.add(new Quest("Before the storm", "Inform the Jarl of Whiterun"));
Adapter questAdapter = new Adapter(this, questlist);
SkyrimCursorAdapter adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
ListView lv = findViewById(R.id.quests_listview);
lv.setAdapter(questAdapter);navigationView.setNavigationItemSelectedListener(this);
lv.setAdapter(adapter);
}
@Override
......
......@@ -2,10 +2,28 @@ package fr.yapbreak.skyrimquest.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataHelper extends SQLiteOpenHelper {
public class DataNotFound extends Exception {
private String msg;
public DataNotFound(String message)
{
msg = message;
}
public String toString()
{
return "DataNotFound: " + msg;
}
}
private final static String DATABASE_NAME = "quest_database";
private final static int DATABASE_VERSION = 1;
......@@ -36,7 +54,7 @@ public class DataHelper extends SQLiteOpenHelper {
public final static String QUEST_CREATION =
"CREATE TABLE " + QUEST_TABLE
+ "( " + PK_QUEST_ID + " INTEGER PRIMARY KEY"
+ ", " + RESOURCE_ID_STRING + " TEXT NOT NULL"
+ ", " + RESOURCE_ID_STRING + " TEXT NOT NULL UNIQUE ON CONFLICT ROLLBACK"
+ ", " + FK_CATEGORY_ID + " INTEGER REFERENCES " + CATEGORY_TABLE + "(" + PK_CATEGORY_ID + ") ON DELETE SET NULL"
+ ")";
public final static String CHARACTER_CREATION =
......@@ -77,6 +95,20 @@ public class DataHelper extends SQLiteOpenHelper {
insert_cat("cat_main_quest_act_1");
insert_cat("cat_main_quest_act_2");
insert_cat("cat_main_quest_act_3");
// Insert Main Quest Act I
try {
int act1 = categoryGetId("cat_main_quest_act_1");
insert_quest("quest_unbound", act1);
insert_quest("quest_before_storm", act1);
insert_quest("quest_bleak_falls_barrow", act1);
insert_quest("quest_dragon_rising", act1);
insert_quest("quest_way_of_voice", act1);
insert_quest("quest_horn_windcaller", act1);
} catch (DataNotFound e) {
Log.e("SKYRIM", e.toString());
}
}
private void insert_cat(String resid)
......@@ -86,6 +118,48 @@ public class DataHelper extends SQLiteOpenHelper {
cat.put(RESOURCE_ID_STRING, resid);
db.insert(CATEGORY_TABLE, null, cat);
try {
db.insert(CATEGORY_TABLE, null, cat);
} catch (SQLiteConstraintException e) {
Log.d("SKYRIM", "Category " + resid + " already in database");
}
}
private void insert_quest(String resid, int category)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues quest = new ContentValues();
quest.put(RESOURCE_ID_STRING, resid);
quest.put(FK_CATEGORY_ID, category);
try {
db.insert(QUEST_TABLE, null, quest);
} catch (SQLiteConstraintException e) {
Log.d("SKYRIM", "Quest " + resid + " already in database");
}
}
private int categoryGetId(String resid) throws DataNotFound
{
SQLiteDatabase db = getReadableDatabase();
String column[] = { PK_CATEGORY_ID };
String values[] = { resid };
Cursor res = db.query(CATEGORY_TABLE, column, RESOURCE_ID_STRING + " = ?", values, null, null, null);
if (res.getCount() != 1) {
throw new DataNotFound("Category " + resid);
}
res.moveToFirst();
int id = res.getInt(0);
res.close();
return id;
}
public Cursor getAllQuests()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("SELECT " + PK_QUEST_ID + " AS _id, " + RESOURCE_ID_STRING + " FROM " + QUEST_TABLE + " ORDER BY _id", null);
}
}
package fr.yapbreak.skyrimquest.quests;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import fr.yapbreak.skyrimquest.R;
public class Adapter extends ArrayAdapter<Quest> {
private static class ViewHolder {
TextView title;
TextView desc;
ImageView status;
}
public Adapter(Context ctx, ArrayList<Quest> quests) {
super(ctx, 0, quests);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Quest quest = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.quest_fragment, parent, false);
viewHolder.title = convertView.findViewById(R.id.quest_title);
viewHolder.desc = convertView.findViewById(R.id.quest_summary);
viewHolder.status = convertView.findViewById(R.id.quest_img);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(quest.getTitle());
viewHolder.desc.setText(quest.getDescription());
viewHolder.status.setImageResource(quest.getStatus().getRes());
viewHolder.status.setOnClickListener(new QuickChangeStatusListener(quest));
viewHolder.status.setOnLongClickListener(new LongChangeStatusListener(quest));
return convertView;
}
}
package fr.yapbreak.skyrimquest.quests;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Field;
import fr.yapbreak.skyrimquest.R;
public class SkyrimCursorAdapter extends CursorAdapter {
private static class ViewHolder {
TextView title;
TextView desc;
ImageView status;
}
public SkyrimCursorAdapter(Context context, Cursor c) {
super(context, c, true);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(context);
View myView = inflater.inflate(R.layout.quest_fragment, viewGroup, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = myView.findViewById(R.id.quest_title);
viewHolder.desc = myView.findViewById(R.id.quest_summary);
viewHolder.status = myView.findViewById(R.id.quest_img);
myView.setTag(viewHolder);
return myView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
String resId = cursor.getString(1);
String title;
String desc;
try {
Field translatedTitle = R.string.class.getDeclaredField(resId);
Field translatedDesc = R.string.class.getDeclaredField(resId + "_desc");
title = context.getString(translatedTitle.getInt(translatedTitle));
desc = context.getString(translatedDesc.getInt(translatedDesc));
} catch (Exception e) {
title = resId;
desc = "";
}
viewHolder.title.setText(title);
viewHolder.desc.setText(desc);
Status s = Status.NOT_STARTED; //Status.values()[cursor.getInt(1)];
viewHolder.status.setImageResource(s.getRes());
Quest q = new Quest(title, desc, s);
viewHolder.status.setOnClickListener(new QuickChangeStatusListener(q));
viewHolder.status.setOnLongClickListener(new LongChangeStatusListener(q));
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Skyrim Quest</string>
<string name="action_character_switch">Changer</string>
<string name="quest_before_storm">Avant la tempête</string>
<string name="quest_before_storm_desc">Informer le Jarl de Blancherive de l’attaque du dragon à Helgen</string>
<string name="quest_bleak_falls_barrow">Tertre des chutes tourmentées</string>
<string name="quest_bleak_falls_barrow_desc">Récupérer la pierre de dragon</string>
<string name="quest_dragon_rising">L’élévation du dragon</string>
<string name="quest_dragon_rising_desc">Mener l’enquête sur un dragon apperçu à côté de Blancherive</string>
<string name="quest_horn_windcaller">La corne de Jurgen Parlevent</string>
<string name="quest_horn_windcaller_desc">Récupérer un artefact des Grises Barbes à Ustengrav.</string>
<string name="quest_img_alt">Statut de la quête</string>
<string name="quest_summary_default">Description de la quête</string>
<string name="quest_title_default">Titre de la quête</string>
<string name="quest_unbound">Libération</string>
<string name="quest_unbound_desc">Échapper à l’empire et à l’attaque d’un dragon</string>
<string name="quest_way_of_voice">L’art de la voix</string>
<string name="quest_way_of_voice_desc">Rencontrer les Grises Barbes</string>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="quest_unbound">Unbound</string>
<string name="quest_unbound_desc">Escape Imperial custody and a dragon raid.</string>
<string name="quest_before_storm">Before the Storm</string>
<string name="quest_before_storm_desc">Inform the Jarl of Whiterun about the dragon attack on Helgen.</string>
<string name="quest_bleak_falls_barrow">Bleak Falls Barrow</string>
<string name="quest_bleak_falls_barrow_desc">Retrieve the Dragonstone.</string>
<string name="quest_dragon_rising">Dragon Rising</string>
<string name="quest_dragon_rising_desc">Investigate a Dragon sighting near Whiterun.</string>
<string name="quest_way_of_voice">The Way of the Voice</string>
<string name="quest_way_of_voice_desc">Meet the Greybeards.</string>
<string name="quest_horn_windcaller">The Horn of Jurgen Windcaller</string>
<string name="quest_horn_windcaller_desc">Retrieve a Greybeard artifact from Ustengrav.</string>
</resources>
<resources>
<string name="app_name">Skyrim Quest</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">Android Studio</string>
<string name="nav_header_subtitle">android.studio@android.com</string>
<string name="nav_header_desc">Navigation header</string>
<string name="app_name" translatable="false">Skyrim Quest</string>
<string name="navigation_drawer_open" translatable="false">Open navigation drawer</string>
<string name="navigation_drawer_close" translatable="false">Close navigation drawer</string>
<string name="nav_header_title" translatable="false">Android Studio</string>
<string name="nav_header_subtitle" translatable="false">android.studio@android.com</string>
<string name="nav_header_desc" translatable="false">Navigation header</string>
<string name="action_character_switch">Switch</string>
<!-- Strings from quest fragment -->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment