hxMac 1 month ago
parent
commit
81317c3f91

+ 11 - 4
webandroid/app/src/main/java/com/example/webapplication/ui/notifications/NotificationsFragment.java

@@ -4,12 +4,12 @@ import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.TextView;
 
 import androidx.annotation.NonNull;
 import androidx.fragment.app.Fragment;
 import androidx.lifecycle.ViewModelProvider;
 
+import com.blankj.utilcode.util.TimeUtils;
 import com.example.webapplication.databinding.FragmentNotificationsBinding;
 
 public class NotificationsFragment extends Fragment {
@@ -20,12 +20,19 @@ public class NotificationsFragment extends Fragment {
                              ViewGroup container, Bundle savedInstanceState) {
         NotificationsViewModel notificationsViewModel =
                 new ViewModelProvider(this).get(NotificationsViewModel.class);
-
+        notificationsViewModel.networkListen();
         binding = FragmentNotificationsBinding.inflate(inflater, container, false);
         View root = binding.getRoot();
 
-        final TextView textView = binding.textNotifications;
-        notificationsViewModel.get().observe(getViewLifecycleOwner(), textView::setText);
+        notificationsViewModel.getSuccess().observe(getViewLifecycleOwner(), aBoolean -> {
+            String time = TimeUtils.date2String(TimeUtils.string2Date(TimeUtils.getNowString())) + " : ";
+            if (aBoolean) {
+                binding.textNotifications.append(time + "访问Tg接口,网络正常\n");
+            } else {
+                binding.textNotifications.append(time + notificationsViewModel.getError().getValue() + "\n");
+
+            }
+        });
         return root;
     }
 

+ 17 - 19
webandroid/app/src/main/java/com/example/webapplication/ui/notifications/NotificationsViewModel.java

@@ -3,12 +3,9 @@ package com.example.webapplication.ui.notifications;
 import android.annotation.SuppressLint;
 import android.util.Log;
 
-import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModel;
 
-import com.blankj.utilcode.util.SPUtils;
-import com.blankj.utilcode.util.StringUtils;
 import com.blankj.utilcode.util.ThreadUtils;
 import com.example.webapplication.util.TgUtils;
 
@@ -16,19 +13,14 @@ import java.util.concurrent.TimeUnit;
 
 public class NotificationsViewModel extends ViewModel {
 
-    private final MutableLiveData<String> mUrl = new MutableLiveData<>();
+    private final MutableLiveData<Boolean> success = new MutableLiveData<>();
 
-    public NotificationsViewModel() {
+    public MutableLiveData<String> getError() {
+        return error;
     }
 
+    private final MutableLiveData<String> error = new MutableLiveData<>();
 
-    public LiveData<String> getUrl() {
-        return mUrl;
-    }
-
-    public void getMsg() {
-        networkListen();
-    }
 
     public void networkListen() {
         ThreadUtils.executeBySingleAtFixRate(new ThreadUtils.Task<String>() {
@@ -40,24 +32,30 @@ public class NotificationsViewModel extends ViewModel {
             @SuppressLint("SetTextI18n")
             @Override
             public void onSuccess(String result) {
-                if (!StringUtils.isEmpty(result)) {
-                    SPUtils.getInstance().put("Message", result, true);
-                    String url = SPUtils.getInstance().getString("Message");
-                    mUrl.postValue(url);
-                }
+                Log.d("hzshkj", "[NotificationsViewModel] onSuccess: "+result);
+                error.postValue(result);
+                success.postValue(true);
             }
 
             @Override
             public void onFail(Throwable t) {
-                Log.e("hzshkj", "onFail: ", t);
+                Log.d("hzshkj", "[NotificationsViewModel] onFail: "+t.getMessage());
+                success.postValue(false);
+                error.postValue(t.getMessage());
                 networkListen();
             }
 
             @Override
             public void onCancel() {
+                success.postValue(false);
+                error.postValue("Task is cancel.");
                 networkListen();
             }
-        }, 5000, 5000, TimeUnit.MILLISECONDS);
+        }, 100, 10000, TimeUnit.MILLISECONDS);
+
+    }
 
+    public MutableLiveData<Boolean> getSuccess() {
+        return success;
     }
 }

+ 0 - 2
webandroid/app/src/main/java/com/example/webapplication/util/TgUtils.java

@@ -73,13 +73,11 @@ public class TgUtils {
         Response response = client.newCall(request).execute();
         if (response.isSuccessful() && response.body() != null) {
             String jsonResponse = response.body().string();
-            Log.d("hzshkj", "[TgUtils] Response: " + jsonResponse);
             JSONObject jsonObject = new JSONObject(jsonResponse);
             JSONArray resultArray = jsonObject.getJSONArray("result");
             for (int i = 0; i < resultArray.length(); i++) {
                 JSONObject message = resultArray.getJSONObject(i).getJSONObject("message");
                 String text = message.getString("text");
-                // 使用 SharedPreferences 保存数据
                 if (StringUtils.equals(text, SPUtils.getInstance().getString("Message"))) {
                     return "";
                 } else {

+ 1 - 1
webandroid/app/src/main/res/layout/fragment_notifications.xml

@@ -21,7 +21,7 @@
             android:layout_height="wrap_content"
             android:textAlignment="textStart"
             android:textColor="#2EFF00"
-            android:textSize="20sp"
+            android:textSize="13sp"
             tools:text="111111" />
     </ScrollView>