Skip to content

Commit

Permalink
[Feature] Enable multi select themes for Style Transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
rh-id committed Aug 8, 2023
1 parent f46a294 commit 06da82d
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.net.Uri;

import java.io.File;
import java.util.Collection;
import java.util.concurrent.ExecutorService;

import io.reactivex.rxjava3.core.Single;
Expand All @@ -21,7 +22,7 @@ public STApplyCommand(Provider provider) {
mSTEngine = provider.get(STEngine.class);
}

public Single<File> execute(Uri uriFile, int theme) {
public Single<File> execute(Uri uriFile, Collection<Integer> themes) {
return Single.fromFuture(mExecutorService.submit(() -> {
String fullPath = uriFile.getPath();
int cut = fullPath.lastIndexOf("/");
Expand All @@ -33,7 +34,7 @@ public Single<File> execute(Uri uriFile, int theme) {
}
File result = mFileHelper
.createImageTempFile(fileName, uriFile);
mSTEngine.enqueueST(result, theme);
mSTEngine.enqueueST(result, themes);
return result;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void onActivityResult(View currentView, Activity activity, INavigator INa
if (!uriList.isEmpty()) {
mRxDisposer.add("onActivityResult_nstApplyPicture_multiple",
Flowable.fromIterable(uriList)
.map(uri -> mSTApplyCommand.execute(uri, mSelectedNSTTheme.getSelectedTheme())
.map(uri -> mSTApplyCommand.execute(uri, mSelectedNSTTheme.getSelectedThemes())
.blockingGet())
.subscribeOn(Schedulers.from(mExecutorService))
.doOnError(throwable -> consumeFile.accept(null, throwable))
Expand All @@ -456,7 +456,7 @@ public void onActivityResult(View currentView, Activity activity, INavigator INa
}
} else {
mRxDisposer.add("onActivityResult_nstApplyPicture",
mSTApplyCommand.execute(fullPhotoUri, mSelectedNSTTheme.getSelectedTheme())
mSTApplyCommand.execute(fullPhotoUri, mSelectedNSTTheme.getSelectedThemes())
.subscribeOn(Schedulers.from(mExecutorService))
.subscribe(consumeFile));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,44 @@
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import co.rh.id.lib.rx3_utils.subject.SerialBehaviorSubject;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import m.co.rh.id.a_jarwis.R;
import m.co.rh.id.a_jarwis.app.ui.page.nav.param.SelectedTheme;
import m.co.rh.id.a_jarwis.base.provider.IStatefulViewProvider;
import m.co.rh.id.a_jarwis.base.rx.RxDisposer;
import m.co.rh.id.a_jarwis.ml_engine.provider.component.STEngine;
import m.co.rh.id.anavigator.StatefulView;
import m.co.rh.id.anavigator.annotation.NavInject;
import m.co.rh.id.anavigator.component.INavigator;
import m.co.rh.id.anavigator.component.RequireComponent;
import m.co.rh.id.aprovider.Provider;

public class SelectSTThemePage extends StatefulView<Activity> implements View.OnClickListener {
public class SelectSTThemePage extends StatefulView<Activity> implements RequireComponent<Provider>, View.OnClickListener {

@NavInject
private transient INavigator mNavigator;

private transient Provider mSvProvider;

private transient RxDisposer mRxDisposer;

private SerialBehaviorSubject<HashSet<Integer>> mSelectedThemesId;

public SelectSTThemePage() {
mSelectedThemesId = new SerialBehaviorSubject<>(new HashSet<>());
}

@Override
public void provideComponent(Provider provider) {
mSvProvider = provider.get(IStatefulViewProvider.class);
mRxDisposer = mSvProvider.get(RxDisposer.class);
}

@Override
protected View createView(Activity activity, ViewGroup container) {
View rootView = activity.getLayoutInflater().inflate(R.layout.page_select_st_theme, container, false);
Expand All @@ -31,24 +57,66 @@ protected View createView(Activity activity, ViewGroup container) {
pointilism.setOnClickListener(this);
View backButton = rootView.findViewById(R.id.button_back);
backButton.setOnClickListener(this);
View nextButton = rootView.findViewById(R.id.button_next);
nextButton.setOnClickListener(this);
mRxDisposer.add("createView_onSelectedThemesChanged", mSelectedThemesId.getSubject()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(integers -> {
mosaic.setSelected(false);
candy.setSelected(false);
rainPrincess.setSelected(false);
udnie.setSelected(false);
pointilism.setSelected(false);
for (Integer id : integers) {
rootView.findViewById(id).setSelected(true);
}
nextButton.setEnabled(!integers.isEmpty());
}));
return rootView;
}

@Override
public void dispose(Activity activity) {
super.dispose(activity);
if (mSvProvider != null) {
mSvProvider.dispose();
mSvProvider = null;
}
}

@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.container_mosaic_theme) {
mNavigator.pop(new SelectedTheme(STEngine.THEME_MOSAIC));
} else if (id == R.id.container_candy_theme) {
mNavigator.pop(new SelectedTheme(STEngine.THEME_CANDY));
} else if (id == R.id.container_rain_princess_theme) {
mNavigator.pop(new SelectedTheme(STEngine.THEME_RAIN_PRINCESS));
} else if (id == R.id.container_udnie_theme) {
mNavigator.pop(new SelectedTheme(STEngine.THEME_UDNIE));
} else if (id == R.id.container_pointilism_theme) {
mNavigator.pop(new SelectedTheme(STEngine.THEME_POINTILISM));
} else if (id == R.id.button_back) {
if (id == R.id.button_back) {
mNavigator.pop();
} else if (id == R.id.button_next) {
List<Integer> result = new ArrayList<>();
HashSet<Integer> hashSet = mSelectedThemesId.getValue();
for (Integer themeId : hashSet) {
if (themeId == R.id.container_mosaic_theme) {
result.add(STEngine.THEME_MOSAIC);
} else if (themeId == R.id.container_candy_theme) {
result.add(STEngine.THEME_CANDY);
} else if (themeId == R.id.container_rain_princess_theme) {
result.add(STEngine.THEME_RAIN_PRINCESS);
} else if (themeId == R.id.container_udnie_theme) {
result.add(STEngine.THEME_UDNIE);
} else if (themeId == R.id.container_pointilism_theme) {
result.add(STEngine.THEME_POINTILISM);
}
}
mNavigator.pop(new SelectedTheme(result));
} else {
selectReselectTheme(id);
}
}

private void selectReselectTheme(int theme) {
HashSet<Integer> hashSet = mSelectedThemesId.getValue();
boolean added = hashSet.add(theme);
if (!added) {
hashSet.remove(theme);
}
mSelectedThemesId.onNext(hashSet);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package m.co.rh.id.a_jarwis.app.ui.page.nav.param;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class SelectedTheme implements Serializable {
private int selectedTheme;
private ArrayList<Integer> selectedThemes;

public SelectedTheme(int selectedTheme) {
this.selectedTheme = selectedTheme;
public SelectedTheme(Collection<Integer> selectedThemes) {
this.selectedThemes = new ArrayList<>(selectedThemes);
}

public int getSelectedTheme() {
return selectedTheme;
public ArrayList<Integer> getSelectedThemes() {
return selectedThemes;
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/state_selected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red_800"
android:state_selected="true"/>
</selector>
32 changes: 26 additions & 6 deletions app/src/main/res/layout/page_select_st_theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="@integer/grid_column_count"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container_mosaic_theme"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_columnWeight="1"
android:layout_margin="@dimen/text_margin">
android:layout_margin="@dimen/text_margin"
android:background="@drawable/state_selected">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/text_margin"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Expand All @@ -55,11 +56,13 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_columnWeight="1"
android:layout_margin="@dimen/text_margin">
android:layout_margin="@dimen/text_margin"
android:background="@drawable/state_selected">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/text_margin"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Expand All @@ -86,11 +89,13 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_columnWeight="1"
android:layout_margin="@dimen/text_margin">
android:layout_margin="@dimen/text_margin"
android:background="@drawable/state_selected">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/text_margin"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Expand All @@ -117,11 +122,13 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_columnWeight="1"
android:layout_margin="@dimen/text_margin">
android:layout_margin="@dimen/text_margin"
android:background="@drawable/state_selected">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/text_margin"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Expand All @@ -148,11 +155,13 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_columnWeight="1"
android:layout_margin="@dimen/text_margin">
android:layout_margin="@dimen/text_margin"
android:background="@drawable/state_selected">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/text_margin"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Expand Down Expand Up @@ -197,5 +206,16 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/red_800"
android:text="@string/next"
app:icon="@drawable/ic_arrow_forward_white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;

import m.co.rh.id.a_jarwis.base.provider.component.helper.FileHelper;
import m.co.rh.id.a_jarwis.base.util.SerializeUtils;
Expand Down Expand Up @@ -78,12 +79,12 @@ public Bitmap apply(Bitmap bitmap, int theme) {
}
}

public void enqueueST(File imageFile, int theme) {
public void enqueueST(File imageFile, Collection<Integer> themes) {
ObjectOutputStream oos = null;
try {
File serialFile = mFileHelper.createTempFile();
oos = new ObjectOutputStream(new FileOutputStream(serialFile));
STApplySerialFile STApplySerialFile = new STApplySerialFile(imageFile, theme);
STApplySerialFile STApplySerialFile = new STApplySerialFile(imageFile, themes);
oos.writeObject(STApplySerialFile);
oos.close();
Data.Builder inputBuilder = new Data.Builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import m.co.rh.id.a_jarwis.base.BaseApplication;
import m.co.rh.id.a_jarwis.base.provider.component.helper.MediaHelper;
Expand Down Expand Up @@ -46,18 +47,18 @@ public Result doWork() {
ois = new ObjectInputStream(new FileInputStream(serialFile));
STApplySerialFile STApplySerialFile = (STApplySerialFile) ois.readObject();
inputFile = STApplySerialFile.getInputFile();
int theme = STApplySerialFile.getTheme();

ArrayList<Integer> themes = STApplySerialFile.getThemes();
Bitmap input = BitmapFactory.decodeFile(inputFile.getAbsolutePath());
Bitmap applied = STEngine.apply(input, theme);
String fileName = inputFile.getName();
if (applied != null) {
mediaHelper.insertImage(applied, fileName, fileName);
applied.recycle();
input.recycle();
logger.i(TAG, getApplicationContext()
.getString(m.co.rh.id.a_jarwis.base.R.string.done_processing_, fileName));
for (Integer theme : themes) {
Bitmap applied = STEngine.apply(input, theme);
if (applied != null) {
mediaHelper.insertImage(applied, fileName, fileName);
applied.recycle();
}
}
logger.i(TAG, getApplicationContext()
.getString(m.co.rh.id.a_jarwis.base.R.string.done_processing_, fileName));
} catch (Exception e) {
logger.e(TAG, e.getMessage(), e);
return Result.failure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class STApplySerialFile implements Serializable {
private File inputFile;
private int theme;
private ArrayList<Integer> themes;

public STApplySerialFile(File inputFile, int theme) {
public STApplySerialFile(File inputFile, Collection<Integer> themes) {
this.inputFile = inputFile;
this.theme = theme;
this.themes = new ArrayList<>(themes);
}

public File getInputFile() {
return inputFile;
}

public int getTheme() {
return theme;
public ArrayList<Integer> getThemes() {
return themes;
}

}

0 comments on commit 06da82d

Please sign in to comment.