Commit 40aa4cbe authored by Adrien Oliva's avatar Adrien Oliva

Merge branch '5-link-quest-achievement-and-character' into 'master'

Resolve "Link quest achievement and character"

Closes #5

See merge request !4
parents f672ee89 30e85f32
Pipeline #555 passed with stage
in 0 seconds
......@@ -31,13 +31,15 @@ import java.io.InputStream;
import java.io.OutputStream;
import fr.yapbreak.skyrimquest.data.DataHelper;
import fr.yapbreak.skyrimquest.quests.Quest;
import fr.yapbreak.skyrimquest.quests.SkyrimCursorAdapter;
import fr.yapbreak.skyrimquest.character.Character;
import fr.yapbreak.skyrimquest.quests.Status;
public class QuestList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String myPreferences = "SkyrimPreference";
public static final String myPreferences = "SkyrimPreference";
public static final String activeCharacter = "active_character";
private static final int CHARACTER_SWITCH = 1;
......@@ -55,6 +57,8 @@ public class QuestList extends AppCompatActivity
private NavigationView navigationView;
private SharedPreferences sharedPreferences;
SkyrimCursorAdapter adapter;
public static DataHelper getDataHandler()
{
return instance;
......@@ -120,21 +124,19 @@ public class QuestList extends AppCompatActivity
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
/*
* Set up navigation view
*/
ListView mainListView = findViewById(R.id.quests_listview);
/* *****************************************************************************************
Set up database
******************************************************************************************/
quest_data = new DataHelper(this);
quest_data.populate();
instance = quest_data;
SkyrimCursorAdapter adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
mainListView.setAdapter(adapter);
/* Set Quest list main view */
adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
mainListView.setAdapter(adapter);
}
@Override
......@@ -160,6 +162,7 @@ public class QuestList extends AppCompatActivity
editor.apply();
updateCharacterInfo();
adapter.notifyDataSetChanged();
}
break;
default:
......@@ -283,4 +286,19 @@ public class QuestList extends AppCompatActivity
updateCharacterInfo();
return true;
}
public void updateQuest(Quest quest) {
int character_id = sharedPreferences.getInt(activeCharacter, 0);
Character activeCharacter = quest_data.getActiveCharacter(character_id);
quest_data.achieve(activeCharacter, quest);
}
public void updateQuestStatus(Quest q) {
int character_id = sharedPreferences.getInt(activeCharacter, 0);
Character activeCharacter = quest_data.getActiveCharacter(character_id);
Status s = quest_data.queryQuestStatus(activeCharacter, q);
q.setStatus(s);
}
}
package fr.yapbreak.skyrimquest.character;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
......@@ -13,6 +14,7 @@ import android.widget.TextView;
import java.lang.reflect.Field;
import fr.yapbreak.skyrimquest.CharacterSwitch;
import fr.yapbreak.skyrimquest.QuestList;
import fr.yapbreak.skyrimquest.R;
public class CharacterCursorAdapter extends CursorAdapter {
......@@ -25,11 +27,17 @@ public class CharacterCursorAdapter extends CursorAdapter {
TextView level;
}
private int activeCharacterId;
private final CharacterSwitch m_parent;
public CharacterCursorAdapter(Context context, Cursor c, CharacterSwitch parent) {
super(context, c, true);
m_parent = parent;
SharedPreferences preferences = context.getSharedPreferences(QuestList.myPreferences, Context.MODE_PRIVATE);
activeCharacterId = preferences.getInt(QuestList.activeCharacter, -1);
}
@Override
......@@ -79,5 +87,9 @@ public class CharacterCursorAdapter extends CursorAdapter {
m_parent.setSelectedCharacter(vh.id);
}
});
if (activeCharacterId == viewHolder.id) {
view.setBackgroundColor(context.getResources().getColor(R.color.grayed, context.getTheme()));
}
}
}
......@@ -9,6 +9,8 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import fr.yapbreak.skyrimquest.character.Character;
import fr.yapbreak.skyrimquest.quests.Quest;
import fr.yapbreak.skyrimquest.quests.Status;
public class DataHelper extends SQLiteOpenHelper {
......@@ -275,6 +277,32 @@ public class DataHelper extends SQLiteOpenHelper {
return id;
}
public Status queryQuestStatus(Character active, Quest q)
{
SQLiteDatabase db = getReadableDatabase();
Status returnStatus;
String columns[] = { STATUS };
String selection[] = { Integer.toString(active.getId()), Integer.toString(q.getId()) };
Cursor c = db.query(ACHIEVEMENT_TABLE,
columns,
FK_CHARACTER_ID + " = ? AND " + FK_QUEST_ID + " = ?",
selection, null, null, null);
if (c.getCount() == 1) {
c.moveToFirst();
int index = c.getColumnIndex(STATUS);
index = c.getInt(index);
returnStatus = Status.values()[index];
} else {
returnStatus = Status.NOT_STARTED;
}
c.close();
return returnStatus;
}
public Cursor getAllQuests()
{
SQLiteDatabase db = getReadableDatabase();
......@@ -363,10 +391,6 @@ public class DataHelper extends SQLiteOpenHelper {
return getCharacterId(c);
}
/*public Character getActiveCharacter() {
return getActiveCharacter(0);
}*/
public Character getActiveCharacter(int active)
{
try {
......@@ -381,4 +405,65 @@ public class DataHelper extends SQLiteOpenHelper {
{
return getWritableDatabase().getPath();
}
private void updateAchievement(int qid, int cid, Status status)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FK_QUEST_ID, qid);
values.put(FK_CHARACTER_ID, cid);
values.put(STATUS, status.ordinal());
String where = FK_QUEST_ID + " = ? AND " + FK_CHARACTER_ID + " = ?";
String selection[] = {
Integer.toString(qid),
Integer.toString(cid)
};
db.update(ACHIEVEMENT_TABLE, values, where, selection);
}
private void insertAchievement(int qid, int cid, Status status)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FK_QUEST_ID, qid);
values.put(FK_CHARACTER_ID, cid);
values.put(STATUS, status.ordinal());
db.insert(ACHIEVEMENT_TABLE, null, values);
}
public void achieve(Character active, Quest quest)
{
int qid = quest.getId();
int cid = active.getId();
Status status = quest.getStatus();
SQLiteDatabase db = getReadableDatabase();
String column[] = {
FK_CHARACTER_ID,
FK_QUEST_ID
};
String values[] = { Integer.toString(cid), Integer.toString(qid) };
Cursor res = db.query(ACHIEVEMENT_TABLE, column, FK_CHARACTER_ID + " = ? AND " + FK_QUEST_ID + " = ?", values, null, null, null);
switch (res.getCount()) {
case 1:
// Need to update value
updateAchievement(qid, cid, status);
break;
case 0:
// Need to add value
insertAchievement(qid, cid, status);
break;
default:
Log.e("SKYRIM", "Too many return in achievement database");
}
res.close();
}
}
......@@ -3,12 +3,16 @@ package fr.yapbreak.skyrimquest.quests;
import android.view.View;
import android.widget.ImageView;
import fr.yapbreak.skyrimquest.QuestList;
class LongChangeStatusListener implements View.OnLongClickListener {
private final Quest quest;
private QuestList parent;
LongChangeStatusListener(Quest q)
LongChangeStatusListener(Quest q, QuestList p)
{
quest = q;
parent = p;
}
@Override
......@@ -29,6 +33,8 @@ class LongChangeStatusListener implements View.OnLongClickListener {
ImageView imageView = (ImageView) view;
imageView.setImageResource(quest.getStatus().getRes());
parent.updateQuest(quest);
return true;
}
}
......@@ -2,14 +2,27 @@ package fr.yapbreak.skyrimquest.quests;
public class Quest {
private int m_id;
private String m_title;
private Status m_status;
private String m_description;
public Quest(int id,
String title,
String desc,
Status status)
{
m_id = id;
m_title = title;
m_description = desc;
m_status = status;
}
public Quest(String title,
String desc,
Status status)
{
m_id = 0;
m_title = title;
m_description = desc;
m_status = status;
......@@ -17,6 +30,7 @@ public class Quest {
public Quest()
{
m_id = 0;
m_title = "";
m_description = "";
m_status = Status.NOT_STARTED;
......@@ -25,6 +39,17 @@ public class Quest {
public Quest(String title,
String desc)
{
m_id = 0;
m_title = title;
m_description = desc;
m_status = Status.NOT_STARTED;
}
public Quest(int id,
String title,
String desc)
{
m_id = id;
m_title = title;
m_description = desc;
m_status = Status.NOT_STARTED;
......@@ -32,6 +57,7 @@ public class Quest {
public Quest(String title)
{
m_id = 0;
m_title = title;
m_description = "";
m_status = Status.NOT_STARTED;
......@@ -49,6 +75,10 @@ public class Quest {
return m_status;
}
public int getId() {
return m_id;
}
public void setTitle(String title)
{
m_title = title;
......
......@@ -3,14 +3,17 @@ package fr.yapbreak.skyrimquest.quests;
import android.view.View;
import android.widget.ImageView;
import fr.yapbreak.skyrimquest.QuestList;
class QuickChangeStatusListener implements View.OnClickListener {
private final Quest myQuest;
private QuestList myParent;
public QuickChangeStatusListener(Quest quest) {
public QuickChangeStatusListener(Quest quest, QuestList parent) {
myQuest = quest;
myParent = parent;
}
@Override
......@@ -31,5 +34,6 @@ class QuickChangeStatusListener implements View.OnClickListener {
ImageView imageView = (ImageView) view;
imageView.setImageResource(myQuest.getStatus().getRes());
myParent.updateQuest(myQuest);
}
}
......@@ -12,18 +12,23 @@ import android.widget.TextView;
import java.lang.reflect.Field;
import fr.yapbreak.skyrimquest.QuestList;
import fr.yapbreak.skyrimquest.R;
public class SkyrimCursorAdapter extends CursorAdapter {
private static class ViewHolder {
TextView title;
TextView desc;
ImageView status;
int id;
TextView title;
TextView desc;
ImageView status;
}
public SkyrimCursorAdapter(Context context, Cursor c) {
super(context, c, true);
private QuestList parent;
public SkyrimCursorAdapter(QuestList p, Cursor c) {
super(p.getBaseContext(), c, true);
parent = p;
}
@Override
......@@ -61,11 +66,11 @@ public class SkyrimCursorAdapter extends CursorAdapter {
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));
Quest q = new Quest(cursor.getInt(0), title, desc);
parent.updateQuestStatus(q);
viewHolder.status.setImageResource(q.getStatus().getRes());
viewHolder.status.setOnClickListener(new QuickChangeStatusListener(q, parent));
viewHolder.status.setOnLongClickListener(new LongChangeStatusListener(q, parent));
}
}
......@@ -3,4 +3,5 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="grayed">#eeeeee</color>
</resources>
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