ytdlnis/app/src/main/java/com/deniscerri/ytdl/MainActivity.java
Denis Çerri 709a3778a2
other new features
- check for updates when app launches
- remove scrollview so that recyclerview can recycle. It wont hide the top bar but thats a sacrifice for the greater good.
- remove download all mini card and made it into a floating action button
- made video loading from playlists as fast as possible even if you have thousands of videos in a list. It adds videos in recyclerview in a form of pagination
- fixed preferences when you just installed the app and you never opened the preference screen you can still download without erroring out
2022-09-18 00:27:36 +02:00

161 lines
5.2 KiB
Java

package com.deniscerri.ytdl;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;
import com.deniscerri.ytdl.databinding.ActivityMainBinding;
import com.deniscerri.ytdl.page.HistoryFragment;
import com.deniscerri.ytdl.page.HomeFragment;
import com.deniscerri.ytdl.page.MoreFragment;
import com.deniscerri.ytdl.page.settings.SettingsActivity;
import com.deniscerri.ytdl.page.settings.SettingsFragment;
import com.deniscerri.ytdl.util.UpdateUtil;
public class MainActivity extends AppCompatActivity{
ActivityMainBinding binding;
Context context;
private static final String TAG = "MainActivity";
private HomeFragment homeFragment;
private HistoryFragment historyFragment;
private MoreFragment moreFragment;
private Fragment lastFragment;
private FragmentManager fm;
private boolean isDownloadServiceRunning = false;
public DownloaderService downloaderService;
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
downloaderService = ((DownloaderService.LocalBinder) service).getService();
isDownloadServiceRunning = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
downloaderService = null;
isDownloadServiceRunning = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
binding = ActivityMainBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(binding.getRoot());
context = getBaseContext();
checkUpdate();
fm = getSupportFragmentManager();
homeFragment = new HomeFragment();
historyFragment = new HistoryFragment();
moreFragment = new MoreFragment();
initFragments();
binding.bottomNavigationView.setOnItemSelectedListener(item -> {
int id = item.getItemId();
if(id == R.id.home){
if(lastFragment == homeFragment){
homeFragment.scrollToTop();
}else{
this.setTitle(R.string.app_name);;
}
replaceFragment(homeFragment);
}else if(id == R.id.history){
if(lastFragment == historyFragment){
historyFragment.scrollToTop();
}else {
this.setTitle(getString(R.string.history));
}
replaceFragment(historyFragment);
}else if(id == R.id.more){
if(lastFragment == moreFragment){
Intent intent = new Intent(context, SettingsActivity.class);
startActivity(intent);
}else{
this.setTitle(getString(R.string.more));
}
replaceFragment(moreFragment);
}
return true;
});
Intent intent = getIntent();
handleIntents(intent);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntents(intent);
}
public void handleIntents(Intent intent){
String action = intent.getAction();
if(Intent.ACTION_SEND.equals(action)){
Log.e(TAG, action);
homeFragment = new HomeFragment();
historyFragment = new HistoryFragment();
moreFragment = new MoreFragment();
homeFragment.handleIntent(intent);
initFragments();
}
}
private void initFragments(){
fm.beginTransaction()
.replace(R.id.frame_layout, homeFragment)
.add(R.id.frame_layout, historyFragment)
.add(R.id.frame_layout, moreFragment)
.hide(historyFragment)
.hide(moreFragment)
.commit();
lastFragment = homeFragment;
}
private void replaceFragment(Fragment f){
fm.beginTransaction().hide(lastFragment).show(f).commit();
lastFragment = f;
}
public void startDownloadService(String title, int id){
if(isDownloadServiceRunning) return;
Intent serviceIntent = new Intent(context, DownloaderService.class);
serviceIntent.putExtra("title", title);
serviceIntent.putExtra("id", id);
context.getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
public void stopDownloadService(){
if(!isDownloadServiceRunning) return;
context.getApplicationContext().unbindService(serviceConnection);
isDownloadServiceRunning = false;
}
private void checkUpdate(){
UpdateUtil updateUtil = new UpdateUtil(this);
updateUtil.updateApp();
}
}