Skip to content

Commit

Permalink
侧滑删除
Browse files Browse the repository at this point in the history
  • Loading branch information
xz-dev committed May 3, 2019
1 parent 794dc86 commit 1d1ce2f
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 89 deletions.
11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "net.xzos.upgradeall"
minSdkVersion 26
targetSdkVersion 28
versionCode 3
versionName "0.0.2"
versionCode 4
versionName "0.0.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -24,16 +24,17 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.14.1'
implementation 'org.litepal.android:java:3.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.yanzhenjie.recyclerview:x:1.3.2'
}
32 changes: 29 additions & 3 deletions app/src/main/java/net/xzos/UpgradeAll/HttpApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package net.xzos.UpgradeAll;

import android.annotation.SuppressLint;
import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -33,7 +39,7 @@ static String getHttpResponse(String api_url) {
}
if (response != null) {
try {
responseString = response.body() != null ? response.body().string() : null;
responseString = response.body() != null ? response.body().string() : "";
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -49,7 +55,7 @@ class GithubApi extends HttpApi {
private static final String TAG = "GithubApi";

GithubApi(String api_url) {
Log.d(TAG, "api_url" + api_url);
Log.d(TAG, "api_url: " + api_url);
setApiUrl(api_url);
try {
flashData();
Expand Down Expand Up @@ -78,12 +84,32 @@ String getLatestRelease() {
}

private JSONObject getRelease(int releaseNum) throws JSONException {
return new JSONObject(this.returnJsonArray.getString(releaseNum));
Log.d(TAG, "getRelease: returnJsonArray: " + returnJsonArray);
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(this.returnJsonArray.getString(releaseNum));
} catch (NullPointerException e) {
e.printStackTrace();
}
return jsonObject;
}

private void setApiUrl(String api_url) {
if (this.api_url == null) {
this.api_url = api_url;
}
}

static String[] getApiUrl(String url) {
String[] temp = url.split("github\\.com");
temp = temp[temp.length - 1].split("/");
List<String> list = new ArrayList<>(Arrays.asList(temp));
list.removeAll(Arrays.asList("", null));
String owner = list.get(0);
String repo = list.get(1);
// 分割网址
String apiUrl = "https://api.github.com/repos/"
+ owner + "/" + repo + "/releases";
return new String[]{apiUrl, repo};
}
}
19 changes: 12 additions & 7 deletions app/src/main/java/net/xzos/UpgradeAll/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.litepal.LitePal;

Expand All @@ -26,8 +28,8 @@ public class MainActivity extends AppCompatActivity {
final private Updater updater = new Updater();

@Override
protected void onPostResume() {
super.onPostResume();
protected void onStart() {
super.onStart();
refreshUpgrade();
}

Expand All @@ -54,6 +56,9 @@ protected void onCreate(Bundle savedInstanceState) {

private void refreshUpgrade() {
new Thread(() -> {
runOnUiThread(() -> {
swipeRefresh.setRefreshing(true);
});
updater.refresh();
runOnUiThread(() -> {
refreshCardView();
Expand Down
50 changes: 31 additions & 19 deletions app/src/main/java/net/xzos/UpgradeAll/UpgradeItemCardAdapter.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
package net.xzos.UpgradeAll;

import android.content.Context;
import android.annotation.SuppressLint;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.yanzhenjie.recyclerview.SwipeMenuLayout;

public class UpgradeItemCardAdapter extends RecyclerView.Adapter<UpgradeItemCardAdapter.ViewHolder> {
import org.litepal.LitePal;

import java.util.List;

private Context mContext;

public class UpgradeItemCardAdapter extends RecyclerView.Adapter<UpgradeItemCardAdapter.ViewHolder> {

private List<UpgradeItemCard> mFruitList;

static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
SwipeMenuLayout cardView;
TextView name;
TextView version;
TextView url;
TextView api;
CardView del_button;
RecyclerView upgradeItemCardList;

ViewHolder(View view) {
super(view);
cardView = (CardView) view;
cardView = (SwipeMenuLayout) view;
name = view.findViewById(R.id.nameTextView);
version = view.findViewById(R.id.versionTextView);
url = view.findViewById(R.id.urlTextView);
api = view.findViewById(R.id.apiTextView);
del_button = view.findViewById(R.id.del_button);
upgradeItemCardList = view.findViewById(R.id.item_list_view);
}
}

Expand All @@ -44,25 +52,29 @@ static class ViewHolder extends RecyclerView.ViewHolder {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (mContext == null) {
mContext = parent.getContext();
}
View view = LayoutInflater.from(mContext).inflate(R.layout.upgrade_item_card_view, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.cardView.setOnClickListener(v -> {
Intent intent = new Intent(mContext, UpgradeItemSettingActivity.class);
mContext.startActivity(intent);
});
return holder;
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(R.layout.upgrade_item_card_view, parent, false));
}

@SuppressLint("ShowToast")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
UpgradeItemCard upgradeItemCard = mFruitList.get(position);
holder.name.setText(upgradeItemCard.getName());
holder.version.setText(upgradeItemCard.getVersion());
holder.api.setText(upgradeItemCard.getApi());
holder.url.setText(upgradeItemCard.getUrl());
holder.del_button.setOnClickListener(v -> {
// 删除数据库
String api_url = GithubApi.getApiUrl(holder.url.getText().toString())[0];
LitePal.deleteAll(RepoDatabase.class, "api_url = ?", api_url);
Toast.makeText(MyApplication.getContext(), String.format("%s已删除", api_url), Toast.LENGTH_LONG);
int item = this.getItemCount();
Intent intent = new Intent(MyApplication.getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
MyApplication.getContext().startActivity(intent);
Log.d("123", "onBindViewHolder: " + item);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
Expand All @@ -23,6 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrade_item_setting);
Button versionCheckButton = findViewById(R.id.versionCheckButton);
// 以下是按键事件
versionCheckButton.setOnClickListener(v -> {
// 版本检查设置
JSONObject versionCheckerJsonObject = getVersionChecker();
Expand Down Expand Up @@ -84,15 +87,9 @@ boolean addRepoDatabase(String name, String api, String url, JSONObject versionC
String api_url = "";
switch (api.toLowerCase()) {
case "github":
String[] temp = url.split("github\\.com");
temp = temp[temp.length - 1].split("/");
List<String> list = new ArrayList<>(Arrays.asList(temp));
list.removeAll(Arrays.asList("", null));
owner = list.get(0);
repo = list.get(1);
// 分割网址
api_url = "https://api.github.com/repos/"
+ owner + "/" + repo + "/releases";
String[] apiUrlStringList = GithubApi.getApiUrl(url);
api_url = apiUrlStringList[0];
repo = apiUrlStringList[1];
break;
}
if (name.length() == 0) {
Expand Down
Loading

0 comments on commit 1d1ce2f

Please sign in to comment.