Implement API ke Form Bantuan
This commit is contained in:
parent
2803182a02
commit
22d0409c0a
@ -21,6 +21,7 @@ import androidx.activity.result.contract.ActivityResultContracts;
|
|||||||
import com.example.bdkipoc.R;
|
import com.example.bdkipoc.R;
|
||||||
import com.example.bdkipoc.LoginActivity;
|
import com.example.bdkipoc.LoginActivity;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -29,7 +30,9 @@ import java.io.OutputStream;
|
|||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -48,6 +51,27 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
private ExecutorService executor = Executors.newSingleThreadExecutor();
|
private ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
private Handler mainHandler = new Handler(Looper.getMainLooper());
|
private Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
|
// Dynamic data lists
|
||||||
|
private List<ParameterDetail> sourceList = new ArrayList<>();
|
||||||
|
private List<ParameterDetail> issueList = new ArrayList<>();
|
||||||
|
private List<ParameterDetail> assignList = new ArrayList<>();
|
||||||
|
|
||||||
|
// Inner class for parameter details
|
||||||
|
public static class ParameterDetail {
|
||||||
|
public int id;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public ParameterDetail(int id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -61,7 +85,11 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
setContentView(R.layout.activity_bantuan_form);
|
setContentView(R.layout.activity_bantuan_form);
|
||||||
|
|
||||||
initViews();
|
initViews();
|
||||||
setupSpinners();
|
|
||||||
|
// Load dynamic data first, then setup spinners
|
||||||
|
loadHeaderParams();
|
||||||
|
loadUsers();
|
||||||
|
|
||||||
setupListeners();
|
setupListeners();
|
||||||
|
|
||||||
// Initialize button state
|
// Initialize button state
|
||||||
@ -87,31 +115,220 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
backNavigation = findViewById(R.id.back_navigation);
|
backNavigation = findViewById(R.id.back_navigation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadHeaderParams() {
|
||||||
|
String authToken = LoginActivity.getToken(this);
|
||||||
|
if (authToken == null || authToken.isEmpty()) {
|
||||||
|
LoginActivity.logout(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.execute(() -> {
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL("https://be-edc.msvc.app/header-params/list");
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setRequestProperty("Authorization", "Bearer " + authToken);
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == 200) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
// Parse response
|
||||||
|
JSONObject jsonResponse = new JSONObject(response.toString());
|
||||||
|
JSONArray dataArray = jsonResponse.getJSONArray("data");
|
||||||
|
|
||||||
|
// Clear existing lists
|
||||||
|
sourceList.clear();
|
||||||
|
issueList.clear();
|
||||||
|
|
||||||
|
// Process each header parameter
|
||||||
|
for (int i = 0; i < dataArray.length(); i++) {
|
||||||
|
JSONObject headerParam = dataArray.getJSONObject(i);
|
||||||
|
String code = headerParam.getString("code");
|
||||||
|
String name = headerParam.getString("name");
|
||||||
|
|
||||||
|
// Check if this is ticket_sources (code "01")
|
||||||
|
if ("01".equals(code) && "ticket_sources".equals(name)) {
|
||||||
|
JSONArray details = headerParam.getJSONArray("details");
|
||||||
|
for (int j = 0; j < details.length(); j++) {
|
||||||
|
JSONObject detail = details.getJSONObject(j);
|
||||||
|
int id = detail.getInt("id");
|
||||||
|
String detailName = detail.getString("name");
|
||||||
|
String status = detail.getString("status");
|
||||||
|
|
||||||
|
// Only add active items
|
||||||
|
if ("1".equals(status)) {
|
||||||
|
sourceList.add(new ParameterDetail(id, detailName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check if this is issue_categories (code "02")
|
||||||
|
else if ("02".equals(code) && "issue_categories".equals(name)) {
|
||||||
|
JSONArray details = headerParam.getJSONArray("details");
|
||||||
|
for (int j = 0; j < details.length(); j++) {
|
||||||
|
JSONObject detail = details.getJSONObject(j);
|
||||||
|
int id = detail.getInt("id");
|
||||||
|
String detailName = detail.getString("name");
|
||||||
|
String status = detail.getString("status");
|
||||||
|
|
||||||
|
// Only add active items
|
||||||
|
if ("1".equals(status)) {
|
||||||
|
issueList.add(new ParameterDetail(id, detailName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update UI on main thread
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
setupSpinners();
|
||||||
|
updateButtonState();
|
||||||
|
});
|
||||||
|
|
||||||
|
} else if (responseCode == 401) {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Session expired. Please login again.", Toast.LENGTH_LONG).show();
|
||||||
|
LoginActivity.logout(this);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Failed to load data. Error: " + responseCode, Toast.LENGTH_LONG).show();
|
||||||
|
// Setup with empty data
|
||||||
|
setupSpinners();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Network error: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
// Setup with empty data
|
||||||
|
setupSpinners();
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadUsers() {
|
||||||
|
String authToken = LoginActivity.getToken(this);
|
||||||
|
if (authToken == null || authToken.isEmpty()) {
|
||||||
|
LoginActivity.logout(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.execute(() -> {
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL("https://be-edc.msvc.app/users");
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setRequestProperty("Authorization", "Bearer " + authToken);
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == 200) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
// Parse response
|
||||||
|
JSONArray usersArray = new JSONArray(response.toString());
|
||||||
|
|
||||||
|
// Clear existing list
|
||||||
|
assignList.clear();
|
||||||
|
|
||||||
|
// Process each user
|
||||||
|
for (int i = 0; i < usersArray.length(); i++) {
|
||||||
|
JSONObject user = usersArray.getJSONObject(i);
|
||||||
|
String role = user.getString("role");
|
||||||
|
boolean isActive = user.getBoolean("is_active");
|
||||||
|
|
||||||
|
// Only add superadmin users who are active
|
||||||
|
if ("superadmin".equals(role) && isActive) {
|
||||||
|
int id = user.getInt("id");
|
||||||
|
String name = user.getString("name");
|
||||||
|
assignList.add(new ParameterDetail(id, name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update UI on main thread
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
setupSpinners();
|
||||||
|
updateButtonState();
|
||||||
|
});
|
||||||
|
|
||||||
|
} else if (responseCode == 401) {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Session expired. Please login again.", Toast.LENGTH_LONG).show();
|
||||||
|
LoginActivity.logout(this);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Failed to load users. Error: " + responseCode, Toast.LENGTH_LONG).show();
|
||||||
|
// Setup with empty data
|
||||||
|
setupSpinners();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
mainHandler.post(() -> {
|
||||||
|
Toast.makeText(this, "Network error loading users: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
// Setup with empty data
|
||||||
|
setupSpinners();
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void setupSpinners() {
|
private void setupSpinners() {
|
||||||
// Setup Source Spinner
|
// Setup Source Spinner (Dynamic)
|
||||||
String[] sourceOptions = {
|
List<String> sourceOptions = new ArrayList<>();
|
||||||
"Pilih Source",
|
sourceOptions.add("Pilih Source");
|
||||||
"WhatsApp",
|
for (ParameterDetail source : sourceList) {
|
||||||
"Website Hubungi Kami"
|
sourceOptions.add(source.name);
|
||||||
};
|
}
|
||||||
ArrayAdapter<String> sourceAdapter = new ArrayAdapter<>(this,
|
ArrayAdapter<String> sourceAdapter = new ArrayAdapter<>(this,
|
||||||
android.R.layout.simple_spinner_item, sourceOptions);
|
android.R.layout.simple_spinner_item, sourceOptions);
|
||||||
sourceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
sourceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
spinnerSource.setAdapter(sourceAdapter);
|
spinnerSource.setAdapter(sourceAdapter);
|
||||||
|
|
||||||
// Setup Issue Spinner
|
// Setup Issue Spinner (Dynamic)
|
||||||
String[] issueOptions = {
|
List<String> issueOptions = new ArrayList<>();
|
||||||
"Pilih Issue",
|
issueOptions.add("Pilih Issue");
|
||||||
"Koneksi EDC Bermasalah",
|
for (ParameterDetail issue : issueList) {
|
||||||
"Permintaan Tambahan EDC",
|
issueOptions.add(issue.name);
|
||||||
"Pergantian EDC"
|
}
|
||||||
};
|
|
||||||
ArrayAdapter<String> issueAdapter = new ArrayAdapter<>(this,
|
ArrayAdapter<String> issueAdapter = new ArrayAdapter<>(this,
|
||||||
android.R.layout.simple_spinner_item, issueOptions);
|
android.R.layout.simple_spinner_item, issueOptions);
|
||||||
issueAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
issueAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
spinnerIssue.setAdapter(issueAdapter);
|
spinnerIssue.setAdapter(issueAdapter);
|
||||||
|
|
||||||
// Setup Merchant Spinner
|
// Setup Merchant Spinner (Static - unchanged)
|
||||||
String[] merchantOptions = {
|
String[] merchantOptions = {
|
||||||
"Pilih Merchant",
|
"Pilih Merchant",
|
||||||
"Surabaya",
|
"Surabaya",
|
||||||
@ -133,16 +350,12 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
merchantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
merchantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
spinnerMerchant.setAdapter(merchantAdapter);
|
spinnerMerchant.setAdapter(merchantAdapter);
|
||||||
|
|
||||||
// Setup Assign Spinner
|
// Setup Assign Spinner (Dynamic)
|
||||||
String[] assignOptions = {
|
List<String> assignOptions = new ArrayList<>();
|
||||||
"Pilih Assign",
|
assignOptions.add("Pilih Assign");
|
||||||
"welno hedi prabowo putro jenaka prima",
|
for (ParameterDetail assign : assignList) {
|
||||||
"rizqika",
|
assignOptions.add(assign.name);
|
||||||
"budi",
|
}
|
||||||
"binomo",
|
|
||||||
"budi",
|
|
||||||
"Irfan Muslim Saputra"
|
|
||||||
};
|
|
||||||
ArrayAdapter<String> assignAdapter = new ArrayAdapter<>(this,
|
ArrayAdapter<String> assignAdapter = new ArrayAdapter<>(this,
|
||||||
android.R.layout.simple_spinner_item, assignOptions);
|
android.R.layout.simple_spinner_item, assignOptions);
|
||||||
assignAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
assignAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
@ -325,20 +538,17 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getSourceId(int position) {
|
private int getSourceId(int position) {
|
||||||
switch (position) {
|
if (position > 0 && position <= sourceList.size()) {
|
||||||
case 1: return 1; // WhatsApp
|
return sourceList.get(position - 1).id;
|
||||||
case 2: return 2; // Website Hubungi Kami
|
|
||||||
default: return 0;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getIssueId(int position) {
|
private int getIssueId(int position) {
|
||||||
switch (position) {
|
if (position > 0 && position <= issueList.size()) {
|
||||||
case 1: return 4; // Koneksi EDC Bermasalah
|
return issueList.get(position - 1).id;
|
||||||
case 2: return 5; // Permintaan Tambahan EDC
|
|
||||||
case 3: return 6; // Pergantian EDC
|
|
||||||
default: return 0;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMerchantId(int position) {
|
private int getMerchantId(int position) {
|
||||||
@ -361,15 +571,10 @@ public class BantuanFormActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getAssignId(int position) {
|
private int getAssignId(int position) {
|
||||||
switch (position) {
|
if (position > 0 && position <= assignList.size()) {
|
||||||
case 1: return 1; // welno hedi prabowo putro jenaka prima
|
return assignList.get(position - 1).id;
|
||||||
case 2: return 2; // rizqika
|
|
||||||
case 3: return 3; // budi
|
|
||||||
case 4: return 4; // binomo
|
|
||||||
case 5: return 5; // budi
|
|
||||||
case 6: return 6; // Irfan Muslim Saputra
|
|
||||||
default: return 0;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void submitForm() {
|
private void submitForm() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user