TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/VideosTimelineActivity.java

176 lines
6.7 KiB
Java
Raw Normal View History

2020-06-30 13:33:43 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-06-30 13:33:43 +02:00
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.viewmodel.TimelineVM.TimelineType.HISTORY;
import android.app.AlertDialog;
2020-06-30 13:33:43 +02:00
import android.os.Bundle;
import android.view.Menu;
2020-06-30 13:33:43 +02:00
import android.view.MenuItem;
2020-11-12 17:08:43 +01:00
import android.view.View;
2020-06-30 13:33:43 +02:00
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import org.jetbrains.annotations.NotNull;
2020-06-30 13:33:43 +02:00
2020-11-12 17:08:43 +01:00
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import app.fedilab.fedilabtube.client.APIResponse;
2020-11-27 15:00:25 +01:00
import app.fedilab.fedilabtube.databinding.ActivityVideosTimelineBinding;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.fragment.DisplayVideosFragment;
2020-10-08 17:16:42 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
2020-06-30 13:33:43 +02:00
2020-10-08 17:16:42 +02:00
public class VideosTimelineActivity extends AppCompatActivity {
2020-06-30 13:33:43 +02:00
2020-09-26 10:22:11 +02:00
private TimelineVM.TimelineType type;
2020-11-12 17:08:43 +01:00
private DisplayVideosFragment displayVideosFragment;
2020-06-30 13:33:43 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2020-11-27 15:00:25 +01:00
ActivityVideosTimelineBinding binding = ActivityVideosTimelineBinding.inflate(getLayoutInflater());
2020-11-12 17:08:43 +01:00
View mainView = binding.getRoot();
setContentView(mainView);
2020-06-30 13:33:43 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2020-07-03 17:35:28 +02:00
Bundle b = getIntent().getExtras();
2020-07-09 17:57:01 +02:00
if (b != null)
2020-09-26 10:22:11 +02:00
type = (TimelineVM.TimelineType) b.get("type");
2020-11-12 17:08:43 +01:00
displayVideosFragment = null;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (savedInstanceState == null) {
displayVideosFragment = new DisplayVideosFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.TIMELINE_TYPE, type);
displayVideosFragment.setArguments(bundle);
ft.add(R.id.container, displayVideosFragment).addToBackStack(null).commit();
}
2020-07-03 17:35:28 +02:00
2020-09-26 10:22:11 +02:00
if (type == TimelineVM.TimelineType.MY_VIDEOS) {
2020-07-03 17:35:28 +02:00
setTitle(R.string.my_videos);
2020-11-12 17:08:43 +01:00
} else if (type == HISTORY) {
2020-09-03 19:08:53 +02:00
setTitle(R.string.my_history);
2020-11-12 17:08:43 +01:00
//TODO: uncomment when available
// binding.historyFilter.setVisibility(View.VISIBLE);
binding.historyFilterAll.setOnClickListener(v -> historyFilter(null));
binding.historyFilterToday.setOnClickListener(v -> {
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.getTime();
historyFilter(cal.getTime());
});
binding.historyFilterLast7Days.setOnClickListener(v -> {
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, -7);
cal.getTime();
historyFilter(cal.getTime());
});
2020-10-03 11:24:26 +02:00
} else if (type == TimelineVM.TimelineType.MOST_LIKED) {
setTitle(R.string.title_most_liked);
2020-07-03 17:35:28 +02:00
}
2020-06-30 13:33:43 +02:00
2020-11-12 17:08:43 +01:00
}
private void historyFilter(Date date) {
String startDate = null;
if (date != null) {
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
startDate = fmtOut.format(date);
}
if (displayVideosFragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
displayVideosFragment = new DisplayVideosFragment();
2020-06-30 13:33:43 +02:00
Bundle bundle = new Bundle();
2020-11-12 17:08:43 +01:00
bundle.putSerializable(Helper.TIMELINE_TYPE, HISTORY);
bundle.putSerializable("startDate", startDate);
2020-09-25 18:58:04 +02:00
displayVideosFragment.setArguments(bundle);
2020-11-12 17:08:43 +01:00
ft.replace(R.id.container, displayVideosFragment);
ft.addToBackStack(null);
ft.commit();
2020-06-30 13:33:43 +02:00
}
}
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
if (type == HISTORY) {
getMenuInflater().inflate(R.menu.main_history, menu);
return true;
} else {
return super.onCreateOptionsMenu(menu);
}
}
2020-11-19 18:08:28 +01:00
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
2020-06-30 13:33:43 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else if (item.getItemId() == R.id.action_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(VideosTimelineActivity.this);
builder.setTitle(R.string.delete_history);
builder.setMessage(R.string.delete_history_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.delete, (dialog, which) -> {
TimelineVM viewModelFeeds = new ViewModelProvider(VideosTimelineActivity.this).get(TimelineVM.class);
viewModelFeeds.deleterHistory().observe(VideosTimelineActivity.this, this::manageVIewVideos);
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
return true;
2020-06-30 13:33:43 +02:00
}
return super.onOptionsItemSelected(item);
}
private void manageVIewVideos(APIResponse apiResponse) {
if (type == HISTORY) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
displayVideosFragment = new DisplayVideosFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.TIMELINE_TYPE, HISTORY);
displayVideosFragment.setArguments(bundle);
ft.replace(R.id.container, displayVideosFragment);
ft.addToBackStack(null);
ft.commit();
}
}
2020-06-30 13:33:43 +02:00
}