@ -3,9 +3,22 @@ package cc.niushuai.dididone.ui.setting.project;
@@ -3,9 +3,22 @@ package cc.niushuai.dididone.ui.setting.project;
import android.content.Intent ;
import android.graphics.Color ;
import android.os.Bundle ;
import android.text.Editable ;
import android.text.TextWatcher ;
import android.view.Gravity ;
import android.view.View ;
import android.widget.AdapterView ;
import android.widget.ArrayAdapter ;
import android.widget.Spinner ;
import android.widget.TextView ;
import androidx.appcompat.app.AlertDialog ;
import androidx.recyclerview.widget.DefaultItemAnimator ;
import androidx.recyclerview.widget.DividerItemDecoration ;
import androidx.recyclerview.widget.LinearLayoutManager ;
import androidx.recyclerview.widget.RecyclerView ;
import com.mikepenz.community_material_typeface_library.CommunityMaterial ;
import com.mikepenz.iconics.IconicsDrawable ;
import com.skydoves.colorpickerview.ColorEnvelope ;
import com.skydoves.colorpickerview.ColorPickerDialog ;
@ -13,10 +26,17 @@ import com.skydoves.colorpickerview.ColorPickerView;
@@ -13,10 +26,17 @@ import com.skydoves.colorpickerview.ColorPickerView;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener ;
import com.skydoves.colorpickerview.listeners.ColorListener ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Set ;
import java.util.function.Predicate ;
import cc.niushuai.dididone.R ;
import cc.niushuai.dididone.biz.BizGlobal ;
import cc.niushuai.dididone.biz.entity.SavedIcon ;
import cc.niushuai.dididone.databinding.ActivityNewProjectBinding ;
import cc.niushuai.dididone.ui.base.BaseActivity ;
import cc.niushuai.dididone.ui.setting.icon.recycle.IconRecycleAdapter ;
import cc.niushuai.dididone.util.Toasts ;
import cc.niushuai.dididone.util.XLog ;
import cn.hutool.core.util.ReflectUtil ;
@ -26,6 +46,12 @@ public class NewProjectActivity extends BaseActivity {
@@ -26,6 +46,12 @@ public class NewProjectActivity extends BaseActivity {
private ActivityNewProjectBinding binding ;
private int selectedColor = - 324225874 ;
private String selectedIcon = CommunityMaterial . Icon . cmd_access_point . name ( ) ;
/ * *
* icon 展示 recycleViewAdapter
* /
private IconRecycleAdapter iconRecycleAdapter ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
@ -43,14 +69,110 @@ public class NewProjectActivity extends BaseActivity {
@@ -43,14 +69,110 @@ public class NewProjectActivity extends BaseActivity {
setTitle ( "添加新打卡项" ) ;
}
private void initIconDialogView ( View iconDialogView ) {
initIconDialogRecyclerView ( iconDialogView ) ;
initIconDialogSpinner ( iconDialogView ) ;
addIconDialogSearchTxtListener ( iconDialogView ) ;
}
private void addIconDialogSearchTxtListener ( View iconDialogView ) {
TextView iconDialogSearchTxtView = iconDialogView . findViewById ( R . id . icons_dialog_search ) ;
iconDialogSearchTxtView . addTextChangedListener ( new TextWatcher ( ) {
@Override
public void beforeTextChanged ( CharSequence s , int start , int count , int after ) {
}
@Override
public void onTextChanged ( CharSequence s , int start , int before , int count ) {
}
@Override
public void afterTextChanged ( Editable s ) {
iconRecycleAdapter . change ( null , item - > item . getIcon ( ) . contains ( s . toString ( ) ) ) ;
}
} ) ;
}
private void initIconDialogRecyclerView ( View iconDialogView ) {
RecyclerView iconDialogRecyclerView = iconDialogView . findViewById ( R . id . icons_dialog_recyclerView ) ;
iconDialogRecyclerView . addItemDecoration ( new DividerItemDecoration ( this , DividerItemDecoration . VERTICAL ) ) ;
DefaultItemAnimator itemAnimator = new DefaultItemAnimator ( ) ;
itemAnimator . setAddDuration ( 1000 ) ;
itemAnimator . setRemoveDuration ( 1000 ) ;
iconDialogRecyclerView . setItemAnimator ( itemAnimator ) ;
LinearLayoutManager layoutManager = new LinearLayoutManager ( this ) ;
iconDialogRecyclerView . setLayoutManager ( layoutManager ) ;
iconRecycleAdapter = new IconRecycleAdapter ( this , CommunityMaterial . Icon . class ) ;
iconDialogRecyclerView . setAdapter ( iconRecycleAdapter ) ;
}
private void initIconDialogSpinner ( View iconDialogView ) {
Set < String > keySet = IconRecycleAdapter . ICON_MAP . keySet ( ) ;
ArrayList < String > keys = new ArrayList < > ( keySet ) ;
Collections . sort ( keys ) ;
Spinner iconDialogSpinner = iconDialogView . findViewById ( R . id . icons_dialog_select ) ;
iconDialogSpinner . setAdapter ( new ArrayAdapter < String > ( this , com . necer . R . layout . support_simple_spinner_dropdown_item , keys ) ) ;
iconDialogSpinner . setSelection ( 0 ) ;
iconDialogSpinner . setOnItemSelectedListener ( new AdapterView . OnItemSelectedListener ( ) {
@Override
public void onItemSelected ( AdapterView < ? > parent , View view , int position , long id ) {
String key = keys . get ( position ) ;
String str = ( ( TextView ) iconDialogView . findViewById ( R . id . icons_dialog_search ) ) . getText ( ) . toString ( ) ;
Predicate < SavedIcon > filter = item - > true ;
if ( null ! = str & & str . length ( ) > 0 ) {
filter = item - > item . getIcon ( ) . contains ( str ) ;
}
iconRecycleAdapter . change ( key , filter ) ;
}
@Override
public void onNothingSelected ( AdapterView < ? > parent ) {
}
} ) ;
}
@Override
protected void setListeners ( ) {
// icon选择器
binding . npIconFill . setOnClickListener ( view - > {
} ) ;
iconClickListener ( ) ;
// 颜色选择器
colorPickListener ( ) ;
// 返回按钮
binding . npButtonBack . setOnClickListener ( view - > finish ( ) ) ;
// 确定按钮
binding . npButtonConfirm . setOnClickListener ( view - > {
// 三种属性
Intent data = new Intent ( ) ;
// 名称
String projectName = binding . npProjectNameTxt . getText ( ) . toString ( ) ;
if ( StrUtil . isEmpty ( projectName ) ) {
Toasts . shortShow ( this , "{}未填" , getResources ( ) . getString ( R . string . np_project_name ) ) ;
}
data . putExtra ( BizGlobal . PROJECT_NAME , projectName ) ;
// 图标
data . putExtra ( BizGlobal . PROJECT_ICON , selectedIcon ) ;
// 颜色
data . putExtra ( BizGlobal . PROJECT_ICON_COLOR , selectedColor ) ;
// 放置结果数据
setResult ( BizGlobal . REQUEST_CODE_GENERAL , data ) ;
finish ( ) ;
} ) ;
}
private void colorPickListener ( ) {
binding . npColorPicker . setOnClickListener ( view - > {
ColorPickerDialog . Builder builder = new ColorPickerDialog . Builder ( this ) ;
@ -81,7 +203,8 @@ public class NewProjectActivity extends BaseActivity {
@@ -81,7 +203,8 @@ public class NewProjectActivity extends BaseActivity {
XLog . d ( "选择的颜色:{} {} {}, fromUser: {}" , envelope . getColor ( ) , envelope . getArgb ( ) , envelope . getHexCode ( ) , fromUser ) ;
selectedColor = envelope . getColor ( ) ;
changeIconColor ( envelope . getColor ( ) ) ;
// 改变图标以及颜色
changeIcon ( null , selectedColor ) ;
}
} )
. setNegativeButton ( getString ( R . string . cancel ) , ( dialogInterface , i ) - > {
@ -94,33 +217,40 @@ public class NewProjectActivity extends BaseActivity {
@@ -94,33 +217,40 @@ public class NewProjectActivity extends BaseActivity {
builder . create ( ) . show ( ) ;
} ) ;
}
// 返回按钮
binding . npButtonBack . setOnClickListener ( view - > finish ( ) ) ;
private void iconClickListener ( ) {
// 确定按钮
binding . npButtonConfirm . setOnClickListener ( view - > {
// 三种属性
Intent data = new Intent ( ) ;
// 名称
String projectName = binding . npProjectNameTxt . getText ( ) . toString ( ) ;
if ( StrUtil . isEmpty ( projectName ) ) {
Toasts . shortShow ( this , "{}未填" , getResources ( ) . getString ( R . string . np_project_name ) ) ;
}
data . putExtra ( BizGlobal . PROJECT_NAME , projectName ) ;
// 图标
data . putExtra ( BizGlobal . PROJECT_ICON , binding . npProjectNameTxt . getText ( ) . toString ( ) ) ;
// 颜色
data . putExtra ( BizGlobal . PROJECT_ICON_COLOR , binding . npProjectNameTxt . getText ( ) . toString ( ) ) ;
// 放置结果数据
setResult ( BizGlobal . REQUEST_CODE_GENERAL , data ) ;
finish ( ) ;
binding . npIconFill . setOnClickListener ( view - > {
View iconDialogView = View . inflate ( this , R . layout . fragment_icon_dialog , null ) ;
// 初始化 并且添加监听事件
initIconDialogView ( iconDialogView ) ;
AlertDialog . Builder builder = new AlertDialog . Builder ( this ) . setTitle ( "选择一个图标" ) ;
AlertDialog alertDialog = builder . setView ( iconDialogView )
. setNegativeButton ( "返回" , ( dialog , which ) - > dialog . dismiss ( ) )
. setPositiveButton ( "选择该图标" , ( dialog , which ) - > {
selectedIcon = iconRecycleAdapter . getSelectedIcon ( ) ;
changeIcon ( selectedIcon , selectedColor ) ;
} ) . create ( ) ;
alertDialog . show ( ) ;
} ) ;
}
private void changeIconColor ( int color ) {
/ * *
* 改变图标以及颜色
*
* @param icon 图标
* @param color 颜色
* /
private void changeIcon ( String icon , int color ) {
if ( StrUtil . isEmpty ( icon ) ) {
icon = ReflectUtil . getFieldValue ( binding . npIconFill . getDrawable ( ) , "mIcon" ) . toString ( ) ;
}
binding . npIconFill . setImageDrawable ( new IconicsDrawable ( this )
. icon ( ReflectUtil . getFieldValue ( binding . npIconFill . getDrawable ( ) , "mIcon" ) . toString ( ) )
. icon ( icon )
. color ( color )
. sizeDp ( 50 )
) ;