Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
Some code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjdev committed Jul 15, 2019
1 parent 80223ba commit b896c3f
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 140 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.d3coding.gmusicapi"
minSdkVersion 28
targetSdkVersion 28
versionCode 26
versionName "0.24"
versionCode 27
versionName "0.24.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/d3coding/gmusicapi/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.d3coding.gmusicapi;

public class Config {

static final int LOGIN_ACTIVITY = 11;
static final int MAX_DOWNLOADS = 25;
static final int SIMULTANEOUS_DOWNLOAD = 5;

}
5 changes: 1 addition & 4 deletions app/src/main/java/com/d3coding/gmusicapi/GMusicDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ public GMusicDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

static int getDatabaseVersion() {
return DATABASE_VERSION;
}

public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE_TRACKS);
db.execSQL(SQL_CREATE_TABLE_DOWNLOAD);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: updateDB
db.execSQL(SQL_DELETE_POSTS + TABLE_TRACKS);
db.execSQL(SQL_DELETE_POSTS + TABLE_DOWNLOAD);
onCreate(db);
Expand Down
164 changes: 63 additions & 101 deletions app/src/main/java/com/d3coding/gmusicapi/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,17 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;


public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

static final int LOGIN_ACTIVITY = 11;

private static final int MAX_DOWNLOADS = 25;

private boolean mState = false;
private DrawerLayout drawer;
private NavigationView navigationView;

private BottomAppBar toolbar;
private FloatingActionButton fab;
SearchView searchView;

MenuItem refreshLogItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -57,46 +53,61 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.ac_home);

toolbar = findViewById(R.id.toolbar);

drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
fab = findViewById(R.id.filter_button);

setSupportActionBar(toolbar);

fab = findViewById(R.id.filter_button);
fab.setOnClickListener((v) -> {
HomeFragment homeFragment = (HomeFragment) getFragment(fragment.HOME);
if (homeFragment != null)
homeFragment.showFilter();
});

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);

if (!mPresets.contains(getString(R.string.last_update)))
refreshDB();
else if (mPresets.getInt(getString(R.string.database_version), 0) != GMusicDB.getDatabaseVersion()) {
refreshDB();
mPresets.edit().putInt(getString(R.string.database_version), GMusicDB.getDatabaseVersion()).apply();
}

swapFragment(fragment.HOME);

swapAndSetupFragment(new HomeFragment());

} else
startActivityForResult(new Intent(this, LoginActivity.class), LOGIN_ACTIVITY);
startActivityForResult(new Intent(this, LoginActivity.class), Config.LOGIN_ACTIVITY);

}

void swapAndSetupFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, fragment).commit();
getSupportFragmentManager().executePendingTransactions();

if (fragment.getClass() == HomeFragment.class) {
fab.setOnClickListener((v) -> ((HomeFragment) fragment).showFilter());

((HomeFragment) fragment).addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1))
fab.hide();
else if (!fab.isExpanded()) {
fab.show();
fab.setCompatElevation(10);
}
}
});

} else if (fragment.getClass() == PlaylistFragment.class) {
fab.setOnClickListener(null);
}

// TODO: reInflateOptionsMenuAccordingToFragment
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.act_icon_refresh_log).setVisible(mState);

MenuItem menuItem = menu.findItem(R.id.act_icon_search);
refreshLogItem = menu.findItem(R.id.act_icon_refresh_log);

menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
Expand All @@ -116,25 +127,23 @@ public boolean onMenuItemActionCollapse(MenuItem item) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
HomeFragment homeFragment = (HomeFragment) getFragment(fragment.HOME);
HomeFragment homeFragment = (HomeFragment) getFragment(HomeFragment.class);
if (homeFragment != null)
homeFragment.filter(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
HomeFragment homeFragment = (HomeFragment) getFragment(fragment.HOME);
HomeFragment homeFragment = (HomeFragment) getFragment(HomeFragment.class);
if (homeFragment != null)
homeFragment.filter(newText);
return false;
}
});

searchView.findViewById(androidx.appcompat.R.id.search_plate).setBackgroundColor(Color.TRANSPARENT);

EditText searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
searchEditText.setHintTextColor(Color.GRAY);
((EditText) searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.GRAY);

return true;
}
Expand All @@ -153,83 +162,39 @@ public void onBackPressed() {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

if (id == R.id.act_icon_refresh_log) {
mState = false;
supportInvalidateOptionsMenu();
refreshLogItem.setVisible(false);
return true;
} else if (id == R.id.action_download_list) {
HomeFragment homeFragment = (HomeFragment) getFragment(fragment.HOME);
HomeFragment homeFragment = (HomeFragment) getFragment(HomeFragment.class);
if (homeFragment != null)
if (homeFragment.getNumItems() <= MAX_DOWNLOADS)
new AlertDialog.Builder(this, R.style.AppTheme_AlertDialog).setPositiveButton("Download!", (DialogInterface dialog, int which) -> {
homeFragment.downloadFilter();
}).setTitle("Caution!").setMessage("By clicking download you will init a thread for every music in the screen, this can take some space and last longer...").setCancelable(true).create().show();
if (homeFragment.getNumItems() <= Config.MAX_DOWNLOADS)
new AlertDialog.Builder(this, R.style.AppTheme_AlertDialog).setPositiveButton("Download!", (DialogInterface dialog, int which) -> homeFragment.downloadFilter()).setTitle("Caution!")
.setMessage("By clicking download you will init a thread for every music in the screen, this can take some space and last longer...").setCancelable(true).create().show();
else
new AlertDialog.Builder(this, R.style.AppTheme_AlertDialog).setTitle("Too many musics!").setMessage("You can filter to get a minor number...").setCancelable(true).create().show();
new AlertDialog.Builder(this, R.style.AppTheme_AlertDialog).setTitle("Too many musics!").setMessage("You can filter to get a minor number...")
.setPositiveButton("ok", null).create().show();

} else if (id == R.id.action_clean_db) {
deleteDatabase(GMusicDB.DATABASE_NAME);
swapFragment(fragment.HOME);
swapAndSetupFragment(new HomeFragment());
return true;
} else if (id == R.id.action_refresh_db) {
mState = true;
supportInvalidateOptionsMenu();
refreshLogItem.setVisible(true);
refreshDB();
return true;
} else if (id == R.id.action_recreate) {
swapFragment(fragment.HOME);
} else if (id == R.id.action_logout) {
this.getSharedPreferences(getString(R.string.preferences_user), Context.MODE_PRIVATE).edit().remove(getString(R.string.token)).remove(getString(R.string.last_update)).apply();
deleteDatabase(GMusicDB.DATABASE_NAME);
swapFragment(fragment.HOME);
return true;
}
return super.onOptionsItemSelected(item);
}

private void swapFragment(fragment mFragment) {
if (mFragment == fragment.PLAYLIST) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, new PlaylistFragment()).commit();
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, new HomeFragment()).commit();
getSupportFragmentManager().executePendingTransactions();

HomeFragment homeFragment = (HomeFragment) getFragment(fragment.HOME);
if (homeFragment != null) {
homeFragment.setOnReachListEndListener(new HomeFragment.OnReachListEndListener() {
@Override
public void OnReachListEnd(RecyclerView recyclerView) {
if (fab != null) {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1))
fab.hide();
else if (!fab.isExpanded()) {
fab.show();
fab.setCompatElevation(10);
}
}
});
}
}
});
}

}
} else if (id == R.id.action_recreate)
swapAndSetupFragment(new HomeFragment());

return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LOGIN_ACTIVITY) {
if (requestCode == Config.LOGIN_ACTIVITY) {
if (resultCode == RESULT_OK) {
(getSharedPreferences(getString(R.string.preferences_user), Context.MODE_PRIVATE).edit()).putString(getString(R.string.token), data.getStringExtra(getString(R.string.token))).apply();
recreate();
Expand All @@ -246,11 +211,11 @@ private void refreshDB() {
new GMusicNet(this).execute(mPresets.getString(getString(R.string.token), ""));
}

private Fragment getFragment(fragment mFragment) {
private Fragment getFragment(Class mClass) {
Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_content);
if (mFragment == fragment.HOME && myFragment.getClass().getName() == HomeFragment.class.getName()) {
if (mClass == HomeFragment.class && myFragment.getClass() == HomeFragment.class) {
return myFragment;
} else if (mFragment == fragment.PLAYLIST && myFragment.getClass().getName() == PlaylistFragment.class.getName()) {
} else if (mClass == PlaylistFragment.class && myFragment.getClass() == PlaylistFragment.class) {
return myFragment;
} else
return null;
Expand All @@ -261,27 +226,24 @@ public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
// Handle navigation view item clicks here.
int id = menuItem.getItemId();

if (id == R.id.nav_all) {
swapFragment(fragment.HOME);
} else if (id == R.id.nav_playlist) {
swapFragment(fragment.PLAYLIST);
} else if (id == R.id.nav_settings) {
// TODO: SettingsActivity
if (id == R.id.nav_all)
swapAndSetupFragment(new HomeFragment());
else if (id == R.id.nav_playlist)
swapAndSetupFragment(new PlaylistFragment());
else if (id == R.id.nav_settings)
Toast.makeText(this, getString(R.string.null_description), Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_log) {
// TODO: LogActivity
else if (id == R.id.nav_log)
Toast.makeText(this, getString(R.string.null_description), Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_info) {
// TODO: InfoActivity
else if (id == R.id.nav_info)
Toast.makeText(this, getString(R.string.null_description), Toast.LENGTH_SHORT).show();
else if (id == R.id.nav_logout) {
this.getSharedPreferences(getString(R.string.preferences_user), Context.MODE_PRIVATE).edit().remove(getString(R.string.token)).remove(getString(R.string.last_update)).apply();
deleteDatabase(GMusicDB.DATABASE_NAME);
recreate();
}

drawer.closeDrawer(GravityCompat.START);
return true;
}

private enum fragment {
HOME, PLAYLIST
}
}

23 changes: 12 additions & 11 deletions app/src/main/java/com/d3coding/gmusicapi/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

public class HomeFragment extends Fragment {

private static final int DOWNLOADS_NUM = 5;

private GMusicDB db;

private int sort = 0;
Expand All @@ -62,11 +60,14 @@ public class HomeFragment extends Fragment {

private GMusicFile gmusicFile;

public OnReachListEndListener onReachListEndListener;
private RecyclerView.OnScrollListener mOnScrollListener;

private ThreadPoolExecutor downloadQueueService;

void setOnReachListEndListener(OnReachListEndListener mOnReachListEndListener) {
this.onReachListEndListener = mOnReachListEndListener;
void addOnScrollListener(RecyclerView.OnScrollListener mOnScrollListener) {
this.mOnScrollListener = mOnScrollListener;
if (recyclerView != null)
recyclerView.addOnScrollListener(mOnScrollListener);
}

@Override
Expand All @@ -83,7 +84,8 @@ public void onViewCreated(@NonNull View layoutView, @Nullable Bundle savedInstan
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

this.onReachListEndListener.OnReachListEnd(recyclerView);
if (mOnScrollListener != null)
recyclerView.addOnScrollListener(mOnScrollListener);

updateList();

Expand Down Expand Up @@ -182,7 +184,7 @@ public void onViewCreated(@NonNull View layoutView, @Nullable Bundle savedInstan

if (progress.doing == GMusicFile.Doing.completed)
exec.shutdown();
}, 100, 100, TimeUnit.MILLISECONDS);
}, 0, 1, TimeUnit.MILLISECONDS);

builder.setOnCancelListener((dialog) -> exec.shutdown());
}
Expand Down Expand Up @@ -218,7 +220,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

void initThreads() {
if (downloadQueueService == null)
downloadQueueService = (ThreadPoolExecutor) Executors.newFixedThreadPool(DOWNLOADS_NUM);
downloadQueueService = (ThreadPoolExecutor) Executors.newFixedThreadPool(Config.SIMULTANEOUS_DOWNLOAD);
}

void downloadFilter() {
Expand Down Expand Up @@ -260,9 +262,6 @@ void filter(String filterText) {
updateList();
}

public interface OnReachListEndListener {
void OnReachListEnd(RecyclerView recyclerView);
}

void updateList() {
if (db == null)
Expand All @@ -271,6 +270,8 @@ void updateList() {
ConvertList.clear();
ConvertList.addAll(db.getMusicItems(sort, sortOnline, filterText, desc));
mAdapter.notifyDataSetChanged();

recyclerView.startLayoutAnimation();
}

}
Loading

0 comments on commit b896c3f

Please sign in to comment.