1818
1919import androidx .appcompat .app .AlertDialog ;
2020import androidx .fragment .app .DialogFragment ;
21- import androidx .fragment .app .Fragment ;
2221
2322import static android .app .Activity .RESULT_CANCELED ;
2423import static android .app .Activity .RESULT_OK ;
2524import static com .macbitsgoa .comrades .CHCKt .TAG_PREFIX ;
2625import static com .macbitsgoa .comrades .GetGoogleSignInActivity .KEY_TOKEN ;
26+ import static com .macbitsgoa .comrades .coursematerial .Uploader .upload ;
2727
2828public class UploadFileFragment extends DialogFragment
2929 implements View .OnClickListener {
@@ -37,6 +37,11 @@ public class UploadFileFragment extends DialogFragment
3737 private FloatingActionButton fabAddImage ;
3838 private TextView file ;
3939 private String filePath ;
40+ private String courseId ;
41+
42+ public UploadFileFragment (final String courseId ) {
43+ this .courseId = courseId ;
44+ }
4045
4146 @ Override
4247 public Dialog onCreateDialog (final Bundle savedInstanceState ) {
@@ -57,8 +62,6 @@ public Dialog onCreateDialog(final Bundle savedInstanceState) {
5762 .setNegativeButton (getString (R .string .cancel ), (dialogInterface , i ) -> {
5863 })
5964 .create ();
60-
61-
6265 }
6366
6467 /**
@@ -72,7 +75,6 @@ private void initUi(final View view) {
7275 fabAddImage = view .findViewById (R .id .fab_image );
7376 fileName = view .findViewById (R .id .et_file_name );
7477 file = view .findViewById (R .id .tv_file_path );
75-
7678 }
7779
7880 private void setPositiveClick () {
@@ -86,10 +88,8 @@ private void setPositiveClick() {
8688 Toast .makeText (getContext (), R .string .warn_empty_file_name , Toast .LENGTH_LONG ).show ();
8789 return ;
8890 }
89- final Fragment dialogFragment = getActivity ().getSupportFragmentManager ()
90- .findFragmentByTag (CourseActivity .ADD_FILE_FRAGMENT );
9191 final Intent signInIntent = new Intent (getActivity (), GetGoogleSignInActivity .class );
92- dialogFragment . startActivityForResult (signInIntent , SIGN_IN_REQUEST_CODE );
92+ startActivityForResult (signInIntent , SIGN_IN_REQUEST_CODE );
9393 };
9494
9595
@@ -102,29 +102,27 @@ private void setPositiveClick() {
102102 */
103103 @ Override
104104 public void onClick (final View view ) {
105- final UploadFileFragment dialogFragment = (UploadFileFragment ) getActivity ().getSupportFragmentManager ()
106- .findFragmentByTag (CourseActivity .ADD_FILE_FRAGMENT );
107105 switch (view .getId ()) {
108106 case R .id .fab_image :
109107 Intent imageIntent = new Intent ();
110108 imageIntent .setType ("image/*" );
111109 imageIntent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , false );
112110 imageIntent .setAction (Intent .ACTION_GET_CONTENT );
113- dialogFragment . startActivityForResult (Intent .createChooser (imageIntent , "Select Picture" ), REQUEST_CHOOSER );
111+ startActivityForResult (Intent .createChooser (imageIntent , "Select Picture" ), REQUEST_CHOOSER );
114112 break ;
115113
116114 case R .id .fab_doc :
117115 Intent fileIntent = new Intent (Intent .ACTION_GET_CONTENT );
118116 fileIntent .addCategory (Intent .CATEGORY_OPENABLE );
119117 fileIntent .setType ("application/pdf" );
120- dialogFragment . startActivityForResult (Intent .createChooser (fileIntent , "Select Pdf" ), REQUEST_CHOOSER );
118+ startActivityForResult (Intent .createChooser (fileIntent , "Select Pdf" ), REQUEST_CHOOSER );
121119 break ;
122120
123121 case R .id .fab_all_files :
124122 Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
125123 intent .addCategory (Intent .CATEGORY_OPENABLE );
126124 intent .setType ("*/*" );
127- dialogFragment . startActivityForResult (Intent .createChooser (intent , "Select File" ), REQUEST_CHOOSER );
125+ startActivityForResult (Intent .createChooser (intent , "Select File" ), REQUEST_CHOOSER );
128126 break ;
129127 default :
130128 if (BuildConfig .DEBUG ) {
@@ -146,11 +144,7 @@ public void onActivityResult(final int requestCode, final int resultCode, final
146144
147145 if (requestCode == SIGN_IN_REQUEST_CODE && resultCode == RESULT_OK ) {
148146 final String accessToken = data .getStringExtra (KEY_TOKEN );
149- Intent uploadIntent = UploadService .makeUploadIntent (getContext (), filePath ,
150- accessToken , fileName .getText ().toString ());
151- Toast .makeText (getContext (), "Upload Started.Check NotificationBar for progress." ,
152- Toast .LENGTH_LONG ).show ();
153- getActivity ().startService (uploadIntent );
147+ upload (filePath , accessToken , fileName .getText ().toString (), courseId );
154148 } else if (resultCode == RESULT_OK ) {
155149 filePath = PathUtil .getPath (getContext (), data .getData ());
156150 if (filePath != null ) {
@@ -163,7 +157,5 @@ public void onActivityResult(final int requestCode, final int resultCode, final
163157 } else if (resultCode == RESULT_CANCELED ) {
164158 Toast .makeText (getContext (), "No File Selected" , Toast .LENGTH_LONG ).show ();
165159 }
166-
167160 }
168-
169161}
0 commit comments