| | |
| | | |
| | | import androidx.lifecycle.MutableLiveData; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.runt.open.mvvm.data.HttpApiResult; |
| | | import com.runt.open.mvvm.data.PageResult; |
| | | import com.runt.open.mvvm.retrofit.observable.HttpObserver; |
| | | |
| | | import org.json.JSONObject; |
| | | |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | * 分页 |
| | | * Created by Administrator on 2021/11/3 0003. |
| | | */ |
| | | public abstract class LoadPageViewModel<RESULT extends PageResult> extends BaseViewModel { |
| | | public abstract class LoadPageViewModel<D> extends BaseViewModel { |
| | | |
| | | public final int SIZE = 10; |
| | | private MutableLiveData<List> liveData = new MutableLiveData<>(); |
| | | private MutableLiveData<List<D>> liveData = new MutableLiveData<>(); |
| | | private MutableLiveData liveFailed = new MutableLiveData(); |
| | | |
| | | /** |
| | | * 请求地址 |
| | | * @return |
| | | */ |
| | | protected abstract String requestUrl(); |
| | | |
| | | /** |
| | | * 数据请求 |
| | | * @param page 页数 |
| | | * @param param 请求参数 |
| | | */ |
| | | public void requestData(int page,Map param){ |
| | | httpObserverOn( commonApi.getPageData(requestUrl(), page, SIZE, param), new HttpObserver<RESULT>() { |
| | | |
| | | final ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); |
| | | Class<D> entityClass = (Class<D>) type.getActualTypeArguments()[0]; |
| | | httpObserverOn( commonApi.getPageData(requestUrl(), page, SIZE, param), new HttpObserver<PageResult>() { |
| | | @Override |
| | | protected void onSuccess(RESULT data) { |
| | | liveData.postValue(data.rows); |
| | | protected void onSuccess(PageResult data) { |
| | | //数据转换 |
| | | List<D> list = new ArrayList<>(); |
| | | for(Object map : data.rows){ |
| | | list.add(new Gson().fromJson(new JSONObject((Map) map).toString(),entityClass)); |
| | | } |
| | | liveData.postValue(list); |
| | | } |
| | | |
| | | @Override |
| | |
| | | }); |
| | | } |
| | | |
| | | public MutableLiveData<List> getLiveData(){ |
| | | public MutableLiveData<List<D>> getLiveData(){ |
| | | return liveData; |
| | | } |
| | | |
| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.runt.open.mvvm.MyApplication; |
| | | import com.runt.open.mvvm.data.PhoneDevice; |
| | | import com.runt.open.mvvm.retrofit.net.NetWorkCost; |
| | | import com.runt.open.mvvm.retrofit.net.NetWorkListenear; |
| | | import com.runt.open.mvvm.retrofit.utils.HttpPrintUtils; |
| | |
| | | Log.d(TAG, "hashcode:" + hashCode); |
| | | } |
| | | Request.Builder requestBuild = requestTemp.newBuilder() |
| | | .addHeader("device", new Gson().toJson(PhoneDevice.getDevice())) |
| | | .addHeader("appVersion", DeviceUtil.getAppVersionName(MyApplication.getApplication())) |
| | | .addHeader("os", DeviceUtil.isHarmonyOS()? "harmony" : "android"); |
| | | Request request = requestBuild.build().newBuilder().build(); |
| | |
| | | |
| | | |
| | | import com.runt.open.mvvm.data.ApkUpGradeResult; |
| | | import com.runt.open.mvvm.data.HttpApiResult; |
| | | import com.runt.open.mvvm.data.PageResult; |
| | | |
| | | import java.util.Map; |
| | | |
| | |
| | | * @return |
| | | */ |
| | | @GET |
| | | Observable<Object> getPageData(@Url String url, @Query("pageNum") int pageNum, @Query("pageSize") int pageSize, @QueryMap Map<String,String> param); |
| | | Observable<HttpApiResult<PageResult>> getPageData(@Url String url, @Query("page") int pageNum, @Query("size") int pageSize, @QueryMap Map<String,String> param); |
| | | |
| | | /** |
| | | * 分页数据 |
| | |
| | | */ |
| | | @FormUrlEncoded |
| | | @POST |
| | | Observable<Object> postPageData(@Url String url, @Field("pageNum") int pageNum, @Field("pageSize") int pageSize, @FieldMap Map<String,String> param); |
| | | Observable<Object> postPageData(@Url String url, @Field("page") int pageNum, @Field("size") int pageSize, @FieldMap Map<String,String> param); |
| | | |
| | | /** |
| | | * app更新 |
| | |
| | | @Multipart |
| | | @POST("updatehead") |
| | | Call<ResponseBody> updateHead(@Part MultipartBody.Part file); |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.runt.open.mvvm.data.HttpApiResult; |
| | | import com.runt.open.mvvm.util.GsonUtils; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonIOException; |
| | | import com.google.gson.TypeAdapter; |
| | | import com.google.gson.stream.JsonReader; |
| | | import com.google.gson.stream.JsonToken; |
| | | import com.runt.open.mvvm.data.HttpApiResult; |
| | | import com.runt.open.mvvm.util.GsonUtils; |
| | | |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | |
| | | */ |
| | | protected String decryptJsonStr(String body) throws Exception { |
| | | Log.e("Converter","decryptJsonStr body:"+body); |
| | | if(body.indexOf("{") == 0) { |
| | | /*if(body.indexOf("{") == 0) { |
| | | JSONObject json = new JSONObject(body); |
| | | body = json.toString(); |
| | | //body = RSAUtils.decrypt(json.getString(ENCRYPT), RSAUtils.getPublicKey(RSAUtils.PUBLIC_KEY));// |
| | | } |
| | | }*/ |
| | | return transHump? GsonUtils.toHumpJson(body):body; |
| | | } |
| | | |
| | |
| | | //设置OKHttpClient |
| | | .client(client) |
| | | //设置baseUrl,注意,baseUrl必须后缀"/" |
| | | .baseUrl(BuildConfig.HOST_IP_ADDR+"api/v1/") |
| | | .baseUrl(BuildConfig.HOST_IP_ADDR+"api/v2/") |
| | | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| | | .build(); |
| | | } |
| | |
| | | mBinding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { |
| | | |
| | | @Override |
| | | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
| | | setTitleStr(position); |
| | | } |
| | | |
| | | @Override |
| | | public void onPageSelected(int position) { |
| | | setTitleStr(position); |
| | | mBinding.navView.getMenu().getItem(position).setChecked(true); |
| | | if(position == 2 && UserBean.getUser() == null){ |
| | | loginLaunch.launch(new Intent(mContext, RegisterLoginActivity.class)); |
| | |
| | | PermissionX.init(MainActivity.this) |
| | | .permissions(Manifest.permission.READ_PHONE_STATE) |
| | | .request((allGranted, grantedList, deniedList) -> { |
| | | if(allGranted){ |
| | | PhoneDevice.setDevice(mContext); |
| | | }else{ |
| | | showPermissionDialog(); |
| | | } |
| | | |
| | | PhoneDevice.setDevice(mContext); |
| | | }); |
| | | } |
| | | } |
| | |
| | | package com.runt.open.mvvm.ui.main.home; |
| | | |
| | | import com.runt.open.mvvm.base.model.LoadPageViewModel; |
| | | import com.runt.open.mvvm.data.Results; |
| | | |
| | | public class HomeViewModel extends LoadPageViewModel<Results.MessageResult> { |
| | | public class HomeViewModel extends LoadPageViewModel<Message> { |
| | | |
| | | @Override |
| | | protected String requestUrl() { |
| | |
| | | * @purpose Created by Runt (qingingrunt2010@qq.com) on 2022/7/27. |
| | | */ |
| | | public class Message { |
| | | public String content,cTime,title; |
| | | public String id,title,content,cTime; |
| | | } |
| | |
| | | import com.runt.open.mvvm.listener.CustomClickListener; |
| | | import com.runt.open.mvvm.util.HandleDate; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * My father is Object, ites purpose of |
| | | * |
| | |
| | | @Override |
| | | protected void onBindView(ItemMsgBinding binding, int position, Message message) { |
| | | binding.txtDetail.setText(message.content); |
| | | Date date = new Date(message.cTime); |
| | | binding.txtTime.setText(HandleDate.getTimeStateNew(date)); |
| | | binding.txtTime.setText(HandleDate.getTimeStateNew(HandleDate.getDateTimeToLong(message.cTime))); |
| | | binding.txtTitle.setText(message.title); |
| | | binding.getRoot().setOnClickListener(new CustomClickListener() { |
| | | @Override |
| | |
| | | return secondsdf.format(date); |
| | | } |
| | | |
| | | public static long getDateTimeToLong(String datetime) { |
| | | try { |
| | | return secondsdf.parse(datetime).getTime(); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将指定long类型的日期转换为string时间 只显示 时分秒 |
| | | */ |