use of de.tum.in.tumcampusapp.database.TcaDb in project TumCampusApp by TCA-Team.
the class ChatActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
this.getIntentData();
TcaDb tcaDb = TcaDb.getInstance(this);
ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
localRepository.setDb(tcaDb);
chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
this.bindUIElements();
}
use of de.tum.in.tumcampusapp.database.TcaDb in project TumCampusApp by TCA-Team.
the class CafeteriaDetailsSectionFragment method showMenu.
/**
* Inflates the cafeteria menu layout.
* This is put into an extra static method to be able to
* reuse it in {@link CafeteriaMenuCard}
*
* @param rootView Parent layout
* @param cafeteriaId Cafeteria id
* @param dateStr Date in yyyy-mm-dd format
* @param big True to show big lines
*/
@SuppressLint("ShowToast")
public static List<View> showMenu(LinearLayout rootView, int cafeteriaId, String dateStr, boolean big, List<CafeteriaMenu> cafeteriaMenus) {
// initialize a few often used things
final Context context = rootView.getContext();
final Map<String, String> rolePrices = CafeteriaPrices.INSTANCE.getRolePrices(context);
final int padding = (int) context.getResources().getDimension(R.dimen.card_text_padding);
List<View> addedViews = new ArrayList<>(32);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TcaDb db = TcaDb.getInstance(context);
final FavoriteDishDao favoriteDishDao = db.favoriteDishDao();
TextView textview;
if (!big) {
// Show opening hours
OpenHoursHelper lm = new OpenHoursHelper(context);
textview = new TextView(context);
textview.setText(lm.getHoursByIdAsString(context, cafeteriaId, DateUtils.getDate(dateStr)));
textview.setTextColor(ContextCompat.getColor(context, R.color.sections_green));
rootView.addView(textview);
addedViews.add(textview);
}
// Show cafeteria menu
String curShort = "";
for (CafeteriaMenu cafeteriaMenu : cafeteriaMenus) {
String typeShort = cafeteriaMenu.getTypeShort();
String typeLong = cafeteriaMenu.getTypeLong();
// Skip unchecked categories if showing card
boolean shouldShow = Utils.getSettingBool(context, "card_cafeteria_" + typeShort, "tg".equals(typeShort) || "ae".equals(typeShort));
if (!big && !shouldShow) {
continue;
}
// Add header if we start with a new category
if (!typeShort.equals(curShort)) {
curShort = typeShort;
View view = inflater.inflate(big ? R.layout.list_header_big : R.layout.card_list_header, rootView, false);
textview = view.findViewById(R.id.list_header);
textview.setText(typeLong.replaceAll("[0-9]", "").trim());
rootView.addView(view);
addedViews.add(view);
}
// Show menu item
String menuName = cafeteriaMenu.getName();
final SpannableString text = menuToSpan(context, big ? menuName : prepare(menuName));
if (rolePrices.containsKey(typeLong)) {
// If price is available
View view = inflater.inflate(big ? R.layout.price_line_big : R.layout.card_price_line, rootView, false);
textview = view.findViewById(R.id.line_name);
TextView priceView = view.findViewById(R.id.line_price);
final View favDish = view.findViewById(R.id.favoriteDish);
favDish.setTag(menuName + "__" + cafeteriaId);
/*
* saved dish id in the favoriteDishButton tag.
* onButton checked getTag->DishID and mark it as favorite locally (favorite=1)
*/
textview.setText(text);
priceView.setText(String.format("%s €", rolePrices.get(typeLong)));
rootView.addView(view);
addedViews.add(view);
Object tag = favDish.getTag();
List<FavoriteDish> isFavourite = favoriteDishDao.checkIfFavoriteDish(tag.toString());
favDish.setSelected(!isFavourite.isEmpty());
favDish.setOnClickListener(view1 -> {
String id = view1.getTag().toString();
String[] data = id.split("__");
String dishName = data[0];
int mensaId = Integer.parseInt(data[1]);
if (!view1.isSelected()) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy");
String currentDate = DateTime.now().toString(formatter);
favoriteDishDao.insertFavouriteDish(FavoriteDish.Companion.create(mensaId, dishName, currentDate, tag.toString()));
view1.setSelected(true);
} else {
favoriteDishDao.deleteFavoriteDish(mensaId, dishName);
view1.setSelected(false);
}
});
} else {
// Without price
textview = new TextView(context);
textview.setText(text);
textview.setPadding(padding, padding, padding, padding);
rootView.addView(textview);
addedViews.add(textview);
}
}
return addedViews;
}
use of de.tum.in.tumcampusapp.database.TcaDb in project TumCampusApp by TCA-Team.
the class SendMessageService method onHandleWork.
@Override
protected void onHandleWork(@NonNull Intent intent) {
TcaDb tcaDb = TcaDb.getInstance(this);
final CompositeDisposable mDisposable = new CompositeDisposable();
ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
localRepository.setDb(tcaDb);
ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
chatMessageViewModel.deleteOldEntries();
// Get all unsent messages from database
List<ChatMessage> unsentMsg = chatMessageViewModel.getUnsent();
if (unsentMsg.isEmpty()) {
return;
}
int numberOfAttempts = 0;
AuthenticationManager am = new AuthenticationManager(this);
// Try to send the message 5 times
while (numberOfAttempts < MAX_SEND_TRIES) {
try {
for (ChatMessage message : unsentMsg) {
// Generate signature and store it in the message
message.setSignature(am.sign(message.getText()));
// Send the message to the server
chatMessageViewModel.sendMessage(message.getRoom(), message, this.getApplicationContext());
Utils.logv("successfully sent message: " + message.getText());
}
// Exit the loop
return;
} catch (NoPrivateKey noPrivateKey) {
// Nothing can be done, just exit
return;
} catch (Exception e) {
Utils.log(e);
numberOfAttempts++;
}
// Sleep for five seconds, maybe the server is currently really busy
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Utils.log(e);
}
}
}
Aggregations