Skip to content

Commit

Permalink
Added the option of selecting out of a prefilled popular player array
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeFot committed Jul 4, 2017
1 parent b33b099 commit 9831921
Show file tree
Hide file tree
Showing 25 changed files with 582 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public synchronized void clear() {
mJobPriorityQueue.clear();
}

public void startFetchPlayersJob(@NonNull final String username) {
mJobPriorityQueue.queueJob(mFactory.getFetchPlayersJob(username));
public void startFetchPlayersJob(@NonNull final String username, final boolean storeToDb) {
mJobPriorityQueue.queueJob(mFactory.getFetchPlayersJob(username, storeToDb));
}

public void startFetchLibraryJob(@NonNull final String steamId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public JobFactory() {
Injector.getComponentStore().getAndroidAwareComponent().inject(this);
}

public FetchPlayersJob getFetchPlayersJob(@NonNull final String username) {
public FetchPlayersJob getFetchPlayersJob(@NonNull final String username,
final boolean storeToDb) {
return new FetchPlayersJob(
username,
storeToDb,
mSteamLoader.getUsersProvider(),
mAppDatabase.getPlayerDao(),
mDataPreferences,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ public class FetchPlayersJob extends BaseJob {
private static final long UPDATE_THRESHOLD = TimeUnit.MINUTES.toMillis(5);

private final String mUsername;
private final boolean mStoreItToDb;
private final UsersApiProvider mApi;
private final DataPreferences mDataPreferences;
private final NetworkResolver mNetworkResolver;
private final PlayerDao mDao;

public FetchPlayersJob(@NonNull final String username,
final boolean isStore,
@NonNull final UsersApiProvider api,
@NonNull final PlayerDao dao,
@NonNull final DataPreferences dataPreferences,
final NetworkResolver networkResolver) {
super();
mUsername = username;
mStoreItToDb = isStore;
mApi = api;
mDao = dao;
mDataPreferences = dataPreferences;
Expand Down Expand Up @@ -140,17 +143,20 @@ private void fetchUserFromId64(@NonNull final Long id64) {
public void onSuccess(final ResponseContainer<PlayerSummaries> result) {
AppLog.d(result.getResponse().getPlayers().size() + " results fetched");
if (result.getResponse() != null) {
if (mStoreItToDb) {
new Thread(new Runnable() {
@Override
public void run() {
// we need to move back to a BG thread as Retrofit 2 callback is on Main
final List<PlayerEntity> entities = PlayerEntity.fromPlayerSummaries(result.getResponse().getPlayers());
mDao.insert(entities);
mDataPreferences.writeProfileUpdated(id64.toString(), System.currentTimeMillis());

new Thread(new Runnable() {
@Override
public void run() {
// we need to move back to a BG thread as Retrofit 2 callback is on Main
final List<PlayerEntity> entities = PlayerEntity.fromPlayerSummaries(result.getResponse().getPlayers());
mDao.insert(entities);
mDataPreferences.writeProfileUpdated(id64.toString(), System.currentTimeMillis());
postResult(result.getResponse().getPlayers());
}
}).start();
}
}).start();
} else {
postResult(result.getResponse().getPlayers());
}

} else {
postError(new Error(ErrorKind.INVALID_CONTENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.michaelfotiadis.dota2viewer.data.loader.jobs.JobFactory;
import com.michaelfotiadis.dota2viewer.ui.activity.details.match.DotaMatchDetailsFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.main.LoginFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.popular.PopularPlayersFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.result.PlayerPickerFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.web.WebViewFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.main.MainActivity;
Expand Down Expand Up @@ -62,4 +63,7 @@ public interface AndroidAwareComponent {
void inject(JobFactory jobFactory);

void inject(WebViewFragment webViewFragment);

void inject(PopularPlayersFragment popularPlayersFragment);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.michaelfotiadis.dota2viewer.R;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.main.LoginFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.popular.PopularPlayersFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.result.PlayerPickerFragment;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.web.WebViewFragment;
import com.michaelfotiadis.dota2viewer.ui.core.base.activity.BaseActivity;
Expand Down Expand Up @@ -80,6 +81,11 @@ public void showHelp(final View view) {
// TODO implement this!!!!
}

@Override
public void onNavigateToPopular(final View view) {
replaceContentFragment(PopularPlayersFragment.newInstance(), CONTENT_ID, FRAGMENT_TAG);
}

@Override
public void onBackPressed() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface LoginNavigationCommand {
void showSteamLogin(View view);

void showHelp(View view);

void onNavigateToPopular(View view);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.support.annotation.NonNull;
import android.view.View;

public interface LoginActionCallbacks {
interface LoginActionCallbacks {

void login(@NonNull String username);

void showLoginHelp(View view);

void showSteamLogin(View view);

void onPopularSelected(View view);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.michaelfotiadis.dota2viewer.data.loader.error.ErrorKind;
import com.michaelfotiadis.dota2viewer.data.persistence.error.UiDataLoadError;
import com.michaelfotiadis.dota2viewer.data.persistence.error.UiDataLoadErrorFactory;
import com.michaelfotiadis.dota2viewer.event.listener.EventLifecycleListener;
import com.michaelfotiadis.dota2viewer.event.steam.FetchedPlayersEvent;
import com.michaelfotiadis.dota2viewer.injection.Injector;
import com.michaelfotiadis.dota2viewer.ui.activity.login.LoginNavigationCommand;
Expand All @@ -32,14 +31,13 @@ public class LoginFragment extends BaseFragment implements LoginActionCallbacks

@Inject
JobScheduler mJobScheduler;
private EventLifecycleListener mEventLifecycleListener;
private LoginPresenter mLoginPresenter;

@Override
public void login(@NonNull final String username) {
hideKeyboard();
mLoginPresenter.showProgress();
mJobScheduler.startFetchPlayersJob(username);
mJobScheduler.startFetchPlayersJob(username, false);
}

@Override
Expand All @@ -56,6 +54,13 @@ public void showSteamLogin(final View view) {
}
}

@Override
public void onPopularSelected(final View view) {
if (getActivity() instanceof LoginNavigationCommand) {
((LoginNavigationCommand) getActivity()).onNavigateToPopular(view);
}
}

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,23 @@ public void onInfoClicked(final ImageView view) {
}
});

mViewHolder.mSteamButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
mCallbacks.showSteamLogin(view);
}
}
mViewHolder.mSteamButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(final View view) {
mCallbacks.showSteamLogin(view);
}
}
);

mViewHolder.mPopularButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(final View view) {
mCallbacks.onPopularSelected(view);
}
});

}

protected void login(final String username) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LoginViewHolder extends BaseViewHolder {
Button mLoginButton;
@BindView(R.id.button_steam)
Button mSteamButton;
@BindView(R.id.button_popular)
Button mPopularButton;

LoginViewHolder(final View view) {
super(view);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.popular;

public class PopularPlayer {

private final String mName;
private final String mId;

public PopularPlayer(final String name, final String id) {
mName = name;
mId = id;
}

public String getName() {
return mName;
}

public String getId() {
return mId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.popular;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.michaelfotiadis.dota2viewer.R;
import com.michaelfotiadis.dota2viewer.data.loader.JobScheduler;
import com.michaelfotiadis.dota2viewer.data.loader.error.Error;
import com.michaelfotiadis.dota2viewer.data.loader.error.ErrorKind;
import com.michaelfotiadis.dota2viewer.event.steam.FetchedPlayersEvent;
import com.michaelfotiadis.dota2viewer.injection.Injector;
import com.michaelfotiadis.dota2viewer.ui.activity.login.LoginNavigationCommand;
import com.michaelfotiadis.dota2viewer.ui.activity.login.fragment.popular.recycler.PopularPlayersRecyclerAdapter;
import com.michaelfotiadis.dota2viewer.ui.core.base.fragment.BaseRecyclerFragment;
import com.michaelfotiadis.dota2viewer.ui.core.base.fragment.Searchable;
import com.michaelfotiadis.dota2viewer.ui.core.base.recyclerview.listener.OnItemSelectedListener;
import com.michaelfotiadis.dota2viewer.ui.core.base.recyclerview.manager.RecyclerManager;
import com.michaelfotiadis.dota2viewer.ui.core.base.recyclerview.manager.State;
import com.michaelfotiadis.dota2viewer.ui.core.base.viewmanagement.SimpleUiStateKeeper;
import com.michaelfotiadis.dota2viewer.ui.core.base.viewmanagement.UiStateKeeper;
import com.michaelfotiadis.dota2viewer.ui.view.utils.RecyclerUtils;
import com.michaelfotiadis.dota2viewer.utils.AppLog;
import com.michaelfotiadis.dota2viewer.utils.TextUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;

public class PopularPlayersFragment extends BaseRecyclerFragment<PopularPlayer> implements OnItemSelectedListener<PopularPlayer>, Searchable {

private final List<PopularPlayer> mData = new ArrayList<>();

@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
@Inject
JobScheduler mJobScheduler;


public static Fragment newInstance() {
return new PopularPlayersFragment();
}

@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getEventLifecycleListener().enable();
initRecyclerManager(view);
loadData();
}

@Override
public void onAttach(final Context context) {
Injector.getComponentStore().getAndroidAwareComponent().inject(this);
super.onAttach(context);
}

@Override
protected void initRecyclerManager(final View view) {

final UiStateKeeper uiStateKeeper = new SimpleUiStateKeeper(view, mRecyclerView);
mRecyclerView.setHasFixedSize(true);

final int columns = RecyclerUtils.getColumnsForScreen(getActivity());
final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);
layoutManager.setOrientation(GridLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(layoutManager);

mRecyclerManager = new RecyclerManager.Builder<>(new PopularPlayersRecyclerAdapter(getContext(), this))
.setRecycler(mRecyclerView)
.setStateKeeper(uiStateKeeper)
.setEmptyMessage("Nothing to see here")
.build();

mRecyclerManager.updateUiState();

}

@Override
protected void loadData() {

mData.clear();

final String[] array = getResources().getStringArray(R.array.popular_players);

final String delimiter = ",";
for (final String item : array) {

if (TextUtils.isNotEmpty(item) && item.contains(delimiter)) {
final String[] parts = item.split(delimiter);
mData.add(new PopularPlayer(parts[0], parts[1]));
}

}

mRecyclerManager.setItems(mData);

}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onDataLoadedEvent(final FetchedPlayersEvent event) {

if (event.getError() != null) {
setRecyclerError(event.getError());
} else if (event.getPlayers() == null || event.getPlayers().isEmpty()) {
setRecyclerError(new Error(ErrorKind.NO_CONTENT_RETURNED));
} else {
AppLog.d(String.format(Locale.UK, "Showing result for %d players", event.getPlayers().size()));
if (getActivity() instanceof LoginNavigationCommand) {
((LoginNavigationCommand) getActivity()).showPlayers(event.getPlayers());
}
}

}

@Override
public void onListItemSelected(final View view, final PopularPlayer item) {
mRecyclerManager.updateUiState(State.PROGRESS);
mJobScheduler.startFetchPlayersJob(item.getId(), false);
}

@Override
public void submitQuery(final String query) {

if (TextUtils.isNotEmpty(query)) {

final List<PopularPlayer> filtered = new ArrayList<>();
for (final PopularPlayer player : mData) {

if (player.getName().toLowerCase().contains(query)) {
filtered.add(player);
}

}
mRecyclerManager.setItems(filtered);

} else {
mRecyclerManager.setItems(mData);
}

}
}
Loading

0 comments on commit 9831921

Please sign in to comment.