Skip to content

Commit

Permalink
Fix warnings and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
charlag committed Jan 11, 2019
1 parent 65313c3 commit 5559cb6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,20 @@

package com.keylesspalace.tusky.fragment;

import androidx.arch.core.util.Function;
import androidx.lifecycle.Lifecycle;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.core.util.Pair;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.adapter.TimelineAdapter;
import com.keylesspalace.tusky.appstore.BlockEvent;
Expand Down Expand Up @@ -79,15 +64,28 @@
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.arch.core.util.Function;
import androidx.core.util.Pair;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import at.connyduck.sparkbutton.helpers.Utils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import kotlin.collections.CollectionsKt;
import retrofit2.Call;
import retrofit2.Callback;
Expand Down Expand Up @@ -157,8 +155,6 @@ private enum FetchEnd {
private boolean alwaysShowSensitiveMedia;
private boolean initialUpdateFailed = false;

private CompositeDisposable disposable = new CompositeDisposable();

@Override
protected TimelineCases timelineCases() {
return timelineCases;
Expand Down Expand Up @@ -252,9 +248,10 @@ private void sendInitialRequest() {
private void tryCache() {
// Request timeline from disk to make it quick, then replace it with timeline from
// the server to update it
this.disposable.add(this.timelineRepo.getStatuses(null, null, LOAD_AT_ONCE,
this.timelineRepo.getStatuses(null, null, LOAD_AT_ONCE,
TimelineRequestMode.DISK)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(statuses -> {
filterStatuses(statuses);

Expand All @@ -268,8 +265,7 @@ private void tryCache() {
}

this.updateCurrent();
})
);
});
}

private void updateCurrent() {
Expand All @@ -279,9 +275,10 @@ private void updateCurrent() {
} else {
topId = CollectionsKt.first(statuses, Either::isRight).asRight().getId();
}
disposable.add(this.timelineRepo.getStatuses(topId, null, LOAD_AT_ONCE,
this.timelineRepo.getStatuses(topId, null, LOAD_AT_ONCE,
TimelineRequestMode.NETWORK)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
(statuses) -> {
this.initialUpdateFailed = false;
Expand Down Expand Up @@ -311,8 +308,7 @@ private void updateCurrent() {
// Indicate that we are not loading anymore
this.progressBar.setVisibility(View.GONE);
this.swipeRefreshLayout.setRefreshing(false);
})
);
});
}

private void setupTimelinePreferences() {
Expand Down Expand Up @@ -537,13 +533,13 @@ public void onReply(int position) {
@Override
public void onReblog(final boolean reblog, final int position) {
final Status status = statuses.get(position).asRight();
disposable.add(timelineCases.reblog(status, reblog)
timelineCases.reblog(status, reblog)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
(newStatus) -> setRebloggedForStatus(position, status, reblog),
(err) -> Log.d(TAG, "Failed to reblog status " + status.getId(), err)
)
);
);
}

private void setRebloggedForStatus(int position, Status status, boolean reblog) {
Expand All @@ -569,13 +565,13 @@ private void setRebloggedForStatus(int position, Status status, boolean reblog)
public void onFavourite(final boolean favourite, final int position) {
final Status status = statuses.get(position).asRight();

disposable.add(timelineCases.favourite(status, favourite)
timelineCases.favourite(status, favourite)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
(newStatus) -> setFavouriteForStatus(position, newStatus, favourite),
(err) -> Log.d(TAG, "Failed to favourite status " + status.getId(), err)
)
);
);
}

private void setFavouriteForStatus(int position, Status status, boolean favourite) {
Expand Down Expand Up @@ -872,14 +868,13 @@ private void sendFetchTimelineRequest(@Nullable String fromId, @Nullable String
} else {
mode = TimelineRequestMode.NETWORK;
}
disposable.add(
timelineRepo.getStatuses(fromId, uptoId, LOAD_AT_ONCE, mode)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
(result) -> onFetchTimelineSuccess(result, fetchEnd, pos),
(err) -> onFetchTimelineFailure(new Exception(err), fetchEnd, pos)
)
);
);
} else {
Callback<List<Status>> callback = new Callback<List<Status>>() {
@Override
Expand Down Expand Up @@ -925,6 +920,11 @@ private void onFetchTimelineSuccess(List<Either<Placeholder, Status>> statuses,
this.statuses.remove(this.statuses.size() - 1);
updateAdapter();
}

if (!statuses.isEmpty() && !statuses.get(statuses.size() - 1).isRight()) {
// Removing placeholder if it's the last one from the cache
statuses.remove(statuses.size() - 1);
}
int oldSize = this.statuses.size();
if (this.statuses.size() > 1) {
addItems(statuses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TimelineRepositoryImpl(
statusesCopy.removeAt(0)
}
if (sinceId != null && statusesCopy.lastOrNull()?.id == sinceId) {
statusesCopy.removeAt(statuses.size - 1)
statusesCopy.removeAt(statusesCopy.size - 1)
}

return statusesCopy.map { s -> Either.Right<Placeholder, Status>(s) }
Expand Down Expand Up @@ -130,7 +130,9 @@ class TimelineRepositoryImpl(
limit: Int): Single<out List<TimelineStatus>> {
return timelineDao.getStatusesForAccount(accountId, maxId, sinceId, limit)
.subscribeOn(Schedulers.io())
.map { statuses -> statuses.map { it.toStatus() } }
.map { statuses ->
statuses.map { it.toStatus() }
}
}

private fun saveStatusesToDb(instance: String, accountId: Long, statuses: List<Status>,
Expand Down

0 comments on commit 5559cb6

Please sign in to comment.