Commit 8a29caf5 authored by Adrien Oliva's avatar Adrien Oliva

Merge branch '1-database-setup' into 'master'

Resolve "Database setup"

Closes #1

See merge request !1
parents 3bf5931b 0790c83e
Pipeline #464 passed with stage
in 0 seconds
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
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,17 +11,23 @@ import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.ArrayList;
import fr.yapbreak.skyrimquest.quests.Adapter;
import fr.yapbreak.skyrimquest.quests.Quest;
import fr.yapbreak.skyrimquest.data.DataHelper;
import fr.yapbreak.skyrimquest.quests.SkyrimCursorAdapter;
public class QuestList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DataHelper quest_data;
@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();
setContentView(R.layout.activity_quest_list);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
......@@ -38,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
......
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;
public final static String QUEST_TABLE = "tbl_quest";
public final static String CATEGORY_TABLE = "tbl_category";
public final static String CHARACTER_TABLE = "tbl_character";
public final static String ACHIEVEMENT_TABLE = "tbl_achievement";
public final static String PK_CATEGORY_ID = "pk_category_id";
public final static String RESOURCE_ID_STRING = "resource_id_string";
public final static String PK_QUEST_ID = "pk_quest_id";
public final static String FK_CATEGORY_ID = "fk_category_id";
public final static String PK_CHARACTER_ID = "pk_character_id";
public final static String NAME = "name";
public final static String PK_ACHIEVEMENT_ID = "pk_achievement_id";
public final static String FK_QUEST_ID = "fk_quest_id";
public final static String FK_CHARACTER_ID = "fk_character_id";
public final static String STATUS = "status";
public final static String CATEGORY_CREATION =
"CREATE TABLE " + CATEGORY_TABLE
+ "( " + PK_CATEGORY_ID + " INTEGER PRIMARY KEY"
+ ", " + RESOURCE_ID_STRING + " TEXT NOT NULL UNIQUE ON CONFLICT ROLLBACK"
+ ")";
public final static String QUEST_CREATION =
"CREATE TABLE " + QUEST_TABLE
+ "( " + PK_QUEST_ID + " INTEGER PRIMARY KEY"
+ ", " + 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 =
"CREATE TABLE " + CHARACTER_TABLE
+ "( " + PK_CHARACTER_ID + " INTEGER PRIMARY KEY"
+ ", " + NAME + " TEXT NOT NULL"
+ ")";
public final static String ACHIEVEMENT_CREATION =
"CREATE TABLE " + ACHIEVEMENT_TABLE
+ "( " + PK_ACHIEVEMENT_ID + " INTEGER PRIMARY KEY"
+ ", " + FK_QUEST_ID + " INTEGER REFERENCES " + QUEST_TABLE + "(" + PK_QUEST_ID + ") ON DELETE SET NULL"
+ ", " + FK_CHARACTER_ID + " INTEGER REFERENCES " + CHARACTER_TABLE + "(" + PK_CHARACTER_ID + ") ON DELETE SET NULL"
+ ", " + STATUS + " INTEGER"
+ ")";
public DataHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
sqLiteDatabase.execSQL(CATEGORY_CREATION);
sqLiteDatabase.execSQL(QUEST_CREATION);
sqLiteDatabase.execSQL(CHARACTER_CREATION);
sqLiteDatabase.execSQL(ACHIEVEMENT_CREATION);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
{
// TODO: When database scheme needs it!
}
public void populate()
{
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)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cat = new ContentValues();
cat.put(RESOURCE_ID_STRING, resid);
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