2021-02-04 04:47:00 +01:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-08-07 21:34:02 +02:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2021-02-04 04:47:00 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Cron;
|
2021-02-04 04:47:00 +01:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.helpers.AlertDialogs;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
|
|
|
/**
|
2022-07-29 14:08:44 +02:00
|
|
|
* @author M M Arif
|
2021-02-04 04:47:00 +01:00
|
|
|
*/
|
2022-09-21 07:43:00 +02:00
|
|
|
public class AdminCronTasksAdapter
|
|
|
|
extends RecyclerView.Adapter<AdminCronTasksAdapter.CronTasksViewHolder> {
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
private final List<Cron> tasksList;
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
static class CronTasksViewHolder extends RecyclerView.ViewHolder {
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private Cron cronTasks;
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2022-08-26 19:00:08 +02:00
|
|
|
private final TextView taskName;
|
|
|
|
|
2021-02-04 04:47:00 +01:00
|
|
|
private CronTasksViewHolder(View itemView) {
|
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
Context ctx = itemView.getContext();
|
|
|
|
|
2021-05-30 19:28:14 +02:00
|
|
|
final Locale locale = ctx.getResources().getConfiguration().locale;
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
final String timeFormat = TinyDB.getInstance(ctx).getString("dateFormat", "pretty");
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
ImageView runTask = itemView.findViewById(R.id.runTask);
|
2021-02-04 04:47:00 +01:00
|
|
|
taskName = itemView.findViewById(R.id.taskName);
|
2021-04-13 16:19:42 +02:00
|
|
|
LinearLayout cronTasksInfo = itemView.findViewById(R.id.cronTasksInfo);
|
|
|
|
LinearLayout cronTasksRun = itemView.findViewById(R.id.cronTasksRun);
|
2021-02-04 04:47:00 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
cronTasksInfo.setOnClickListener(
|
|
|
|
taskInfo -> {
|
|
|
|
String nextRun = "";
|
|
|
|
String lastRun = "";
|
|
|
|
|
|
|
|
if (cronTasks.getNext() != null) {
|
|
|
|
nextRun =
|
|
|
|
TimeHelper.formatTime(
|
|
|
|
cronTasks.getNext(), locale, timeFormat, ctx);
|
|
|
|
}
|
|
|
|
if (cronTasks.getPrev() != null) {
|
|
|
|
lastRun =
|
|
|
|
TimeHelper.formatTime(
|
|
|
|
cronTasks.getPrev(), locale, timeFormat, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
View view =
|
|
|
|
LayoutInflater.from(ctx)
|
|
|
|
.inflate(R.layout.layout_cron_task_info, null);
|
|
|
|
|
|
|
|
TextView taskScheduleContent = view.findViewById(R.id.taskScheduleContent);
|
|
|
|
TextView nextRunContent = view.findViewById(R.id.nextRunContent);
|
|
|
|
TextView lastRunContent = view.findViewById(R.id.lastRunContent);
|
|
|
|
TextView execTimeContent = view.findViewById(R.id.execTimeContent);
|
|
|
|
|
|
|
|
taskScheduleContent.setText(cronTasks.getSchedule());
|
|
|
|
nextRunContent.setText(nextRun);
|
|
|
|
lastRunContent.setText(lastRun);
|
|
|
|
execTimeContent.setText(String.valueOf(cronTasks.getExecTimes()));
|
|
|
|
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(ctx)
|
|
|
|
.setTitle(
|
|
|
|
StringUtils.capitalize(
|
|
|
|
cronTasks.getName().replace("_", " ")))
|
|
|
|
.setView(view)
|
|
|
|
.setNeutralButton(ctx.getString(R.string.close), null);
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
|
|
|
});
|
|
|
|
|
|
|
|
cronTasksRun.setOnClickListener(
|
|
|
|
taskInfo -> {
|
|
|
|
runCronTask(ctx, cronTasks.getName());
|
|
|
|
});
|
2021-02-04 04:47:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:00:08 +02:00
|
|
|
public AdminCronTasksAdapter(List<Cron> tasksListMain) {
|
|
|
|
this.tasksList = tasksListMain;
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@NonNull @Override
|
|
|
|
public AdminCronTasksAdapter.CronTasksViewHolder onCreateViewHolder(
|
|
|
|
@NonNull ViewGroup parent, int viewType) {
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
View v =
|
|
|
|
LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.list_admin_cron_tasks, parent, false);
|
2022-08-26 19:00:08 +02:00
|
|
|
return new AdminCronTasksAdapter.CronTasksViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-09-21 07:43:00 +02:00
|
|
|
public void onBindViewHolder(
|
|
|
|
@NonNull AdminCronTasksAdapter.CronTasksViewHolder holder, int position) {
|
2022-08-26 19:00:08 +02:00
|
|
|
|
|
|
|
Cron currentItem = tasksList.get(position);
|
|
|
|
|
|
|
|
holder.cronTasks = currentItem;
|
|
|
|
holder.taskName.setText(StringUtils.capitalize(currentItem.getName().replace("_", " ")));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void runCronTask(final Context ctx, final String taskName) {
|
|
|
|
|
|
|
|
Call<Void> call = RetrofitClient.getApiInterface(ctx).adminCronRun(taskName);
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<Void>() {
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
switch (response.code()) {
|
|
|
|
case 204:
|
|
|
|
Toasty.success(
|
|
|
|
ctx,
|
|
|
|
ctx.getString(R.string.adminCronTaskSuccessMsg, taskName));
|
|
|
|
break;
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
case 401:
|
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(ctx);
|
|
|
|
break;
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
case 403:
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.authorizeError));
|
|
|
|
break;
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
case 404:
|
|
|
|
Toasty.warning(ctx, ctx.getString(R.string.apiNotFound));
|
|
|
|
break;
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
default:
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
2022-08-26 19:00:08 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-26 19:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return tasksList.size();
|
|
|
|
}
|
2021-02-04 04:47:00 +01:00
|
|
|
}
|