diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java new file mode 100644 index 0000000..3f0ca2c --- /dev/null +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java @@ -0,0 +1,33 @@ +package com.example.tampopo_client.viewmodels; + +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; + +public class ChatViewModel extends RealTimeViewModel { + private final List notificationListeners = new ArrayList<>(); + + private static final double NOTIFICATION_RECEIVE_PROBABILITY = 0.01; + + @Override + public Runnable onUpdate() { + return () -> { + // 1% の確率で onNotificationReceived() が呼び出される + double borderValue = Math.floor(Math.random() * 100); + double currentValue = NOTIFICATION_RECEIVE_PROBABILITY * 100; + if (currentValue >= borderValue) { + Log.d("ChatViewModel", "Received test notification."); + notificationListeners.forEach(listener -> listener.onNotificationReceived()); + } + }; + } + + public void addNotificationListener(NotificationListener listener) { + notificationListeners.add(listener); + } + + public void clearNotificationListener() { + notificationListeners.clear(); + } +}