-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathAboutActivity.java
More file actions
208 lines (173 loc) · 7.35 KB
/
AboutActivity.java
File metadata and controls
208 lines (173 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.wmods.wppenhacer.activities;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.wmods.wppenhacer.R;
import com.wmods.wppenhacer.activities.base.BaseActivity;
import com.wmods.wppenhacer.databinding.ActivityAboutBinding;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class AboutActivity extends BaseActivity {
private ActivityAboutBinding binding;
private ContributorAdapter adapter;
private List<Contributor> contributorList = new ArrayList<>();
private static final String API_URL = "https://api.github.com/repos/mubashardev/WaEnhancer/contributors";
private static final OkHttpClient client = new OkHttpClient();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityAboutBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.btnTelegram.setOnClickListener(v -> openUrl("https://t.me/waenhancer1"));
binding.btnGithub.setOnClickListener(view -> openUrl("https://github.com/mubashardev/WaEnhancer"));
adapter = new ContributorAdapter();
binding.rvContributors.setAdapter(adapter);
fetchContributors();
}
private void fetchContributors() {
android.content.SharedPreferences prefs = getSharedPreferences("github_api_cache", MODE_PRIVATE);
long lastFetch = prefs.getLong("last_fetch", 0);
String cachedJson = prefs.getString("contributors_json", null);
// 1-hour cache
if (cachedJson != null && (System.currentTimeMillis() - lastFetch < 3600000)) {
parseContributorsAndRefresh(cachedJson);
return;
}
Request request = new Request.Builder()
.url(API_URL)
.header("User-Agent", "WaEnhancer-App")
.header("Accept", "application/vnd.github.v3+json")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull java.io.IOException e) {
runOnUiThread(() -> {
// Fallback to cache if failed
if (cachedJson != null) {
parseContributorsAndRefresh(cachedJson);
} else {
Toast.makeText(AboutActivity.this, "Failed to load contributors", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws java.io.IOException {
if (!response.isSuccessful() || response.body() == null) {
runOnUiThread(() -> {
if (cachedJson != null) {
parseContributorsAndRefresh(cachedJson);
} else {
Toast.makeText(AboutActivity.this, "Error fetching contributors", Toast.LENGTH_SHORT)
.show();
}
});
return;
}
try {
String json = response.body().string();
prefs.edit()
.putLong("last_fetch", System.currentTimeMillis())
.putString("contributors_json", json)
.apply();
runOnUiThread(() -> parseContributorsAndRefresh(json));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void parseContributorsAndRefresh(String json) {
try {
JSONArray jsonArray = new JSONArray(json);
List<Contributor> fetchList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
Contributor c = new Contributor();
c.login = obj.optString("login");
c.avatarUrl = obj.optString("avatar_url");
c.htmlUrl = obj.optString("html_url");
c.contributions = obj.optInt("contributions", 0);
fetchList.add(c);
}
contributorList.clear();
contributorList.addAll(fetchList);
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
private void openUrl(String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
private static class Contributor {
String login;
String avatarUrl;
String htmlUrl;
int contributions;
}
private class ContributorAdapter extends RecyclerView.Adapter<ContributorAdapter.ViewHolder> {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contributor, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Contributor c = contributorList.get(position);
holder.ivAvatar.clearColorFilter(); // Remove default tint when loading real image
com.bumptech.glide.Glide.with(holder.itemView.getContext())
.load(new com.bumptech.glide.load.model.GlideUrl(c.avatarUrl,
new com.bumptech.glide.load.model.LazyHeaders.Builder()
.addHeader("User-Agent", "WaEnhancer-App")
.build()))
.placeholder(R.drawable.ic_github)
.into(holder.ivAvatar);
holder.ivAvatar.setOnClickListener(v -> com.wmods.wppenhacer.ui.helpers.BottomSheetHelper
.showUserProfile(AboutActivity.this, c.login, c.avatarUrl, c.htmlUrl, c.contributions));
}
@Override
public int getItemCount() {
return contributorList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivAvatar;
ViewHolder(View itemView) {
super(itemView);
ivAvatar = itemView.findViewById(R.id.ivAvatar);
}
}
}
}