MyFlag Step 10: Background Code Decoupling and Client Specific Function Design

Posted by Ryyo on Tue, 25 Jun 2019 21:05:37 +0200

Introduction

This week happens to be the mid-term of the development cycle. We are developing in an orderly manner according to the original plan. At this stage, our main tasks are as follows:

  • Code Rewriting and Decoupling in DAO Layer
  • Comment + My News

DAO Layer Code Rewriting and Decoupling

In the last stage of this week, we successfully built the basic model of Flag. During the process of code construction, we encountered many problems. Then we improved the shortcomings of the original code design. Although the amount of code is not much, the coordination of related functions and Flag function itself is the most complex module of logic, so it is very painful to think about. Module design in the previous stage:

  • Flag is used to create Flag
  • Flagbrief is primarily for use in lists
  • Flagdetail is used to view specific Flags

    The initial plan is to put them together, but it will make it difficult to optimize and modify the code later, separating them based on the principle of single decoupling of module functions.
    public List selectSelfFlag(int id,long time){

    Connection conn=ConnectionPool.getConnection();
    PreparedStatement ptmt1=null;
    PreparedStatement ptmt2=null;
    PreparedStatement ptmt3=null;
    ResultSet rs1=null;
    ResultSet rs2=null;
    ResultSet rs3=null;
    Vector fg=new Vector();
    Vector sv=new Vector();
    Vector fd=new Vector();

    Vector members=new Vector();

    //Vactor<>
    String sql1="select fid,uid,content,award,achieve,isTeam,startTime,endTime,createTime,nickname from fg_flag natural join fg_user where uid = ? and createTime < ? order by startTime Desc limit 15 ";
    String sql2="select fid,uid,agree ,photo,achieve,evaluate,time,nickname from fg_supervise natural join fg_user where fid in (select t.fid from (select * from fg_flag where uid = ? and createTime < ? order by startTime Desc limit 15) as t) and agree=2 ";
    String sql3="select uid,fid,nickname from fg_member natural join fg_user where fid in (select t.fid from (select * from fg_flag where uid = ? and createTime < ? order by startTime Desc limit 15) as t)";
    try {
    conn.setAutoCommit(false);
    ptmt1=conn.prepareStatement(sql1);

        ptmt1.setInt(1, id);
        ptmt1.setLong(2, time);
        //System.out.println(ptmt1.toString());
        rs1=ptmt1.executeQuery();
    
    
        ptmt2=conn.prepareStatement(sql2);
        ptmt2.setInt(1, id);
        ptmt2.setLong(2, time);
    
        rs2=ptmt2.executeQuery();
    
        ptmt3=conn.prepareStatement(sql3);
        ptmt3.setInt(1, id);
        ptmt3.setLong(2, time);
        rs3=ptmt3.executeQuery();
    
        conn.commit();
        while (rs1.next()) {
            Flag tempFlag=new Flag();
            tempFlag.setAchieve(rs1.getInt("achieve"));
            try {
                tempFlag.setAward(new String (rs1.getBytes("award"),"utf-8") + " ");
            } catch (Exception e) {
                // TODO: handle exception
                tempFlag.setAward("");
            }
            try {
                tempFlag.setContent(new String (rs1.getBytes("content"),"utf-8") + " ");
    
            } catch (Exception e) {
                // TODO: handle exception
                tempFlag.setContent("");
            }
            tempFlag.setStartTime(rs1.getLong("startTime"));
            tempFlag.setEndTime(rs1.getLong("endTime"));
            tempFlag.setFid(rs1.getInt("fid"));
            tempFlag.setIsTeam(rs1.getBoolean("isTeam"));
            tempFlag.setUid(rs1.getInt("uid"));
            tempFlag.setCreateTime(rs1.getLong("createTime"));
            try {
                //System.out.println(new String (rs1.getBytes("nickname"),"utf-8") + " ");
                tempFlag.setNickname(new String (rs1.getBytes("nickname"),"utf-8") + " ");
            } catch (Exception e) {
                // TODO: handle exception
                tempFlag.setNickname("");
            }
            fg.add(tempFlag);
    
    
        }
    
        while (rs2.next()) {
            Supervise tempSupervise=new Supervise();
            tempSupervise.setPhoto(rs2.getInt("photo"));
            tempSupervise.setAchieve(rs2.getInt("achieve"));
            try {
                tempSupervise.setEvaluate(new String (rs2.getBytes("evaluate"),"utf-8") + " ");
            } catch (Exception e) {
                // TODO: handle exception
                tempSupervise.setEvaluate("");
            }
    
            tempSupervise.setFid(rs2.getInt("fid"));
            tempSupervise.setAgree(rs2.getInt("agree"));
            tempSupervise.setTime(rs2.getLong("time"));
            tempSupervise.setUid(rs2.getInt("uid"));
            try {
                tempSupervise.setNickname(new String (rs2.getBytes("nickname"),"utf-8") + " ");
            } catch (Exception e) {
                // TODO: handle exception
                tempSupervise.setNickname("");
            }
            sv.add(tempSupervise);
        }
        while(rs3.next()){
            Member tempMember=new Member();
            tempMember.setFid(rs3.getInt("fid"));
            tempMember.setUid(rs3.getInt("uid"));
            try {
                tempMember.setNickname(new String (rs3.getBytes("nickname"),"utf-8") + " ");
            } catch (Exception e) {
                // TODO: handle exception
                tempMember.setNickname("");
            }
            members.add(tempMember);
        }
    
        for(int i=0;i<fg.size();i++)
        {
            Vector<UserBrief>  memr=new Vector<UserBrief>();
            FlagDetail tempDetail=new FlagDetail();
            Vector<SuperviseBrief> spb=new Vector<SuperviseBrief>();
            tempDetail.setFlag(fg.get(i));
            int x=fg.get(i).getFid();
            for (int j = 0; j < sv.size(); j++) {
                if(sv.get(j).getFid()==x){
                    SuperviseBrief  tempBrief=new SuperviseBrief();
                    Supervise tempSupervise=sv.get(j);
                    tempBrief.setAchieve(tempSupervise.getAchieve());
                    tempBrief.setEvaluate(tempSupervise.getEvaluate());
                    tempBrief.setAgree(tempSupervise.getAgree());
                    tempBrief.setUid(tempSupervise.getUid());
                    tempBrief.setNickname(tempSupervise.getNickname());
                    tempBrief.setPhoto(tempSupervise.getPhoto());
                    spb.add(tempBrief);
                    sv.remove(j);
                    j--;
                }
            }
            for(int j=0;j<members.size();j++){
                if(members.get(j).getFid()==x){
                    UserBrief ub=new UserBrief();
                    ub.setNickname(members.get(j).getNickname());
                    ub.setUid(members.get(j).getUid());
                    memr.add(ub);
                    members.remove(j);
                    j--;
                }
            }
            tempDetail.setFriendsJudge(spb);
            tempDetail.setMember(memr);
            fd.add(tempDetail);
        }
    
    
        rs1.close();
        rs2.close();
        ptmt1.close();
        ptmt2.close();
        conn.close();
        return fd;
     } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        return null;
     }
     finally {
        try {
            if(rs1!=null){
                rs1.close();
            }
            if(rs2!=null){
                rs2.close();
            }
            if (ptmt1 != null) {
                ptmt1.close();
            }
            if (ptmt2 != null) {
                ptmt2.close();
            }
            if (conn != null) {
                conn.close();
            }
    
        } catch (SQLException e) {
            e.printStackTrace();
        }
     } 
    }
    

To solve these problems, Flagdao's main architecture comes naturally.

Comment + My News

1. The evaluation interface allows friends who monitor flag to evaluate flag, including whether it has been completed and whether it has been commented on.
The xml file is as follows:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/white"
    android:padding="0dp">

    <ImageButton
        android:layout_width="?attr/actionBarSize"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentLeft="true"
        android:background="@drawable/toolbar_back_bg"
        android:onClick="CommentBack"
        android:src="?attr/homeAsUpIndicator" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="comment"
        android:textColor="@color/black"
        android:textSize="19sp" />
</RelativeLayout>

View:

The Activity of my friend's message is SuperViseJudgeActivity, the core code is as follows:

    class JudgeCallBack implements NetUtil.CallBackForResult {
    @Override
    public void onFailure(final IOException e) {
        SuperViseJudgeActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(SuperViseJudgeActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onSuccess(Response response) {
        if (response.isSuccessful()) {
            try {
                final String res = response.body().string();
                SuperViseJudgeActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (res.equals("1")) {
                            Toast.makeText(SuperViseJudgeActivity.this, "Successful comments", Toast.LENGTH_SHORT).show();
                            SuperViseJudgeActivity.this.finish();
                        }
                        else if(res.equals("2")){
                            Toast.makeText(SuperViseJudgeActivity.this, "You have commented on that. FLAGļ¼ŒUnrepeatable evaluation", Toast.LENGTH_SHORT).show();
                            SuperViseJudgeActivity.this.finish();
                        }
                        else {
                            Toast.makeText(SuperViseJudgeActivity.this, "Comment Failure", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

This code is a drop-back function after getting data from the server. According to the parameters returned by the server, different operations are carried out, such as returning 1, that is, the comment is successful, returning 2, that is, the comment has already been commented.

2. @My Message Interface:
@ My message interface shows a friend request to invite me as flag supervisor.
The Xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/activity_bg_gray"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/white"
    android:padding="0dp">

    <ImageButton
        android:layout_width="?attr/actionBarSize"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentLeft="true"
        android:background="@drawable/toolbar_back_bg"
        android:onClick="myMessageSuperviseBack"
        android:src="?attr/homeAsUpIndicator" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="\@My news"
        android:textColor="@color/black"
        android:textSize="19sp" />
</RelativeLayout>

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/supervise_swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp">

    <ListView
        android:id="@+id/myMessageSuperViseListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:background="@color/white" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

The code contains a SwipeRefreshLayout to refresh the ListView, which displays specific messages. The main view and the specific item are as follows:

Main View Item

@ My message interface Activity is SuperViseDetailActivity, with functions:
After the request is successful, each data information returned by the server is retrieved, and each data packet is packaged in the bean and passed to the adapter of the listview, so that each item view can be created and displayed.

After the code is written, we test each other. For different input information, we design a number of groups of test data. Then we test each group of data on the real machine to check whether the back-end return data is correct until the tests of these two functions run correctly.

summary

Although this week's workload is relatively large, but we all benefit from our tacit cooperation and perfect plan, always achieve our desired goals.

Topics: Android xml supervisor encoding