diff --git a/.idea/misc.xml b/.idea/misc.xml index 851612f..880cb14 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 0cb36ba..e75cccf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,6 +55,9 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.13.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0' + //沉浸式 + implementation 'com.gitee.zackratos:UltimateBarX:0.8.0' + testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseActivity.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseActivity.java index 95eb75b..69e7c7b 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseActivity.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseActivity.java @@ -83,18 +83,18 @@ protected void onDestroy() { } @Override - public void showLoading() { - MyUtil.showLoading(this); + public void showLoading(boolean isShow) { + if (isShow) MyUtil.showLoading(this); } @Override - public void SuccessHideLoading() { - new Handler().postDelayed(MyUtil::dismissSuccessLoading,500); + public void SuccessHideLoading(boolean isShow) { + if (isShow) new Handler().postDelayed(MyUtil::dismissSuccessLoading,500); } @Override - public void FailedHideLoading() { - new Handler().postDelayed(MyUtil::dismissFailedLoading,500); + public void FailedHideLoading(boolean isShow) { + if (isShow) new Handler().postDelayed(MyUtil::dismissFailedLoading,500); } /** diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseView.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseView.java index 2a623df..bacef1e 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseView.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/base/BaseView.java @@ -12,15 +12,15 @@ public interface BaseView { /** * 展示载入动画 */ - void showLoading(); + void showLoading(boolean isShow); /** * 操作成功隐藏dialog和显示成功 */ - void SuccessHideLoading(); + void SuccessHideLoading(boolean isShow); /** * 操作失败隐藏dialog和显示失败 */ - void FailedHideLoading(); + void FailedHideLoading(boolean isShow); } diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/main/activity/MainActivity.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/main/activity/MainActivity.java index 9ac9d22..d3315ea 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/main/activity/MainActivity.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/main/activity/MainActivity.java @@ -1,5 +1,6 @@ package com.dyj.requestpagingscheduleemulator.module.main.activity; +import android.graphics.Color; import android.os.Handler; import com.dyj.requestpagingscheduleemulator.base.BaseActivity; diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/activity/ResultActivity.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/activity/ResultActivity.java index cd4f1bb..90953dc 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/activity/ResultActivity.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/activity/ResultActivity.java @@ -1,15 +1,16 @@ package com.dyj.requestpagingscheduleemulator.module.result.activity; -import androidx.appcompat.app.AppCompatActivity; + +import android.view.View; + import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import android.graphics.Color; -import android.os.Bundle; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.request.RequestOptions; +import com.chad.library.adapter.base.BaseQuickAdapter; import com.dyj.requestpagingscheduleemulator.R; import com.dyj.requestpagingscheduleemulator.base.BaseActivity; import com.dyj.requestpagingscheduleemulator.bean.PageData; @@ -22,6 +23,7 @@ import com.dyj.requestpagingscheduleemulator.util.ActivityUtil; import com.dyj.requestpagingscheduleemulator.util.MyUtil; import com.dyj.requestpagingscheduleemulator.util.ToastUtil; +import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX; import java.util.List; @@ -43,7 +45,9 @@ protected ResultPresenter createPresenter() { */ @Override protected void initView() { - getWindow().setStatusBarColor(Color.TRANSPARENT); + UltimateBarX.statusBarOnly(this) + .transparent() + .apply(); String PIC_URL = "https://www.lxtlovely.top/getpic.php?rand=true"; showPic(PIC_URL); } @@ -54,23 +58,17 @@ protected void initView() { @Override protected void initData() { int data = ActivityUtil.getIntentData(); - getBinding().button1CreateData.setOnClickListener(v -> presenter.random()); - switch (data){ - case GlobalConstant.OPT: - getBinding().button1Todo.setOnClickListener(v -> presenter.OPT()); - break; - case GlobalConstant.LRU: - getBinding().button1Todo.setOnClickListener(v -> presenter.LRU()); - break; - case GlobalConstant.FIFO: - getBinding().button1Todo.setOnClickListener(v -> presenter.FIFO()); - break; - default: - break; - - } + getBinding().button1CreateData.setOnClickListener(v -> presenter.random(true)); + getBinding().button2Todo.setOnClickListener(v -> presenter.option(data,true)); + getBinding().refreshLayout.setOnRefreshListener(v -> { + presenter.random(false); + presenter.option(data,false); + getBinding().refreshLayout.finishRefresh(500); + }); } + + /** * 展示背景图 */ @@ -93,6 +91,8 @@ public void showPic(String url) { @Override public void showInstructions(List datas) { DataAdapter adapter = new DataAdapter(R.layout.item_data,datas); + adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn); + adapter.setAnimationFirstOnly(true); getBinding().rvDatas.setAdapter(adapter); getBinding().rvDatas.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false)); @@ -104,6 +104,8 @@ public void showInstructions(List datas) { @Override public void showPages(List list) { PageAdapter adapter = new PageAdapter(R.layout.item_page,list); + adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn); + adapter.setAnimationFirstOnly(true); getBinding().rvPages.setAdapter(adapter); getBinding().rvPages.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)); } @@ -115,11 +117,30 @@ public void showPages(List list) { public void showMissingPageRate(int lost) { double res = (double)lost/320; String text = 100*res + "%"; - getBinding().missingPageRate.setText(text); + getBinding().tvMissingPageRate.setText(text); } + /** + * 展示error toast + */ @Override public void showReInit() { ToastUtil.showToast(MyUtil.getString(R.string.reinit)); } + + /** + * 展示data数据 + */ + @Override + public void showRVData() { + if (getBinding().rvDatas.getVisibility() == View.GONE) getBinding().rvDatas.setVisibility(View.VISIBLE); + } + + /** + * 展示pages数据 + */ + @Override + public void showRVPages() { + if (getBinding().rvPages.getVisibility() == View.GONE) getBinding().rvPages.setVisibility(View.VISIBLE); + } } \ No newline at end of file diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/presenter/ResultPresenter.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/presenter/ResultPresenter.java index 73bf45a..dd58505 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/presenter/ResultPresenter.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/presenter/ResultPresenter.java @@ -2,6 +2,7 @@ import com.dyj.requestpagingscheduleemulator.base.BasePresenter; import com.dyj.requestpagingscheduleemulator.bean.PageData; +import com.dyj.requestpagingscheduleemulator.common.GlobalConstant; import com.dyj.requestpagingscheduleemulator.module.result.view.IResult; import java.util.ArrayList; @@ -41,8 +42,13 @@ public ResultPresenter(IResult baseView) { super(baseView); } - public void random(){ - baseView.showLoading(); + /** + * 载入随机数据 + * + * @param isShow 是否展示加载动画 + */ + public void random(boolean isShow){ + baseView.showLoading(isShow); Random random = new Random(); datas.clear(); showData.clear(); @@ -51,18 +57,25 @@ public void random(){ datas.add(r); } showData.addAll(datas); - baseView.SuccessHideLoading(); + baseView.SuccessHideLoading(isShow); + baseView.showRVData(); baseView.showInstructions(showData); } - public void OPT(){ + /** + * 执行算法 + * + * @param op 来自哪个算法的路径 + * @param isShow 是否展示加载动画 + */ + public void option(int op,boolean isShow){ lost = 0; - list.clear(); if (datas.isEmpty()) { baseView.showReInit(); }else { - baseView.showLoading(); + list.clear(); + baseView.showLoading(isShow); while (datas.size() > 0){ int waitData = datas.get(0); datas.remove(0); @@ -71,6 +84,7 @@ public void OPT(){ PageData pageData = new PageData(waitData,waitData); lost++; list.add(pageData); + if (op == GlobalConstant.LRU) makeRecently(waitData,1); }else if (list.size() == 1 || lost == 1) { pre = list.get(list.size() - 1); PageData pageData = new PageData(waitData,pre.getData1()); @@ -79,6 +93,7 @@ public void OPT(){ lost++; } list.add(pageData); + if (op == GlobalConstant.LRU) makeRecently(waitData,2); }else if (list.size() == 2 || lost == 2) { pre = list.get(list.size() - 1); PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2()); @@ -87,6 +102,7 @@ public void OPT(){ lost++; } list.add(pageData); + if (op == GlobalConstant.LRU) makeRecently(waitData,3); } else if (list.size() == 3 || lost == 3) { pre = list.get(list.size() - 1); PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3()); @@ -95,158 +111,101 @@ public void OPT(){ lost++; } list.add(pageData); + if (op == GlobalConstant.LRU) makeRecently(waitData,4); }else { - int listSize = list.size(); - pre = list.get(listSize-1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(),pre.getData4()); - if (check(pre,waitData)){ - list.add(pageData); - }else { - map.put(pre.getData1(),Collections.frequency(datas,pre.getData1())); - map.put(pre.getData2(),Collections.frequency(datas,pre.getData2())); - map.put(pre.getData3(),Collections.frequency(datas,pre.getData3())); - map.put(pre.getData4(),Collections.frequency(datas,pre.getData4())); - int min = map.get(pre.getData1()); - min = Math.min(min,map.get(pre.getData2())); - min = Math.min(min,map.get(pre.getData3())); - min = Math.min(min,map.get(pre.getData4())); - changeOPT(pageData,min,waitData); + switch (op){ + case GlobalConstant.LRU: + LRU(waitData); + break; + case GlobalConstant.OPT: + OPT(waitData); + break; + case GlobalConstant.FIFO: + FIFO(waitData); + break; + default: + break; } } } - baseView.SuccessHideLoading(); + baseView.SuccessHideLoading(isShow); baseView.showMissingPageRate(lost); + baseView.showRVPages(); baseView.showPages(list); } - } - public void FIFO(){ - lost = 0; - list.clear(); - if (datas.isEmpty()) { - baseView.showReInit(); - + /** + * 内存满时,OPT的替换策略 + * + * @param waitData 最新等待执行的页号 + */ + private void OPT(int waitData){ + int listSize = list.size(); + PageData pre = list.get(listSize - 1); + PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(), pre.getData3(), pre.getData4()); + if (check(pre,waitData)){ + list.add(pageData); }else { - baseView.showLoading(); - while (datas.size() > 0){ - int waitData = datas.get(0); - datas.remove(0); - PageData pre; - if (list.size() == 0) { - PageData pageData = new PageData(waitData,waitData); - lost++; - list.add(pageData); - }else if (list.size() == 1 || lost == 1) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData,pre.getData1()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), waitData); - lost++; - } - list.add(pageData); - }else if (list.size() == 2 || lost == 2) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), pre.getData2(),waitData); - lost++; - } - list.add(pageData); - } else if (list.size() == 3 || lost == 3) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(), waitData); - lost++; - } - list.add(pageData); - }else { - int listSize = list.size(); - pre = list.get(listSize-1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(),pre.getData4()); - if (check(pre,waitData)){ - list.add(pageData); - }else { - int op = (lost+1)%4; - changFIFO(pageData,op,waitData); - } - } - - } - baseView.SuccessHideLoading(); - baseView.showMissingPageRate(lost); - baseView.showPages(list); + map.put(pre.getData1(),Collections.frequency(datas, pre.getData1())); + map.put(pre.getData2(),Collections.frequency(datas, pre.getData2())); + map.put(pre.getData3(),Collections.frequency(datas, pre.getData3())); + map.put(pre.getData4(),Collections.frequency(datas, pre.getData4())); + int min = map.get(pre.getData1()); + min = Math.min(min,map.get(pre.getData2())); + min = Math.min(min,map.get(pre.getData3())); + min = Math.min(min,map.get(pre.getData4())); + changeOPT(pageData,min,waitData); } } - public void LRU(){ - lost = 0; - list.clear(); - if (datas.isEmpty()) { - baseView.showReInit(); - + /** + * 内存满时,FIFO的替换策略 + * + * @param waitData 最新等待执行的页号 + */ + private void FIFO(int waitData){ + int listSize = list.size(); + PageData pre = list.get(listSize-1); + PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(),pre.getData4()); + if (check(pre,waitData)){ + list.add(pageData); }else { - baseView.showLoading(); - while (datas.size() > 0){ - int waitData = datas.get(0); - datas.remove(0); - PageData pre; - if (list.size() == 0) { - PageData pageData = new PageData(waitData,waitData); - lost++; - list.add(pageData); - makeRecently(waitData,1); - }else if (list.size() == 1 || lost == 1) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData,pre.getData1()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), waitData); - lost++; - } - list.add(pageData); - makeRecently(waitData,2); - }else if (list.size() == 2 || lost == 2) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), pre.getData2(),waitData); - lost++; - } - list.add(pageData); - makeRecently(waitData,3); - } else if (list.size() == 3 || lost == 3) { - pre = list.get(list.size() - 1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3()); - if (!check(pre,waitData)){ - pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(), waitData); - lost++; - } - list.add(pageData); - makeRecently(waitData,4); - }else { - int listSize = list.size(); - pre = list.get(listSize-1); - PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(),pre.getData4()); - if (check(pre,waitData)){ - list.add(pageData); - }else { - int oldestKey = link.keySet().iterator().next(); - int op = link.get(oldestKey); - changLRU(pageData,op,waitData); - } - } - - } - baseView.SuccessHideLoading(); - baseView.showMissingPageRate(lost); - baseView.showPages(list); + int op = (lost+1)%4; + changFIFO(pageData,op,waitData); } + } + /** + * 内存满时,LRU的替换策略 + * + * @param waitData 最新等待执行的页号 + */ + private void LRU(int waitData){ + int listSize = list.size(); + PageData pre = list.get(listSize-1); + PageData pageData = new PageData(waitData, pre.getData1(), pre.getData2(),pre.getData3(),pre.getData4()); + if (check(pre,waitData)){ + list.add(pageData); + }else { + int oldestKey = link.keySet().iterator().next(); + int op = link.get(oldestKey); + changLRU(pageData,op,waitData); + } + } + /** + * 检查是否不缺页 + * 不缺页返回 true + * 缺页返回 false + * + * @param data 最小的子item + * @param num 请求的页面号 + * @return 返回是否不缺页 + */ private boolean check(PageData data, int num){ if (data.getData1() == num) return true; else if (data.getData2() == num) return true; @@ -254,6 +213,11 @@ private boolean check(PageData data, int num){ else return data.getData4() == num; } + /** + * @param data 载入最新子item + * @param min 后续data数据列最小的值 + * @param wait 需更替的最新数据 + */ private void changeOPT(PageData data, int min, int wait){ if (map.get(data.getData1()) == min) data.setData1(wait); else if (map.get(data.getData2()) == min) data.setData2(wait); @@ -263,7 +227,14 @@ private void changeOPT(PageData data, int min, int wait){ lost++; } - public void changFIFO(PageData data,int op,int wait){ + /** + * FIFO缺页时更改页面 + * + * @param data 载入最新子item + * @param op 替换节点 + * @param wait 需更替的最新数据 + */ + private void changFIFO(PageData data,int op,int wait){ switch (op){ case 1: data.setData1(wait); @@ -284,7 +255,14 @@ public void changFIFO(PageData data,int op,int wait){ lost++; } - public void changLRU(PageData data,int op,int wait){ + /** + * LRU缺页时更改页面 + * + * @param data 载入最新子item + * @param op 替换节点 + * @param wait 需更替的最新数据 + */ + private void changLRU(PageData data,int op,int wait){ switch (op){ case 1: data.setData1(wait); @@ -306,6 +284,12 @@ public void changLRU(PageData data,int op,int wait){ lost++; } + /** + * 利用linkedHashMap实现核心LRU算法 + * + * @param key 传入 waitData + * @param op 传入载入位置 + */ private void makeRecently(int key,int op) { if (link.get(key) == null && link.size() >= 4 ){ int oldestKey = link.keySet().iterator().next(); diff --git a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/view/IResult.java b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/view/IResult.java index b5b0c76..d6fcc0c 100644 --- a/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/view/IResult.java +++ b/app/src/main/java/com/dyj/requestpagingscheduleemulator/module/result/view/IResult.java @@ -33,5 +33,19 @@ public interface IResult extends BaseView { */ void showMissingPageRate(int lost); + + /** + * 展示error toast + */ void showReInit(); + + /** + * 展示data数据 + */ + void showRVData(); + + /** + * 展示pages数据 + */ + void showRVPages(); } diff --git a/app/src/main/res/layout/activity_result.xml b/app/src/main/res/layout/activity_result.xml index af87832..b312298 100644 --- a/app/src/main/res/layout/activity_result.xml +++ b/app/src/main/res/layout/activity_result.xml @@ -4,101 +4,126 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".module.result.activity.ResultActivity" - android:fitsSystemWindows="true"> + tools:context=".module.result.activity.ResultActivity"> - + android:scaleType="centerCrop" + android:src="@mipmap/pic_bg" /> - + + - - + android:layout_height="match_parent"> - - - - - - - - - - + android:orientation="horizontal" + style="@style/HomeCard" + android:visibility="gone" + app:layout_constraintBottom_toTopOf="@+id/guideline2" + app:layout_constraintTop_toTopOf="parent" + tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + tools:listitem="@layout/item_page" /> + + + + + + + + + + + + + - + \ No newline at end of file