11package com .sparkleseditor .fragments ;
22
33import android .annotation .SuppressLint ;
4+ import android .content .Intent ;
45import android .graphics .Color ;
6+ import android .net .Uri ;
57import android .os .Bundle ;
8+ import android .os .Environment ;
9+ import android .provider .DocumentsContract ;
10+ import android .util .Log ;
611import android .view .LayoutInflater ;
712import android .view .View ;
813import android .view .ViewGroup ;
14+ import android .widget .Toast ;
915
16+ import com .zyron .filetree .events .FileTreeEventListener ;
17+
18+ import androidx .activity .result .ActivityResultCallback ;
19+ import androidx .activity .result .ActivityResultLauncher ;
20+ import androidx .activity .result .contract .ActivityResultContracts ;
1021import androidx .annotation .NonNull ;
11- import androidx .appcompat .app .ActionBarDrawerToggle ;
1222import androidx .core .view .GravityCompat ;
13- import androidx .core .view .ViewCompat ;
1423import androidx .drawerlayout .widget .DrawerLayout ;
1524import androidx .fragment .app .Fragment ;
25+ import androidx .transition .TransitionManager ;
1626
27+ import com .google .android .material .snackbar .Snackbar ;
28+ import com .google .android .material .transition .MaterialSharedAxis ;
1729import com .sparkleseditor .R ;
1830import com .sparkleseditor .components .ExpandableLayout ;
1931import com .sparkleseditor .databinding .FragmentMainBinding ;
2032import com .sparkleseditor .navigation .Navigator ;
33+ import com .zyron .filetree .events .FileTreeEventListener ;
34+ import com .zyron .filetree .provider .FileTreeIconProvider ;
35+
36+ import java .io .File ;
2137
2238import io .github .rosemoe .sora .widget .SymbolInputView ;
2339
24- public class MainFragment extends BaseFragment {
40+ public class MainFragment extends BaseFragment implements FileTreeEventListener {
41+
42+
43+ private final ActivityResultLauncher <Uri > folderPickerLauncher =
44+ registerForActivityResult (
45+ new ActivityResultContracts .OpenDocumentTree (),
46+ treeUri -> {
47+ if (treeUri == null ) return ;
48+
49+ int flags = Intent .FLAG_GRANT_READ_URI_PERMISSION |
50+ Intent .FLAG_GRANT_WRITE_URI_PERMISSION ;
51+
52+ requireContext ().getContentResolver ()
53+ .takePersistableUriPermission (treeUri , flags );
54+
55+ path = getRealPathFromUri (treeUri );
56+ setupFileTree ();
57+ Toast .makeText (requireContext (), "Folder selected!" , Toast .LENGTH_LONG ).show ();
58+
59+ }
60+ );
61+ private String getRealPathFromUri (Uri treeUri ) {
62+ String docId = DocumentsContract .getTreeDocumentId (treeUri );
63+ String [] split = docId .split (":" );
64+ String type = split [0 ];
65+ String relativePath = split .length > 1 ? split [1 ] : "" ;
66+ if ("primary" .equalsIgnoreCase (type )) {
67+ return Environment .getExternalStorageDirectory () + "/" + relativePath ;
68+ } else {
69+ String externalStorage = System .getenv ("SECONDARY_STORAGE" );
70+ if (externalStorage == null ) {
71+ externalStorage = System .getenv ("EXTERNAL_STORAGE" );
72+ }
73+ return externalStorage + "/" + relativePath ;
74+ }
75+ }
2576
2677 private FragmentMainBinding binding ;
78+ private FileTreeIconProvider fileIconProvider ;
79+ private String path ="" ;
80+
81+ private static final int REQUEST_CODE_OPEN_DIRECTORY = 1001 ;
2782
2883 public static final String [] SYMBOLS = {
2984 "TAB" ,"↵" , "{" , "}" , "(" , ")" ,
@@ -39,6 +94,11 @@ public class MainFragment extends BaseFragment {
3994 ">" , "[" , "]" , ":"
4095 };
4196
97+ public interface FileTreeEventListener {
98+ void onFileClick (File file );
99+ void onFolderClick (File folder );
100+ }
101+
42102 @ Override
43103 public View onCreateView (
44104 @ NonNull LayoutInflater inflater , ViewGroup container ,
@@ -52,14 +112,50 @@ public View onCreateView(
52112
53113 public void onViewCreated (@ NonNull View view , Bundle savedInstanceState ) {
54114 super .onViewCreated (view , savedInstanceState );
115+
116+ //AppStruct
55117 setupToolbar ();
56118 setupToolbox ();
57119 setupInputView ();
58120 setupTabLayoutTemp ();
59121 slideXDrawer ();
122+ drawerLeftContent ();
60123 binding .fab .setTranslationY (-12 );
124+ }
61125
126+ private void drawerLeftContent () {
127+
128+ binding .fileTree .setVisibility (View .VISIBLE );
129+ binding .contentGit .setVisibility (View .GONE );
130+ binding .btmOptions .setOnNavigationItemSelectedListener (
131+ item -> {
132+ var sharedAxis = new MaterialSharedAxis (MaterialSharedAxis .X , true );
133+ TransitionManager .beginDelayedTransition (binding .container , sharedAxis );
134+ if (item .getItemId () == R .id .option_file_tree ) {
135+ binding .contentGit .setVisibility (View .GONE );
136+ binding .fileTree .setVisibility (View .VISIBLE );
137+ } else if (item .getItemId () == R .id .option_git ) {
138+ binding .contentGit .setVisibility (View .VISIBLE );
139+ binding .fileTree .setVisibility (View .GONE );
140+ }
141+ return true ;
142+ }
143+ );
144+ setupFileTree ();
145+ }
62146
147+ private void setupFileTree () {
148+ if (path != null && !path .isEmpty ()) {
149+ binding .contentFileTree .setVisibility (View .VISIBLE );
150+ binding .requireFolder .setVisibility (View .GONE );
151+ binding .fileTreeView .initializeFileTree (path , this , fileIconProvider );
152+ }else {
153+ binding .contentFileTree .setVisibility (View .GONE );
154+ binding .requireFolder .setVisibility (View .VISIBLE );
155+ binding .openFolder .setOnClickListener (v -> {
156+ folderPickerLauncher .launch (null );
157+ });
158+ }
63159 }
64160
65161 private void slideXDrawer () {
@@ -189,6 +285,27 @@ public void onDestroyView() {
189285 binding = null ;
190286 }
191287
288+ public void onFileClick (File file ) {
289+ }
290+
291+
292+ public void onFolderClick (File folder ) {}
293+
294+
295+ public boolean onFileLongClick (File file ) {
296+ return true ;
297+ }
298+
299+
300+ public boolean onFolderLongClick (File folder ) {
301+
302+ return true ;
303+ }
304+
305+
306+ public void onFileTreeViewUpdated (int startPosition , int itemCount ) {}
307+
308+
192309
193310
194311
0 commit comments