Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Study Cards #495

Merged
merged 8 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ android {
}
}
}
dataBinding {
enabled = true
}
}

def androidSupportVersion = '27.0.2'
Expand Down Expand Up @@ -104,6 +107,7 @@ dependencies {
annotationProcessor "android.arch.lifecycle:compiler:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"
implementation "com.google.android.gms:play-services-location:$firebaseVersion"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'openinghours

//DB Debugging: adb forward tcp:8080 tcp:8080 - look for DebugDB in logcat
debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'
Expand Down
31 changes: 30 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,35 @@
android:launchMode="singleTop"
android:parentActivityName=".activities.FeedbackActivity">
</activity>
<activity
android:name=".activities.CardsActivity"
android:label="@string/title_activity_cards"
android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.tum.in.tumcampusapp.activities.MainActivity" />
</activity>

<activity
android:name=".activities.CardsDetailActivity"
android:label="@string/title_activity_cards_detail"
android:parentActivityName=".activities.CardsActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.tum.in.tumcampusapp.activities.CardsActivity" />
</activity>

<activity
android:name=".activities.CardsQuizActivity"
android:label="@string/title_activity_cards_quiz"
android:parentActivityName=".activities.CardsActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.tum.in.tumcampusapp.activities.CardsActivity" />
</activity>

<!-- receiver for the mensaWidget -->
<receiver
Expand Down Expand Up @@ -575,7 +604,7 @@

<receiver
android:name=".widgets.TimetableWidget"
android:label="@string/timetable_widget">>
android:label="@string/timetable_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
Expand Down
106 changes: 106 additions & 0 deletions app/src/main/java/de/tum/in/tumcampusapp/activities/CardsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package de.tum.in.tumcampusapp.activities;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.tum.in.tumcampusapp.R;
import de.tum.in.tumcampusapp.activities.generic.ActivityForLoadingInBackground;
import de.tum.in.tumcampusapp.adapters.NoResultsAdapter;
import de.tum.in.tumcampusapp.adapters.StudyCardListAdapter;
import de.tum.in.tumcampusapp.api.TUMCabeClient;
import de.tum.in.tumcampusapp.auxiliary.Utils;
import de.tum.in.tumcampusapp.databinding.ActivityCardsBinding;
import de.tum.in.tumcampusapp.models.cards.CardActivityState;
import de.tum.in.tumcampusapp.models.cards.StudyCard;

public class CardsActivity extends ActivityForLoadingInBackground<Void, List<StudyCard>> implements AdapterView.OnItemClickListener {

ActivityCardsBinding binding;
CardActivityState state;

public CardsActivity() {
super(R.layout.activity_cards);
this.state = new CardActivityState();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void setUpLayout() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_cards);
binding.setState(this.state);
binding.cardList.setOnItemClickListener(this);
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(binding.fab.getContext(), CardsQuizActivity.class);
startActivity(intent);
}
});
}

@Override
protected void onResume() {
super.onResume();
startLoading();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_activity_cards, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.create_card:
Intent intent = new Intent(this, CardsDetailActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
protected List<StudyCard> onLoadInBackground(Void... arg) {
List<StudyCard> cards = new ArrayList<StudyCard>();
try {
cards = TUMCabeClient.getInstance(this).getStudyCards();
} catch (IOException e) {
Utils.log(e);
}
return cards;
}

@Override
protected void onLoadFinished(List<StudyCard> cards) {
showLoadingEnded();
if (cards.size() == 0) {
binding.cardList.setAdapter(new NoResultsAdapter(this));
} else {
StudyCardListAdapter adapter = new StudyCardListAdapter(this, cards);
binding.cardList.setAdapter(adapter);
}
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO open edit/view
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package de.tum.in.tumcampusapp.activities;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import java.io.IOException;

import de.tum.in.tumcampusapp.R;
import de.tum.in.tumcampusapp.activities.generic.ActivityForLoadingInBackground;
import de.tum.in.tumcampusapp.api.TUMCabeClient;
import de.tum.in.tumcampusapp.auxiliary.Const;
import de.tum.in.tumcampusapp.auxiliary.Utils;
import de.tum.in.tumcampusapp.databinding.ActivityCardsDetailBinding;
import de.tum.in.tumcampusapp.exceptions.NoPrivateKey;
import de.tum.in.tumcampusapp.models.cards.StudyCard;
import de.tum.in.tumcampusapp.models.tumcabe.ChatMember;
import de.tum.in.tumcampusapp.models.tumcabe.ChatVerification;

public class CardsDetailActivity extends ActivityForLoadingInBackground<Void, StudyCard> {
StudyCard card;

public CardsDetailActivity() {
super(R.layout.activity_cards_detail);
this.card = new StudyCard();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// TODO handle different states (add/view/edit)
setTitle(this.getResources().getString(R.string.title_activity_cards_detail));
}

@Override
public void setUpLayout() {
ActivityCardsDetailBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_cards_detail);
binding.setCard(card);
}

@Override
public void setUpToolbar() {
super.setUpToolbar();
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_activity_cards_detail, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.save_card:
startLoading();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
protected StudyCard onLoadInBackground(Void... arg) {
if (!card.is_valid()) {
return null;
}
try {
ChatMember chatMember = Utils.getSetting(this, Const.CHAT_MEMBER, ChatMember.class);
final ChatVerification v = ChatVerification.Companion.getChatVerification(this.getApplicationContext(), chatMember);
final Context c = this;
return TUMCabeClient.getInstance(c).addStudyCard(card, v);
} catch (IOException e) {
Utils.log(e);
} catch (NoPrivateKey e) {
Utils.log(e);
}
return null;
}

@Override
protected void onLoadFinished(StudyCard card) {
if (card != null) {
finish();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package de.tum.in.tumcampusapp.activities;

import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.tum.in.tumcampusapp.R;
import de.tum.in.tumcampusapp.activities.generic.ActivityForLoadingInBackground;
import de.tum.in.tumcampusapp.api.TUMCabeClient;
import de.tum.in.tumcampusapp.auxiliary.Utils;
import de.tum.in.tumcampusapp.databinding.ActivityCardsQuizBinding;
import de.tum.in.tumcampusapp.models.cards.StudyCard;

public class CardsQuizActivity extends ActivityForLoadingInBackground<Void, List<StudyCard>> {
ActivityCardsQuizBinding binding;
List<StudyCard> cards;
int nextCard;
public final ObservableField<Boolean> isShowingAnswer = new ObservableField<>(false);

public CardsQuizActivity() {
super(R.layout.activity_cards_quiz);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startLoading();
}

@Override
public void setUpLayout() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_cards_quiz);
binding.setHandler(this);
}

@Override
public void setUpToolbar() {
super.setUpToolbar();
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear);
}
}

@Override
protected List<StudyCard> onLoadInBackground(Void... arg) {
List<StudyCard> cards = new ArrayList<StudyCard>();
try {
cards = TUMCabeClient.getInstance(this).getStudyCards();
} catch (IOException e) {
Utils.log(e);
}
return cards;
}

@Override
protected void onLoadFinished(List<StudyCard> cards) {
showLoadingEnded();
if (cards.size() == 0) {
finish();
Toast toast = Toast.makeText(this, "No Cards in Quiz", Toast.LENGTH_SHORT);
toast.show();
} else {
this.cards = cards;
this.nextCard = 0;
setUpNextCard();
}
}

public void setUpNextCard() {
if (this.nextCard >= this.cards.size()) {
finish();
Toast toast = Toast.makeText(this, "Finished Quiz", Toast.LENGTH_SHORT);
toast.show();
} else {
this.setTitle((this.nextCard + 1) + "/" + this.cards.size() + ": " + this.cards.get(this.nextCard).getTitle());
this.isShowingAnswer.set(false);
binding.setCard(this.cards.get(this.nextCard));
this.nextCard++;
}
}

public void showAnswer() {
this.isShowingAnswer.set(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Loading