|
1 | 1 | package org.shirakawatyu.swust.utils; |
2 | 2 |
|
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | + |
| 6 | +import androidx.annotation.NonNull; |
| 7 | + |
| 8 | +import com.alibaba.fastjson2.JSON; |
| 9 | +import com.alibaba.fastjson2.JSONObject; |
| 10 | + |
| 11 | +import java.io.IOException; |
3 | 12 | import java.text.ParseException; |
4 | 13 | import java.text.SimpleDateFormat; |
5 | 14 | import java.util.Calendar; |
6 | 15 | import java.util.Date; |
7 | 16 | import java.util.TimeZone; |
| 17 | +import java.util.logging.Level; |
| 18 | +import java.util.logging.Logger; |
| 19 | + |
| 20 | +import okhttp3.Call; |
| 21 | +import okhttp3.Callback; |
| 22 | +import okhttp3.OkHttpClient; |
| 23 | +import okhttp3.Request; |
| 24 | +import okhttp3.Response; |
| 25 | + |
8 | 26 |
|
9 | 27 | public class DateUtils { |
10 | | - public static long START_DATE = DateUtils.getDate("2022-8-29"); |
11 | 28 |
|
12 | | - public static String curWeek() { |
13 | | - long cur = (System.currentTimeMillis() - START_DATE) / (1000 * 60 * 60 * 24 * 7) + 1; |
| 29 | + public static String curWeek(Context context) { |
| 30 | + SharedPreferences week = context.getSharedPreferences("week", Context.MODE_PRIVATE); |
| 31 | + new Thread(() -> { |
| 32 | + OkHttpClient httpClient = new OkHttpClient(); |
| 33 | + Request request = new Request.Builder().url("https://swust.aliceblog.co/api/week").get().build(); |
| 34 | + try { |
| 35 | + Response response = httpClient.newCall(request).execute(); |
| 36 | + if (response.isSuccessful()) { |
| 37 | + JSONObject jsonObject = JSON.parseObject(response.body().string()); |
| 38 | + week.edit().putLong("startDate", jsonObject.getLong("startDate")).apply(); |
| 39 | + week.edit().putInt("total", jsonObject.getIntValue("total")).apply(); |
| 40 | + } |
| 41 | + }catch (Exception ignored) {} |
| 42 | + }).start(); |
| 43 | + long cur = (System.currentTimeMillis() - week.getLong("startDate", DateUtils.getDate("2022-7-29"))) / (1000 * 60 * 60 * 24 * 7) + 1; |
| 44 | + int total = week.getInt("total", 20); |
| 45 | + if (cur > total) return Integer.toString(total); |
14 | 46 | return Long.toString(cur); |
15 | 47 | } |
16 | 48 |
|
|
0 commit comments